tortls.c 13 KB

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