tortls_nss.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /* Copyright (c) 2003, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file tortls_nss.c
  7. * \brief Wrapper functions to present a consistent interface to
  8. * TLS and SSL X.509 functions from NSS.
  9. **/
  10. #include "orconfig.h"
  11. #define TORTLS_PRIVATE
  12. #define TOR_X509_PRIVATE
  13. #ifdef _WIN32
  14. #include <winsock2.h>
  15. #include <ws2tcpip.h>
  16. #endif
  17. #include "lib/crypt_ops/crypto_cipher.h"
  18. #include "lib/crypt_ops/crypto_rand.h"
  19. #include "lib/crypt_ops/crypto_dh.h"
  20. #include "lib/crypt_ops/crypto_util.h"
  21. #include "lib/crypt_ops/crypto_nss_mgt.h"
  22. #include "lib/string/printf.h"
  23. #include "lib/tls/x509.h"
  24. #include "lib/tls/x509_internal.h"
  25. #include "lib/tls/tortls.h"
  26. #include "lib/tls/tortls_st.h"
  27. #include "lib/tls/tortls_internal.h"
  28. #include "lib/tls/nss_countbytes.h"
  29. #include "lib/log/util_bug.h"
  30. DISABLE_GCC_WARNING(strict-prototypes)
  31. #include <prio.h>
  32. // For access to rar sockets.
  33. #include <private/pprio.h>
  34. #include <ssl.h>
  35. #include <sslt.h>
  36. #include <sslproto.h>
  37. #include <certt.h>
  38. ENABLE_GCC_WARNING(strict-prototypes)
  39. static SECStatus always_accept_cert_cb(void *, PRFileDesc *, PRBool, PRBool);
  40. MOCK_IMPL(void,
  41. try_to_extract_certs_from_tls,(int severity, tor_tls_t *tls,
  42. tor_x509_cert_impl_t **cert_out,
  43. tor_x509_cert_impl_t **id_cert_out))
  44. {
  45. tor_assert(tls);
  46. tor_assert(cert_out);
  47. tor_assert(id_cert_out);
  48. (void) severity;
  49. *cert_out = *id_cert_out = NULL;
  50. CERTCertificate *peer = SSL_PeerCertificate(tls->ssl);
  51. if (!peer)
  52. return;
  53. *cert_out = peer; /* Now owns pointer. */
  54. CERTCertList *chain = SSL_PeerCertificateChain(tls->ssl);
  55. CERTCertListNode *c = CERT_LIST_HEAD(chain);
  56. for (; !CERT_LIST_END(c, chain); c = CERT_LIST_NEXT(c)) {
  57. if (CERT_CompareCerts(c->cert, peer) == PR_FALSE) {
  58. *id_cert_out = CERT_DupCertificate(c->cert);
  59. break;
  60. }
  61. }
  62. CERT_DestroyCertList(chain);
  63. }
  64. static bool
  65. we_like_ssl_cipher(SSLCipherAlgorithm ca)
  66. {
  67. switch (ca) {
  68. case ssl_calg_null: return false;
  69. case ssl_calg_rc4: return false;
  70. case ssl_calg_rc2: return false;
  71. case ssl_calg_des: return false;
  72. case ssl_calg_3des: return false; /* ???? */
  73. case ssl_calg_idea: return false;
  74. case ssl_calg_fortezza: return false;
  75. case ssl_calg_camellia: return false;
  76. case ssl_calg_seed: return false;
  77. case ssl_calg_aes: return true;
  78. case ssl_calg_aes_gcm: return true;
  79. case ssl_calg_chacha20: return true;
  80. default: return true;
  81. }
  82. }
  83. static bool
  84. we_like_ssl_kea(SSLKEAType kt)
  85. {
  86. switch (kt) {
  87. case ssl_kea_null: return false;
  88. case ssl_kea_rsa: return false; /* ??? */
  89. case ssl_kea_fortezza: return false;
  90. case ssl_kea_ecdh_psk: return false;
  91. case ssl_kea_dh_psk: return false;
  92. case ssl_kea_dh: return true;
  93. case ssl_kea_ecdh: return true;
  94. case ssl_kea_tls13_any: return true;
  95. case ssl_kea_size: return true; /* prevent a warning. */
  96. default: return true;
  97. }
  98. }
  99. static bool
  100. we_like_mac_algorithm(SSLMACAlgorithm ma)
  101. {
  102. switch (ma) {
  103. case ssl_mac_null: return false;
  104. case ssl_mac_md5: return false;
  105. case ssl_hmac_md5: return false;
  106. case ssl_mac_sha: return true;
  107. case ssl_hmac_sha: return true;
  108. case ssl_hmac_sha256: return true;
  109. case ssl_mac_aead: return true;
  110. case ssl_hmac_sha384: return true;
  111. default: return true;
  112. }
  113. }
  114. static bool
  115. we_like_auth_type(SSLAuthType at)
  116. {
  117. switch (at) {
  118. case ssl_auth_null: return false;
  119. case ssl_auth_rsa_decrypt: return false;
  120. case ssl_auth_dsa: return false;
  121. case ssl_auth_kea: return false;
  122. case ssl_auth_ecdsa: return true;
  123. case ssl_auth_ecdh_rsa: return true;
  124. case ssl_auth_ecdh_ecdsa: return true;
  125. case ssl_auth_rsa_sign: return true;
  126. case ssl_auth_rsa_pss: return true;
  127. case ssl_auth_psk: return true;
  128. case ssl_auth_tls13_any: return true;
  129. case ssl_auth_size: return true; /* prevent a warning. */
  130. default: return true;
  131. }
  132. }
  133. tor_tls_context_t *
  134. tor_tls_context_new(crypto_pk_t *identity,
  135. unsigned int key_lifetime, unsigned flags, int is_client)
  136. {
  137. SECStatus s;
  138. tor_assert(identity);
  139. tor_tls_init();
  140. tor_tls_context_t *ctx = tor_malloc_zero(sizeof(tor_tls_context_t));
  141. ctx->refcnt = 1;
  142. if (! is_client) {
  143. if (tor_tls_context_init_certificates(ctx, identity,
  144. key_lifetime, flags) < 0) {
  145. goto err;
  146. }
  147. }
  148. {
  149. /* Create the "model" PRFileDesc that we will use to base others on. */
  150. PRFileDesc *tcp = PR_NewTCPSocket();
  151. if (!tcp)
  152. goto err;
  153. ctx->ctx = SSL_ImportFD(NULL, tcp);
  154. if (!ctx->ctx) {
  155. PR_Close(tcp);
  156. goto err;
  157. }
  158. }
  159. // Configure the certificate.
  160. if (!is_client) {
  161. s = SSL_ConfigServerCert(ctx->ctx,
  162. ctx->my_link_cert->cert,
  163. (SECKEYPrivateKey *)
  164. crypto_pk_get_nss_privkey(ctx->link_key),
  165. NULL, /* ExtraServerCertData */
  166. 0 /* DataLen */);
  167. if (s != SECSuccess)
  168. goto err;
  169. }
  170. // We need a certificate from the other side.
  171. if (is_client) {
  172. // XXXX does this do anything?
  173. s = SSL_OptionSet(ctx->ctx, SSL_REQUIRE_CERTIFICATE, PR_TRUE);
  174. if (s != SECSuccess)
  175. goto err;
  176. }
  177. // Always accept other side's cert; we'll check it ourselves in goofy
  178. // tor ways.
  179. s = SSL_AuthCertificateHook(ctx->ctx, always_accept_cert_cb, NULL);
  180. // We allow simultaneous read and write.
  181. s = SSL_OptionSet(ctx->ctx, SSL_ENABLE_FDX, PR_TRUE);
  182. if (s != SECSuccess)
  183. goto err;
  184. // XXXX SSL_ROLLBACK_DETECTION??
  185. // XXXX SSL_ENABLE_ALPN??
  186. // Force client-mode or server_mode.
  187. s = SSL_OptionSet(ctx->ctx,
  188. is_client ? SSL_HANDSHAKE_AS_CLIENT : SSL_HANDSHAKE_AS_SERVER,
  189. PR_TRUE);
  190. if (s != SECSuccess)
  191. goto err;
  192. // Disable everything before TLS 1.0; support everything else.
  193. {
  194. SSLVersionRange vrange;
  195. memset(&vrange, 0, sizeof(vrange));
  196. s = SSL_VersionRangeGetSupported(ssl_variant_stream, &vrange);
  197. if (s != SECSuccess)
  198. goto err;
  199. if (vrange.min < SSL_LIBRARY_VERSION_TLS_1_0)
  200. vrange.min = SSL_LIBRARY_VERSION_TLS_1_0;
  201. s = SSL_VersionRangeSet(ctx->ctx, &vrange);
  202. if (s != SECSuccess)
  203. goto err;
  204. }
  205. // Only support strong ciphers.
  206. {
  207. const PRUint16 *ciphers = SSL_GetImplementedCiphers();
  208. const PRUint16 n_ciphers = SSL_GetNumImplementedCiphers();
  209. PRUint16 i;
  210. for (i = 0; i < n_ciphers; ++i) {
  211. SSLCipherSuiteInfo info;
  212. memset(&info, 0, sizeof(info));
  213. s = SSL_GetCipherSuiteInfo(ciphers[i], &info, sizeof(info));
  214. if (s != SECSuccess)
  215. goto err;
  216. if (BUG(info.cipherSuite != ciphers[i]))
  217. goto err;
  218. int disable = info.effectiveKeyBits < 128 ||
  219. info.macBits < 128 ||
  220. !we_like_ssl_cipher(info.symCipher) ||
  221. !we_like_ssl_kea(info.keaType) ||
  222. !we_like_mac_algorithm(info.macAlgorithm) ||
  223. !we_like_auth_type(info.authType)/* Requires NSS 3.24 */;
  224. s = SSL_CipherPrefSet(ctx->ctx, ciphers[i],
  225. disable ? PR_FALSE : PR_TRUE);
  226. if (s != SECSuccess)
  227. goto err;
  228. }
  229. }
  230. // Only use DH and ECDH keys once.
  231. s = SSL_OptionSet(ctx->ctx, SSL_REUSE_SERVER_ECDHE_KEY, PR_FALSE);
  232. if (s != SECSuccess)
  233. goto err;
  234. // don't cache sessions.
  235. s = SSL_OptionSet(ctx->ctx, SSL_NO_CACHE, PR_TRUE);
  236. if (s != SECSuccess)
  237. goto err;
  238. // Enable DH.
  239. s = SSL_OptionSet(ctx->ctx, SSL_ENABLE_SERVER_DHE, PR_TRUE);
  240. if (s != SECSuccess)
  241. goto err;
  242. // Set DH and ECDH groups.
  243. SSLNamedGroup groups[] = {
  244. ssl_grp_ec_curve25519,
  245. ssl_grp_ec_secp256r1,
  246. ssl_grp_ec_secp224r1,
  247. ssl_grp_ffdhe_2048,
  248. };
  249. s = SSL_NamedGroupConfig(ctx->ctx, groups, ARRAY_LENGTH(groups));
  250. if (s != SECSuccess)
  251. goto err;
  252. // These features are off by default, so we don't need to disable them:
  253. // Session tickets
  254. // Renegotiation
  255. // Compression
  256. goto done;
  257. err:
  258. tor_tls_context_decref(ctx);
  259. ctx = NULL;
  260. done:
  261. return ctx;
  262. }
  263. void
  264. tor_tls_context_impl_free_(tor_tls_context_impl_t *ctx)
  265. {
  266. if (!ctx)
  267. return;
  268. PR_Close(ctx);
  269. }
  270. void
  271. tor_tls_get_state_description(tor_tls_t *tls, char *buf, size_t sz)
  272. {
  273. (void)tls;
  274. (void)buf;
  275. (void)sz;
  276. // AFAICT, NSS doesn't expose its internal state.
  277. buf[0]=0;
  278. }
  279. void
  280. tor_tls_init(void)
  281. {
  282. tor_nss_countbytes_init();
  283. }
  284. void
  285. tls_log_errors(tor_tls_t *tls, int severity, int domain,
  286. const char *doing)
  287. {
  288. /* This implementation is a little different for NSS than it is for OpenSSL
  289. -- it logs the last error whether anything actually failed or not. So we
  290. have to only call it when something has gone wrong and we have a real
  291. error to report. */
  292. (void)tls;
  293. PRErrorCode code = PORT_GetError();
  294. const char *addr = tls ? tls->address : NULL;
  295. const char *string = PORT_ErrorToString(code);
  296. const char *name = PORT_ErrorToName(code);
  297. char buf[16];
  298. if (!string)
  299. string = "<unrecognized>";
  300. if (!name) {
  301. tor_snprintf(buf, sizeof(buf), "%d", code);
  302. name = buf;
  303. }
  304. const char *with = addr ? " with " : "";
  305. addr = addr ? addr : "";
  306. if (doing) {
  307. log_fn(severity, domain, "TLS error %s while %s%s%s: %s",
  308. name, doing, with, addr, string);
  309. } else {
  310. log_fn(severity, domain, "TLS error %s%s%s: %s", name, string,
  311. with, addr);
  312. }
  313. }
  314. tor_tls_t *
  315. tor_tls_new(tor_socket_t sock, int is_server)
  316. {
  317. (void)sock;
  318. tor_tls_context_t *ctx = tor_tls_context_get(is_server);
  319. PRFileDesc *tcp = NULL;
  320. if (SOCKET_OK(sock)) {
  321. tcp = PR_ImportTCPSocket(sock);
  322. } else {
  323. tcp = PR_NewTCPSocket();
  324. }
  325. if (!tcp)
  326. return NULL;
  327. PRFileDesc *count = tor_wrap_prfiledesc_with_byte_counter(tcp);
  328. if (! count)
  329. return NULL;
  330. PRFileDesc *ssl = SSL_ImportFD(ctx->ctx, count);
  331. if (!ssl) {
  332. PR_Close(tcp);
  333. return NULL;
  334. }
  335. tor_tls_t *tls = tor_malloc_zero(sizeof(tor_tls_t));
  336. tls->magic = TOR_TLS_MAGIC;
  337. tls->context = ctx;
  338. tor_tls_context_incref(ctx);
  339. tls->ssl = ssl;
  340. tls->socket = sock;
  341. tls->state = TOR_TLS_ST_HANDSHAKE;
  342. tls->isServer = !!is_server;
  343. if (!is_server) {
  344. /* Set a random SNI */
  345. char *fake_hostname = crypto_random_hostname(4,25, "www.",".com");
  346. SSL_SetURL(tls->ssl, fake_hostname);
  347. tor_free(fake_hostname);
  348. }
  349. SECStatus s = SSL_ResetHandshake(ssl, is_server ? PR_TRUE : PR_FALSE);
  350. if (s != SECSuccess) {
  351. tls_log_errors(tls, LOG_WARN, LD_CRYPTO, "resetting handshake state");
  352. }
  353. return tls;
  354. }
  355. void
  356. tor_tls_set_renegotiate_callback(tor_tls_t *tls,
  357. void (*cb)(tor_tls_t *, void *arg),
  358. void *arg)
  359. {
  360. tor_assert(tls);
  361. (void)cb;
  362. (void)arg;
  363. /* We don't support renegotiation-based TLS with NSS. */
  364. }
  365. /**
  366. * Tell the TLS library that the underlying socket for <b>tls</b> has been
  367. * closed, and the library should not attempt to free that socket itself.
  368. */
  369. void
  370. tor_tls_release_socket(tor_tls_t *tls)
  371. {
  372. if (! tls)
  373. return;
  374. /* NSS doesn't have the equivalent of BIO_NO_CLOSE. If you replace the
  375. * fd with something that's invalid, it causes a memory leak in PR_Close.
  376. *
  377. * If there were a way to put the PRFileDesc into the CLOSED state, that
  378. * would prevent it from closing its fd -- but there doesn't seem to be a
  379. * supported way to do that either.
  380. *
  381. * So instead: we make a new sacrificial socket, and replace the original
  382. * socket with that one. This seems to be the best we can do, until we
  383. * redesign the mainloop code enough to make this function unnecessary.
  384. */
  385. tor_socket_t sock =
  386. tor_open_socket_nonblocking(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  387. if (! SOCKET_OK(sock)) {
  388. log_warn(LD_NET, "Out of sockets when trying to shut down an NSS "
  389. "connection");
  390. return;
  391. }
  392. PRFileDesc *tcp = PR_GetIdentitiesLayer(tls->ssl, PR_NSPR_IO_LAYER);
  393. if (BUG(! tcp)) {
  394. tor_close_socket(sock);
  395. return;
  396. }
  397. PR_ChangeFileDescNativeHandle(tcp, sock);
  398. /* Tell our socket accounting layer that we don't own this socket any more:
  399. * NSS is about to free it for us. */
  400. tor_release_socket_ownership(sock);
  401. }
  402. void
  403. tor_tls_impl_free_(tor_tls_impl_t *tls)
  404. {
  405. // XXXX This will close the underlying fd, which our OpenSSL version does
  406. // not do!
  407. if (!tls)
  408. return;
  409. PR_Close(tls);
  410. }
  411. int
  412. tor_tls_peer_has_cert(tor_tls_t *tls)
  413. {
  414. CERTCertificate *cert = SSL_PeerCertificate(tls->ssl);
  415. int result = (cert != NULL);
  416. CERT_DestroyCertificate(cert);
  417. return result;
  418. }
  419. MOCK_IMPL(tor_x509_cert_t *,
  420. tor_tls_get_peer_cert,(tor_tls_t *tls))
  421. {
  422. CERTCertificate *cert = SSL_PeerCertificate(tls->ssl);
  423. if (cert)
  424. return tor_x509_cert_new(cert);
  425. else
  426. return NULL;
  427. }
  428. MOCK_IMPL(tor_x509_cert_t *,
  429. tor_tls_get_own_cert,(tor_tls_t *tls))
  430. {
  431. tor_assert(tls);
  432. CERTCertificate *cert = SSL_LocalCertificate(tls->ssl);
  433. if (cert)
  434. return tor_x509_cert_new(cert);
  435. else
  436. return NULL;
  437. }
  438. MOCK_IMPL(int,
  439. tor_tls_read, (tor_tls_t *tls, char *cp, size_t len))
  440. {
  441. tor_assert(tls);
  442. tor_assert(cp);
  443. tor_assert(len < INT_MAX);
  444. PRInt32 rv = PR_Read(tls->ssl, cp, (int)len);
  445. // log_debug(LD_NET, "PR_Read(%zu) returned %d", n, (int)rv);
  446. if (rv > 0) {
  447. return rv;
  448. }
  449. if (rv == 0)
  450. return TOR_TLS_CLOSE;
  451. PRErrorCode err = PORT_GetError();
  452. if (err == PR_WOULD_BLOCK_ERROR) {
  453. return TOR_TLS_WANTREAD; // XXXX ????
  454. } else {
  455. tls_log_errors(tls, LOG_NOTICE, LD_CRYPTO, "reading"); // XXXX
  456. return TOR_TLS_ERROR_MISC; // ????
  457. }
  458. }
  459. int
  460. tor_tls_write(tor_tls_t *tls, const char *cp, size_t n)
  461. {
  462. tor_assert(tls);
  463. tor_assert(cp || n == 0);
  464. tor_assert(n < INT_MAX);
  465. PRInt32 rv = PR_Write(tls->ssl, cp, (int)n);
  466. // log_debug(LD_NET, "PR_Write(%zu) returned %d", n, (int)rv);
  467. if (rv > 0) {
  468. return rv;
  469. }
  470. if (rv == 0)
  471. return TOR_TLS_ERROR_MISC;
  472. PRErrorCode err = PORT_GetError();
  473. if (err == PR_WOULD_BLOCK_ERROR) {
  474. return TOR_TLS_WANTWRITE; // XXXX ????
  475. } else {
  476. tls_log_errors(tls, LOG_NOTICE, LD_CRYPTO, "writing"); // XXXX
  477. return TOR_TLS_ERROR_MISC; // ????
  478. }
  479. }
  480. int
  481. tor_tls_handshake(tor_tls_t *tls)
  482. {
  483. tor_assert(tls);
  484. tor_assert(tls->state == TOR_TLS_ST_HANDSHAKE);
  485. SECStatus s = SSL_ForceHandshake(tls->ssl);
  486. if (s == SECSuccess) {
  487. tls->state = TOR_TLS_ST_OPEN;
  488. log_debug(LD_NET, "SSL handshake is supposedly complete.");
  489. return tor_tls_finish_handshake(tls);
  490. }
  491. if (PORT_GetError() == PR_WOULD_BLOCK_ERROR)
  492. return TOR_TLS_WANTREAD; /* XXXX What about wantwrite? */
  493. return TOR_TLS_ERROR_MISC; // XXXX
  494. }
  495. int
  496. tor_tls_finish_handshake(tor_tls_t *tls)
  497. {
  498. tor_assert(tls);
  499. // We don't need to do any of the weird handshake nonsense stuff on NSS,
  500. // since we only support recent handshakes.
  501. return TOR_TLS_DONE;
  502. }
  503. void
  504. tor_tls_unblock_renegotiation(tor_tls_t *tls)
  505. {
  506. tor_assert(tls);
  507. /* We don't support renegotiation with NSS. */
  508. }
  509. void
  510. tor_tls_block_renegotiation(tor_tls_t *tls)
  511. {
  512. tor_assert(tls);
  513. /* We don't support renegotiation with NSS. */
  514. }
  515. void
  516. tor_tls_assert_renegotiation_unblocked(tor_tls_t *tls)
  517. {
  518. tor_assert(tls);
  519. /* We don't support renegotiation with NSS. */
  520. }
  521. int
  522. tor_tls_get_pending_bytes(tor_tls_t *tls)
  523. {
  524. tor_assert(tls);
  525. int n = SSL_DataPending(tls->ssl);
  526. if (n < 0) {
  527. tls_log_errors(tls, LOG_WARN, LD_CRYPTO, "looking up pending bytes");
  528. return 0;
  529. }
  530. return (int)n;
  531. }
  532. size_t
  533. tor_tls_get_forced_write_size(tor_tls_t *tls)
  534. {
  535. tor_assert(tls);
  536. /* NSS doesn't have the same "forced write" restriction as openssl. */
  537. return 0;
  538. }
  539. void
  540. tor_tls_get_n_raw_bytes(tor_tls_t *tls,
  541. size_t *n_read, size_t *n_written)
  542. {
  543. tor_assert(tls);
  544. tor_assert(n_read);
  545. tor_assert(n_written);
  546. uint64_t r, w;
  547. if (tor_get_prfiledesc_byte_counts(tls->ssl, &r, &w) < 0) {
  548. *n_read = *n_written = 0;
  549. return;
  550. }
  551. *n_read = (size_t)(r - tls->last_read_count);
  552. *n_written = (size_t)(w - tls->last_write_count);
  553. tls->last_read_count = r;
  554. tls->last_write_count = w;
  555. }
  556. int
  557. tor_tls_get_buffer_sizes(tor_tls_t *tls,
  558. size_t *rbuf_capacity, size_t *rbuf_bytes,
  559. size_t *wbuf_capacity, size_t *wbuf_bytes)
  560. {
  561. tor_assert(tls);
  562. tor_assert(rbuf_capacity);
  563. tor_assert(rbuf_bytes);
  564. tor_assert(wbuf_capacity);
  565. tor_assert(wbuf_bytes);
  566. /* This is an acceptable way to say "we can't measure this." */
  567. return -1;
  568. }
  569. MOCK_IMPL(double,
  570. tls_get_write_overhead_ratio, (void))
  571. {
  572. /* XXX We don't currently have a way to measure this in NSS; we could do that
  573. * XXX with a PRIO layer, but it'll take a little coding. */
  574. return 0.95;
  575. }
  576. int
  577. tor_tls_used_v1_handshake(tor_tls_t *tls)
  578. {
  579. tor_assert(tls);
  580. /* We don't support or allow the V1 handshake with NSS.
  581. */
  582. return 0;
  583. }
  584. int
  585. tor_tls_server_got_renegotiate(tor_tls_t *tls)
  586. {
  587. tor_assert(tls);
  588. return 0; /* We don't support renegotiation with NSS */
  589. }
  590. MOCK_IMPL(int,
  591. tor_tls_cert_matches_key,(const tor_tls_t *tls,
  592. const struct tor_x509_cert_t *cert))
  593. {
  594. tor_assert(tls);
  595. tor_assert(cert);
  596. int rv = 0;
  597. CERTCertificate *peercert = SSL_PeerCertificate(tls->ssl);
  598. if (!peercert)
  599. goto done;
  600. CERTSubjectPublicKeyInfo *peer_info = &peercert->subjectPublicKeyInfo;
  601. CERTSubjectPublicKeyInfo *cert_info = &cert->cert->subjectPublicKeyInfo;
  602. rv = SECOID_CompareAlgorithmID(&peer_info->algorithm,
  603. &cert_info->algorithm) == 0 &&
  604. SECITEM_ItemsAreEqual(&peer_info->subjectPublicKey,
  605. &cert_info->subjectPublicKey);
  606. done:
  607. if (peercert)
  608. CERT_DestroyCertificate(peercert);
  609. return rv;
  610. }
  611. MOCK_IMPL(int,
  612. tor_tls_get_tlssecrets,(tor_tls_t *tls, uint8_t *secrets_out))
  613. {
  614. tor_assert(tls);
  615. tor_assert(secrets_out);
  616. /* There's no way to get this information out of NSS. */
  617. return -1;
  618. }
  619. MOCK_IMPL(int,
  620. tor_tls_export_key_material,(tor_tls_t *tls, uint8_t *secrets_out,
  621. const uint8_t *context,
  622. size_t context_len,
  623. const char *label))
  624. {
  625. tor_assert(tls);
  626. tor_assert(secrets_out);
  627. tor_assert(context);
  628. tor_assert(label);
  629. tor_assert(strlen(label) <= UINT_MAX);
  630. tor_assert(context_len <= UINT_MAX);
  631. SECStatus s;
  632. s = SSL_ExportKeyingMaterial(tls->ssl,
  633. label, (unsigned)strlen(label),
  634. PR_TRUE, context, (unsigned)context_len,
  635. secrets_out, DIGEST256_LEN);
  636. return (s == SECSuccess) ? 0 : -1;
  637. }
  638. const char *
  639. tor_tls_get_ciphersuite_name(tor_tls_t *tls)
  640. {
  641. tor_assert(tls);
  642. SSLChannelInfo channel_info;
  643. SSLCipherSuiteInfo cipher_info;
  644. memset(&channel_info, 0, sizeof(channel_info));
  645. memset(&cipher_info, 0, sizeof(cipher_info));
  646. SECStatus s = SSL_GetChannelInfo(tls->ssl,
  647. &channel_info, sizeof(channel_info));
  648. if (s != SECSuccess)
  649. return NULL;
  650. s = SSL_GetCipherSuiteInfo(channel_info.cipherSuite,
  651. &cipher_info, sizeof(cipher_info));
  652. if (s != SECSuccess)
  653. return NULL;
  654. return cipher_info.cipherSuiteName;
  655. }
  656. /** The group we should use for ecdhe when none was selected. */
  657. #define SEC_OID_TOR_DEFAULT_ECDHE_GROUP SEC_OID_ANSIX962_EC_PRIME256V1
  658. int
  659. evaluate_ecgroup_for_tls(const char *ecgroup)
  660. {
  661. SECOidTag tag;
  662. if (!ecgroup)
  663. tag = SEC_OID_TOR_DEFAULT_ECDHE_GROUP;
  664. else if (!strcasecmp(ecgroup, "P256"))
  665. tag = SEC_OID_ANSIX962_EC_PRIME256V1;
  666. else if (!strcasecmp(ecgroup, "P224"))
  667. tag = SEC_OID_SECG_EC_SECP224R1;
  668. else
  669. return 0;
  670. /* I don't think we need any additional tests here for NSS */
  671. (void) tag;
  672. return 1;
  673. }
  674. static SECStatus
  675. always_accept_cert_cb(void *arg, PRFileDesc *ssl, PRBool checkSig,
  676. PRBool isServer)
  677. {
  678. (void)arg;
  679. (void)ssl;
  680. (void)checkSig;
  681. (void)isServer;
  682. return SECSuccess;
  683. }