tortls_nss.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. /**
  134. * Return true iff this ciphersuite will be hit by a mozilla bug 1312976,
  135. * which makes TLS key exporters not work with TLS 1.2 non-SHA256
  136. * ciphersuites.
  137. **/
  138. static bool
  139. ciphersuite_has_nss_export_bug(const SSLCipherSuiteInfo *info)
  140. {
  141. /* For more information on the bug, see
  142. https://bugzilla.mozilla.org/show_bug.cgi?id=1312976 */
  143. /* This bug only exists in TLS 1.2. */
  144. if (info->authType == ssl_auth_tls13_any)
  145. return false;
  146. /* Sadly, there's no way to get this information from the
  147. * CipherSuiteInfo object itself other than by looking at the
  148. * name. */
  149. if (strstr(info->cipherSuiteName, "_SHA384") ||
  150. strstr(info->cipherSuiteName, "_SHA512")) {
  151. return true;
  152. }
  153. return false;
  154. }
  155. tor_tls_context_t *
  156. tor_tls_context_new(crypto_pk_t *identity,
  157. unsigned int key_lifetime, unsigned flags, int is_client)
  158. {
  159. SECStatus s;
  160. tor_assert(identity);
  161. tor_tls_init();
  162. tor_tls_context_t *ctx = tor_malloc_zero(sizeof(tor_tls_context_t));
  163. ctx->refcnt = 1;
  164. if (! is_client) {
  165. if (tor_tls_context_init_certificates(ctx, identity,
  166. key_lifetime, flags) < 0) {
  167. goto err;
  168. }
  169. }
  170. {
  171. /* Create the "model" PRFileDesc that we will use to base others on. */
  172. PRFileDesc *tcp = PR_NewTCPSocket();
  173. if (!tcp)
  174. goto err;
  175. ctx->ctx = SSL_ImportFD(NULL, tcp);
  176. if (!ctx->ctx) {
  177. PR_Close(tcp);
  178. goto err;
  179. }
  180. }
  181. // Configure the certificate.
  182. if (!is_client) {
  183. s = SSL_ConfigServerCert(ctx->ctx,
  184. ctx->my_link_cert->cert,
  185. (SECKEYPrivateKey *)
  186. crypto_pk_get_nss_privkey(ctx->link_key),
  187. NULL, /* ExtraServerCertData */
  188. 0 /* DataLen */);
  189. if (s != SECSuccess)
  190. goto err;
  191. }
  192. // We need a certificate from the other side.
  193. if (is_client) {
  194. // XXXX does this do anything?
  195. s = SSL_OptionSet(ctx->ctx, SSL_REQUIRE_CERTIFICATE, PR_TRUE);
  196. if (s != SECSuccess)
  197. goto err;
  198. }
  199. // Always accept other side's cert; we'll check it ourselves in goofy
  200. // tor ways.
  201. s = SSL_AuthCertificateHook(ctx->ctx, always_accept_cert_cb, NULL);
  202. // We allow simultaneous read and write.
  203. s = SSL_OptionSet(ctx->ctx, SSL_ENABLE_FDX, PR_TRUE);
  204. if (s != SECSuccess)
  205. goto err;
  206. // XXXX SSL_ROLLBACK_DETECTION??
  207. // XXXX SSL_ENABLE_ALPN??
  208. // Force client-mode or server_mode.
  209. s = SSL_OptionSet(ctx->ctx,
  210. is_client ? SSL_HANDSHAKE_AS_CLIENT : SSL_HANDSHAKE_AS_SERVER,
  211. PR_TRUE);
  212. if (s != SECSuccess)
  213. goto err;
  214. // Disable everything before TLS 1.0; support everything else.
  215. {
  216. SSLVersionRange vrange;
  217. memset(&vrange, 0, sizeof(vrange));
  218. s = SSL_VersionRangeGetSupported(ssl_variant_stream, &vrange);
  219. if (s != SECSuccess)
  220. goto err;
  221. if (vrange.min < SSL_LIBRARY_VERSION_TLS_1_0)
  222. vrange.min = SSL_LIBRARY_VERSION_TLS_1_0;
  223. s = SSL_VersionRangeSet(ctx->ctx, &vrange);
  224. if (s != SECSuccess)
  225. goto err;
  226. }
  227. // Only support strong ciphers.
  228. {
  229. const PRUint16 *ciphers = SSL_GetImplementedCiphers();
  230. const PRUint16 n_ciphers = SSL_GetNumImplementedCiphers();
  231. PRUint16 i;
  232. for (i = 0; i < n_ciphers; ++i) {
  233. SSLCipherSuiteInfo info;
  234. memset(&info, 0, sizeof(info));
  235. s = SSL_GetCipherSuiteInfo(ciphers[i], &info, sizeof(info));
  236. if (s != SECSuccess)
  237. goto err;
  238. if (BUG(info.cipherSuite != ciphers[i]))
  239. goto err;
  240. int disable = info.effectiveKeyBits < 128 ||
  241. info.macBits < 128 ||
  242. !we_like_ssl_cipher(info.symCipher) ||
  243. !we_like_ssl_kea(info.keaType) ||
  244. !we_like_mac_algorithm(info.macAlgorithm) ||
  245. !we_like_auth_type(info.authType)/* Requires NSS 3.24 */;
  246. if (ciphersuite_has_nss_export_bug(&info)) {
  247. /* SSL_ExportKeyingMaterial will fail; we can't use this cipher.
  248. */
  249. disable = 1;
  250. }
  251. s = SSL_CipherPrefSet(ctx->ctx, ciphers[i],
  252. disable ? PR_FALSE : PR_TRUE);
  253. if (s != SECSuccess)
  254. goto err;
  255. }
  256. }
  257. // Only use DH and ECDH keys once.
  258. s = SSL_OptionSet(ctx->ctx, SSL_REUSE_SERVER_ECDHE_KEY, PR_FALSE);
  259. if (s != SECSuccess)
  260. goto err;
  261. // don't cache sessions.
  262. s = SSL_OptionSet(ctx->ctx, SSL_NO_CACHE, PR_TRUE);
  263. if (s != SECSuccess)
  264. goto err;
  265. // Enable DH.
  266. s = SSL_OptionSet(ctx->ctx, SSL_ENABLE_SERVER_DHE, PR_TRUE);
  267. if (s != SECSuccess)
  268. goto err;
  269. // Set DH and ECDH groups.
  270. SSLNamedGroup groups[] = {
  271. ssl_grp_ec_curve25519,
  272. ssl_grp_ec_secp256r1,
  273. ssl_grp_ec_secp224r1,
  274. ssl_grp_ffdhe_2048,
  275. };
  276. s = SSL_NamedGroupConfig(ctx->ctx, groups, ARRAY_LENGTH(groups));
  277. if (s != SECSuccess)
  278. goto err;
  279. // These features are off by default, so we don't need to disable them:
  280. // Session tickets
  281. // Renegotiation
  282. // Compression
  283. goto done;
  284. err:
  285. tor_tls_context_decref(ctx);
  286. ctx = NULL;
  287. done:
  288. return ctx;
  289. }
  290. void
  291. tor_tls_context_impl_free_(tor_tls_context_impl_t *ctx)
  292. {
  293. if (!ctx)
  294. return;
  295. PR_Close(ctx);
  296. }
  297. void
  298. tor_tls_get_state_description(tor_tls_t *tls, char *buf, size_t sz)
  299. {
  300. (void)tls;
  301. (void)buf;
  302. (void)sz;
  303. // AFAICT, NSS doesn't expose its internal state.
  304. buf[0]=0;
  305. }
  306. void
  307. tor_tls_init(void)
  308. {
  309. tor_nss_countbytes_init();
  310. }
  311. void
  312. tls_log_errors(tor_tls_t *tls, int severity, int domain,
  313. const char *doing)
  314. {
  315. /* This implementation is a little different for NSS than it is for OpenSSL
  316. -- it logs the last error whether anything actually failed or not. So we
  317. have to only call it when something has gone wrong and we have a real
  318. error to report. */
  319. (void)tls;
  320. PRErrorCode code = PORT_GetError();
  321. const char *addr = tls ? tls->address : NULL;
  322. const char *string = PORT_ErrorToString(code);
  323. const char *name = PORT_ErrorToName(code);
  324. char buf[16];
  325. if (!string)
  326. string = "<unrecognized>";
  327. if (!name) {
  328. tor_snprintf(buf, sizeof(buf), "%d", code);
  329. name = buf;
  330. }
  331. const char *with = addr ? " with " : "";
  332. addr = addr ? addr : "";
  333. if (doing) {
  334. log_fn(severity, domain, "TLS error %s while %s%s%s: %s",
  335. name, doing, with, addr, string);
  336. } else {
  337. log_fn(severity, domain, "TLS error %s%s%s: %s", name, string,
  338. with, addr);
  339. }
  340. }
  341. tor_tls_t *
  342. tor_tls_new(tor_socket_t sock, int is_server)
  343. {
  344. (void)sock;
  345. tor_tls_context_t *ctx = tor_tls_context_get(is_server);
  346. PRFileDesc *tcp = NULL;
  347. if (SOCKET_OK(sock)) {
  348. tcp = PR_ImportTCPSocket(sock);
  349. } else {
  350. tcp = PR_NewTCPSocket();
  351. }
  352. if (!tcp)
  353. return NULL;
  354. PRFileDesc *count = tor_wrap_prfiledesc_with_byte_counter(tcp);
  355. if (! count)
  356. return NULL;
  357. PRFileDesc *ssl = SSL_ImportFD(ctx->ctx, count);
  358. if (!ssl) {
  359. PR_Close(tcp);
  360. return NULL;
  361. }
  362. tor_tls_t *tls = tor_malloc_zero(sizeof(tor_tls_t));
  363. tls->magic = TOR_TLS_MAGIC;
  364. tls->context = ctx;
  365. tor_tls_context_incref(ctx);
  366. tls->ssl = ssl;
  367. tls->socket = sock;
  368. tls->state = TOR_TLS_ST_HANDSHAKE;
  369. tls->isServer = !!is_server;
  370. if (!is_server) {
  371. /* Set a random SNI */
  372. char *fake_hostname = crypto_random_hostname(4,25, "www.",".com");
  373. SSL_SetURL(tls->ssl, fake_hostname);
  374. tor_free(fake_hostname);
  375. }
  376. SECStatus s = SSL_ResetHandshake(ssl, is_server ? PR_TRUE : PR_FALSE);
  377. if (s != SECSuccess) {
  378. tls_log_errors(tls, LOG_WARN, LD_CRYPTO, "resetting handshake state");
  379. }
  380. return tls;
  381. }
  382. void
  383. tor_tls_set_renegotiate_callback(tor_tls_t *tls,
  384. void (*cb)(tor_tls_t *, void *arg),
  385. void *arg)
  386. {
  387. tor_assert(tls);
  388. (void)cb;
  389. (void)arg;
  390. /* We don't support renegotiation-based TLS with NSS. */
  391. }
  392. /**
  393. * Tell the TLS library that the underlying socket for <b>tls</b> has been
  394. * closed, and the library should not attempt to free that socket itself.
  395. */
  396. void
  397. tor_tls_release_socket(tor_tls_t *tls)
  398. {
  399. if (! tls)
  400. return;
  401. /* NSS doesn't have the equivalent of BIO_NO_CLOSE. If you replace the
  402. * fd with something that's invalid, it causes a memory leak in PR_Close.
  403. *
  404. * If there were a way to put the PRFileDesc into the CLOSED state, that
  405. * would prevent it from closing its fd -- but there doesn't seem to be a
  406. * supported way to do that either.
  407. *
  408. * So instead: we make a new sacrificial socket, and replace the original
  409. * socket with that one. This seems to be the best we can do, until we
  410. * redesign the mainloop code enough to make this function unnecessary.
  411. */
  412. tor_socket_t sock =
  413. tor_open_socket_nonblocking(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  414. if (! SOCKET_OK(sock)) {
  415. log_warn(LD_NET, "Out of sockets when trying to shut down an NSS "
  416. "connection");
  417. return;
  418. }
  419. PRFileDesc *tcp = PR_GetIdentitiesLayer(tls->ssl, PR_NSPR_IO_LAYER);
  420. if (BUG(! tcp)) {
  421. tor_close_socket(sock);
  422. return;
  423. }
  424. PR_ChangeFileDescNativeHandle(tcp, sock);
  425. /* Tell our socket accounting layer that we don't own this socket any more:
  426. * NSS is about to free it for us. */
  427. tor_release_socket_ownership(sock);
  428. }
  429. void
  430. tor_tls_impl_free_(tor_tls_impl_t *tls)
  431. {
  432. // XXXX This will close the underlying fd, which our OpenSSL version does
  433. // not do!
  434. if (!tls)
  435. return;
  436. PR_Close(tls);
  437. }
  438. int
  439. tor_tls_peer_has_cert(tor_tls_t *tls)
  440. {
  441. CERTCertificate *cert = SSL_PeerCertificate(tls->ssl);
  442. int result = (cert != NULL);
  443. CERT_DestroyCertificate(cert);
  444. return result;
  445. }
  446. MOCK_IMPL(tor_x509_cert_t *,
  447. tor_tls_get_peer_cert,(tor_tls_t *tls))
  448. {
  449. CERTCertificate *cert = SSL_PeerCertificate(tls->ssl);
  450. if (cert)
  451. return tor_x509_cert_new(cert);
  452. else
  453. return NULL;
  454. }
  455. MOCK_IMPL(tor_x509_cert_t *,
  456. tor_tls_get_own_cert,(tor_tls_t *tls))
  457. {
  458. tor_assert(tls);
  459. CERTCertificate *cert = SSL_LocalCertificate(tls->ssl);
  460. if (cert)
  461. return tor_x509_cert_new(cert);
  462. else
  463. return NULL;
  464. }
  465. MOCK_IMPL(int,
  466. tor_tls_read, (tor_tls_t *tls, char *cp, size_t len))
  467. {
  468. tor_assert(tls);
  469. tor_assert(cp);
  470. tor_assert(len < INT_MAX);
  471. PRInt32 rv = PR_Read(tls->ssl, cp, (int)len);
  472. // log_debug(LD_NET, "PR_Read(%zu) returned %d", n, (int)rv);
  473. if (rv > 0) {
  474. return rv;
  475. }
  476. if (rv == 0)
  477. return TOR_TLS_CLOSE;
  478. PRErrorCode err = PORT_GetError();
  479. if (err == PR_WOULD_BLOCK_ERROR) {
  480. return TOR_TLS_WANTREAD; // XXXX ????
  481. } else {
  482. tls_log_errors(tls, LOG_NOTICE, LD_CRYPTO, "reading"); // XXXX
  483. return TOR_TLS_ERROR_MISC; // ????
  484. }
  485. }
  486. int
  487. tor_tls_write(tor_tls_t *tls, const char *cp, size_t n)
  488. {
  489. tor_assert(tls);
  490. tor_assert(cp || n == 0);
  491. tor_assert(n < INT_MAX);
  492. PRInt32 rv = PR_Write(tls->ssl, cp, (int)n);
  493. // log_debug(LD_NET, "PR_Write(%zu) returned %d", n, (int)rv);
  494. if (rv > 0) {
  495. return rv;
  496. }
  497. if (rv == 0)
  498. return TOR_TLS_ERROR_MISC;
  499. PRErrorCode err = PORT_GetError();
  500. if (err == PR_WOULD_BLOCK_ERROR) {
  501. return TOR_TLS_WANTWRITE; // XXXX ????
  502. } else {
  503. tls_log_errors(tls, LOG_NOTICE, LD_CRYPTO, "writing"); // XXXX
  504. return TOR_TLS_ERROR_MISC; // ????
  505. }
  506. }
  507. int
  508. tor_tls_handshake(tor_tls_t *tls)
  509. {
  510. tor_assert(tls);
  511. tor_assert(tls->state == TOR_TLS_ST_HANDSHAKE);
  512. SECStatus s = SSL_ForceHandshake(tls->ssl);
  513. if (s == SECSuccess) {
  514. tls->state = TOR_TLS_ST_OPEN;
  515. log_debug(LD_NET, "SSL handshake is supposedly complete.");
  516. return tor_tls_finish_handshake(tls);
  517. }
  518. if (PORT_GetError() == PR_WOULD_BLOCK_ERROR)
  519. return TOR_TLS_WANTREAD; /* XXXX What about wantwrite? */
  520. return TOR_TLS_ERROR_MISC; // XXXX
  521. }
  522. int
  523. tor_tls_finish_handshake(tor_tls_t *tls)
  524. {
  525. tor_assert(tls);
  526. // We don't need to do any of the weird handshake nonsense stuff on NSS,
  527. // since we only support recent handshakes.
  528. return TOR_TLS_DONE;
  529. }
  530. void
  531. tor_tls_unblock_renegotiation(tor_tls_t *tls)
  532. {
  533. tor_assert(tls);
  534. /* We don't support renegotiation with NSS. */
  535. }
  536. void
  537. tor_tls_block_renegotiation(tor_tls_t *tls)
  538. {
  539. tor_assert(tls);
  540. /* We don't support renegotiation with NSS. */
  541. }
  542. void
  543. tor_tls_assert_renegotiation_unblocked(tor_tls_t *tls)
  544. {
  545. tor_assert(tls);
  546. /* We don't support renegotiation with NSS. */
  547. }
  548. int
  549. tor_tls_get_pending_bytes(tor_tls_t *tls)
  550. {
  551. tor_assert(tls);
  552. int n = SSL_DataPending(tls->ssl);
  553. if (n < 0) {
  554. tls_log_errors(tls, LOG_WARN, LD_CRYPTO, "looking up pending bytes");
  555. return 0;
  556. }
  557. return (int)n;
  558. }
  559. size_t
  560. tor_tls_get_forced_write_size(tor_tls_t *tls)
  561. {
  562. tor_assert(tls);
  563. /* NSS doesn't have the same "forced write" restriction as openssl. */
  564. return 0;
  565. }
  566. void
  567. tor_tls_get_n_raw_bytes(tor_tls_t *tls,
  568. size_t *n_read, size_t *n_written)
  569. {
  570. tor_assert(tls);
  571. tor_assert(n_read);
  572. tor_assert(n_written);
  573. uint64_t r, w;
  574. if (tor_get_prfiledesc_byte_counts(tls->ssl, &r, &w) < 0) {
  575. *n_read = *n_written = 0;
  576. return;
  577. }
  578. *n_read = (size_t)(r - tls->last_read_count);
  579. *n_written = (size_t)(w - tls->last_write_count);
  580. tls->last_read_count = r;
  581. tls->last_write_count = w;
  582. }
  583. int
  584. tor_tls_get_buffer_sizes(tor_tls_t *tls,
  585. size_t *rbuf_capacity, size_t *rbuf_bytes,
  586. size_t *wbuf_capacity, size_t *wbuf_bytes)
  587. {
  588. tor_assert(tls);
  589. tor_assert(rbuf_capacity);
  590. tor_assert(rbuf_bytes);
  591. tor_assert(wbuf_capacity);
  592. tor_assert(wbuf_bytes);
  593. /* This is an acceptable way to say "we can't measure this." */
  594. return -1;
  595. }
  596. MOCK_IMPL(double,
  597. tls_get_write_overhead_ratio, (void))
  598. {
  599. /* XXX We don't currently have a way to measure this in NSS; we could do that
  600. * XXX with a PRIO layer, but it'll take a little coding. */
  601. return 0.95;
  602. }
  603. int
  604. tor_tls_used_v1_handshake(tor_tls_t *tls)
  605. {
  606. tor_assert(tls);
  607. /* We don't support or allow the V1 handshake with NSS.
  608. */
  609. return 0;
  610. }
  611. int
  612. tor_tls_server_got_renegotiate(tor_tls_t *tls)
  613. {
  614. tor_assert(tls);
  615. return 0; /* We don't support renegotiation with NSS */
  616. }
  617. MOCK_IMPL(int,
  618. tor_tls_cert_matches_key,(const tor_tls_t *tls,
  619. const struct tor_x509_cert_t *cert))
  620. {
  621. tor_assert(tls);
  622. tor_assert(cert);
  623. int rv = 0;
  624. CERTCertificate *peercert = SSL_PeerCertificate(tls->ssl);
  625. if (!peercert)
  626. goto done;
  627. CERTSubjectPublicKeyInfo *peer_info = &peercert->subjectPublicKeyInfo;
  628. CERTSubjectPublicKeyInfo *cert_info = &cert->cert->subjectPublicKeyInfo;
  629. rv = SECOID_CompareAlgorithmID(&peer_info->algorithm,
  630. &cert_info->algorithm) == 0 &&
  631. SECITEM_ItemsAreEqual(&peer_info->subjectPublicKey,
  632. &cert_info->subjectPublicKey);
  633. done:
  634. if (peercert)
  635. CERT_DestroyCertificate(peercert);
  636. return rv;
  637. }
  638. MOCK_IMPL(int,
  639. tor_tls_get_tlssecrets,(tor_tls_t *tls, uint8_t *secrets_out))
  640. {
  641. tor_assert(tls);
  642. tor_assert(secrets_out);
  643. /* There's no way to get this information out of NSS. */
  644. return -1;
  645. }
  646. MOCK_IMPL(int,
  647. tor_tls_export_key_material,(tor_tls_t *tls, uint8_t *secrets_out,
  648. const uint8_t *context,
  649. size_t context_len,
  650. const char *label))
  651. {
  652. tor_assert(tls);
  653. tor_assert(secrets_out);
  654. tor_assert(context);
  655. tor_assert(label);
  656. tor_assert(strlen(label) <= UINT_MAX);
  657. tor_assert(context_len <= UINT_MAX);
  658. SECStatus s;
  659. /* Make sure that the error code is set here, so that we can be sure that
  660. * any error code set after a failure was in fact caused by
  661. * SSL_ExportKeyingMaterial. */
  662. PR_SetError(PR_UNKNOWN_ERROR, 0);
  663. s = SSL_ExportKeyingMaterial(tls->ssl,
  664. label, (unsigned)strlen(label),
  665. PR_TRUE, context, (unsigned)context_len,
  666. secrets_out, DIGEST256_LEN);
  667. if (s != SECSuccess) {
  668. tls_log_errors(tls, LOG_WARN, LD_CRYPTO,
  669. "exporting key material for a TLS handshake");
  670. }
  671. return (s == SECSuccess) ? 0 : -1;
  672. }
  673. const char *
  674. tor_tls_get_ciphersuite_name(tor_tls_t *tls)
  675. {
  676. tor_assert(tls);
  677. SSLChannelInfo channel_info;
  678. SSLCipherSuiteInfo cipher_info;
  679. memset(&channel_info, 0, sizeof(channel_info));
  680. memset(&cipher_info, 0, sizeof(cipher_info));
  681. SECStatus s = SSL_GetChannelInfo(tls->ssl,
  682. &channel_info, sizeof(channel_info));
  683. if (s != SECSuccess)
  684. return NULL;
  685. s = SSL_GetCipherSuiteInfo(channel_info.cipherSuite,
  686. &cipher_info, sizeof(cipher_info));
  687. if (s != SECSuccess)
  688. return NULL;
  689. return cipher_info.cipherSuiteName;
  690. }
  691. /** The group we should use for ecdhe when none was selected. */
  692. #define SEC_OID_TOR_DEFAULT_ECDHE_GROUP SEC_OID_ANSIX962_EC_PRIME256V1
  693. int
  694. evaluate_ecgroup_for_tls(const char *ecgroup)
  695. {
  696. SECOidTag tag;
  697. if (!ecgroup)
  698. tag = SEC_OID_TOR_DEFAULT_ECDHE_GROUP;
  699. else if (!strcasecmp(ecgroup, "P256"))
  700. tag = SEC_OID_ANSIX962_EC_PRIME256V1;
  701. else if (!strcasecmp(ecgroup, "P224"))
  702. tag = SEC_OID_SECG_EC_SECP224R1;
  703. else
  704. return 0;
  705. /* I don't think we need any additional tests here for NSS */
  706. (void) tag;
  707. return 1;
  708. }
  709. static SECStatus
  710. always_accept_cert_cb(void *arg, PRFileDesc *ssl, PRBool checkSig,
  711. PRBool isServer)
  712. {
  713. (void)arg;
  714. (void)ssl;
  715. (void)checkSig;
  716. (void)isServer;
  717. return SECSuccess;
  718. }