tortls.c 18 KB

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