tortls.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. #define TORTLS_PRIVATE
  6. #define TOR_X509_PRIVATE
  7. #include "lib/tls/x509.h"
  8. #include "lib/tls/x509_internal.h"
  9. #include "lib/tls/tortls_sys.h"
  10. #include "lib/tls/tortls.h"
  11. #include "lib/tls/tortls_st.h"
  12. #include "lib/tls/tortls_internal.h"
  13. #include "lib/log/util_bug.h"
  14. #include "lib/intmath/cmp.h"
  15. #include "lib/crypt_ops/crypto_rsa.h"
  16. #include "lib/crypt_ops/crypto_rand.h"
  17. #include "lib/net/socket.h"
  18. #include "lib/subsys/subsys.h"
  19. #ifdef _WIN32
  20. #include <winsock2.h>
  21. #include <ws2tcpip.h>
  22. #endif
  23. #include <time.h>
  24. /** Global TLS contexts. We keep them here because nobody else needs
  25. * to touch them.
  26. *
  27. * @{ */
  28. STATIC tor_tls_context_t *server_tls_context = NULL;
  29. STATIC tor_tls_context_t *client_tls_context = NULL;
  30. /**@}*/
  31. /**
  32. * Return the appropriate TLS context.
  33. */
  34. tor_tls_context_t *
  35. tor_tls_context_get(int is_server)
  36. {
  37. return is_server ? server_tls_context : client_tls_context;
  38. }
  39. /** Convert an errno (or a WSAerrno on windows) into a TOR_TLS_* error
  40. * code. */
  41. int
  42. tor_errno_to_tls_error(int e)
  43. {
  44. switch (e) {
  45. case SOCK_ERRNO(ECONNRESET): // most common
  46. return TOR_TLS_ERROR_CONNRESET;
  47. case SOCK_ERRNO(ETIMEDOUT):
  48. return TOR_TLS_ERROR_TIMEOUT;
  49. case SOCK_ERRNO(EHOSTUNREACH):
  50. case SOCK_ERRNO(ENETUNREACH):
  51. return TOR_TLS_ERROR_NO_ROUTE;
  52. case SOCK_ERRNO(ECONNREFUSED):
  53. return TOR_TLS_ERROR_CONNREFUSED; // least common
  54. default:
  55. return TOR_TLS_ERROR_MISC;
  56. }
  57. }
  58. /** Set *<b>link_cert_out</b> and *<b>id_cert_out</b> to the link certificate
  59. * and ID certificate that we're currently using for our V3 in-protocol
  60. * handshake's certificate chain. If <b>server</b> is true, provide the certs
  61. * that we use in server mode (auth, ID); otherwise, provide the certs that we
  62. * use in client mode. (link, ID) */
  63. int
  64. tor_tls_get_my_certs(int server,
  65. const tor_x509_cert_t **link_cert_out,
  66. const tor_x509_cert_t **id_cert_out)
  67. {
  68. tor_tls_context_t *ctx = tor_tls_context_get(server);
  69. int rv = -1;
  70. const tor_x509_cert_t *link_cert = NULL;
  71. const tor_x509_cert_t *id_cert = NULL;
  72. if (ctx) {
  73. rv = 0;
  74. link_cert = server ? ctx->my_link_cert : ctx->my_auth_cert;
  75. id_cert = ctx->my_id_cert;
  76. }
  77. if (link_cert_out)
  78. *link_cert_out = link_cert;
  79. if (id_cert_out)
  80. *id_cert_out = id_cert;
  81. return rv;
  82. }
  83. /**
  84. * Return the authentication key that we use to authenticate ourselves as a
  85. * client in the V3 in-protocol handshake.
  86. */
  87. crypto_pk_t *
  88. tor_tls_get_my_client_auth_key(void)
  89. {
  90. tor_tls_context_t *context = tor_tls_context_get(0);
  91. if (! context)
  92. return NULL;
  93. return context->auth_key;
  94. }
  95. /** Increase the reference count of <b>ctx</b>. */
  96. void
  97. tor_tls_context_incref(tor_tls_context_t *ctx)
  98. {
  99. ++ctx->refcnt;
  100. }
  101. /** Remove a reference to <b>ctx</b>, and free it if it has no more
  102. * references. */
  103. void
  104. tor_tls_context_decref(tor_tls_context_t *ctx)
  105. {
  106. tor_assert(ctx);
  107. if (--ctx->refcnt == 0) {
  108. tor_tls_context_impl_free(ctx->ctx);
  109. tor_x509_cert_free(ctx->my_link_cert);
  110. tor_x509_cert_free(ctx->my_id_cert);
  111. tor_x509_cert_free(ctx->my_auth_cert);
  112. crypto_pk_free(ctx->link_key);
  113. crypto_pk_free(ctx->auth_key);
  114. /* LCOV_EXCL_BR_START since ctx will never be NULL here */
  115. tor_free(ctx);
  116. /* LCOV_EXCL_BR_STOP */
  117. }
  118. }
  119. /** Free all global TLS structures. */
  120. void
  121. tor_tls_free_all(void)
  122. {
  123. check_no_tls_errors();
  124. if (server_tls_context) {
  125. tor_tls_context_t *ctx = server_tls_context;
  126. server_tls_context = NULL;
  127. tor_tls_context_decref(ctx);
  128. }
  129. if (client_tls_context) {
  130. tor_tls_context_t *ctx = client_tls_context;
  131. client_tls_context = NULL;
  132. tor_tls_context_decref(ctx);
  133. }
  134. }
  135. /** Given a TOR_TLS_* error code, return a string equivalent. */
  136. const char *
  137. tor_tls_err_to_string(int err)
  138. {
  139. if (err >= 0)
  140. return "[Not an error.]";
  141. switch (err) {
  142. case TOR_TLS_ERROR_MISC: return "misc error";
  143. case TOR_TLS_ERROR_IO: return "unexpected close";
  144. case TOR_TLS_ERROR_CONNREFUSED: return "connection refused";
  145. case TOR_TLS_ERROR_CONNRESET: return "connection reset";
  146. case TOR_TLS_ERROR_NO_ROUTE: return "host unreachable";
  147. case TOR_TLS_ERROR_TIMEOUT: return "connection timed out";
  148. case TOR_TLS_CLOSE: return "closed";
  149. case TOR_TLS_WANTREAD: return "want to read";
  150. case TOR_TLS_WANTWRITE: return "want to write";
  151. default: return "(unknown error code)";
  152. }
  153. }
  154. /** Create new global client and server TLS contexts.
  155. *
  156. * If <b>server_identity</b> is NULL, this will not generate a server
  157. * TLS context. If TOR_TLS_CTX_IS_PUBLIC_SERVER is set in <b>flags</b>, use
  158. * the same TLS context for incoming and outgoing connections, and
  159. * ignore <b>client_identity</b>. If one of TOR_TLS_CTX_USE_ECDHE_P{224,256}
  160. * is set in <b>flags</b>, use that ECDHE group if possible; otherwise use
  161. * the default ECDHE group. */
  162. int
  163. tor_tls_context_init(unsigned flags,
  164. crypto_pk_t *client_identity,
  165. crypto_pk_t *server_identity,
  166. unsigned int key_lifetime)
  167. {
  168. int rv1 = 0;
  169. int rv2 = 0;
  170. const int is_public_server = flags & TOR_TLS_CTX_IS_PUBLIC_SERVER;
  171. check_no_tls_errors();
  172. if (is_public_server) {
  173. tor_tls_context_t *new_ctx;
  174. tor_tls_context_t *old_ctx;
  175. tor_assert(server_identity != NULL);
  176. rv1 = tor_tls_context_init_one(&server_tls_context,
  177. server_identity,
  178. key_lifetime, flags, 0);
  179. if (rv1 >= 0) {
  180. new_ctx = server_tls_context;
  181. tor_tls_context_incref(new_ctx);
  182. old_ctx = client_tls_context;
  183. client_tls_context = new_ctx;
  184. if (old_ctx != NULL) {
  185. tor_tls_context_decref(old_ctx);
  186. }
  187. } else {
  188. tls_log_errors(NULL, LOG_WARN, LD_CRYPTO,
  189. "constructing a TLS context");
  190. }
  191. } else {
  192. if (server_identity != NULL) {
  193. rv1 = tor_tls_context_init_one(&server_tls_context,
  194. server_identity,
  195. key_lifetime,
  196. flags,
  197. 0);
  198. if (rv1 < 0)
  199. tls_log_errors(NULL, LOG_WARN, LD_CRYPTO,
  200. "constructing a server TLS context");
  201. } else {
  202. tor_tls_context_t *old_ctx = server_tls_context;
  203. server_tls_context = NULL;
  204. if (old_ctx != NULL) {
  205. tor_tls_context_decref(old_ctx);
  206. }
  207. }
  208. rv2 = tor_tls_context_init_one(&client_tls_context,
  209. client_identity,
  210. key_lifetime,
  211. flags,
  212. 1);
  213. if (rv2 < 0)
  214. tls_log_errors(NULL, LOG_WARN, LD_CRYPTO,
  215. "constructing a client TLS context");
  216. }
  217. return MIN(rv1, rv2);
  218. }
  219. /** Create a new global TLS context.
  220. *
  221. * You can call this function multiple times. Each time you call it,
  222. * it generates new certificates; all new connections will use
  223. * the new SSL context.
  224. */
  225. int
  226. tor_tls_context_init_one(tor_tls_context_t **ppcontext,
  227. crypto_pk_t *identity,
  228. unsigned int key_lifetime,
  229. unsigned int flags,
  230. int is_client)
  231. {
  232. tor_tls_context_t *new_ctx = tor_tls_context_new(identity,
  233. key_lifetime,
  234. flags,
  235. is_client);
  236. tor_tls_context_t *old_ctx = *ppcontext;
  237. if (new_ctx != NULL) {
  238. *ppcontext = new_ctx;
  239. /* Free the old context if one existed. */
  240. if (old_ctx != NULL) {
  241. /* This is safe even if there are open connections: we reference-
  242. * count tor_tls_context_t objects. */
  243. tor_tls_context_decref(old_ctx);
  244. }
  245. }
  246. return ((new_ctx != NULL) ? 0 : -1);
  247. }
  248. /** Size of the RSA key to use for our TLS link keys */
  249. #define RSA_LINK_KEY_BITS 2048
  250. /** How long do identity certificates live? (sec) */
  251. #define IDENTITY_CERT_LIFETIME (365*24*60*60)
  252. /**
  253. * Initialize the certificates and keys for a TLS context <b>result</b>
  254. *
  255. * Other arguments as for tor_tls_context_new().
  256. */
  257. int
  258. tor_tls_context_init_certificates(tor_tls_context_t *result,
  259. crypto_pk_t *identity,
  260. unsigned key_lifetime,
  261. unsigned flags)
  262. {
  263. (void)flags;
  264. int rv = -1;
  265. char *nickname = NULL, *nn2 = NULL;
  266. crypto_pk_t *rsa = NULL, *rsa_auth = NULL;
  267. tor_x509_cert_impl_t *cert = NULL, *idcert = NULL, *authcert = NULL;
  268. nickname = crypto_random_hostname(8, 20, "www.", ".net");
  269. #ifdef DISABLE_V3_LINKPROTO_SERVERSIDE
  270. nn2 = crypto_random_hostname(8, 20, "www.", ".net");
  271. #else
  272. nn2 = crypto_random_hostname(8, 20, "www.", ".com");
  273. #endif
  274. /* Generate short-term RSA key for use with TLS. */
  275. if (!(rsa = crypto_pk_new()))
  276. goto error;
  277. if (crypto_pk_generate_key_with_bits(rsa, RSA_LINK_KEY_BITS)<0)
  278. goto error;
  279. /* Generate short-term RSA key for use in the in-protocol ("v3")
  280. * authentication handshake. */
  281. if (!(rsa_auth = crypto_pk_new()))
  282. goto error;
  283. if (crypto_pk_generate_key(rsa_auth)<0)
  284. goto error;
  285. /* Create a link certificate signed by identity key. */
  286. cert = tor_tls_create_certificate(rsa, identity, nickname, nn2,
  287. key_lifetime);
  288. /* Create self-signed certificate for identity key. */
  289. idcert = tor_tls_create_certificate(identity, identity, nn2, nn2,
  290. IDENTITY_CERT_LIFETIME);
  291. /* Create an authentication certificate signed by identity key. */
  292. authcert = tor_tls_create_certificate(rsa_auth, identity, nickname, nn2,
  293. key_lifetime);
  294. if (!cert || !idcert || !authcert) {
  295. log_warn(LD_CRYPTO, "Error creating certificate");
  296. goto error;
  297. }
  298. result->my_link_cert = tor_x509_cert_new(cert);
  299. cert = NULL;
  300. result->my_id_cert = tor_x509_cert_new(idcert);
  301. idcert = NULL;
  302. result->my_auth_cert = tor_x509_cert_new(authcert);
  303. authcert = NULL;
  304. if (!result->my_link_cert || !result->my_id_cert || !result->my_auth_cert)
  305. goto error;
  306. result->link_key = rsa;
  307. rsa = NULL;
  308. result->auth_key = rsa_auth;
  309. rsa_auth = NULL;
  310. rv = 0;
  311. error:
  312. tor_free(nickname);
  313. tor_free(nn2);
  314. tor_x509_cert_impl_free(cert);
  315. tor_x509_cert_impl_free(idcert);
  316. tor_x509_cert_impl_free(authcert);
  317. crypto_pk_free(rsa);
  318. crypto_pk_free(rsa_auth);
  319. return rv;
  320. }
  321. /** Make future log messages about <b>tls</b> display the address
  322. * <b>address</b>.
  323. */
  324. void
  325. tor_tls_set_logged_address(tor_tls_t *tls, const char *address)
  326. {
  327. tor_assert(tls);
  328. tor_free(tls->address);
  329. tls->address = tor_strdup(address);
  330. }
  331. /** Return whether this tls initiated the connect (client) or
  332. * received it (server). */
  333. int
  334. tor_tls_is_server(tor_tls_t *tls)
  335. {
  336. tor_assert(tls);
  337. return tls->isServer;
  338. }
  339. /** Release resources associated with a TLS object. Does not close the
  340. * underlying file descriptor.
  341. */
  342. void
  343. tor_tls_free_(tor_tls_t *tls)
  344. {
  345. if (!tls)
  346. return;
  347. tor_assert(tls->ssl);
  348. {
  349. size_t r,w;
  350. tor_tls_get_n_raw_bytes(tls,&r,&w); /* ensure written_by_tls is updated */
  351. }
  352. tor_tls_impl_free(tls->ssl);
  353. tls->ssl = NULL;
  354. #ifdef ENABLE_OPENSSL
  355. tls->negotiated_callback = NULL;
  356. #endif
  357. if (tls->context)
  358. tor_tls_context_decref(tls->context);
  359. tor_free(tls->address);
  360. tls->magic = 0x99999999;
  361. tor_free(tls);
  362. }
  363. /** If the provided tls connection is authenticated and has a
  364. * certificate chain that is currently valid and signed, then set
  365. * *<b>identity_key</b> to the identity certificate's key and return
  366. * 0. Else, return -1 and log complaints with log-level <b>severity</b>.
  367. */
  368. int
  369. tor_tls_verify(int severity, tor_tls_t *tls, crypto_pk_t **identity)
  370. {
  371. tor_x509_cert_impl_t *cert = NULL, *id_cert = NULL;
  372. tor_x509_cert_t *peer_x509 = NULL, *id_x509 = NULL;
  373. tor_assert(tls);
  374. tor_assert(identity);
  375. int rv = -1;
  376. try_to_extract_certs_from_tls(severity, tls, &cert, &id_cert);
  377. if (!cert)
  378. goto done;
  379. if (!id_cert) {
  380. log_fn(severity,LD_PROTOCOL,"No distinct identity certificate found");
  381. goto done;
  382. }
  383. peer_x509 = tor_x509_cert_new(cert);
  384. id_x509 = tor_x509_cert_new(id_cert);
  385. cert = id_cert = NULL; /* Prevent double-free */
  386. if (! tor_tls_cert_is_valid(severity, peer_x509, id_x509, time(NULL), 0)) {
  387. goto done;
  388. }
  389. *identity = tor_tls_cert_get_key(id_x509);
  390. rv = 0;
  391. done:
  392. tor_x509_cert_impl_free(cert);
  393. tor_x509_cert_impl_free(id_cert);
  394. tor_x509_cert_free(peer_x509);
  395. tor_x509_cert_free(id_x509);
  396. return rv;
  397. }
  398. static void
  399. subsys_tortls_shutdown(void)
  400. {
  401. tor_tls_free_all();
  402. }
  403. const subsys_fns_t sys_tortls = {
  404. .name = "tortls",
  405. .level = -50,
  406. .shutdown = subsys_tortls_shutdown
  407. };