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. //
  391. // /* We don't support renegotiation-based TLS with NSS. */
  392. //}
  393. /**
  394. * Tell the TLS library that the underlying socket for <b>tls</b> has been
  395. * closed, and the library should not attempt to free that socket itself.
  396. */
  397. void
  398. tor_tls_release_socket(tor_tls_t *tls)
  399. {
  400. if (! tls)
  401. return;
  402. /* NSS doesn't have the equivalent of BIO_NO_CLOSE. If you replace the
  403. * fd with something that's invalid, it causes a memory leak in PR_Close.
  404. *
  405. * If there were a way to put the PRFileDesc into the CLOSED state, that
  406. * would prevent it from closing its fd -- but there doesn't seem to be a
  407. * supported way to do that either.
  408. *
  409. * So instead: we make a new sacrificial socket, and replace the original
  410. * socket with that one. This seems to be the best we can do, until we
  411. * redesign the mainloop code enough to make this function unnecessary.
  412. */
  413. tor_socket_t sock =
  414. tor_open_socket_nonblocking(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  415. if (! SOCKET_OK(sock)) {
  416. log_warn(LD_NET, "Out of sockets when trying to shut down an NSS "
  417. "connection");
  418. return;
  419. }
  420. PRFileDesc *tcp = PR_GetIdentitiesLayer(tls->ssl, PR_NSPR_IO_LAYER);
  421. if (BUG(! tcp)) {
  422. tor_close_socket(sock);
  423. return;
  424. }
  425. PR_ChangeFileDescNativeHandle(tcp, sock);
  426. /* Tell our socket accounting layer that we don't own this socket any more:
  427. * NSS is about to free it for us. */
  428. tor_release_socket_ownership(sock);
  429. }
  430. void
  431. tor_tls_impl_free_(tor_tls_impl_t *tls)
  432. {
  433. // XXXX This will close the underlying fd, which our OpenSSL version does
  434. // not do!
  435. if (!tls)
  436. return;
  437. PR_Close(tls);
  438. }
  439. int
  440. tor_tls_peer_has_cert(tor_tls_t *tls)
  441. {
  442. CERTCertificate *cert = SSL_PeerCertificate(tls->ssl);
  443. int result = (cert != NULL);
  444. CERT_DestroyCertificate(cert);
  445. return result;
  446. }
  447. MOCK_IMPL(tor_x509_cert_t *,
  448. tor_tls_get_peer_cert,(tor_tls_t *tls))
  449. {
  450. CERTCertificate *cert = SSL_PeerCertificate(tls->ssl);
  451. if (cert)
  452. return tor_x509_cert_new(cert);
  453. else
  454. return NULL;
  455. }
  456. MOCK_IMPL(tor_x509_cert_t *,
  457. tor_tls_get_own_cert,(tor_tls_t *tls))
  458. {
  459. tor_assert(tls);
  460. CERTCertificate *cert = SSL_LocalCertificate(tls->ssl);
  461. if (cert)
  462. return tor_x509_cert_new(cert);
  463. else
  464. return NULL;
  465. }
  466. MOCK_IMPL(int,
  467. tor_tls_read, (tor_tls_t *tls, char *cp, size_t len))
  468. {
  469. tor_assert(tls);
  470. tor_assert(cp);
  471. tor_assert(len < INT_MAX);
  472. PRInt32 rv = PR_Read(tls->ssl, cp, (int)len);
  473. // log_debug(LD_NET, "PR_Read(%zu) returned %d", n, (int)rv);
  474. if (rv > 0) {
  475. return rv;
  476. }
  477. if (rv == 0)
  478. return TOR_TLS_CLOSE;
  479. PRErrorCode err = PORT_GetError();
  480. if (err == PR_WOULD_BLOCK_ERROR) {
  481. return TOR_TLS_WANTREAD; // XXXX ????
  482. } else {
  483. tls_log_errors(tls, LOG_NOTICE, LD_CRYPTO, "reading"); // XXXX
  484. return TOR_TLS_ERROR_MISC; // ????
  485. }
  486. }
  487. int
  488. tor_tls_write(tor_tls_t *tls, const char *cp, size_t n)
  489. {
  490. tor_assert(tls);
  491. tor_assert(cp || n == 0);
  492. tor_assert(n < INT_MAX);
  493. PRInt32 rv = PR_Write(tls->ssl, cp, (int)n);
  494. // log_debug(LD_NET, "PR_Write(%zu) returned %d", n, (int)rv);
  495. if (rv > 0) {
  496. return rv;
  497. }
  498. if (rv == 0)
  499. return TOR_TLS_ERROR_MISC;
  500. PRErrorCode err = PORT_GetError();
  501. if (err == PR_WOULD_BLOCK_ERROR) {
  502. return TOR_TLS_WANTWRITE; // XXXX ????
  503. } else {
  504. tls_log_errors(tls, LOG_NOTICE, LD_CRYPTO, "writing"); // XXXX
  505. return TOR_TLS_ERROR_MISC; // ????
  506. }
  507. }
  508. int
  509. tor_tls_handshake(tor_tls_t *tls)
  510. {
  511. tor_assert(tls);
  512. tor_assert(tls->state == TOR_TLS_ST_HANDSHAKE);
  513. SECStatus s = SSL_ForceHandshake(tls->ssl);
  514. if (s == SECSuccess) {
  515. tls->state = TOR_TLS_ST_OPEN;
  516. log_debug(LD_NET, "SSL handshake is supposedly complete.");
  517. return tor_tls_finish_handshake(tls);
  518. }
  519. if (PORT_GetError() == PR_WOULD_BLOCK_ERROR)
  520. return TOR_TLS_WANTREAD; /* XXXX What about wantwrite? */
  521. return TOR_TLS_ERROR_MISC; // XXXX
  522. }
  523. int
  524. tor_tls_finish_handshake(tor_tls_t *tls)
  525. {
  526. tor_assert(tls);
  527. // We don't need to do any of the weird handshake nonsense stuff on NSS,
  528. // since we only support recent handshakes.
  529. return TOR_TLS_DONE;
  530. }
  531. void
  532. tor_tls_unblock_renegotiation(tor_tls_t *tls)
  533. {
  534. tor_assert(tls);
  535. /* We don't support renegotiation with NSS. */
  536. }
  537. void
  538. tor_tls_block_renegotiation(tor_tls_t *tls)
  539. {
  540. tor_assert(tls);
  541. /* We don't support renegotiation with NSS. */
  542. }
  543. void
  544. tor_tls_assert_renegotiation_unblocked(tor_tls_t *tls)
  545. {
  546. tor_assert(tls);
  547. /* We don't support renegotiation with NSS. */
  548. }
  549. int
  550. tor_tls_get_pending_bytes(tor_tls_t *tls)
  551. {
  552. tor_assert(tls);
  553. int n = SSL_DataPending(tls->ssl);
  554. if (n < 0) {
  555. tls_log_errors(tls, LOG_WARN, LD_CRYPTO, "looking up pending bytes");
  556. return 0;
  557. }
  558. return (int)n;
  559. }
  560. size_t
  561. tor_tls_get_forced_write_size(tor_tls_t *tls)
  562. {
  563. tor_assert(tls);
  564. /* NSS doesn't have the same "forced write" restriction as openssl. */
  565. return 0;
  566. }
  567. void
  568. tor_tls_get_n_raw_bytes(tor_tls_t *tls,
  569. size_t *n_read, size_t *n_written)
  570. {
  571. tor_assert(tls);
  572. tor_assert(n_read);
  573. tor_assert(n_written);
  574. uint64_t r, w;
  575. if (tor_get_prfiledesc_byte_counts(tls->ssl, &r, &w) < 0) {
  576. *n_read = *n_written = 0;
  577. return;
  578. }
  579. *n_read = (size_t)(r - tls->last_read_count);
  580. *n_written = (size_t)(w - tls->last_write_count);
  581. tls->last_read_count = r;
  582. tls->last_write_count = w;
  583. }
  584. int
  585. tor_tls_get_buffer_sizes(tor_tls_t *tls,
  586. size_t *rbuf_capacity, size_t *rbuf_bytes,
  587. size_t *wbuf_capacity, size_t *wbuf_bytes)
  588. {
  589. tor_assert(tls);
  590. tor_assert(rbuf_capacity);
  591. tor_assert(rbuf_bytes);
  592. tor_assert(wbuf_capacity);
  593. tor_assert(wbuf_bytes);
  594. /* This is an acceptable way to say "we can't measure this." */
  595. return -1;
  596. }
  597. MOCK_IMPL(double,
  598. tls_get_write_overhead_ratio, (void))
  599. {
  600. /* XXX We don't currently have a way to measure this in NSS; we could do that
  601. * XXX with a PRIO layer, but it'll take a little coding. */
  602. return 0.95;
  603. }
  604. int
  605. tor_tls_used_v1_handshake(tor_tls_t *tls)
  606. {
  607. tor_assert(tls);
  608. /* We don't support or allow the V1 handshake with NSS.
  609. */
  610. return 0;
  611. }
  612. int
  613. tor_tls_server_got_renegotiate(tor_tls_t *tls)
  614. {
  615. tor_assert(tls);
  616. return 0; /* We don't support renegotiation with NSS */
  617. }
  618. MOCK_IMPL(int,
  619. tor_tls_cert_matches_key,(const tor_tls_t *tls,
  620. const struct tor_x509_cert_t *cert))
  621. {
  622. tor_assert(tls);
  623. tor_assert(cert);
  624. int rv = 0;
  625. CERTCertificate *peercert = SSL_PeerCertificate(tls->ssl);
  626. if (!peercert)
  627. goto done;
  628. CERTSubjectPublicKeyInfo *peer_info = &peercert->subjectPublicKeyInfo;
  629. CERTSubjectPublicKeyInfo *cert_info = &cert->cert->subjectPublicKeyInfo;
  630. rv = SECOID_CompareAlgorithmID(&peer_info->algorithm,
  631. &cert_info->algorithm) == 0 &&
  632. SECITEM_ItemsAreEqual(&peer_info->subjectPublicKey,
  633. &cert_info->subjectPublicKey);
  634. done:
  635. if (peercert)
  636. CERT_DestroyCertificate(peercert);
  637. return rv;
  638. }
  639. MOCK_IMPL(int,
  640. tor_tls_get_tlssecrets,(tor_tls_t *tls, uint8_t *secrets_out))
  641. {
  642. tor_assert(tls);
  643. tor_assert(secrets_out);
  644. /* There's no way to get this information out of NSS. */
  645. return -1;
  646. }
  647. MOCK_IMPL(int,
  648. tor_tls_export_key_material,(tor_tls_t *tls, uint8_t *secrets_out,
  649. const uint8_t *context,
  650. size_t context_len,
  651. const char *label))
  652. {
  653. tor_assert(tls);
  654. tor_assert(secrets_out);
  655. tor_assert(context);
  656. tor_assert(label);
  657. tor_assert(strlen(label) <= UINT_MAX);
  658. tor_assert(context_len <= UINT_MAX);
  659. SECStatus s;
  660. /* Make sure that the error code is set here, so that we can be sure that
  661. * any error code set after a failure was in fact caused by
  662. * SSL_ExportKeyingMaterial. */
  663. PR_SetError(PR_UNKNOWN_ERROR, 0);
  664. s = SSL_ExportKeyingMaterial(tls->ssl,
  665. label, (unsigned)strlen(label),
  666. PR_TRUE, context, (unsigned)context_len,
  667. secrets_out, DIGEST256_LEN);
  668. if (s != SECSuccess) {
  669. tls_log_errors(tls, LOG_WARN, LD_CRYPTO,
  670. "exporting key material for a TLS handshake");
  671. }
  672. return (s == SECSuccess) ? 0 : -1;
  673. }
  674. const char *
  675. tor_tls_get_ciphersuite_name(tor_tls_t *tls)
  676. {
  677. tor_assert(tls);
  678. SSLChannelInfo channel_info;
  679. SSLCipherSuiteInfo cipher_info;
  680. memset(&channel_info, 0, sizeof(channel_info));
  681. memset(&cipher_info, 0, sizeof(cipher_info));
  682. SECStatus s = SSL_GetChannelInfo(tls->ssl,
  683. &channel_info, sizeof(channel_info));
  684. if (s != SECSuccess)
  685. return NULL;
  686. s = SSL_GetCipherSuiteInfo(channel_info.cipherSuite,
  687. &cipher_info, sizeof(cipher_info));
  688. if (s != SECSuccess)
  689. return NULL;
  690. return cipher_info.cipherSuiteName;
  691. }
  692. /** The group we should use for ecdhe when none was selected. */
  693. #define SEC_OID_TOR_DEFAULT_ECDHE_GROUP SEC_OID_ANSIX962_EC_PRIME256V1
  694. int
  695. evaluate_ecgroup_for_tls(const char *ecgroup)
  696. {
  697. SECOidTag tag;
  698. if (!ecgroup)
  699. tag = SEC_OID_TOR_DEFAULT_ECDHE_GROUP;
  700. else if (!strcasecmp(ecgroup, "P256"))
  701. tag = SEC_OID_ANSIX962_EC_PRIME256V1;
  702. else if (!strcasecmp(ecgroup, "P224"))
  703. tag = SEC_OID_SECG_EC_SECP224R1;
  704. else
  705. return 0;
  706. /* I don't think we need any additional tests here for NSS */
  707. (void) tag;
  708. return 1;
  709. }
  710. static SECStatus
  711. always_accept_cert_cb(void *arg, PRFileDesc *ssl, PRBool checkSig,
  712. PRBool isServer)
  713. {
  714. (void)arg;
  715. (void)ssl;
  716. (void)checkSig;
  717. (void)isServer;
  718. return SECSuccess;
  719. }