BBS2chProxySecureSocket.cpp 16 KB

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