tortls_nss.c 20 KB

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