tortls_nss.c 18 KB

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