1 /* Copyright 2003 Roger Dingledine. */
2 /* See LICENSE for licensing information */
4 const char tortls_c_id
[] = "$Id$";
9 * \brief TLS wrappers for Tor.
11 /* (Unlike other tor functions, these
12 * are prefixed with tor_ in order to avoid conflicting with OpenSSL
13 * functions and variables.)
22 /* Copied from or.h */
23 #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
26 #include <openssl/ssl.h>
27 #include <openssl/err.h>
28 #include <openssl/tls1.h>
29 #include <openssl/asn1.h>
30 #include <openssl/bio.h>
32 /** How long do identity certificates live? (sec) */
33 #define IDENTITY_CERT_LIFETIME (365*24*60*60)
35 typedef struct tor_tls_context_st
{
37 SSL_CTX
*client_only_ctx
;
40 /** Holds a SSL object and its associated data. Members are only
41 * accessed from within tortls.c.
44 SSL
*ssl
; /**< An OpenSSL SSL object. */
45 int socket
; /**< The underlying file descriptor for this TLS connection. */
47 TOR_TLS_ST_HANDSHAKE
, TOR_TLS_ST_OPEN
, TOR_TLS_ST_GOTCLOSE
,
48 TOR_TLS_ST_SENTCLOSE
, TOR_TLS_ST_CLOSED
49 } state
; /**< The current SSL state, depending on which operations have
50 * completed successfully. */
52 size_t wantwrite_n
; /**< 0 normally, >0 if we returned wantwrite last time. */
55 static X509
* tor_tls_create_certificate(crypto_pk_env_t
*rsa
,
56 crypto_pk_env_t
*rsa_sign
,
58 const char *cname_sign
,
59 unsigned int lifetime
);
61 /** Global tls context. We keep it here because nobody else needs to
63 static tor_tls_context
*global_tls_context
= NULL
;
64 /** True iff tor_tls_init() has been called. */
65 static int tls_library_is_initialized
= 0;
67 /* Module-internal error codes. */
68 #define _TOR_TLS_SYSCALL -6
69 #define _TOR_TLS_ZERORETURN -5
71 /* These functions are declared in crypto.c but not exported. */
72 EVP_PKEY
*_crypto_pk_env_get_evp_pkey(crypto_pk_env_t
*env
, int private);
73 crypto_pk_env_t
*_crypto_new_pk_env_rsa(RSA
*rsa
);
74 DH
*_crypto_dh_env_get_dh(crypto_dh_env_t
*dh
);
76 /** Log all pending tls errors at level <b>severity</b>. Use
77 * <b>doing</b> to describe our current activities.
80 tls_log_errors(int severity
, const char *doing
)
83 const char *msg
, *lib
, *func
;
84 while ((err
= ERR_get_error()) != 0) {
85 msg
= (const char*)ERR_reason_error_string(err
);
86 lib
= (const char*)ERR_lib_error_string(err
);
87 func
= (const char*)ERR_func_error_string(err
);
88 if (!msg
) msg
= "(null)";
90 log(severity
, "TLS error while %s: %s (in %s:%s)", doing
, msg
, lib
,func
);
92 log(severity
, "TLS error: %s (in %s:%s)", msg
, lib
, func
);
97 #define CATCH_SYSCALL 1
100 /** Given a TLS object and the result of an SSL_* call, use
101 * SSL_get_error to determine whether an error has occurred, and if so
102 * which one. Return one of TOR_TLS_{DONE|WANTREAD|WANTWRITE|ERROR}.
103 * If extra&CATCH_SYSCALL is true, return _TOR_TLS_SYSCALL instead of
104 * reporting syscall errors. If extra&CATCH_ZERO is true, return
105 * _TOR_TLS_ZERORETURN instead of reporting zero-return errors.
107 * If an error has occurred, log it at level <b>severity</b> and describe the
108 * current action as <b>doing</b>.
111 tor_tls_get_error(tor_tls
*tls
, int r
, int extra
,
112 const char *doing
, int severity
)
114 int err
= SSL_get_error(tls
->ssl
, r
);
118 case SSL_ERROR_WANT_READ
:
119 return TOR_TLS_WANTREAD
;
120 case SSL_ERROR_WANT_WRITE
:
121 return TOR_TLS_WANTWRITE
;
122 case SSL_ERROR_SYSCALL
:
123 if (extra
&CATCH_SYSCALL
)
124 return _TOR_TLS_SYSCALL
;
126 log(severity
, "TLS error: unexpected close while %s", doing
);
128 int e
= tor_socket_errno(tls
->socket
);
129 log(severity
, "TLS error: <syscall error while %s> (errno=%d: %s)",
130 doing
, e
, tor_socket_strerror(e
));
132 tls_log_errors(severity
, doing
);
133 return TOR_TLS_ERROR
;
134 case SSL_ERROR_ZERO_RETURN
:
135 if (extra
&CATCH_ZERO
)
136 return _TOR_TLS_ZERORETURN
;
137 log(severity
, "TLS error: Zero return");
138 tls_log_errors(severity
, doing
);
139 return TOR_TLS_ERROR
;
141 tls_log_errors(severity
, doing
);
142 return TOR_TLS_ERROR
;
146 /** Initialize OpenSSL, unless it has already been initialized.
150 if (!tls_library_is_initialized
) {
152 SSL_load_error_strings();
153 crypto_global_init();
154 OpenSSL_add_all_algorithms();
155 tls_library_is_initialized
= 1;
159 /** We need to give OpenSSL a callback to verify certificates. This is
160 * it: We always accept peer certs and complete the handshake. We
161 * don't validate them until later.
163 static int always_accept_verify_cb(int preverify_ok
,
164 X509_STORE_CTX
*x509_ctx
)
169 /** Generate and sign an X509 certificate with the public key <b>rsa</b>,
170 * signed by the private key <b>rsa_sign</b>. The commonName of the
171 * certificate will be <b>cname</b>; the commonName of the issuer will be
172 * <b>cname_sign</b>. The cert will be valid for <b>cert_lifetime</b> seconds
173 * starting from now. Return a certificate on success, NULL on
177 tor_tls_create_certificate(crypto_pk_env_t
*rsa
,
178 crypto_pk_env_t
*rsa_sign
,
180 const char *cname_sign
,
181 unsigned int cert_lifetime
)
183 time_t start_time
, end_time
;
184 EVP_PKEY
*sign_pkey
= NULL
, *pkey
=NULL
;
186 X509_NAME
*name
= NULL
, *name_issuer
=NULL
;
191 start_time
= time(NULL
);
195 tor_assert(rsa_sign
);
196 tor_assert(cname_sign
);
197 if (!(sign_pkey
= _crypto_pk_env_get_evp_pkey(rsa_sign
,1)))
199 if (!(pkey
= _crypto_pk_env_get_evp_pkey(rsa
,0)))
201 if (!(x509
= X509_new()))
203 if (!(X509_set_version(x509
, 2)))
205 if (!(ASN1_INTEGER_set(X509_get_serialNumber(x509
), (long)start_time
)))
208 if (!(name
= X509_NAME_new()))
210 if ((nid
= OBJ_txt2nid("organizationName")) == NID_undef
) goto error
;
211 if (!(X509_NAME_add_entry_by_NID(name
, nid
, MBSTRING_ASC
,
212 (char*)"TOR", -1, -1, 0))) goto error
;
213 if ((nid
= OBJ_txt2nid("commonName")) == NID_undef
) goto error
;
214 if (!(X509_NAME_add_entry_by_NID(name
, nid
, MBSTRING_ASC
,
215 (char*)cname
, -1, -1, 0))) goto error
;
216 if (!(X509_set_subject_name(x509
, name
)))
219 if (!(name_issuer
= X509_NAME_new()))
221 if ((nid
= OBJ_txt2nid("organizationName")) == NID_undef
) goto error
;
222 if (!(X509_NAME_add_entry_by_NID(name_issuer
, nid
, MBSTRING_ASC
,
223 (char*)"TOR", -1, -1, 0))) goto error
;
224 if ((nid
= OBJ_txt2nid("commonName")) == NID_undef
) goto error
;
225 if (!(X509_NAME_add_entry_by_NID(name_issuer
, nid
, MBSTRING_ASC
,
226 (char*)cname_sign
, -1, -1, 0))) goto error
;
227 if (!(X509_set_issuer_name(x509
, name_issuer
)))
230 if (!X509_time_adj(X509_get_notBefore(x509
),0,&start_time
))
232 end_time
= start_time
+ cert_lifetime
;
233 if (!X509_time_adj(X509_get_notAfter(x509
),0,&end_time
))
235 if (!X509_set_pubkey(x509
, pkey
))
237 if (!X509_sign(x509
, sign_pkey
, EVP_sha1()))
242 tls_log_errors(LOG_WARN
, "generating certificate");
249 EVP_PKEY_free(sign_pkey
);
253 X509_NAME_free(name
);
255 X509_NAME_free(name_issuer
);
259 #ifdef EVERYONE_HAS_AES
260 /* Everybody is running OpenSSL 0.9.7 or later, so no backward compatibility
262 #define CIPHER_LIST TLS1_TXT_DHE_RSA_WITH_AES_128_SHA
263 #elif defined(TLS1_TXT_DHE_RSA_WITH_AES_128_SHA)
264 /* Some people are running OpenSSL before 0.9.7, but we aren't.
265 * We can support AES and 3DES.
267 #define CIPHER_LIST (TLS1_TXT_DHE_RSA_WITH_AES_128_SHA ":" \
268 SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA)
270 /* We're running OpenSSL before 0.9.7. We only support 3DES. */
271 #define CIPHER_LIST SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA
274 /** Create a new TLS context. If we are going to be using it as a
275 * server, it must have isServer set to true, <b>identity</b> set to the
276 * identity key used to sign that certificate, and <b>nickname</b> set to
277 * the server's nickname. If we're only going to be a client,
278 * isServer should be false, identity should be NULL, and nickname
279 * should be NULL. Return -1 if failure, else 0.
281 * You can call this function multiple times. Each time you call it,
282 * it generates new certificates; all new connections will use
283 * the new SSL context.
286 tor_tls_context_new(crypto_pk_env_t
*identity
,
287 int isServer
, const char *nickname
,
288 unsigned int key_lifetime
)
290 crypto_pk_env_t
*rsa
= NULL
;
291 crypto_dh_env_t
*dh
= NULL
;
292 EVP_PKEY
*pkey
= NULL
;
293 tor_tls_context
*result
= NULL
;
294 X509
*cert
= NULL
, *idcert
= NULL
;
300 tor_snprintf(nn2
, sizeof(nn2
), "%s <identity>", nickname
);
305 /* Generate short-term RSA key. */
306 if (!(rsa
= crypto_new_pk_env()))
308 if (crypto_pk_generate_key(rsa
)<0)
310 /* Create certificate signed by identity key. */
311 cert
= tor_tls_create_certificate(rsa
, identity
, nickname
, nn2
,
313 /* Create self-signed certificate for identity key. */
314 idcert
= tor_tls_create_certificate(identity
, identity
, nn2
, nn2
,
315 IDENTITY_CERT_LIFETIME
);
316 if (!cert
|| !idcert
) {
317 log(LOG_WARN
, "Error creating certificate");
322 result
= tor_malloc(sizeof(tor_tls_context
));
323 result
->ctx
= result
->client_only_ctx
= NULL
;
324 for (client_only
=0; client_only
<= 1; ++client_only
) {
325 ctx
= client_only
? &result
->client_only_ctx
: &result
->ctx
;
326 #ifdef EVERYONE_HAS_AES
327 /* Tell OpenSSL to only use TLS1 */
328 if (!(*ctx
= SSL_CTX_new(TLSv1_method())))
331 /* Tell OpenSSL to use SSL3 or TLS1 but not SSL2. */
332 if (!(*ctx
= SSL_CTX_new(SSLv23_method())))
334 SSL_CTX_set_options(*ctx
, SSL_OP_NO_SSLv2
);
336 if (!SSL_CTX_set_cipher_list(*ctx
, CIPHER_LIST
))
339 if (cert
&& !SSL_CTX_use_certificate(*ctx
,cert
))
341 X509_free(cert
); /* We just added a reference to cert. */
343 if (idcert
&& !SSL_CTX_add_extra_chain_cert(*ctx
,idcert
))
345 idcert
=NULL
; /* The context now owns the reference to idcert */
347 SSL_CTX_set_session_cache_mode(*ctx
, SSL_SESS_CACHE_OFF
);
348 if (isServer
&& !client_only
) {
350 if (!(pkey
= _crypto_pk_env_get_evp_pkey(rsa
,1)))
352 if (!SSL_CTX_use_PrivateKey(*ctx
, pkey
))
356 if (!SSL_CTX_check_private_key(*ctx
))
359 dh
= crypto_dh_new();
360 SSL_CTX_set_tmp_dh(*ctx
, _crypto_dh_env_get_dh(dh
));
362 SSL_CTX_set_verify(*ctx
, SSL_VERIFY_PEER
,
363 always_accept_verify_cb
);
364 /* let us realloc bufs that we're writing from */
365 SSL_CTX_set_mode(*ctx
, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
);
367 /* Free the old context if one exists. */
368 if (global_tls_context
) {
369 /* This is safe even if there are open connections: OpenSSL does
370 * reference counting with SSL and SSL_CTX objects. */
371 SSL_CTX_free(global_tls_context
->ctx
);
372 SSL_CTX_free(global_tls_context
->client_only_ctx
);
373 tor_free(global_tls_context
);
375 global_tls_context
= result
;
377 crypto_free_pk_env(rsa
);
381 tls_log_errors(LOG_WARN
, "creating TLS context");
385 crypto_free_pk_env(rsa
);
388 if (result
&& result
->ctx
)
389 SSL_CTX_free(result
->ctx
);
390 if (result
&& result
->client_only_ctx
)
391 SSL_CTX_free(result
->client_only_ctx
);
401 /** Create a new TLS object from a file descriptor, and a flag to
402 * determine whether it is functioning as a server.
405 tor_tls_new(int sock
, int isServer
, int use_no_cert
)
407 tor_tls
*result
= tor_malloc(sizeof(tor_tls
));
409 tor_assert(global_tls_context
); /* make sure somebody made it first */
410 ctx
= use_no_cert
? global_tls_context
->client_only_ctx
411 : global_tls_context
->ctx
;
412 if (!(result
->ssl
= SSL_new(ctx
)))
414 result
->socket
= sock
;
415 SSL_set_fd(result
->ssl
, sock
);
416 result
->state
= TOR_TLS_ST_HANDSHAKE
;
417 result
->isServer
= isServer
;
418 result
->wantwrite_n
= 0;
422 /** Release resources associated with a TLS object. Does not close the
423 * underlying file descriptor.
426 tor_tls_free(tor_tls
*tls
)
432 /** Underlying function for TLS reading. Reads up to <b>len</b>
433 * characters from <b>tls</b> into <b>cp</b>. On success, returns the
434 * number of characters read. On failure, returns TOR_TLS_ERROR,
435 * TOR_TLS_CLOSE, TOR_TLS_WANTREAD, or TOR_TLS_WANTWRITE.
438 tor_tls_read(tor_tls
*tls
, char *cp
, size_t len
)
442 tor_assert(tls
->ssl
);
443 tor_assert(tls
->state
== TOR_TLS_ST_OPEN
);
444 r
= SSL_read(tls
->ssl
, cp
, len
);
447 err
= tor_tls_get_error(tls
, r
, CATCH_ZERO
, "reading", LOG_INFO
);
448 if (err
== _TOR_TLS_ZERORETURN
) {
449 log_fn(LOG_DEBUG
,"read returned r=%d; TLS is closed",r
);
450 tls
->state
= TOR_TLS_ST_CLOSED
;
451 return TOR_TLS_CLOSE
;
453 tor_assert(err
!= TOR_TLS_DONE
);
454 log_fn(LOG_DEBUG
,"read returned r=%d, err=%d",r
,err
);
459 /** Underlying function for TLS writing. Write up to <b>n</b>
460 * characters from <b>cp</b> onto <b>tls</b>. On success, returns the
461 * number of characters written. On failure, returns TOR_TLS_ERROR,
462 * TOR_TLS_WANTREAD, or TOR_TLS_WANTWRITE.
465 tor_tls_write(tor_tls
*tls
, char *cp
, size_t n
)
469 tor_assert(tls
->ssl
);
470 tor_assert(tls
->state
== TOR_TLS_ST_OPEN
);
473 if (tls
->wantwrite_n
) {
474 /* if WANTWRITE last time, we must use the _same_ n as before */
475 tor_assert(n
>= tls
->wantwrite_n
);
476 log_fn(LOG_DEBUG
,"resuming pending-write, (%d to flush, reusing %d)",
477 (int)n
, (int)tls
->wantwrite_n
);
478 n
= tls
->wantwrite_n
;
479 tls
->wantwrite_n
= 0;
481 r
= SSL_write(tls
->ssl
, cp
, n
);
482 err
= tor_tls_get_error(tls
, r
, 0, "writing", LOG_INFO
);
483 if (err
== TOR_TLS_DONE
) {
486 if (err
== TOR_TLS_WANTWRITE
|| err
== TOR_TLS_WANTREAD
) {
487 tls
->wantwrite_n
= n
;
492 /** Perform initial handshake on <b>tls</b>. When finished, returns
493 * TOR_TLS_DONE. On failure, returns TOR_TLS_ERROR, TOR_TLS_WANTREAD,
494 * or TOR_TLS_WANTWRITE.
497 tor_tls_handshake(tor_tls
*tls
)
501 tor_assert(tls
->ssl
);
502 tor_assert(tls
->state
== TOR_TLS_ST_HANDSHAKE
);
504 r
= SSL_accept(tls
->ssl
);
506 r
= SSL_connect(tls
->ssl
);
508 r
= tor_tls_get_error(tls
,r
,0, "handshaking", LOG_INFO
);
509 if (r
== TOR_TLS_DONE
) {
510 tls
->state
= TOR_TLS_ST_OPEN
;
515 /** Shut down an open tls connection <b>tls</b>. When finished, returns
516 * TOR_TLS_DONE. On failure, returns TOR_TLS_ERROR, TOR_TLS_WANTREAD,
517 * or TOR_TLS_WANTWRITE.
520 tor_tls_shutdown(tor_tls
*tls
)
525 tor_assert(tls
->ssl
);
528 if (tls
->state
== TOR_TLS_ST_SENTCLOSE
) {
529 /* If we've already called shutdown once to send a close message,
530 * we read until the other side has closed too.
533 r
= SSL_read(tls
->ssl
, buf
, 128);
535 err
= tor_tls_get_error(tls
, r
, CATCH_ZERO
, "reading to shut down",
537 if (err
== _TOR_TLS_ZERORETURN
) {
538 tls
->state
= TOR_TLS_ST_GOTCLOSE
;
539 /* fall through... */
545 r
= SSL_shutdown(tls
->ssl
);
547 /* If shutdown returns 1, the connection is entirely closed. */
548 tls
->state
= TOR_TLS_ST_CLOSED
;
551 err
= tor_tls_get_error(tls
, r
, CATCH_SYSCALL
|CATCH_ZERO
, "shutting down",
553 if (err
== _TOR_TLS_SYSCALL
) {
554 /* The underlying TCP connection closed while we were shutting down. */
555 tls
->state
= TOR_TLS_ST_CLOSED
;
557 } else if (err
== _TOR_TLS_ZERORETURN
) {
558 /* The TLS connection says that it sent a shutdown record, but
559 * isn't done shutting down yet. Make sure that this hasn't
560 * happened before, then go back to the start of the function
563 if (tls
->state
== TOR_TLS_ST_GOTCLOSE
||
564 tls
->state
== TOR_TLS_ST_SENTCLOSE
) {
566 "TLS returned \"half-closed\" value while already half-closed");
567 return TOR_TLS_ERROR
;
569 tls
->state
= TOR_TLS_ST_SENTCLOSE
;
570 /* fall through ... */
577 /** Return true iff this TLS connection is authenticated.
580 tor_tls_peer_has_cert(tor_tls
*tls
)
583 if (!(cert
= SSL_get_peer_certificate(tls
->ssl
)))
589 /** Write the nickname (if any) that the peer connected on <b>tls</b>
590 * claims to have into the first <b>buflen</b> characters of <b>buf</b>.
591 * Truncate the nickname if it is longer than buflen-1 characters. Always
592 * NUL-terminate. Return 0 on success, -1 on failure.
595 tor_tls_get_peer_cert_nickname(tor_tls
*tls
, char *buf
, size_t buflen
)
598 X509_NAME
*name
= NULL
;
602 if (!(cert
= SSL_get_peer_certificate(tls
->ssl
))) {
603 log_fn(LOG_WARN
, "Peer has no certificate");
606 if (!(name
= X509_get_subject_name(cert
))) {
607 log_fn(LOG_WARN
, "Peer certificate has no subject name");
610 if ((nid
= OBJ_txt2nid("commonName")) == NID_undef
)
613 lenout
= X509_NAME_get_text_by_NID(name
, nid
, buf
, buflen
);
616 if (((int)strspn(buf
, LEGAL_NICKNAME_CHARACTERS
)) < lenout
) {
617 log_fn(LOG_WARN
, "Peer certificate nickname has illegal characters.");
629 static void log_cert_lifetime(X509
*cert
, const char *problem
)
633 char *s1
=NULL
, *s2
=NULL
;
635 time_t now
= time(NULL
);
638 log_fn(LOG_WARN
,"Certificate %s: is your system clock set incorrectly?",
641 if (!(bio
= BIO_new(BIO_s_mem()))) {
642 log_fn(LOG_WARN
, "Couldn't allocate BIO!"); goto end
;
644 if (!(ASN1_TIME_print(bio
, X509_get_notBefore(cert
)))) {
645 tls_log_errors(LOG_WARN
, "printing certificate lifetime");
648 BIO_get_mem_ptr(bio
, &buf
);
649 s1
= tor_strndup(buf
->data
, buf
->length
);
652 if (!(ASN1_TIME_print(bio
, X509_get_notAfter(cert
)))) {
653 tls_log_errors(LOG_WARN
, "printing certificate lifetime");
656 BIO_get_mem_ptr(bio
, &buf
);
657 s2
= tor_strndup(buf
->data
, buf
->length
);
659 strftime(mytime
, 32, "%b %d %H:%M:%S %Y GMT", gmtime(&now
));
661 log_fn(LOG_WARN
, "(certificate lifetime runs from %s through %s. Your time is %s.)",s1
,s2
,mytime
);
672 /** If the provided tls connection is authenticated and has a
673 * certificate that is currently valid and signed, then set
674 * *<b>identity_key</b> to the identity certificate's key and return
675 * 0. Else, return -1.
678 tor_tls_verify(tor_tls
*tls
, crypto_pk_env_t
**identity_key
)
680 X509
*cert
= NULL
, *id_cert
= NULL
;
681 STACK_OF(X509
) *chain
= NULL
;
682 EVP_PKEY
*id_pkey
= NULL
;
687 *identity_key
= NULL
;
689 if (!(cert
= SSL_get_peer_certificate(tls
->ssl
)))
691 if (!(chain
= SSL_get_peer_cert_chain(tls
->ssl
)))
693 num_in_chain
= sk_X509_num(chain
);
694 /* 1 means we're receiving (server-side), and it's just the id_cert.
695 * 2 means we're connecting (client-side), and it's both the link
696 * cert and the id_cert.
698 if (num_in_chain
< 1) {
699 log_fn(LOG_WARN
,"Unexpected number of certificates in chain (%d)",
703 for (i
=0; i
<num_in_chain
; ++i
) {
704 id_cert
= sk_X509_value(chain
, i
);
705 if (X509_cmp(id_cert
, cert
) != 0)
709 log_fn(LOG_WARN
,"No distinct identity certificate found");
713 if (!(id_pkey
= X509_get_pubkey(id_cert
)) ||
714 X509_verify(cert
, id_pkey
) <= 0) {
715 log_fn(LOG_WARN
,"X509_verify on cert and pkey returned <= 0");
716 tls_log_errors(LOG_WARN
,"verifying certificate");
720 rsa
= EVP_PKEY_get1_RSA(id_pkey
);
723 *identity_key
= _crypto_new_pk_env_rsa(rsa
);
731 EVP_PKEY_free(id_pkey
);
733 /* This should never get invoked, but let's make sure in case OpenSSL
734 * acts unexpectedly. */
735 tls_log_errors(LOG_WARN
, "finishing tor_tls_verify");
740 /** Check whether the certificate set on the connection <b>tls</b> is
741 * expired or not-yet-valid, give or take <b>tolerance</b>
742 * seconds. Return 0 for valid, -1 for failure.
744 * NOTE: you should call tor_tls_verify before tor_tls_check_lifetime.
747 tor_tls_check_lifetime(tor_tls
*tls
, int tolerance
)
755 if (!(cert
= SSL_get_peer_certificate(tls
->ssl
)))
759 if (X509_cmp_time(X509_get_notBefore(cert
), &t
) > 0) {
760 log_cert_lifetime(cert
, "not yet valid");
764 if (X509_cmp_time(X509_get_notAfter(cert
), &t
) < 0) {
765 log_cert_lifetime(cert
, "already expired");
777 /** Return the number of bytes available for reading from <b>tls</b>.
780 tor_tls_get_pending_bytes(tor_tls
*tls
)
783 #if OPENSSL_VERSION_NUMBER < 0x0090700fl
784 if (tls
->ssl
->rstate
== SSL_ST_READ_BODY
)
786 if (tls
->ssl
->s3
->rrec
.type
!= SSL3_RT_APPLICATION_DATA
)
789 return SSL_pending(tls
->ssl
);
793 /** Return the number of bytes read across the underlying socket. */
794 unsigned long tor_tls_get_n_bytes_read(tor_tls
*tls
)
797 return BIO_number_read(SSL_get_rbio(tls
->ssl
));
799 /** Return the number of bytes written across the underlying socket. */
800 unsigned long tor_tls_get_n_bytes_written(tor_tls
*tls
)
803 return BIO_number_written(SSL_get_wbio(tls
->ssl
));
806 /** Implement assert_no_tls_errors: If there are any pending OpenSSL
807 * errors, log an error message and assert(0). */
808 void _assert_no_tls_errors(const char *fname
, int line
)
810 if (ERR_peek_error() == 0)
812 log_fn(LOG_ERR
, "Unhandled OpenSSL errors found at %s:%d: ",
814 tls_log_errors(LOG_ERR
, NULL
);