tortls.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #include "lib/tls/x509.h"
  7. #include "lib/tls/x509_internal.h"
  8. #include "lib/tls/tortls.h"
  9. #include "lib/tls/tortls_st.h"
  10. #include "lib/tls/tortls_internal.h"
  11. #include "lib/log/util_bug.h"
  12. #include "lib/intmath/cmp.h"
  13. #include "lib/crypt_ops/crypto_rsa.h"
  14. #include "lib/crypt_ops/crypto_rand.h"
  15. /** Global TLS contexts. We keep them here because nobody else needs
  16. * to touch them.
  17. *
  18. * @{ */
  19. STATIC tor_tls_context_t *server_tls_context = NULL;
  20. STATIC tor_tls_context_t *client_tls_context = NULL;
  21. /**@}*/
  22. /**
  23. * Return the appropriate TLS context.
  24. */
  25. tor_tls_context_t *
  26. tor_tls_context_get(int is_server)
  27. {
  28. return is_server ? server_tls_context : client_tls_context;
  29. }
  30. /** Set *<b>link_cert_out</b> and *<b>id_cert_out</b> to the link certificate
  31. * and ID certificate that we're currently using for our V3 in-protocol
  32. * handshake's certificate chain. If <b>server</b> is true, provide the certs
  33. * that we use in server mode (auth, ID); otherwise, provide the certs that we
  34. * use in client mode. (link, ID) */
  35. int
  36. tor_tls_get_my_certs(int server,
  37. const tor_x509_cert_t **link_cert_out,
  38. const tor_x509_cert_t **id_cert_out)
  39. {
  40. tor_tls_context_t *ctx = tor_tls_context_get(server);
  41. if (! ctx)
  42. return -1;
  43. if (link_cert_out)
  44. *link_cert_out = server ? ctx->my_link_cert : ctx->my_auth_cert;
  45. if (id_cert_out)
  46. *id_cert_out = ctx->my_id_cert;
  47. return 0;
  48. }
  49. /**
  50. * Return the authentication key that we use to authenticate ourselves as a
  51. * client in the V3 in-protocol handshake.
  52. */
  53. crypto_pk_t *
  54. tor_tls_get_my_client_auth_key(void)
  55. {
  56. tor_tls_context_t *context = tor_tls_context_get(0);
  57. if (! context)
  58. return NULL;
  59. return context->auth_key;
  60. }
  61. /** Increase the reference count of <b>ctx</b>. */
  62. void
  63. tor_tls_context_incref(tor_tls_context_t *ctx)
  64. {
  65. ++ctx->refcnt;
  66. }
  67. /** Remove a reference to <b>ctx</b>, and free it if it has no more
  68. * references. */
  69. void
  70. tor_tls_context_decref(tor_tls_context_t *ctx)
  71. {
  72. tor_assert(ctx);
  73. if (--ctx->refcnt == 0) {
  74. tor_tls_context_impl_free(ctx->ctx);
  75. tor_x509_cert_free(ctx->my_link_cert);
  76. tor_x509_cert_free(ctx->my_id_cert);
  77. tor_x509_cert_free(ctx->my_auth_cert);
  78. crypto_pk_free(ctx->link_key);
  79. crypto_pk_free(ctx->auth_key);
  80. /* LCOV_EXCL_BR_START since ctx will never be NULL here */
  81. tor_free(ctx);
  82. /* LCOV_EXCL_BR_STOP */
  83. }
  84. }
  85. /** Free all global TLS structures. */
  86. void
  87. tor_tls_free_all(void)
  88. {
  89. check_no_tls_errors();
  90. if (server_tls_context) {
  91. tor_tls_context_t *ctx = server_tls_context;
  92. server_tls_context = NULL;
  93. tor_tls_context_decref(ctx);
  94. }
  95. if (client_tls_context) {
  96. tor_tls_context_t *ctx = client_tls_context;
  97. client_tls_context = NULL;
  98. tor_tls_context_decref(ctx);
  99. }
  100. }
  101. /** Given a TOR_TLS_* error code, return a string equivalent. */
  102. const char *
  103. tor_tls_err_to_string(int err)
  104. {
  105. if (err >= 0)
  106. return "[Not an error.]";
  107. switch (err) {
  108. case TOR_TLS_ERROR_MISC: return "misc error";
  109. case TOR_TLS_ERROR_IO: return "unexpected close";
  110. case TOR_TLS_ERROR_CONNREFUSED: return "connection refused";
  111. case TOR_TLS_ERROR_CONNRESET: return "connection reset";
  112. case TOR_TLS_ERROR_NO_ROUTE: return "host unreachable";
  113. case TOR_TLS_ERROR_TIMEOUT: return "connection timed out";
  114. case TOR_TLS_CLOSE: return "closed";
  115. case TOR_TLS_WANTREAD: return "want to read";
  116. case TOR_TLS_WANTWRITE: return "want to write";
  117. default: return "(unknown error code)";
  118. }
  119. }
  120. /** Create new global client and server TLS contexts.
  121. *
  122. * If <b>server_identity</b> is NULL, this will not generate a server
  123. * TLS context. If TOR_TLS_CTX_IS_PUBLIC_SERVER is set in <b>flags</b>, use
  124. * the same TLS context for incoming and outgoing connections, and
  125. * ignore <b>client_identity</b>. If one of TOR_TLS_CTX_USE_ECDHE_P{224,256}
  126. * is set in <b>flags</b>, use that ECDHE group if possible; otherwise use
  127. * the default ECDHE group. */
  128. int
  129. tor_tls_context_init(unsigned flags,
  130. crypto_pk_t *client_identity,
  131. crypto_pk_t *server_identity,
  132. unsigned int key_lifetime)
  133. {
  134. int rv1 = 0;
  135. int rv2 = 0;
  136. const int is_public_server = flags & TOR_TLS_CTX_IS_PUBLIC_SERVER;
  137. check_no_tls_errors();
  138. if (is_public_server) {
  139. tor_tls_context_t *new_ctx;
  140. tor_tls_context_t *old_ctx;
  141. tor_assert(server_identity != NULL);
  142. rv1 = tor_tls_context_init_one(&server_tls_context,
  143. server_identity,
  144. key_lifetime, flags, 0);
  145. if (rv1 >= 0) {
  146. new_ctx = server_tls_context;
  147. tor_tls_context_incref(new_ctx);
  148. old_ctx = client_tls_context;
  149. client_tls_context = new_ctx;
  150. if (old_ctx != NULL) {
  151. tor_tls_context_decref(old_ctx);
  152. }
  153. }
  154. } else {
  155. if (server_identity != NULL) {
  156. rv1 = tor_tls_context_init_one(&server_tls_context,
  157. server_identity,
  158. key_lifetime,
  159. flags,
  160. 0);
  161. } else {
  162. tor_tls_context_t *old_ctx = server_tls_context;
  163. server_tls_context = NULL;
  164. if (old_ctx != NULL) {
  165. tor_tls_context_decref(old_ctx);
  166. }
  167. }
  168. rv2 = tor_tls_context_init_one(&client_tls_context,
  169. client_identity,
  170. key_lifetime,
  171. flags,
  172. 1);
  173. }
  174. tls_log_errors(NULL, LOG_WARN, LD_CRYPTO, "constructing a TLS context");
  175. return MIN(rv1, rv2);
  176. }
  177. /** Create a new global TLS context.
  178. *
  179. * You can call this function multiple times. Each time you call it,
  180. * it generates new certificates; all new connections will use
  181. * the new SSL context.
  182. */
  183. int
  184. tor_tls_context_init_one(tor_tls_context_t **ppcontext,
  185. crypto_pk_t *identity,
  186. unsigned int key_lifetime,
  187. unsigned int flags,
  188. int is_client)
  189. {
  190. tor_tls_context_t *new_ctx = tor_tls_context_new(identity,
  191. key_lifetime,
  192. flags,
  193. is_client);
  194. tor_tls_context_t *old_ctx = *ppcontext;
  195. if (new_ctx != NULL) {
  196. *ppcontext = new_ctx;
  197. /* Free the old context if one existed. */
  198. if (old_ctx != NULL) {
  199. /* This is safe even if there are open connections: we reference-
  200. * count tor_tls_context_t objects. */
  201. tor_tls_context_decref(old_ctx);
  202. }
  203. }
  204. return ((new_ctx != NULL) ? 0 : -1);
  205. }
  206. /** Size of the RSA key to use for our TLS link keys */
  207. #define RSA_LINK_KEY_BITS 2048
  208. /** How long do identity certificates live? (sec) */
  209. #define IDENTITY_CERT_LIFETIME (365*24*60*60)
  210. /**
  211. * Initialize the certificates and keys for a TLS context <b>result</b>
  212. *
  213. * Other arguments as for tor_tls_context_new().
  214. */
  215. int
  216. tor_tls_context_init_certificates(tor_tls_context_t *result,
  217. crypto_pk_t *identity,
  218. unsigned key_lifetime,
  219. unsigned flags)
  220. {
  221. (void)flags;
  222. int rv = -1;
  223. char *nickname = NULL, *nn2 = NULL;
  224. crypto_pk_t *rsa = NULL, *rsa_auth = NULL;
  225. tor_x509_cert_impl_t *cert = NULL, *idcert = NULL, *authcert = NULL;
  226. nickname = crypto_random_hostname(8, 20, "www.", ".net");
  227. #ifdef DISABLE_V3_LINKPROTO_SERVERSIDE
  228. nn2 = crypto_random_hostname(8, 20, "www.", ".net");
  229. #else
  230. nn2 = crypto_random_hostname(8, 20, "www.", ".com");
  231. #endif
  232. /* Generate short-term RSA key for use with TLS. */
  233. if (!(rsa = crypto_pk_new()))
  234. goto error;
  235. if (crypto_pk_generate_key_with_bits(rsa, RSA_LINK_KEY_BITS)<0)
  236. goto error;
  237. /* Generate short-term RSA key for use in the in-protocol ("v3")
  238. * authentication handshake. */
  239. if (!(rsa_auth = crypto_pk_new()))
  240. goto error;
  241. if (crypto_pk_generate_key(rsa_auth)<0)
  242. goto error;
  243. /* Create a link certificate signed by identity key. */
  244. cert = tor_tls_create_certificate(rsa, identity, nickname, nn2,
  245. key_lifetime);
  246. /* Create self-signed certificate for identity key. */
  247. idcert = tor_tls_create_certificate(identity, identity, nn2, nn2,
  248. IDENTITY_CERT_LIFETIME);
  249. /* Create an authentication certificate signed by identity key. */
  250. authcert = tor_tls_create_certificate(rsa_auth, identity, nickname, nn2,
  251. key_lifetime);
  252. if (!cert || !idcert || !authcert) {
  253. log_warn(LD_CRYPTO, "Error creating certificate");
  254. goto error;
  255. }
  256. result->my_link_cert = tor_x509_cert_new(cert);
  257. cert = NULL;
  258. result->my_id_cert = tor_x509_cert_new(idcert);
  259. idcert = NULL;
  260. result->my_auth_cert = tor_x509_cert_new(authcert);
  261. authcert = NULL;
  262. if (!result->my_link_cert || !result->my_id_cert || !result->my_auth_cert)
  263. goto error;
  264. result->link_key = rsa;
  265. rsa = NULL;
  266. result->auth_key = rsa_auth;
  267. rsa_auth = NULL;
  268. rv = 0;
  269. error:
  270. tor_free(nickname);
  271. tor_free(nn2);
  272. if (cert)
  273. tor_x509_cert_impl_free_(cert);
  274. if (idcert)
  275. tor_x509_cert_impl_free_(idcert);
  276. if (authcert)
  277. tor_x509_cert_impl_free_(authcert);
  278. crypto_pk_free(rsa);
  279. crypto_pk_free(rsa_auth);
  280. return rv;
  281. }
  282. /** Make future log messages about <b>tls</b> display the address
  283. * <b>address</b>.
  284. */
  285. void
  286. tor_tls_set_logged_address(tor_tls_t *tls, const char *address)
  287. {
  288. tor_assert(tls);
  289. tor_free(tls->address);
  290. tls->address = tor_strdup(address);
  291. }
  292. /** Return whether this tls initiated the connect (client) or
  293. * received it (server). */
  294. int
  295. tor_tls_is_server(tor_tls_t *tls)
  296. {
  297. tor_assert(tls);
  298. return tls->isServer;
  299. }