tortls.c 12 KB

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