NeonIntf.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "NeonIntf.h"
  5. #include "Interface.h"
  6. #include "CoreMain.h"
  7. #include "Exceptions.h"
  8. #include "Security.h"
  9. #include <TextsCore.h>
  10. #include <ne_auth.h>
  11. #include <ne_redirect.h>
  12. #include <StrUtils.hpp>
  13. //---------------------------------------------------------------------------
  14. #define SESSION_PROXY_AUTH_KEY "proxyauth"
  15. #define SESSION_TLS_INIT_KEY "tlsinit"
  16. //---------------------------------------------------------------------------
  17. void NeonParseUrl(const UnicodeString & Url, ne_uri & uri)
  18. {
  19. if (ne_uri_parse(StrToNeon(Url), &uri) != 0)
  20. {
  21. // should never happen
  22. throw Exception(FMTLOAD(INVALID_URL, (Url)));
  23. }
  24. // Will never happen for initial URL, but may happen for redirect URLs
  25. if (uri.port == 0)
  26. {
  27. uri.port = ne_uri_defaultport(uri.scheme);
  28. }
  29. }
  30. //---------------------------------------------------------------------------
  31. bool IsTlsUri(const ne_uri & uri)
  32. {
  33. return SameText(StrFromNeon(uri.scheme), HttpsProtocol);
  34. }
  35. //---------------------------------------------------------------------------
  36. struct TProxyAuthData
  37. {
  38. UnicodeString UserName;
  39. UnicodeString Password;
  40. };
  41. //------------------------------------------------------------------------------
  42. static int NeonProxyAuth(
  43. void * UserData, const char * /*Realm*/, int Attempt, char * UserName, char * Password)
  44. {
  45. TProxyAuthData * ProxyAuthData = static_cast<TProxyAuthData *>(UserData);
  46. int Result;
  47. // no point trying too many times as we always return the same credentials
  48. // (maybe just one would be enough)
  49. if (Attempt >= 2)
  50. {
  51. Result = 1;
  52. }
  53. else
  54. {
  55. strncpy(UserName, StrToNeon(ProxyAuthData->UserName), NE_ABUFSIZ);
  56. strncpy(Password, StrToNeon(ProxyAuthData->Password), NE_ABUFSIZ);
  57. Result = 0;
  58. }
  59. return Result;
  60. }
  61. //---------------------------------------------------------------------------
  62. ne_session * CreateNeonSession(const ne_uri & uri)
  63. {
  64. return ne_session_create(uri.scheme, uri.host, uri.port);
  65. }
  66. //---------------------------------------------------------------------------
  67. void InitNeonSession(ne_session * Session, TProxyMethod ProxyMethod, const UnicodeString & ProxyHost,
  68. int ProxyPort, const UnicodeString & ProxyUsername, const UnicodeString & ProxyPassword)
  69. {
  70. if (ProxyMethod != ::pmNone)
  71. {
  72. if ((ProxyMethod == pmSocks4) || (ProxyMethod == pmSocks5))
  73. {
  74. enum ne_sock_sversion vers = (ProxyMethod == pmSocks4) ? NE_SOCK_SOCKSV4A : NE_SOCK_SOCKSV5;
  75. ne_session_socks_proxy(Session, vers, StrToNeon(ProxyHost), ProxyPort, StrToNeon(ProxyUsername), StrToNeon(ProxyPassword));
  76. }
  77. else if (!ProxyHost.IsEmpty())
  78. {
  79. ne_session_proxy(Session, StrToNeon(ProxyHost), ProxyPort);
  80. if (!ProxyUsername.IsEmpty())
  81. {
  82. TProxyAuthData * ProxyAuthData = new TProxyAuthData();
  83. ProxyAuthData->UserName = ProxyUsername;
  84. ProxyAuthData->Password = ProxyPassword;
  85. ne_set_session_private(Session, SESSION_PROXY_AUTH_KEY, ProxyAuthData);
  86. ne_set_proxy_auth(Session, NeonProxyAuth, ProxyAuthData);
  87. }
  88. else
  89. {
  90. // Enable (only) the Negotiate scheme for proxy
  91. // authentication, if no username/password is
  92. // configured.
  93. ne_add_proxy_auth(Session, NE_AUTH_NEGOTIATE, NULL, NULL);
  94. }
  95. }
  96. }
  97. ne_redirect_register(Session);
  98. ne_set_useragent(Session, StrToNeon(FORMAT(L"%s/%s", (AppNameString(), Configuration->Version))));
  99. }
  100. //---------------------------------------------------------------------------
  101. void DestroyNeonSession(ne_session * Session)
  102. {
  103. TProxyAuthData * ProxyAuthData =
  104. static_cast<TProxyAuthData *>(ne_get_session_private(Session, SESSION_PROXY_AUTH_KEY));
  105. if (ProxyAuthData != NULL)
  106. {
  107. delete ProxyAuthData;
  108. }
  109. ne_session_destroy(Session);
  110. }
  111. //---------------------------------------------------------------------------
  112. UnicodeString GetNeonError(ne_session * Session)
  113. {
  114. return StrFromNeon(ne_get_error(Session));
  115. }
  116. //---------------------------------------------------------------------------
  117. void CheckNeonStatus(ne_session * Session, int NeonStatus,
  118. const UnicodeString & HostName, const UnicodeString & CustomError)
  119. {
  120. if (NeonStatus == NE_OK)
  121. {
  122. // noop
  123. }
  124. else
  125. {
  126. UnicodeString NeonError = GetNeonError(Session);
  127. UnicodeString Error;
  128. if (!CustomError.IsEmpty())
  129. {
  130. Error = CustomError;
  131. }
  132. else
  133. {
  134. switch (NeonStatus)
  135. {
  136. case NE_ERROR:
  137. // noop
  138. DebugAssert(!NeonError.IsEmpty());
  139. Error = NeonError;
  140. NeonError = L"";
  141. break;
  142. case NE_LOOKUP:
  143. Error = ReplaceStr(LoadStr(NET_TRANSL_HOST_NOT_EXIST2), L"%HOST%", HostName);
  144. break;
  145. case NE_AUTH:
  146. Error = LoadStr(AUTHENTICATION_FAILED);
  147. break;
  148. case NE_PROXYAUTH:
  149. Error = LoadStr(PROXY_AUTHENTICATION_FAILED);
  150. break;
  151. case NE_CONNECT:
  152. Error = LoadStr(CONNECTION_FAILED);
  153. break;
  154. case NE_TIMEOUT:
  155. Error = ReplaceStr(LoadStr(NET_TRANSL_TIMEOUT2), L"%HOST%", HostName);
  156. break;
  157. case NE_REDIRECT:
  158. {
  159. char * Uri = ne_uri_unparse(ne_redirect_location(Session));
  160. Error = FMTLOAD(REQUEST_REDIRECTED, (Uri));
  161. ne_free(Uri);
  162. }
  163. break;
  164. case NE_FAILED: // never used by neon as of 0.30.0
  165. case NE_RETRY: // not sure if this is a public API
  166. default:
  167. DebugFail();
  168. Error = FORMAT(L"Unexpected neon error %d", (NeonStatus));
  169. break;
  170. }
  171. }
  172. throw ExtException(Error, NeonError);
  173. }
  174. }
  175. //---------------------------------------------------------------------------
  176. UnicodeString GetNeonRedirectUrl(ne_session * Session)
  177. {
  178. const ne_uri * RedirectUri = ne_redirect_location(Session);
  179. char * RedirectUriStr = ne_uri_unparse(RedirectUri);
  180. UnicodeString Result = StrFromNeon(RedirectUriStr);
  181. ne_free(RedirectUriStr);
  182. return Result;
  183. }
  184. //---------------------------------------------------------------------------
  185. #define MAX_REDIRECT_ATTEMPTS 5
  186. //---------------------------------------------------------------------------
  187. void CheckRedirectLoop(const UnicodeString & RedirectUrl, TStrings * AttemptedUrls)
  188. {
  189. if (AttemptedUrls->Count > MAX_REDIRECT_ATTEMPTS)
  190. {
  191. throw Exception(LoadStr(TOO_MANY_REDIRECTS));
  192. }
  193. else
  194. {
  195. // Make sure we've not attempted this URL before.
  196. if (AttemptedUrls->IndexOf(RedirectUrl) >= 0)
  197. {
  198. throw Exception(LoadStr(REDIRECT_LOOP));
  199. }
  200. AttemptedUrls->Add(RedirectUrl);
  201. }
  202. }
  203. //---------------------------------------------------------------------------
  204. extern "C"
  205. {
  206. void ne_init_ssl_session(struct ssl_st * Ssl, ne_session * Session)
  207. {
  208. TNeonTlsInit OnNeonTlsInit =
  209. reinterpret_cast<TNeonTlsInit>(ne_get_session_private(Session, SESSION_TLS_INIT_KEY));
  210. if (DebugAlwaysTrue(OnNeonTlsInit != NULL))
  211. {
  212. OnNeonTlsInit(Ssl, Session);
  213. }
  214. }
  215. } // extern "C"
  216. //---------------------------------------------------------------------------
  217. void SetNeonTlsInit(ne_session * Session, TNeonTlsInit OnNeonTlsInit)
  218. {
  219. ne_set_session_private(Session, SESSION_TLS_INIT_KEY, OnNeonTlsInit);
  220. }
  221. //---------------------------------------------------------------------------
  222. AnsiString NeonExportCertificate(const ne_ssl_certificate * Certificate)
  223. {
  224. char * AsciiCert = ne_ssl_cert_export(Certificate);
  225. AnsiString Result = AsciiCert;
  226. ne_free(AsciiCert);
  227. return Result;
  228. }
  229. //---------------------------------------------------------------------------
  230. bool NeonWindowsValidateCertificate(int & Failures, const AnsiString & AsciiCert, UnicodeString & Error)
  231. {
  232. bool Result = false;
  233. // We can accept only unknown certificate authority.
  234. if (FLAGSET(Failures, NE_SSL_UNTRUSTED))
  235. {
  236. unsigned char * Certificate;
  237. size_t CertificateLen = ne_unbase64(AsciiCert.c_str(), &Certificate);
  238. if (CertificateLen > 0)
  239. {
  240. if (WindowsValidateCertificate(Certificate, CertificateLen, Error))
  241. {
  242. Failures &= ~NE_SSL_UNTRUSTED;
  243. Result = true;
  244. }
  245. ne_free(Certificate);
  246. }
  247. }
  248. return Result;
  249. }
  250. //---------------------------------------------------------------------------
  251. UnicodeString NeonCertificateFailuresErrorStr(int Failures, const UnicodeString & HostName)
  252. {
  253. int FailuresToList = Failures;
  254. UnicodeString Result;
  255. if (FLAGSET(FailuresToList, NE_SSL_NOTYETVALID))
  256. {
  257. AddToList(Result, LoadStr(CERT_ERR_CERT_NOT_YET_VALID), L" ");
  258. FailuresToList &= ~NE_SSL_NOTYETVALID;
  259. }
  260. if (FLAGSET(FailuresToList, NE_SSL_EXPIRED))
  261. {
  262. AddToList(Result, LoadStr(CERT_ERR_CERT_HAS_EXPIRED), L" ");
  263. FailuresToList &= ~NE_SSL_EXPIRED;
  264. }
  265. // NEON checks certificate host name on its own
  266. if (FLAGSET(FailuresToList, NE_SSL_IDMISMATCH))
  267. {
  268. AddToList(Result, FMTLOAD(CERT_NAME_MISMATCH, (HostName)), L" ");
  269. FailuresToList &= ~NE_SSL_IDMISMATCH;
  270. }
  271. if (FLAGSET(FailuresToList, NE_SSL_UNTRUSTED))
  272. {
  273. AddToList(Result, LoadStr(CERT_ERR_CERT_UNTRUSTED), L" ");
  274. FailuresToList &= ~NE_SSL_UNTRUSTED;
  275. }
  276. if (FLAGSET(FailuresToList, NE_SSL_BADCHAIN))
  277. {
  278. AddToList(Result, LoadStr(CERT_ERR_BAD_CHAIN), L" ");
  279. FailuresToList &= ~NE_SSL_BADCHAIN;
  280. }
  281. // nb, NE_SSL_REVOKED is never used by OpenSSL implementation
  282. if (FailuresToList != 0)
  283. {
  284. AddToList(Result, LoadStr(CERT_ERR_UNKNOWN), L" ");
  285. }
  286. return Result;
  287. }