BBS2chProxySecureSocket.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #ifdef USE_MITM
  2. #include <stdexcept>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #ifdef USE_GNUTLS
  7. #include <gnutls/x509.h>
  8. #include <gnutls/crypto.h>
  9. #else
  10. #include <openssl/pem.h>
  11. #include <openssl/x509v3.h>
  12. #include <openssl/err.h>
  13. #endif
  14. #ifdef _WIN32
  15. #include <winsock2.h>
  16. #include <windows.h>
  17. #define CLOSESOCKET(x) closesocket(x)
  18. #define SHUT_RDWR SD_BOTH
  19. #else
  20. #define CLOSESOCKET(x) ::close(x)
  21. #endif
  22. #include "BBS2chProxySecureSocket.h"
  23. #ifdef USE_GNUTLS
  24. static gnutls_x509_crt_t ca_cert;
  25. static gnutls_x509_privkey_t ca_privkey;
  26. static gnutls_x509_privkey_t server_privkey;
  27. static gnutls_priority_t priority;
  28. int BBS2chProxySecureSocket::initializeCerts(const char *certPath, const char *keyPath)
  29. {
  30. static int initialized;
  31. if (initialized) return 0;
  32. gnutls_global_init();
  33. gnutls_datum_t data;
  34. int ret = gnutls_load_file(certPath, &data);
  35. if (ret < 0) {
  36. fprintf(stderr, "Unable to open CA certificate from %s\n", certPath);
  37. return -1;
  38. }
  39. gnutls_x509_crt_init(&ca_cert);
  40. ret = gnutls_x509_crt_import(ca_cert, &data, GNUTLS_X509_FMT_PEM);
  41. if (ret < 0) {
  42. fprintf(stderr, "Error loading CA certificate: %s\n", gnutls_strerror(ret));
  43. return -1;
  44. }
  45. gnutls_free(data.data);
  46. ret = gnutls_load_file(keyPath, &data);
  47. if (ret < 0) {
  48. fprintf(stderr, "Unable to open CA private key from %s\n", keyPath);
  49. return -1;
  50. }
  51. gnutls_x509_privkey_init(&ca_privkey);
  52. ret = gnutls_x509_privkey_import(ca_privkey, &data, GNUTLS_X509_FMT_PEM);
  53. if (ret < 0) {
  54. fprintf(stderr, "Error loading CA private key: %s\n", gnutls_strerror(ret));
  55. return -1;
  56. }
  57. gnutls_free(data.data);
  58. gnutls_x509_privkey_init(&server_privkey);
  59. #ifdef USE_ECDSA_KEY
  60. gnutls_x509_privkey_generate(server_privkey, GNUTLS_PK_EC, GNUTLS_CURVE_TO_BITS(GNUTLS_ECC_CURVE_SECP256R1), 0);
  61. #else
  62. gnutls_x509_privkey_generate(server_privkey, GNUTLS_PK_RSA, 2048, 0);
  63. #endif
  64. gnutls_priority_init(&priority, NULL, NULL);
  65. initialized = 1;
  66. return 0;
  67. }
  68. void BBS2chProxySecureSocket::generateAndPrintSelfSignedCertificate(void)
  69. {
  70. unsigned char tmp[4096];
  71. size_t bufsize = 4096;
  72. time_t now = time(NULL);
  73. unsigned long long serial;
  74. gnutls_x509_crt_t cert;
  75. gnutls_x509_privkey_t key;
  76. gnutls_global_init();
  77. gnutls_rnd(GNUTLS_RND_NONCE, &serial, sizeof(serial));
  78. gnutls_x509_privkey_init(&key);
  79. #ifdef USE_ECDSA_KEY
  80. gnutls_x509_privkey_generate(key, GNUTLS_PK_EC, GNUTLS_CURVE_TO_BITS(GNUTLS_ECC_CURVE_SECP256R1), 0);
  81. #else
  82. gnutls_x509_privkey_generate(key, GNUTLS_PK_RSA, 2048, 0);
  83. #endif
  84. gnutls_x509_crt_init(&cert);
  85. gnutls_x509_crt_set_version(cert, 3);
  86. gnutls_x509_crt_set_ca_status(cert, 1);
  87. gnutls_x509_crt_set_activation_time(cert, now);
  88. gnutls_x509_crt_set_expiration_time(cert, now + 31536000*3);
  89. gnutls_x509_crt_set_dn_by_oid(cert, GNUTLS_OID_X520_COUNTRY_NAME, 0, "JP", strlen("JP"));
  90. gnutls_x509_crt_set_dn_by_oid(cert, GNUTLS_OID_X520_ORGANIZATION_NAME, 0, "proxy2ch certificate generator", strlen("proxy2ch certificate generator"));
  91. gnutls_x509_crt_set_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, 0, "proxy2ch", strlen("proxy2ch"));
  92. gnutls_x509_crt_set_serial(cert, &serial, sizeof(serial));
  93. gnutls_x509_crt_set_key(cert, key);
  94. gnutls_x509_crt_set_key_purpose_oid(cert, GNUTLS_KP_TLS_WWW_SERVER, 0);
  95. gnutls_x509_crt_set_key_purpose_oid(cert, GNUTLS_KP_TLS_WWW_CLIENT, 0);
  96. gnutls_x509_crt_set_key_usage(cert, GNUTLS_KEY_DIGITAL_SIGNATURE|GNUTLS_KEY_KEY_CERT_SIGN|GNUTLS_KEY_CRL_SIGN);
  97. gnutls_x509_crt_get_key_id(cert, 0, tmp, &bufsize);
  98. gnutls_x509_crt_set_subject_key_id(cert, tmp, bufsize);
  99. gnutls_x509_crt_sign2(cert, cert, key, GNUTLS_DIG_SHA256, 0);
  100. bufsize = 4096;
  101. gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_PEM, tmp, &bufsize);
  102. fwrite(tmp, 1, bufsize, stdout);
  103. bufsize = 4096;
  104. gnutls_x509_privkey_export(key, GNUTLS_X509_FMT_PEM, tmp, &bufsize);
  105. fwrite(tmp, 1, bufsize, stdout);
  106. fflush(stdout);
  107. gnutls_x509_privkey_deinit(key);
  108. gnutls_x509_crt_deinit(cert);
  109. }
  110. BBS2chProxySecureSocket::BBS2chProxySecureSocket(int sock, const char *host) :
  111. socket(sock), session(NULL), x509_cred(NULL)
  112. {
  113. bool hostIsDomain = false;
  114. for (int i=strlen(host)-1; i>=0; i--) {
  115. if (host[i] != '.' && !(host[i] >= '0' && host[i] <= '9')) {
  116. hostIsDomain = true;
  117. break;
  118. }
  119. }
  120. unsigned char tmp[4096];
  121. size_t bufsize = 4096;
  122. time_t now = time(NULL);
  123. unsigned long long serial;
  124. gnutls_x509_crt_t cert;
  125. gnutls_rnd(GNUTLS_RND_NONCE, &serial, sizeof(serial));
  126. gnutls_x509_crt_init(&cert);
  127. gnutls_x509_crt_set_version(cert, 3);
  128. gnutls_x509_crt_set_ca_status(cert, 0);
  129. gnutls_x509_crt_set_activation_time(cert, now - 600);
  130. gnutls_x509_crt_set_expiration_time(cert, now + 31536000);
  131. gnutls_x509_crt_set_dn_by_oid(cert, GNUTLS_OID_X520_COUNTRY_NAME, 0, "JP", strlen("JP"));
  132. gnutls_x509_crt_set_dn_by_oid(cert, GNUTLS_OID_X520_ORGANIZATION_NAME, 0, "proxy2ch", strlen("proxy2ch"));
  133. gnutls_x509_crt_set_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME, 0, host, strlen(host));
  134. gnutls_x509_crt_set_serial(cert, &serial, sizeof(serial));
  135. gnutls_x509_crt_set_key(cert, server_privkey);
  136. gnutls_x509_crt_set_key_purpose_oid(cert, GNUTLS_KP_TLS_WWW_SERVER, 0);
  137. gnutls_x509_crt_set_key_purpose_oid(cert, GNUTLS_KP_TLS_WWW_CLIENT, 0);
  138. gnutls_x509_crt_set_key_usage(cert, GNUTLS_KEY_DIGITAL_SIGNATURE|GNUTLS_KEY_KEY_ENCIPHERMENT);
  139. gnutls_x509_crt_get_key_id(cert, 0, tmp, &bufsize);
  140. gnutls_x509_crt_set_subject_key_id(cert, tmp, bufsize);
  141. gnutls_x509_crt_set_subject_alt_name(cert, hostIsDomain ? GNUTLS_SAN_DNSNAME : GNUTLS_SAN_IPADDRESS, host, strlen(host), GNUTLS_FSAN_APPEND);
  142. gnutls_x509_crt_set_subject_alt_name(cert, GNUTLS_SAN_DNSNAME, "*.5ch.net", strlen("*.5ch.net"), GNUTLS_FSAN_APPEND);
  143. gnutls_x509_crt_set_subject_alt_name(cert, GNUTLS_SAN_DNSNAME, "*.2ch.net", strlen("*.2ch.net"), GNUTLS_FSAN_APPEND);
  144. gnutls_x509_crt_set_subject_alt_name(cert, GNUTLS_SAN_DNSNAME, "*.bbspink.com", strlen("*.bbspink.com"), GNUTLS_FSAN_APPEND);
  145. gnutls_x509_crt_sign2(cert, ca_cert, ca_privkey, GNUTLS_DIG_SHA256, 0);
  146. gnutls_certificate_allocate_credentials(&x509_cred);
  147. gnutls_certificate_set_x509_key(x509_cred, &cert, 1, server_privkey);
  148. gnutls_x509_crt_deinit(cert);
  149. #if GNUTLS_VERSION_NUMBER >= 0x030506
  150. gnutls_certificate_set_known_dh_params(x509_cred, GNUTLS_SEC_PARAM_MEDIUM);
  151. #endif
  152. int ret = gnutls_init(&session, GNUTLS_SERVER);
  153. if (ret < 0) {
  154. gnutls_certificate_free_credentials(x509_cred);
  155. std::string str("Unable to create GnuTLS session: ");
  156. str += gnutls_strerror(ret);
  157. throw std::runtime_error(str);
  158. }
  159. gnutls_priority_set(session, priority);
  160. ret = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
  161. if (ret < 0) {
  162. gnutls_deinit(session);
  163. gnutls_certificate_free_credentials(x509_cred);
  164. std::string str("Unable to set server credentials: ");
  165. str += gnutls_strerror(ret);
  166. throw std::runtime_error(str);
  167. }
  168. gnutls_certificate_server_set_request(session, GNUTLS_CERT_IGNORE);
  169. gnutls_handshake_set_timeout(session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
  170. gnutls_transport_set_int(session, sock);
  171. ret = gnutls_handshake(session);
  172. if (ret < 0) {
  173. gnutls_deinit(session);
  174. gnutls_certificate_free_credentials(x509_cred);
  175. std::string str("Unable to establish SSL/TLS connection: ");
  176. str += gnutls_strerror(ret);
  177. throw std::runtime_error(str);
  178. }
  179. }
  180. #else
  181. static X509 *ca_cert;
  182. static EVP_PKEY *ca_privkey;
  183. static EVP_PKEY *server_privkey;
  184. static int add_ext(X509 *cert, int nid, const char *value)
  185. {
  186. X509_EXTENSION *ex;
  187. X509V3_CTX ctx;
  188. X509V3_set_ctx_nodb(&ctx);
  189. X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
  190. ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
  191. if (!ex)
  192. return 0;
  193. X509_add_ext(cert,ex,-1);
  194. X509_EXTENSION_free(ex);
  195. return 1;
  196. }
  197. int BBS2chProxySecureSocket::initializeCerts(const char *certPath, const char *keyPath)
  198. {
  199. static int initialized;
  200. if (initialized) return 0;
  201. FILE *fp = fopen(certPath, "rb");
  202. if (!fp) {
  203. fprintf(stderr, "Unable to open CA certificate from %s\n", certPath);
  204. return -1;
  205. }
  206. ca_cert = PEM_read_X509(fp, NULL, NULL, NULL);
  207. if (!ca_cert) {
  208. fprintf(stderr, "Error loading CA certificate: ");
  209. ERR_print_errors_fp(stderr);
  210. return -1;
  211. }
  212. fclose(fp);
  213. fp = fopen(keyPath, "rb");
  214. if (!fp) {
  215. fprintf(stderr, "Unable to open CA private key from %s\n", keyPath);
  216. return -1;
  217. }
  218. ca_privkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
  219. if (!ca_privkey) {
  220. fprintf(stderr, "Error loading CA private key: ");
  221. ERR_print_errors_fp(stderr);
  222. return -1;
  223. }
  224. fclose(fp);
  225. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  226. #if defined(USE_ECDSA_KEY)
  227. server_privkey = EVP_EC_gen("P-256");
  228. #else
  229. server_privkey = EVP_RSA_gen(2048);
  230. #endif
  231. #else
  232. server_privkey = EVP_PKEY_new();
  233. #if defined(USE_ECDSA_KEY) && (OPENSSL_VERSION_NUMBER >= 0x10000000L)
  234. EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  235. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  236. EC_KEY_generate_key(ec_key);
  237. EVP_PKEY_assign_EC_KEY(server_privkey, ec_key);
  238. #else
  239. BIGNUM *bn = BN_new();
  240. BN_set_word(bn, RSA_F4);
  241. RSA *rsa = RSA_new();
  242. RSA_generate_key_ex(rsa, 2048, bn, NULL);
  243. EVP_PKEY_assign_RSA(server_privkey, rsa);
  244. BN_free(bn);
  245. #endif
  246. #endif
  247. initialized = 1;
  248. return 0;
  249. }
  250. void BBS2chProxySecureSocket::generateAndPrintSelfSignedCertificate(void)
  251. {
  252. BIGNUM *bn;
  253. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  254. #if defined(USE_ECDSA_KEY)
  255. EVP_PKEY *key = EVP_EC_gen("P-256");
  256. #else
  257. EVP_PKEY *key = EVP_RSA_gen(2048);
  258. #endif
  259. #else
  260. EVP_PKEY *key = EVP_PKEY_new();
  261. #if defined(USE_ECDSA_KEY) && (OPENSSL_VERSION_NUMBER >= 0x10000000L)
  262. EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  263. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  264. EC_KEY_generate_key(ec_key);
  265. EVP_PKEY_assign_EC_KEY(key, ec_key);
  266. #else
  267. bn = BN_new();
  268. BN_set_word(bn, RSA_F4);
  269. RSA *rsa = RSA_new();
  270. RSA_generate_key_ex(rsa, 2048, bn, NULL);
  271. EVP_PKEY_assign_RSA(key, rsa);
  272. BN_free(bn);
  273. #endif
  274. #endif
  275. X509 *cert = X509_new();
  276. X509_set_version(cert, 2);
  277. ASN1_INTEGER *serial = ASN1_INTEGER_new();
  278. bn = BN_new();
  279. BN_rand(bn, 64, 0, 0);
  280. BN_to_ASN1_INTEGER(bn, serial);
  281. X509_set_serialNumber(cert, serial);
  282. ASN1_INTEGER_free(serial);
  283. BN_free(bn);
  284. X509_name_st *name = X509_get_subject_name(cert);
  285. X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char *)"JP", -1, -1, 0);
  286. X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned char *)"proxy2ch certificate generator", -1, -1, 0);
  287. X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *)"proxy2ch", -1, -1, 0);
  288. X509_set_subject_name(cert, name);
  289. X509_set_issuer_name(cert, name);
  290. X509_set_pubkey(cert, key);
  291. X509_gmtime_adj(X509_get_notBefore(cert), 0);
  292. X509_gmtime_adj(X509_get_notAfter(cert), 31536000*3);
  293. add_ext(cert, NID_basic_constraints, "critical,CA:TRUE");
  294. add_ext(cert, NID_key_usage, "critical,digitalSignature,keyCertSign,cRLSign");
  295. add_ext(cert, NID_ext_key_usage, "serverAuth,clientAuth");
  296. add_ext(cert, NID_subject_key_identifier, "hash");
  297. X509_sign(cert, key, EVP_sha256());
  298. PEM_write_X509(stdout, cert);
  299. PEM_write_PrivateKey(stdout, key, NULL, NULL, 0, NULL, NULL);
  300. X509_free(cert);
  301. EVP_PKEY_free(key);
  302. }
  303. BBS2chProxySecureSocket::BBS2chProxySecureSocket(int sock, const char *host) :
  304. socket(sock), ctx(NULL), ssl(NULL)
  305. {
  306. bool hostIsDomain = false;
  307. for (int i=strlen(host)-1; i>=0; i--) {
  308. if (host[i] != '.' && !(host[i] >= '0' && host[i] <= '9')) {
  309. hostIsDomain = true;
  310. break;
  311. }
  312. }
  313. X509 *cert = X509_new();
  314. X509_set_version(cert, 2);
  315. ASN1_INTEGER *serial = ASN1_INTEGER_new();
  316. BIGNUM *bn = BN_new();
  317. BN_rand(bn, 64, 0, 0);
  318. BN_to_ASN1_INTEGER(bn, serial);
  319. X509_set_serialNumber(cert, serial);
  320. ASN1_INTEGER_free(serial);
  321. BN_free(bn);
  322. X509_name_st *name = X509_get_subject_name(cert);
  323. X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char *)"JP", -1, -1, 0);
  324. X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned char *)"proxy2ch", -1, -1, 0);
  325. X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *)host, -1, -1, 0);
  326. X509_set_subject_name(cert, name);
  327. X509_set_issuer_name(cert, X509_get_subject_name(ca_cert));
  328. X509_set_pubkey(cert, server_privkey);
  329. X509_gmtime_adj(X509_get_notBefore(cert), -600);
  330. X509_gmtime_adj(X509_get_notAfter(cert), 31536000);
  331. add_ext(cert, NID_basic_constraints, "critical,CA:FALSE");
  332. add_ext(cert, NID_key_usage, "critical,digitalSignature,keyEncipherment");
  333. add_ext(cert, NID_ext_key_usage, "serverAuth,clientAuth");
  334. add_ext(cert, NID_subject_key_identifier, "hash");
  335. std::string sni(hostIsDomain ? "DNS:" : "IP:");
  336. sni += host;
  337. sni += ",DNS:*.5ch.net,DNS:*.2ch.net,DNS:*.bbspink.com";
  338. add_ext(cert, NID_subject_alt_name, sni.c_str());
  339. X509_sign(cert, ca_privkey, EVP_sha256());
  340. ctx = SSL_CTX_new(TLS_server_method());
  341. if (!ctx) {
  342. X509_free(cert);
  343. throw std::runtime_error("Unable to create SSL context");
  344. }
  345. #if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(OPENSSL_NO_SECURITY_DOWNGRADE)
  346. /* Downgrade security level because 3.0 depreacted use of SHA-1 which is used by TLS v1.1 and earlier clients */
  347. SSL_CTX_set_security_level(ctx, 0);
  348. #endif
  349. if (SSL_CTX_use_certificate(ctx, cert) <= 0) {
  350. fprintf(stderr, "Unable to load server certificate\n");
  351. }
  352. if (SSL_CTX_use_PrivateKey(ctx, server_privkey) <= 0) {
  353. fprintf(stderr, "Unable to load server private key\n");
  354. }
  355. X509_free(cert);
  356. ssl = SSL_new(ctx);
  357. SSL_set_fd(ssl, socket);
  358. if (SSL_accept(ssl) <= 0) {
  359. char errbuf[256];
  360. ERR_error_string_n(ERR_get_error(), errbuf, 256);
  361. SSL_free(ssl);
  362. SSL_CTX_free(ctx);
  363. std::string str("Unable to establish SSL/TLS connection: ");
  364. str += errbuf;
  365. throw std::runtime_error(str);
  366. }
  367. }
  368. #endif
  369. BBS2chProxySecureSocket::~BBS2chProxySecureSocket()
  370. {
  371. }
  372. int BBS2chProxySecureSocket::read(char *buffer, int length)
  373. {
  374. #ifdef USE_GNUTLS
  375. return gnutls_record_recv(session, buffer, length);
  376. #else
  377. return SSL_read(ssl, buffer, length);
  378. #endif
  379. }
  380. int BBS2chProxySecureSocket::readLine(char *buffer, int maxLength)
  381. {
  382. char *ptr = buffer;
  383. while (ptr < buffer + maxLength - 1) {
  384. #ifdef USE_GNUTLS
  385. int read = gnutls_record_recv(session, ptr, 1);
  386. #else
  387. int read = SSL_read(ssl, ptr, 1);
  388. #endif
  389. if (read != 1) {
  390. return 0;
  391. }
  392. if (*ptr++ == '\n') {
  393. break;
  394. }
  395. }
  396. *ptr = 0;
  397. return 1;
  398. }
  399. int BBS2chProxySecureSocket::write(const char *buffer, int length)
  400. {
  401. #ifdef USE_GNUTLS
  402. int sent = 0;
  403. while (length > 0) {
  404. int ret = gnutls_record_send(session, buffer+sent, length);
  405. if (ret <= 0) break;
  406. sent += ret;
  407. length -= ret;
  408. }
  409. return sent;
  410. #else
  411. return SSL_write(ssl, buffer, length);
  412. #endif
  413. }
  414. int BBS2chProxySecureSocket::writeString(const std::string &str)
  415. {
  416. #ifdef USE_GNUTLS
  417. return write(str.data(), str.length());
  418. #else
  419. return SSL_write(ssl, str.data(), str.length());
  420. #endif
  421. }
  422. void BBS2chProxySecureSocket::close(void)
  423. {
  424. #ifdef USE_GNUTLS
  425. if (socket >= 0) {
  426. gnutls_bye(session, GNUTLS_SHUT_WR);
  427. #ifdef _WIN32
  428. Sleep(10);
  429. #else
  430. usleep(10000);
  431. #endif
  432. CLOSESOCKET(socket);
  433. socket = -1;
  434. }
  435. if (session) {
  436. gnutls_deinit(session);
  437. session = NULL;
  438. }
  439. if (x509_cred) {
  440. gnutls_certificate_free_credentials(x509_cred);
  441. x509_cred = NULL;
  442. }
  443. #else
  444. if (ssl) {
  445. SSL_shutdown(ssl);
  446. SSL_free(ssl);
  447. ssl = NULL;
  448. #ifdef _WIN32
  449. Sleep(10);
  450. #else
  451. usleep(10000);
  452. #endif
  453. }
  454. if (socket >= 0) {
  455. CLOSESOCKET(socket);
  456. socket = -1;
  457. }
  458. if (ctx) {
  459. SSL_CTX_free(ctx);
  460. ctx = NULL;
  461. }
  462. #endif
  463. }
  464. #endif