tortls.c 12 KB

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