tortls.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /* Copyright 2003 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /* TLS wrappers for The Onion Router. (Unlike other tor functions, these
  5. * are prefixed with tor_ in order to avoid conflicting with OpenSSL
  6. * functions and variables.)
  7. */
  8. #include "./crypto.h"
  9. #include "./tortls.h"
  10. #include "./util.h"
  11. #include "./log.h"
  12. #include <string.h>
  13. /* Copied from or.h */
  14. #define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  15. #include <assert.h>
  16. #include <openssl/ssl.h>
  17. #include <openssl/err.h>
  18. #include <openssl/tls1.h>
  19. #include <openssl/asn1.h>
  20. #include <openssl/bio.h>
  21. /* How long do identity certificates live? (sec) */
  22. #define IDENTITY_CERT_LIFETIME (365*24*60*60)
  23. /* How much clock skew do we tolerate when checking certificates? (sec) */
  24. #define CERT_ALLOW_SKEW (90*60)
  25. struct tor_tls_context_st {
  26. SSL_CTX *ctx;
  27. };
  28. struct tor_tls_st {
  29. SSL *ssl;
  30. int socket;
  31. enum {
  32. TOR_TLS_ST_HANDSHAKE, TOR_TLS_ST_OPEN, TOR_TLS_ST_GOTCLOSE,
  33. TOR_TLS_ST_SENTCLOSE, TOR_TLS_ST_CLOSED
  34. } state;
  35. int isServer;
  36. int wantwrite_n; /* 0 normally, >0 if we returned wantwrite last time */
  37. };
  38. static X509* tor_tls_create_certificate(crypto_pk_env_t *rsa,
  39. crypto_pk_env_t *rsa_sign,
  40. const char *cname,
  41. const char *cname_sign,
  42. unsigned int lifetime);
  43. /* global tls context, keep it here because nobody else needs to touch it */
  44. static tor_tls_context *global_tls_context = NULL;
  45. static int tls_library_is_initialized = 0;
  46. #define _TOR_TLS_SYSCALL -6
  47. #define _TOR_TLS_ZERORETURN -5
  48. /* These functions are declared in crypto.c but not exported. */
  49. EVP_PKEY *_crypto_pk_env_get_evp_pkey(crypto_pk_env_t *env, int private);
  50. crypto_pk_env_t *_crypto_new_pk_env_rsa(RSA *rsa);
  51. DH *_crypto_dh_env_get_dh(crypto_dh_env_t *dh);
  52. static void
  53. tls_log_errors(int severity, const char *doing)
  54. {
  55. int err;
  56. const char *msg, *lib, *func;
  57. while ((err = ERR_get_error()) != 0) {
  58. msg = (const char*)ERR_reason_error_string(err);
  59. lib = (const char*)ERR_lib_error_string(err);
  60. func = (const char*)ERR_func_error_string(err);
  61. if (!msg) msg = "(null)";
  62. if (doing) {
  63. log(severity, "TLS error while %s: %s (in %s:%s)", doing, msg, lib,func);
  64. } else {
  65. log(severity, "TLS error: %s (in %s:%s)", msg, lib, func);
  66. }
  67. }
  68. }
  69. #define CATCH_SYSCALL 1
  70. #define CATCH_ZERO 2
  71. static int
  72. tor_tls_get_error(tor_tls *tls, int r, int extra,
  73. const char *doing, int severity)
  74. {
  75. int err = SSL_get_error(tls->ssl, r);
  76. switch (err) {
  77. case SSL_ERROR_NONE:
  78. return TOR_TLS_DONE;
  79. case SSL_ERROR_WANT_READ:
  80. return TOR_TLS_WANTREAD;
  81. case SSL_ERROR_WANT_WRITE:
  82. return TOR_TLS_WANTWRITE;
  83. case SSL_ERROR_SYSCALL:
  84. if (extra&CATCH_SYSCALL)
  85. return _TOR_TLS_SYSCALL;
  86. log(severity, "TLS error: <syscall error> (errno=%d: %s)",errno,
  87. strerror(errno));
  88. tls_log_errors(severity, doing);
  89. return TOR_TLS_ERROR;
  90. case SSL_ERROR_ZERO_RETURN:
  91. if (extra&CATCH_ZERO)
  92. return _TOR_TLS_ZERORETURN;
  93. log(severity, "TLS error: Zero return");
  94. tls_log_errors(severity, doing);
  95. return TOR_TLS_ERROR;
  96. default:
  97. tls_log_errors(severity, doing);
  98. return TOR_TLS_ERROR;
  99. }
  100. }
  101. static void
  102. tor_tls_init() {
  103. if (!tls_library_is_initialized) {
  104. SSL_library_init();
  105. SSL_load_error_strings();
  106. crypto_global_init();
  107. OpenSSL_add_all_algorithms();
  108. tls_library_is_initialized = 1;
  109. }
  110. }
  111. static int always_accept_verify_cb(int preverify_ok,
  112. X509_STORE_CTX *x509_ctx)
  113. {
  114. /* We always accept peer certs and complete the handshake. We don't validate
  115. * them until later. */
  116. return 1;
  117. }
  118. /* Generate a self-signed certificate with the private key 'rsa' and
  119. * identity key 'identity and commonName 'nickname'. Return a certificate
  120. * on success, NULL on failure.
  121. * DOCDOC
  122. */
  123. X509 *
  124. tor_tls_create_certificate(crypto_pk_env_t *rsa,
  125. crypto_pk_env_t *rsa_sign,
  126. const char *cname,
  127. const char *cname_sign,
  128. unsigned int cert_lifetime)
  129. {
  130. time_t start_time, end_time;
  131. EVP_PKEY *sign_pkey = NULL, *pkey=NULL;
  132. X509 *x509 = NULL;
  133. X509_NAME *name = NULL, *name_issuer=NULL;
  134. int nid;
  135. tor_tls_init();
  136. start_time = time(NULL);
  137. tor_assert(rsa && cname && rsa_sign && cname_sign);
  138. if (!(sign_pkey = _crypto_pk_env_get_evp_pkey(rsa_sign,1)))
  139. goto error;
  140. if (!(pkey = _crypto_pk_env_get_evp_pkey(rsa,0)))
  141. goto error;
  142. if (!(x509 = X509_new()))
  143. goto error;
  144. if (!(X509_set_version(x509, 2)))
  145. goto error;
  146. if (!(ASN1_INTEGER_set(X509_get_serialNumber(x509), (long)start_time)))
  147. goto error;
  148. if (!(name = X509_NAME_new()))
  149. goto error;
  150. if ((nid = OBJ_txt2nid("organizationName")) == NID_undef) goto error;
  151. if (!(X509_NAME_add_entry_by_NID(name, nid, MBSTRING_ASC,
  152. "TOR", -1, -1, 0))) goto error;
  153. if ((nid = OBJ_txt2nid("commonName")) == NID_undef) goto error;
  154. if (!(X509_NAME_add_entry_by_NID(name, nid, MBSTRING_ASC,
  155. (char*)cname, -1, -1, 0))) goto error;
  156. if (!(X509_set_subject_name(x509, name)))
  157. goto error;
  158. if (!(name_issuer = X509_NAME_new()))
  159. goto error;
  160. if ((nid = OBJ_txt2nid("organizationName")) == NID_undef) goto error;
  161. if (!(X509_NAME_add_entry_by_NID(name_issuer, nid, MBSTRING_ASC,
  162. "TOR", -1, -1, 0))) goto error;
  163. if ((nid = OBJ_txt2nid("commonName")) == NID_undef) goto error;
  164. if (!(X509_NAME_add_entry_by_NID(name_issuer, nid, MBSTRING_ASC,
  165. (char*)cname_sign, -1, -1, 0))) goto error;
  166. if (!(X509_set_issuer_name(x509, name_issuer)))
  167. goto error;
  168. if (!X509_time_adj(X509_get_notBefore(x509),0,&start_time))
  169. goto error;
  170. end_time = start_time + cert_lifetime;
  171. if (!X509_time_adj(X509_get_notAfter(x509),0,&end_time))
  172. goto error;
  173. if (!X509_set_pubkey(x509, pkey))
  174. goto error;
  175. if (!X509_sign(x509, sign_pkey, EVP_sha1()))
  176. goto error;
  177. goto done;
  178. error:
  179. tls_log_errors(LOG_WARN, "generating certificate");
  180. if (x509) {
  181. X509_free(x509);
  182. x509 = NULL;
  183. }
  184. done:
  185. if (sign_pkey)
  186. EVP_PKEY_free(sign_pkey);
  187. if (pkey)
  188. EVP_PKEY_free(pkey);
  189. if (name)
  190. X509_NAME_free(name);
  191. if (name_issuer)
  192. X509_NAME_free(name_issuer);
  193. return x509;
  194. }
  195. #ifdef EVERYONE_HAS_AES
  196. /* Everybody is running OpenSSL 0.9.7 or later, so no backward compatibility
  197. * is needed. */
  198. #define CIPHER_LIST TLS1_TXT_DHE_RSA_WITH_AES_128_SHA
  199. #elif defined(TLS1_TXT_DHE_RSA_WITH_AES_128_SHA)
  200. /* Some people are running OpenSSL before 0.9.7, but we aren't.
  201. * We can support AES and 3DES.
  202. */
  203. #define CIPHER_LIST (TLS1_TXT_DHE_RSA_WITH_AES_128_SHA ":" \
  204. SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA)
  205. #else
  206. /* We're running OpenSSL before 0.9.7. We only support 3DES. */
  207. #define CIPHER_LIST SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA
  208. #endif
  209. /* Create a new TLS context. If we are going to be using it as a
  210. * server, it must have isServer set to true, 'identity' set to the
  211. * identity key used to sign that certificate, and 'nickname' set to
  212. * the server's nickname. Return -1 if failure, else 0.
  213. */
  214. int
  215. tor_tls_context_new(crypto_pk_env_t *identity,
  216. int isServer, const char *nickname,
  217. unsigned int key_lifetime)
  218. {
  219. crypto_pk_env_t *rsa = NULL;
  220. crypto_dh_env_t *dh = NULL;
  221. EVP_PKEY *pkey = NULL;
  222. tor_tls_context *result = NULL;
  223. X509 *cert = NULL, *idcert = NULL;
  224. char nn2[1024];
  225. sprintf(nn2, "%s <identity>", nickname);
  226. tor_tls_init();
  227. if (isServer) {
  228. if (!(rsa = crypto_new_pk_env()))
  229. goto error;
  230. if (crypto_pk_generate_key(rsa)<0)
  231. goto error;
  232. cert = tor_tls_create_certificate(rsa, identity, nickname, nn2,
  233. key_lifetime);
  234. idcert = tor_tls_create_certificate(identity, identity, nn2, nn2,
  235. IDENTITY_CERT_LIFETIME);
  236. if (!cert || !idcert) {
  237. log(LOG_WARN, "Error creating certificate");
  238. goto error;
  239. }
  240. }
  241. result = tor_malloc(sizeof(tor_tls_context));
  242. result->ctx = NULL;
  243. #ifdef EVERYONE_HAS_AES
  244. /* Tell OpenSSL to only use TLS1 */
  245. if (!(result->ctx = SSL_CTX_new(TLSv1_method())))
  246. goto error;
  247. #else
  248. /* Tell OpenSSL to use SSL3 or TLS1 but not SSL2. */
  249. if (!(result->ctx = SSL_CTX_new(SSLv23_method())))
  250. goto error;
  251. SSL_CTX_set_options(result->ctx, SSL_OP_NO_SSLv2);
  252. #endif
  253. if (!SSL_CTX_set_cipher_list(result->ctx, CIPHER_LIST))
  254. goto error;
  255. if (cert && !SSL_CTX_use_certificate(result->ctx,cert))
  256. goto error;
  257. if (idcert && !SSL_CTX_add_extra_chain_cert(result->ctx,idcert))
  258. goto error;
  259. SSL_CTX_set_session_cache_mode(result->ctx, SSL_SESS_CACHE_OFF);
  260. if (isServer) {
  261. tor_assert(rsa);
  262. if (!(pkey = _crypto_pk_env_get_evp_pkey(rsa,1)))
  263. goto error;
  264. if (!SSL_CTX_use_PrivateKey(result->ctx, pkey))
  265. goto error;
  266. EVP_PKEY_free(pkey);
  267. pkey = NULL;
  268. if (cert) {
  269. if (!SSL_CTX_check_private_key(result->ctx))
  270. goto error;
  271. }
  272. }
  273. dh = crypto_dh_new();
  274. SSL_CTX_set_tmp_dh(result->ctx, _crypto_dh_env_get_dh(dh));
  275. crypto_dh_free(dh);
  276. SSL_CTX_set_verify(result->ctx, SSL_VERIFY_PEER,
  277. always_accept_verify_cb);
  278. /* let us realloc bufs that we're writing from */
  279. SSL_CTX_set_mode(result->ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
  280. /* Free the old context if one exists. */
  281. if (global_tls_context) {
  282. /* This is safe even if there are open connections: OpenSSL does
  283. * reference counting with SSL and SSL_CTX objects. */
  284. SSL_CTX_free(global_tls_context->ctx);
  285. free(global_tls_context);
  286. }
  287. global_tls_context = result;
  288. return 0;
  289. error:
  290. tls_log_errors(LOG_WARN, "creating TLS context");
  291. if (pkey)
  292. EVP_PKEY_free(pkey);
  293. if (rsa)
  294. crypto_free_pk_env(rsa);
  295. if (dh)
  296. crypto_dh_free(dh);
  297. if (result && result->ctx)
  298. SSL_CTX_free(result->ctx);
  299. if (result)
  300. free(result);
  301. /* leak certs XXXX ? */
  302. return -1;
  303. }
  304. /* Create a new TLS object from a TLS context, a filedescriptor, and
  305. * a flag to determine whether it is functioning as a server.
  306. */
  307. tor_tls *
  308. tor_tls_new(int sock, int isServer)
  309. {
  310. tor_tls *result = tor_malloc(sizeof(tor_tls));
  311. tor_assert(global_tls_context); /* make sure somebody made it first */
  312. if (!(result->ssl = SSL_new(global_tls_context->ctx)))
  313. return NULL;
  314. result->socket = sock;
  315. SSL_set_fd(result->ssl, sock);
  316. result->state = TOR_TLS_ST_HANDSHAKE;
  317. result->isServer = isServer;
  318. result->wantwrite_n = 0;
  319. return result;
  320. }
  321. /* Release resources associated with a TLS object. Does not close the
  322. * underlying file descriptor.
  323. */
  324. void
  325. tor_tls_free(tor_tls *tls)
  326. {
  327. SSL_free(tls->ssl);
  328. free(tls);
  329. }
  330. /* Underlying function for TLS reading. Reads up to 'len' characters
  331. * from 'tls' into 'cp'. On success, returns the number of characters
  332. * read. On failure, returns TOR_TLS_ERROR, TOR_TLS_CLOSE,
  333. * TOR_TLS_WANTREAD, or TOR_TLS_WANTWRITE.
  334. */
  335. int
  336. tor_tls_read(tor_tls *tls, char *cp, int len)
  337. {
  338. int r, err;
  339. tor_assert(tls && tls->ssl);
  340. tor_assert(tls->state == TOR_TLS_ST_OPEN);
  341. r = SSL_read(tls->ssl, cp, len);
  342. if (r > 0)
  343. return r;
  344. err = tor_tls_get_error(tls, r, CATCH_ZERO, "reading", LOG_INFO);
  345. log_fn(LOG_DEBUG,"returned r=%d, err=%d",r,err);
  346. if (err == _TOR_TLS_ZERORETURN) {
  347. tls->state = TOR_TLS_ST_CLOSED;
  348. return TOR_TLS_CLOSE;
  349. } else {
  350. tor_assert(err != TOR_TLS_DONE);
  351. return err;
  352. }
  353. }
  354. /* Underlying function for TLS writing. Write up to 'n' characters
  355. * from 'cp' onto 'tls'. On success, returns the number of characters
  356. * written. On failure, returns TOR_TLS_ERROR, TOR_TLS_WANTREAD,
  357. * or TOR_TLS_WANTWRITE.
  358. */
  359. int
  360. tor_tls_write(tor_tls *tls, char *cp, int n)
  361. {
  362. int r, err;
  363. tor_assert(tls && tls->ssl);
  364. tor_assert(tls->state == TOR_TLS_ST_OPEN);
  365. if (n == 0)
  366. return 0;
  367. if(tls->wantwrite_n) {
  368. /* if WANTWRITE last time, we must use the _same_ n as before */
  369. tor_assert(n >= tls->wantwrite_n);
  370. log_fn(LOG_DEBUG,"resuming pending-write, (%d to flush, reusing %d)",
  371. n, tls->wantwrite_n);
  372. n = tls->wantwrite_n;
  373. tls->wantwrite_n = 0;
  374. }
  375. r = SSL_write(tls->ssl, cp, n);
  376. err = tor_tls_get_error(tls, r, 0, "writing", LOG_INFO);
  377. if (err == TOR_TLS_DONE) {
  378. return r;
  379. }
  380. if (err == TOR_TLS_WANTWRITE || err == TOR_TLS_WANTREAD) {
  381. // log_fn(LOG_INFO,"wantwrite or wantread. remembering the number %d.",n);
  382. tls->wantwrite_n = n;
  383. }
  384. return err;
  385. }
  386. /* Perform initial handshake on 'tls'. When finished, returns
  387. * TOR_TLS_DONE. On failure, returns TOR_TLS_ERROR, TOR_TLS_WANTREAD,
  388. * or TOR_TLS_WANTWRITE.
  389. */
  390. int
  391. tor_tls_handshake(tor_tls *tls)
  392. {
  393. int r;
  394. tor_assert(tls && tls->ssl);
  395. tor_assert(tls->state == TOR_TLS_ST_HANDSHAKE);
  396. if (tls->isServer) {
  397. r = SSL_accept(tls->ssl);
  398. } else {
  399. r = SSL_connect(tls->ssl);
  400. }
  401. r = tor_tls_get_error(tls,r,0, "handshaking", LOG_INFO);
  402. if (r == TOR_TLS_DONE) {
  403. tls->state = TOR_TLS_ST_OPEN;
  404. }
  405. return r;
  406. }
  407. /* Shut down an open tls connection 'tls'. When finished, returns
  408. * TOR_TLS_DONE. On failure, returns TOR_TLS_ERROR, TOR_TLS_WANTREAD,
  409. * or TOR_TLS_WANTWRITE.
  410. */
  411. int
  412. tor_tls_shutdown(tor_tls *tls)
  413. {
  414. int r, err;
  415. char buf[128];
  416. tor_assert(tls && tls->ssl);
  417. while (1) {
  418. if (tls->state == TOR_TLS_ST_SENTCLOSE) {
  419. /* If we've already called shutdown once to send a close message,
  420. * we read until the other side has closed too.
  421. */
  422. do {
  423. r = SSL_read(tls->ssl, buf, 128);
  424. } while (r>0);
  425. err = tor_tls_get_error(tls, r, CATCH_ZERO, "reading to shut down",
  426. LOG_INFO);
  427. if (err == _TOR_TLS_ZERORETURN) {
  428. tls->state = TOR_TLS_ST_GOTCLOSE;
  429. /* fall through... */
  430. } else {
  431. return err;
  432. }
  433. }
  434. r = SSL_shutdown(tls->ssl);
  435. if (r == 1) {
  436. /* If shutdown returns 1, the connection is entirely closed. */
  437. tls->state = TOR_TLS_ST_CLOSED;
  438. return TOR_TLS_DONE;
  439. }
  440. err = tor_tls_get_error(tls, r, CATCH_SYSCALL|CATCH_ZERO, "shutting down",
  441. LOG_INFO);
  442. if (err == _TOR_TLS_SYSCALL) {
  443. /* The underlying TCP connection closed while we were shutting down. */
  444. tls->state = TOR_TLS_ST_CLOSED;
  445. return TOR_TLS_DONE;
  446. } else if (err == _TOR_TLS_ZERORETURN) {
  447. /* The TLS connection says that it sent a shutdown record, but
  448. * isn't done shutting down yet. Make sure that this hasn't
  449. * happened before, then go back to the start of the function
  450. * and try to read.
  451. */
  452. if (tls->state == TOR_TLS_ST_GOTCLOSE ||
  453. tls->state == TOR_TLS_ST_SENTCLOSE) {
  454. log(LOG_WARN,
  455. "TLS returned \"half-closed\" value while already half-closed");
  456. return TOR_TLS_ERROR;
  457. }
  458. tls->state = TOR_TLS_ST_SENTCLOSE;
  459. /* fall through ... */
  460. } else {
  461. return err;
  462. }
  463. } /* end loop */
  464. }
  465. /* Return true iff this TLS connection is authenticated.
  466. */
  467. int
  468. tor_tls_peer_has_cert(tor_tls *tls)
  469. {
  470. X509 *cert;
  471. if (!(cert = SSL_get_peer_certificate(tls->ssl)))
  472. return 0;
  473. X509_free(cert);
  474. return 1;
  475. }
  476. int
  477. tor_tls_get_peer_cert_nickname(tor_tls *tls, char *buf, int buflen)
  478. {
  479. X509 *cert = NULL;
  480. X509_NAME *name = NULL;
  481. int nid;
  482. int lenout;
  483. if (!(cert = SSL_get_peer_certificate(tls->ssl))) {
  484. log_fn(LOG_WARN, "Peer has no certificate");
  485. goto error;
  486. }
  487. if (!(name = X509_get_subject_name(cert))) {
  488. log_fn(LOG_WARN, "Peer certificate has no subject name");
  489. goto error;
  490. }
  491. if ((nid = OBJ_txt2nid("commonName")) == NID_undef)
  492. goto error;
  493. lenout = X509_NAME_get_text_by_NID(name, nid, buf, buflen);
  494. if (lenout == -1)
  495. goto error;
  496. if (((int)strspn(buf, LEGAL_NICKNAME_CHARACTERS)) < lenout) {
  497. log_fn(LOG_WARN, "Peer certificate nickname has illegal characters.");
  498. goto error;
  499. }
  500. return 0;
  501. error:
  502. if (cert)
  503. X509_free(cert);
  504. if (name)
  505. X509_NAME_free(name);
  506. return -1;
  507. }
  508. /* If the provided tls connection is authenticated and has a
  509. * certificate that is currently valid and is correctly signed by
  510. * identity_key, return 0. Else, return -1.
  511. */
  512. int
  513. tor_tls_verify(tor_tls *tls, crypto_pk_env_t *identity_key)
  514. {
  515. X509 *cert = NULL;
  516. EVP_PKEY *id_pkey = NULL;
  517. time_t now, t;
  518. int r = -1;
  519. if (!(cert = SSL_get_peer_certificate(tls->ssl)))
  520. return -1;
  521. now = time(NULL);
  522. t = now + CERT_ALLOW_SKEW;
  523. if (X509_cmp_time(X509_get_notBefore(cert), &t) > 0) {
  524. log_fn(LOG_WARN,"Certificate becomes valid in the future: possible clock skew.");
  525. goto done;
  526. }
  527. t = now - CERT_ALLOW_SKEW;
  528. if (X509_cmp_time(X509_get_notAfter(cert), &t) < 0) {
  529. log_fn(LOG_WARN,"Certificate already expired; possible clock skew.");
  530. goto done;
  531. }
  532. /* Get the public key. */
  533. if (!(id_pkey = _crypto_pk_env_get_evp_pkey(identity_key,0)) ||
  534. X509_verify(cert, id_pkey) <= 0) {
  535. log_fn(LOG_WARN,"X509_verify on cert and pkey returned <= 0");
  536. tls_log_errors(LOG_WARN,"verifying certificate");
  537. goto done;
  538. }
  539. r = 0;
  540. done:
  541. if (cert)
  542. X509_free(cert);
  543. if (id_pkey)
  544. EVP_PKEY_free(id_pkey);
  545. /* XXXX */
  546. tls_log_errors(LOG_WARN, "finishing tor_tls_verify");
  547. return r;
  548. }
  549. int
  550. tor_tls_get_pending_bytes(tor_tls *tls)
  551. {
  552. tor_assert(tls);
  553. return SSL_pending(tls->ssl);
  554. }
  555. /* Return the number of bytes read across the underlying socket. */
  556. unsigned long tor_tls_get_n_bytes_read(tor_tls *tls)
  557. {
  558. tor_assert(tls);
  559. return BIO_number_read(SSL_get_rbio(tls->ssl));
  560. }
  561. unsigned long tor_tls_get_n_bytes_written(tor_tls *tls)
  562. {
  563. tor_assert(tls);
  564. return BIO_number_written(SSL_get_wbio(tls->ssl));
  565. }
  566. void _assert_no_tls_errors(const char *fname, int line)
  567. {
  568. if (ERR_peek_error() == 0)
  569. return;
  570. log_fn(LOG_ERR, "Unhandled OpenSSL errors found at %s:%d: ",
  571. fname, line);
  572. tls_log_errors(LOG_ERR, NULL);
  573. tor_assert(0);
  574. }
  575. /*
  576. Local Variables:
  577. mode:c
  578. indent-tabs-mode:nil
  579. c-basic-offset:2
  580. End:
  581. */