11 #define TORTLS_PRIVATE
12 #define TOR_X509_PRIVATE
14 #include "lib/tls/x509_internal.h"
56 case SOCK_ERRNO(ECONNRESET):
57 return TOR_TLS_ERROR_CONNRESET;
58 case SOCK_ERRNO(ETIMEDOUT):
59 return TOR_TLS_ERROR_TIMEOUT;
60 case SOCK_ERRNO(EHOSTUNREACH):
61 case SOCK_ERRNO(ENETUNREACH):
62 return TOR_TLS_ERROR_NO_ROUTE;
63 case SOCK_ERRNO(ECONNREFUSED):
64 return TOR_TLS_ERROR_CONNREFUSED;
66 return TOR_TLS_ERROR_MISC;
77 const tor_x509_cert_t **link_cert_out,
78 const tor_x509_cert_t **id_cert_out)
82 const tor_x509_cert_t *link_cert = NULL;
83 const tor_x509_cert_t *id_cert = NULL;
86 link_cert = server ? ctx->my_link_cert : ctx->my_auth_cert;
87 id_cert = ctx->my_id_cert;
90 *link_cert_out = link_cert;
92 *id_cert_out = id_cert;
106 return context->auth_key;
122 if (--ctx->refcnt == 0) {
123 tor_tls_context_impl_free(ctx->ctx);
124 tor_x509_cert_free(ctx->my_link_cert);
125 tor_x509_cert_free(ctx->my_id_cert);
126 tor_x509_cert_free(ctx->my_auth_cert);
127 crypto_pk_free(ctx->link_key);
128 crypto_pk_free(ctx->auth_key);
139 check_no_tls_errors();
146 if (client_tls_context) {
148 client_tls_context = NULL;
158 return "[Not an error.]";
160 case TOR_TLS_ERROR_MISC:
return "misc error";
161 case TOR_TLS_ERROR_IO:
return "unexpected close";
162 case TOR_TLS_ERROR_CONNREFUSED:
return "connection refused";
163 case TOR_TLS_ERROR_CONNRESET:
return "connection reset";
164 case TOR_TLS_ERROR_NO_ROUTE:
return "host unreachable";
165 case TOR_TLS_ERROR_TIMEOUT:
return "connection timed out";
166 case TOR_TLS_CLOSE:
return "closed";
167 case TOR_TLS_WANTREAD:
return "want to read";
168 case TOR_TLS_WANTWRITE:
return "want to write";
169 default:
return "(unknown error code)";
185 unsigned int key_lifetime)
189 const int is_public_server = flags & TOR_TLS_CTX_IS_PUBLIC_SERVER;
190 check_no_tls_errors();
192 if (is_public_server) {
200 key_lifetime, flags, 0);
205 old_ctx = client_tls_context;
206 client_tls_context = new_ctx;
208 if (old_ctx != NULL) {
213 "constructing a TLS context");
216 if (server_identity != NULL) {
224 "constructing a server TLS context");
229 if (old_ctx != NULL) {
241 "constructing a client TLS context");
244 return MIN(rv1, rv2);
256 unsigned int key_lifetime,
266 if (new_ctx != NULL) {
267 *ppcontext = new_ctx;
270 if (old_ctx != NULL) {
277 return ((new_ctx != NULL) ? 0 : -1);
281 #define RSA_LINK_KEY_BITS 2048
284 #define IDENTITY_CERT_LIFETIME (365*24*60*60)
294 unsigned key_lifetime,
299 char *nickname = NULL, *nn2 = NULL;
301 tor_x509_cert_impl_t *cert = NULL, *idcert = NULL, *authcert = NULL;
305 #ifdef DISABLE_V3_LINKPROTO_SERVERSIDE
321 if (crypto_pk_generate_key(rsa_auth)<0)
333 if (!cert || !idcert || !authcert) {
334 log_warn(
LD_CRYPTO,
"Error creating certificate");
338 result->my_link_cert = tor_x509_cert_new(cert);
340 result->my_id_cert = tor_x509_cert_new(idcert);
342 result->my_auth_cert = tor_x509_cert_new(authcert);
344 if (!result->my_link_cert || !result->my_id_cert || !result->my_auth_cert)
346 result->link_key = rsa;
348 result->auth_key = rsa_auth;
357 tor_x509_cert_impl_free(cert);
358 tor_x509_cert_impl_free(idcert);
359 tor_x509_cert_impl_free(authcert);
361 crypto_pk_free(rsa_auth);
373 tls->
address = tor_strdup(address);
398 tor_tls_impl_free(tls->
ssl);
400 #ifdef ENABLE_OPENSSL
401 tls->negotiated_callback = NULL;
406 tls->magic = 0x99999999;
418 tor_x509_cert_impl_t *cert = NULL, *id_cert = NULL;
419 tor_x509_cert_t *peer_x509 = NULL, *id_x509 = NULL;
424 try_to_extract_certs_from_tls(severity, tls, &cert, &id_cert);
431 peer_x509 = tor_x509_cert_new(cert);
432 id_x509 = tor_x509_cert_new(id_cert);
433 cert = id_cert = NULL;
443 tor_x509_cert_impl_free(cert);
444 tor_x509_cert_impl_free(id_cert);
445 tor_x509_cert_free(peer_x509);
446 tor_x509_cert_free(id_x509);
452 subsys_tortls_shutdown(
void)
461 .shutdown = subsys_tortls_shutdown
Macro definitions for MIN, MAX, and CLAMP.
char * crypto_random_hostname(int min_rand_len, int max_rand_len, const char *prefix, const char *suffix)
Common functions for using (pseudo-)random number generators.
Headers for crypto_rsa.c.
int crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits)
crypto_pk_t * crypto_pk_new(void)
#define log_fn(severity, domain, args,...)
Types used to declare a subsystem.
#define SUBSYS_DECLARE_LOCATION()
Definitions for timing-related constants.
#define RSA_LINK_KEY_BITS
int tor_tls_context_init(unsigned flags, crypto_pk_t *client_identity, crypto_pk_t *server_identity, unsigned int key_lifetime)
void tor_tls_context_incref(tor_tls_context_t *ctx)
void tor_tls_context_decref(tor_tls_context_t *ctx)
int tor_errno_to_tls_error(int e)
void tor_tls_free_(tor_tls_t *tls)
#define IDENTITY_CERT_LIFETIME
int tor_tls_get_my_certs(int server, const tor_x509_cert_t **link_cert_out, const tor_x509_cert_t **id_cert_out)
crypto_pk_t * tor_tls_get_my_client_auth_key(void)
int tor_tls_is_server(tor_tls_t *tls)
tor_tls_context_t * tor_tls_context_get(int is_server)
void tor_tls_set_logged_address(tor_tls_t *tls, const char *address)
int tor_tls_context_init_certificates(tor_tls_context_t *result, crypto_pk_t *identity, unsigned key_lifetime, unsigned flags)
const char * tor_tls_err_to_string(int err)
STATIC tor_tls_context_t * server_tls_context
void tor_tls_free_all(void)
int tor_tls_verify(int severity, tor_tls_t *tls, crypto_pk_t **identity)
int tor_tls_context_init_one(tor_tls_context_t **ppcontext, crypto_pk_t *identity, unsigned int key_lifetime, unsigned int flags, int is_client)
void tor_tls_get_n_raw_bytes(tor_tls_t *tls, size_t *n_read, size_t *n_written)
void tls_log_errors(tor_tls_t *tls, int severity, int domain, const char *doing)
Declare internal functions for lib/tls.
tor_tls_context_t * tor_tls_context_new(crypto_pk_t *identity, unsigned int key_lifetime, unsigned flags, int is_client)
Structure declarations for internal TLS types.
Declare subsystem object for the tortls module.
Macros to manage assertions, fatal and non-fatal.
crypto_pk_t * tor_tls_cert_get_key(tor_x509_cert_t *cert)
int tor_tls_cert_is_valid(int severity, const tor_x509_cert_t *cert, const tor_x509_cert_t *signing_cert, time_t now, int check_rsa_1024)
X509 * tor_tls_create_certificate(crypto_pk_t *rsa, crypto_pk_t *rsa_sign, const char *cname, const char *cname_sign, unsigned int cert_lifetime)