tortls.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. /* Copyright (c) 2003, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2011, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file tortls.c
  7. * \brief Wrapper functions to present a consistent interface to
  8. * TLS, SSL, and X.509 functions from OpenSSL.
  9. **/
  10. /* (Unlike other tor functions, these
  11. * are prefixed with tor_ in order to avoid conflicting with OpenSSL
  12. * functions and variables.)
  13. */
  14. #include "orconfig.h"
  15. #include <assert.h>
  16. #ifdef MS_WINDOWS /*wrkard for dtls1.h >= 0.9.8m of "#include <winsock.h>"*/
  17. #define WIN32_WINNT 0x400
  18. #define _WIN32_WINNT 0x400
  19. #define WIN32_LEAN_AND_MEAN
  20. #if defined(_MSC_VER) && (_MSC_VER < 1300)
  21. #include <winsock.h>
  22. #else
  23. #include <winsock2.h>
  24. #include <ws2tcpip.h>
  25. #endif
  26. #endif
  27. #include <openssl/ssl.h>
  28. #include <openssl/ssl3.h>
  29. #include <openssl/err.h>
  30. #include <openssl/tls1.h>
  31. #include <openssl/asn1.h>
  32. #include <openssl/bio.h>
  33. #include <openssl/opensslv.h>
  34. #if OPENSSL_VERSION_NUMBER < 0x00907000l
  35. #error "We require OpenSSL >= 0.9.7"
  36. #endif
  37. #define CRYPTO_PRIVATE /* to import prototypes from crypto.h */
  38. #include "crypto.h"
  39. #include "tortls.h"
  40. #include "util.h"
  41. #include "log.h"
  42. #include "container.h"
  43. #include "ht.h"
  44. #include <string.h>
  45. /* Enable the "v2" TLS handshake.
  46. */
  47. #define V2_HANDSHAKE_SERVER
  48. #define V2_HANDSHAKE_CLIENT
  49. /* Copied from or.h */
  50. #define LEGAL_NICKNAME_CHARACTERS \
  51. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  52. /** How long do identity certificates live? (sec) */
  53. #define IDENTITY_CERT_LIFETIME (365*24*60*60)
  54. #define ADDR(tls) (((tls) && (tls)->address) ? tls->address : "peer")
  55. /* We redefine these so that we can run correctly even if the vendor gives us
  56. * a version of OpenSSL that does not match its header files. (Apple: I am
  57. * looking at you.)
  58. */
  59. #ifndef SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
  60. #define SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION 0x00040000L
  61. #endif
  62. #ifndef SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
  63. #define SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION 0x0010
  64. #endif
  65. /** Does the run-time openssl version look like we need
  66. * SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION? */
  67. static int use_unsafe_renegotiation_op = 0;
  68. /** Does the run-time openssl version look like we need
  69. * SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION? */
  70. static int use_unsafe_renegotiation_flag = 0;
  71. /** Structure holding the TLS state for a single connection. */
  72. typedef struct tor_tls_context_t {
  73. int refcnt;
  74. SSL_CTX *ctx;
  75. X509 *my_cert;
  76. X509 *my_id_cert;
  77. crypto_pk_env_t *key;
  78. } tor_tls_context_t;
  79. /** Holds a SSL object and its associated data. Members are only
  80. * accessed from within tortls.c.
  81. */
  82. struct tor_tls_t {
  83. HT_ENTRY(tor_tls_t) node;
  84. tor_tls_context_t *context; /** A link to the context object for this tls. */
  85. SSL *ssl; /**< An OpenSSL SSL object. */
  86. int socket; /**< The underlying file descriptor for this TLS connection. */
  87. char *address; /**< An address to log when describing this connection. */
  88. enum {
  89. TOR_TLS_ST_HANDSHAKE, TOR_TLS_ST_OPEN, TOR_TLS_ST_GOTCLOSE,
  90. TOR_TLS_ST_SENTCLOSE, TOR_TLS_ST_CLOSED, TOR_TLS_ST_RENEGOTIATE,
  91. } state : 3; /**< The current SSL state, depending on which operations have
  92. * completed successfully. */
  93. unsigned int isServer:1; /**< True iff this is a server-side connection */
  94. unsigned int wasV2Handshake:1; /**< True iff the original handshake for
  95. * this connection used the updated version
  96. * of the connection protocol (client sends
  97. * different cipher list, server sends only
  98. * one certificate). */
  99. /** True iff we should call negotiated_callback when we're done reading. */
  100. unsigned int got_renegotiate:1;
  101. size_t wantwrite_n; /**< 0 normally, >0 if we returned wantwrite last
  102. * time. */
  103. /** Last values retrieved from BIO_number_read()/write(); see
  104. * tor_tls_get_n_raw_bytes() for usage.
  105. */
  106. unsigned long last_write_count;
  107. unsigned long last_read_count;
  108. /** If set, a callback to invoke whenever the client tries to renegotiate
  109. * the handshake. */
  110. void (*negotiated_callback)(tor_tls_t *tls, void *arg);
  111. /** Argument to pass to negotiated_callback. */
  112. void *callback_arg;
  113. };
  114. #ifdef V2_HANDSHAKE_CLIENT
  115. /** An array of fake SSL_CIPHER objects that we use in order to trick OpenSSL
  116. * in client mode into advertising the ciphers we want. See
  117. * rectify_client_ciphers() for details. */
  118. static SSL_CIPHER *CLIENT_CIPHER_DUMMIES = NULL;
  119. /** A stack of SSL_CIPHER objects, some real, some fake.
  120. * See rectify_client_ciphers() for details. */
  121. static STACK_OF(SSL_CIPHER) *CLIENT_CIPHER_STACK = NULL;
  122. #endif
  123. /** Helper: compare tor_tls_t objects by its SSL. */
  124. static INLINE int
  125. tor_tls_entries_eq(const tor_tls_t *a, const tor_tls_t *b)
  126. {
  127. return a->ssl == b->ssl;
  128. }
  129. /** Helper: return a hash value for a tor_tls_t by its SSL. */
  130. static INLINE unsigned int
  131. tor_tls_entry_hash(const tor_tls_t *a)
  132. {
  133. #if SIZEOF_INT == SIZEOF_VOID_P
  134. return ((unsigned int)(uintptr_t)a->ssl);
  135. #else
  136. return (unsigned int) ((((uint64_t)a->ssl)>>2) & UINT_MAX);
  137. #endif
  138. }
  139. /** Map from SSL* pointers to tor_tls_t objects using those pointers.
  140. */
  141. static HT_HEAD(tlsmap, tor_tls_t) tlsmap_root = HT_INITIALIZER();
  142. HT_PROTOTYPE(tlsmap, tor_tls_t, node, tor_tls_entry_hash,
  143. tor_tls_entries_eq)
  144. HT_GENERATE(tlsmap, tor_tls_t, node, tor_tls_entry_hash,
  145. tor_tls_entries_eq, 0.6, malloc, realloc, free)
  146. /** Helper: given a SSL* pointer, return the tor_tls_t object using that
  147. * pointer. */
  148. static INLINE tor_tls_t *
  149. tor_tls_get_by_ssl(const SSL *ssl)
  150. {
  151. tor_tls_t search, *result;
  152. memset(&search, 0, sizeof(search));
  153. search.ssl = (SSL*)ssl;
  154. result = HT_FIND(tlsmap, &tlsmap_root, &search);
  155. return result;
  156. }
  157. static void tor_tls_context_decref(tor_tls_context_t *ctx);
  158. static void tor_tls_context_incref(tor_tls_context_t *ctx);
  159. static X509* tor_tls_create_certificate(crypto_pk_env_t *rsa,
  160. crypto_pk_env_t *rsa_sign,
  161. const char *cname,
  162. const char *cname_sign,
  163. unsigned int lifetime);
  164. static void tor_tls_unblock_renegotiation(tor_tls_t *tls);
  165. /** Global tls context. We keep it here because nobody else needs to
  166. * touch it. */
  167. static tor_tls_context_t *global_tls_context = NULL;
  168. /** True iff tor_tls_init() has been called. */
  169. static int tls_library_is_initialized = 0;
  170. /* Module-internal error codes. */
  171. #define _TOR_TLS_SYSCALL (_MIN_TOR_TLS_ERROR_VAL - 2)
  172. #define _TOR_TLS_ZERORETURN (_MIN_TOR_TLS_ERROR_VAL - 1)
  173. /** Log all pending tls errors at level <b>severity</b>. Use
  174. * <b>doing</b> to describe our current activities.
  175. */
  176. static void
  177. tls_log_errors(tor_tls_t *tls, int severity, const char *doing)
  178. {
  179. unsigned long err;
  180. const char *msg, *lib, *func, *addr;
  181. addr = tls ? tls->address : NULL;
  182. while ((err = ERR_get_error()) != 0) {
  183. msg = (const char*)ERR_reason_error_string(err);
  184. lib = (const char*)ERR_lib_error_string(err);
  185. func = (const char*)ERR_func_error_string(err);
  186. if (!msg) msg = "(null)";
  187. if (!lib) lib = "(null)";
  188. if (!func) func = "(null)";
  189. if (doing) {
  190. log(severity, LD_NET, "TLS error while %s%s%s: %s (in %s:%s)",
  191. doing, addr?" with ":"", addr?addr:"",
  192. msg, lib, func);
  193. } else {
  194. log(severity, LD_NET, "TLS error%s%s: %s (in %s:%s)",
  195. addr?" with ":"", addr?addr:"",
  196. msg, lib, func);
  197. }
  198. }
  199. }
  200. /** Convert an errno (or a WSAerrno on windows) into a TOR_TLS_* error
  201. * code. */
  202. static int
  203. tor_errno_to_tls_error(int e)
  204. {
  205. #if defined(MS_WINDOWS)
  206. switch (e) {
  207. case WSAECONNRESET: // most common
  208. return TOR_TLS_ERROR_CONNRESET;
  209. case WSAETIMEDOUT:
  210. return TOR_TLS_ERROR_TIMEOUT;
  211. case WSAENETUNREACH:
  212. case WSAEHOSTUNREACH:
  213. return TOR_TLS_ERROR_NO_ROUTE;
  214. case WSAECONNREFUSED:
  215. return TOR_TLS_ERROR_CONNREFUSED; // least common
  216. default:
  217. return TOR_TLS_ERROR_MISC;
  218. }
  219. #else
  220. switch (e) {
  221. case ECONNRESET: // most common
  222. return TOR_TLS_ERROR_CONNRESET;
  223. case ETIMEDOUT:
  224. return TOR_TLS_ERROR_TIMEOUT;
  225. case EHOSTUNREACH:
  226. case ENETUNREACH:
  227. return TOR_TLS_ERROR_NO_ROUTE;
  228. case ECONNREFUSED:
  229. return TOR_TLS_ERROR_CONNREFUSED; // least common
  230. default:
  231. return TOR_TLS_ERROR_MISC;
  232. }
  233. #endif
  234. }
  235. /** Given a TOR_TLS_* error code, return a string equivalent. */
  236. const char *
  237. tor_tls_err_to_string(int err)
  238. {
  239. if (err >= 0)
  240. return "[Not an error.]";
  241. switch (err) {
  242. case TOR_TLS_ERROR_MISC: return "misc error";
  243. case TOR_TLS_ERROR_IO: return "unexpected close";
  244. case TOR_TLS_ERROR_CONNREFUSED: return "connection refused";
  245. case TOR_TLS_ERROR_CONNRESET: return "connection reset";
  246. case TOR_TLS_ERROR_NO_ROUTE: return "host unreachable";
  247. case TOR_TLS_ERROR_TIMEOUT: return "connection timed out";
  248. case TOR_TLS_CLOSE: return "closed";
  249. case TOR_TLS_WANTREAD: return "want to read";
  250. case TOR_TLS_WANTWRITE: return "want to write";
  251. default: return "(unknown error code)";
  252. }
  253. }
  254. #define CATCH_SYSCALL 1
  255. #define CATCH_ZERO 2
  256. /** Given a TLS object and the result of an SSL_* call, use
  257. * SSL_get_error to determine whether an error has occurred, and if so
  258. * which one. Return one of TOR_TLS_{DONE|WANTREAD|WANTWRITE|ERROR}.
  259. * If extra&CATCH_SYSCALL is true, return _TOR_TLS_SYSCALL instead of
  260. * reporting syscall errors. If extra&CATCH_ZERO is true, return
  261. * _TOR_TLS_ZERORETURN instead of reporting zero-return errors.
  262. *
  263. * If an error has occurred, log it at level <b>severity</b> and describe the
  264. * current action as <b>doing</b>.
  265. */
  266. static int
  267. tor_tls_get_error(tor_tls_t *tls, int r, int extra,
  268. const char *doing, int severity)
  269. {
  270. int err = SSL_get_error(tls->ssl, r);
  271. int tor_error = TOR_TLS_ERROR_MISC;
  272. switch (err) {
  273. case SSL_ERROR_NONE:
  274. return TOR_TLS_DONE;
  275. case SSL_ERROR_WANT_READ:
  276. return TOR_TLS_WANTREAD;
  277. case SSL_ERROR_WANT_WRITE:
  278. return TOR_TLS_WANTWRITE;
  279. case SSL_ERROR_SYSCALL:
  280. if (extra&CATCH_SYSCALL)
  281. return _TOR_TLS_SYSCALL;
  282. if (r == 0) {
  283. log(severity, LD_NET, "TLS error: unexpected close while %s", doing);
  284. tor_error = TOR_TLS_ERROR_IO;
  285. } else {
  286. int e = tor_socket_errno(tls->socket);
  287. log(severity, LD_NET,
  288. "TLS error: <syscall error while %s> (errno=%d: %s)",
  289. doing, e, tor_socket_strerror(e));
  290. tor_error = tor_errno_to_tls_error(e);
  291. }
  292. tls_log_errors(tls, severity, doing);
  293. return tor_error;
  294. case SSL_ERROR_ZERO_RETURN:
  295. if (extra&CATCH_ZERO)
  296. return _TOR_TLS_ZERORETURN;
  297. log(severity, LD_NET, "TLS connection closed while %s", doing);
  298. tls_log_errors(tls, severity, doing);
  299. return TOR_TLS_CLOSE;
  300. default:
  301. tls_log_errors(tls, severity, doing);
  302. return TOR_TLS_ERROR_MISC;
  303. }
  304. }
  305. /** Initialize OpenSSL, unless it has already been initialized.
  306. */
  307. static void
  308. tor_tls_init(void)
  309. {
  310. if (!tls_library_is_initialized) {
  311. long version;
  312. SSL_library_init();
  313. SSL_load_error_strings();
  314. crypto_global_init(-1);
  315. version = SSLeay();
  316. /* OpenSSL 0.9.8l introdeced SSL3_FLAGS_ALLOW_UNSAGE_LEGACY_RENEGOTIATION
  317. * here, but without thinking too hard about it: it turns out that the
  318. * flag in question needed to be set at the last minute, and that it
  319. * conflicted with an existing flag number that had already been added
  320. * in the OpenSSL 1.0.0 betas. OpenSSL 0.9.8m thoughtfully replaced
  321. * the flag with an option and (it seems) broke anything that used
  322. * SSL3_FLAGS_* for the purpose. So we need to know how to do both,
  323. * and we mustn't use the SSL3_FLAGS option with anything besides
  324. * OpenSSL 0.9.8l.
  325. *
  326. * No, we can't just set flag 0x0010 everywhere. It breaks Tor with
  327. * OpenSSL 1.0.0beta3 and later. On the other hand, we might be able to
  328. * set option 0x00040000L everywhere.
  329. *
  330. * No, we can't simply detect whether the flag or the option is present
  331. * in the headers at build-time: some vendors (notably Apple) like to
  332. * leave their headers out of sync with their libraries.
  333. *
  334. * Yes, it _is_ almost as if the OpenSSL developers decided that no
  335. * program should be allowed to use renegotiation its first passed an
  336. * test of intelligence and determination.
  337. */
  338. if (version >= 0x009080c0L && version < 0x009080d0L) {
  339. log_notice(LD_GENERAL, "OpenSSL %s looks like version 0.9.8l; "
  340. "I will try SSL3_FLAGS to enable renegotation.",
  341. SSLeay_version(SSLEAY_VERSION));
  342. use_unsafe_renegotiation_flag = 1;
  343. use_unsafe_renegotiation_op = 1;
  344. } else if (version >= 0x009080d0L) {
  345. log_notice(LD_GENERAL, "OpenSSL %s looks like version 0.9.8m or later; "
  346. "I will try SSL_OP to enable renegotiation",
  347. SSLeay_version(SSLEAY_VERSION));
  348. use_unsafe_renegotiation_op = 1;
  349. } else if (version < 0x009080c0L) {
  350. log_notice(LD_GENERAL, "OpenSSL %s [%lx] looks like it's older than "
  351. "0.9.8l, but some vendors have backported 0.9.8l's "
  352. "renegotiation code to earlier versions, and some have "
  353. "backported the code from 0.9.8m or 0.9.8n. I'll set both "
  354. "SSL3_FLAGS and SSL_OP just to be safe.",
  355. SSLeay_version(SSLEAY_VERSION), version);
  356. use_unsafe_renegotiation_flag = 1;
  357. use_unsafe_renegotiation_op = 1;
  358. } else {
  359. log_info(LD_GENERAL, "OpenSSL %s has version %lx",
  360. SSLeay_version(SSLEAY_VERSION), version);
  361. }
  362. tls_library_is_initialized = 1;
  363. }
  364. }
  365. /** Free all global TLS structures. */
  366. void
  367. tor_tls_free_all(void)
  368. {
  369. if (global_tls_context) {
  370. tor_tls_context_decref(global_tls_context);
  371. global_tls_context = NULL;
  372. }
  373. if (!HT_EMPTY(&tlsmap_root)) {
  374. log_warn(LD_MM, "Still have entries in the tlsmap at shutdown.");
  375. }
  376. HT_CLEAR(tlsmap, &tlsmap_root);
  377. #ifdef V2_HANDSHAKE_CLIENT
  378. if (CLIENT_CIPHER_DUMMIES)
  379. tor_free(CLIENT_CIPHER_DUMMIES);
  380. if (CLIENT_CIPHER_STACK)
  381. sk_SSL_CIPHER_free(CLIENT_CIPHER_STACK);
  382. #endif
  383. }
  384. /** We need to give OpenSSL a callback to verify certificates. This is
  385. * it: We always accept peer certs and complete the handshake. We
  386. * don't validate them until later.
  387. */
  388. static int
  389. always_accept_verify_cb(int preverify_ok,
  390. X509_STORE_CTX *x509_ctx)
  391. {
  392. (void) preverify_ok;
  393. (void) x509_ctx;
  394. return 1;
  395. }
  396. /** Return a newly allocated X509 name with commonName <b>cname</b>. */
  397. static X509_NAME *
  398. tor_x509_name_new(const char *cname)
  399. {
  400. int nid;
  401. X509_NAME *name;
  402. if (!(name = X509_NAME_new()))
  403. return NULL;
  404. if ((nid = OBJ_txt2nid("commonName")) == NID_undef) goto error;
  405. if (!(X509_NAME_add_entry_by_NID(name, nid, MBSTRING_ASC,
  406. (unsigned char*)cname, -1, -1, 0)))
  407. goto error;
  408. return name;
  409. error:
  410. X509_NAME_free(name);
  411. return NULL;
  412. }
  413. /** Generate and sign an X509 certificate with the public key <b>rsa</b>,
  414. * signed by the private key <b>rsa_sign</b>. The commonName of the
  415. * certificate will be <b>cname</b>; the commonName of the issuer will be
  416. * <b>cname_sign</b>. The cert will be valid for <b>cert_lifetime</b> seconds
  417. * starting from now. Return a certificate on success, NULL on
  418. * failure.
  419. */
  420. static X509 *
  421. tor_tls_create_certificate(crypto_pk_env_t *rsa,
  422. crypto_pk_env_t *rsa_sign,
  423. const char *cname,
  424. const char *cname_sign,
  425. unsigned int cert_lifetime)
  426. {
  427. time_t start_time, end_time;
  428. EVP_PKEY *sign_pkey = NULL, *pkey=NULL;
  429. X509 *x509 = NULL;
  430. X509_NAME *name = NULL, *name_issuer=NULL;
  431. tor_tls_init();
  432. start_time = time(NULL);
  433. tor_assert(rsa);
  434. tor_assert(cname);
  435. tor_assert(rsa_sign);
  436. tor_assert(cname_sign);
  437. if (!(sign_pkey = _crypto_pk_env_get_evp_pkey(rsa_sign,1)))
  438. goto error;
  439. if (!(pkey = _crypto_pk_env_get_evp_pkey(rsa,0)))
  440. goto error;
  441. if (!(x509 = X509_new()))
  442. goto error;
  443. if (!(X509_set_version(x509, 2)))
  444. goto error;
  445. if (!(ASN1_INTEGER_set(X509_get_serialNumber(x509), (long)start_time)))
  446. goto error;
  447. if (!(name = tor_x509_name_new(cname)))
  448. goto error;
  449. if (!(X509_set_subject_name(x509, name)))
  450. goto error;
  451. if (!(name_issuer = tor_x509_name_new(cname_sign)))
  452. goto error;
  453. if (!(X509_set_issuer_name(x509, name_issuer)))
  454. goto error;
  455. if (!X509_time_adj(X509_get_notBefore(x509),0,&start_time))
  456. goto error;
  457. end_time = start_time + cert_lifetime;
  458. if (!X509_time_adj(X509_get_notAfter(x509),0,&end_time))
  459. goto error;
  460. if (!X509_set_pubkey(x509, pkey))
  461. goto error;
  462. if (!X509_sign(x509, sign_pkey, EVP_sha1()))
  463. goto error;
  464. goto done;
  465. error:
  466. if (x509) {
  467. X509_free(x509);
  468. x509 = NULL;
  469. }
  470. done:
  471. tls_log_errors(NULL, LOG_WARN, "generating certificate");
  472. if (sign_pkey)
  473. EVP_PKEY_free(sign_pkey);
  474. if (pkey)
  475. EVP_PKEY_free(pkey);
  476. if (name)
  477. X509_NAME_free(name);
  478. if (name_issuer)
  479. X509_NAME_free(name_issuer);
  480. return x509;
  481. }
  482. /** List of ciphers that servers should select from.*/
  483. #define SERVER_CIPHER_LIST \
  484. (TLS1_TXT_DHE_RSA_WITH_AES_256_SHA ":" \
  485. TLS1_TXT_DHE_RSA_WITH_AES_128_SHA ":" \
  486. SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA)
  487. /* Note: for setting up your own private testing network with link crypto
  488. * disabled, set the cipher lists to your cipher list to
  489. * SSL3_TXT_RSA_NULL_SHA. If you do this, you won't be able to communicate
  490. * with any of the "real" Tors, though. */
  491. #ifdef V2_HANDSHAKE_CLIENT
  492. #define CIPHER(id, name) name ":"
  493. #define XCIPHER(id, name)
  494. /** List of ciphers that clients should advertise, omitting items that
  495. * our OpenSSL doesn't know about. */
  496. static const char CLIENT_CIPHER_LIST[] =
  497. #include "./ciphers.inc"
  498. ;
  499. #undef CIPHER
  500. #undef XCIPHER
  501. /** Holds a cipher that we want to advertise, and its 2-byte ID. */
  502. typedef struct cipher_info_t { unsigned id; const char *name; } cipher_info_t;
  503. /** A list of all the ciphers that clients should advertise, including items
  504. * that OpenSSL might not know about. */
  505. static const cipher_info_t CLIENT_CIPHER_INFO_LIST[] = {
  506. #define CIPHER(id, name) { id, name },
  507. #define XCIPHER(id, name) { id, #name },
  508. #include "./ciphers.inc"
  509. #undef CIPHER
  510. #undef XCIPHER
  511. };
  512. /** The length of CLIENT_CIPHER_INFO_LIST and CLIENT_CIPHER_DUMMIES. */
  513. static const int N_CLIENT_CIPHERS =
  514. sizeof(CLIENT_CIPHER_INFO_LIST)/sizeof(CLIENT_CIPHER_INFO_LIST[0]);
  515. #endif
  516. #ifndef V2_HANDSHAKE_CLIENT
  517. #undef CLIENT_CIPHER_LIST
  518. #define CLIENT_CIPHER_LIST (TLS1_TXT_DHE_RSA_WITH_AES_128_SHA ":" \
  519. SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA)
  520. #endif
  521. /** Remove a reference to <b>ctx</b>, and free it if it has no more
  522. * references. */
  523. static void
  524. tor_tls_context_decref(tor_tls_context_t *ctx)
  525. {
  526. tor_assert(ctx);
  527. if (--ctx->refcnt == 0) {
  528. SSL_CTX_free(ctx->ctx);
  529. X509_free(ctx->my_cert);
  530. X509_free(ctx->my_id_cert);
  531. crypto_free_pk_env(ctx->key);
  532. tor_free(ctx);
  533. }
  534. }
  535. /** Increase the reference count of <b>ctx</b>. */
  536. static void
  537. tor_tls_context_incref(tor_tls_context_t *ctx)
  538. {
  539. ++ctx->refcnt;
  540. }
  541. /** Create a new TLS context for use with Tor TLS handshakes.
  542. * <b>identity</b> should be set to the identity key used to sign the
  543. * certificate, and <b>nickname</b> set to the nickname to use.
  544. *
  545. * You can call this function multiple times. Each time you call it,
  546. * it generates new certificates; all new connections will use
  547. * the new SSL context.
  548. */
  549. int
  550. tor_tls_context_new(crypto_pk_env_t *identity, unsigned int key_lifetime)
  551. {
  552. crypto_pk_env_t *rsa = NULL;
  553. EVP_PKEY *pkey = NULL;
  554. tor_tls_context_t *result = NULL;
  555. X509 *cert = NULL, *idcert = NULL;
  556. char *nickname = NULL, *nn2 = NULL;
  557. tor_tls_init();
  558. nickname = crypto_random_hostname(8, 20, "www.", ".net");
  559. nn2 = crypto_random_hostname(8, 20, "www.", ".net");
  560. /* Generate short-term RSA key. */
  561. if (!(rsa = crypto_new_pk_env()))
  562. goto error;
  563. if (crypto_pk_generate_key(rsa)<0)
  564. goto error;
  565. /* Create certificate signed by identity key. */
  566. cert = tor_tls_create_certificate(rsa, identity, nickname, nn2,
  567. key_lifetime);
  568. /* Create self-signed certificate for identity key. */
  569. idcert = tor_tls_create_certificate(identity, identity, nn2, nn2,
  570. IDENTITY_CERT_LIFETIME);
  571. if (!cert || !idcert) {
  572. log(LOG_WARN, LD_CRYPTO, "Error creating certificate");
  573. goto error;
  574. }
  575. result = tor_malloc_zero(sizeof(tor_tls_context_t));
  576. result->refcnt = 1;
  577. result->my_cert = X509_dup(cert);
  578. result->my_id_cert = X509_dup(idcert);
  579. result->key = crypto_pk_dup_key(rsa);
  580. #ifdef EVERYONE_HAS_AES
  581. /* Tell OpenSSL to only use TLS1 */
  582. if (!(result->ctx = SSL_CTX_new(TLSv1_method())))
  583. goto error;
  584. #else
  585. /* Tell OpenSSL to use SSL3 or TLS1 but not SSL2. */
  586. if (!(result->ctx = SSL_CTX_new(SSLv23_method())))
  587. goto error;
  588. SSL_CTX_set_options(result->ctx, SSL_OP_NO_SSLv2);
  589. #endif
  590. SSL_CTX_set_options(result->ctx, SSL_OP_SINGLE_DH_USE);
  591. #ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  592. SSL_CTX_set_options(result->ctx,
  593. SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
  594. #endif
  595. /* Yes, we know what we are doing here. No, we do not treat a renegotiation
  596. * as authenticating any earlier-received data.
  597. */
  598. if (use_unsafe_renegotiation_op) {
  599. SSL_CTX_set_options(result->ctx,
  600. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
  601. }
  602. /* Don't actually allow compression; it uses ram and time, but the data
  603. * we transmit is all encrypted anyway. */
  604. if (result->ctx->comp_methods)
  605. result->ctx->comp_methods = NULL;
  606. #ifdef SSL_MODE_RELEASE_BUFFERS
  607. SSL_CTX_set_mode(result->ctx, SSL_MODE_RELEASE_BUFFERS);
  608. #endif
  609. if (cert && !SSL_CTX_use_certificate(result->ctx,cert))
  610. goto error;
  611. X509_free(cert); /* We just added a reference to cert. */
  612. cert=NULL;
  613. if (idcert) {
  614. X509_STORE *s = SSL_CTX_get_cert_store(result->ctx);
  615. tor_assert(s);
  616. X509_STORE_add_cert(s, idcert);
  617. X509_free(idcert); /* The context now owns the reference to idcert */
  618. idcert = NULL;
  619. }
  620. SSL_CTX_set_session_cache_mode(result->ctx, SSL_SESS_CACHE_OFF);
  621. tor_assert(rsa);
  622. if (!(pkey = _crypto_pk_env_get_evp_pkey(rsa,1)))
  623. goto error;
  624. if (!SSL_CTX_use_PrivateKey(result->ctx, pkey))
  625. goto error;
  626. EVP_PKEY_free(pkey);
  627. pkey = NULL;
  628. if (!SSL_CTX_check_private_key(result->ctx))
  629. goto error;
  630. {
  631. crypto_dh_env_t *dh = crypto_dh_new();
  632. SSL_CTX_set_tmp_dh(result->ctx, _crypto_dh_env_get_dh(dh));
  633. crypto_dh_free(dh);
  634. }
  635. SSL_CTX_set_verify(result->ctx, SSL_VERIFY_PEER,
  636. always_accept_verify_cb);
  637. /* let us realloc bufs that we're writing from */
  638. SSL_CTX_set_mode(result->ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
  639. /* Free the old context if one exists. */
  640. if (global_tls_context) {
  641. /* This is safe even if there are open connections: OpenSSL does
  642. * reference counting with SSL and SSL_CTX objects. */
  643. tor_tls_context_decref(global_tls_context);
  644. }
  645. global_tls_context = result;
  646. if (rsa)
  647. crypto_free_pk_env(rsa);
  648. tor_free(nickname);
  649. tor_free(nn2);
  650. return 0;
  651. error:
  652. tls_log_errors(NULL, LOG_WARN, "creating TLS context");
  653. tor_free(nickname);
  654. tor_free(nn2);
  655. if (pkey)
  656. EVP_PKEY_free(pkey);
  657. if (rsa)
  658. crypto_free_pk_env(rsa);
  659. if (result)
  660. tor_tls_context_decref(result);
  661. if (cert)
  662. X509_free(cert);
  663. if (idcert)
  664. X509_free(idcert);
  665. return -1;
  666. }
  667. #ifdef V2_HANDSHAKE_SERVER
  668. /** Return true iff the cipher list suggested by the client for <b>ssl</b> is
  669. * a list that indicates that the client knows how to do the v2 TLS connection
  670. * handshake. */
  671. static int
  672. tor_tls_client_is_using_v2_ciphers(const SSL *ssl, const char *address)
  673. {
  674. int i;
  675. SSL_SESSION *session;
  676. /* If we reached this point, we just got a client hello. See if there is
  677. * a cipher list. */
  678. if (!(session = SSL_get_session((SSL *)ssl))) {
  679. log_warn(LD_NET, "No session on TLS?");
  680. return 0;
  681. }
  682. if (!session->ciphers) {
  683. log_warn(LD_NET, "No ciphers on session");
  684. return 0;
  685. }
  686. /* Now we need to see if there are any ciphers whose presence means we're
  687. * dealing with an updated Tor. */
  688. for (i = 0; i < sk_SSL_CIPHER_num(session->ciphers); ++i) {
  689. SSL_CIPHER *cipher = sk_SSL_CIPHER_value(session->ciphers, i);
  690. const char *ciphername = SSL_CIPHER_get_name(cipher);
  691. if (strcmp(ciphername, TLS1_TXT_DHE_RSA_WITH_AES_128_SHA) &&
  692. strcmp(ciphername, TLS1_TXT_DHE_RSA_WITH_AES_256_SHA) &&
  693. strcmp(ciphername, SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA) &&
  694. strcmp(ciphername, "(NONE)")) {
  695. /* XXXX should be ld_debug */
  696. log_info(LD_NET, "Got a non-version-1 cipher called '%s'", ciphername);
  697. // return 1;
  698. goto dump_list;
  699. }
  700. }
  701. return 0;
  702. dump_list:
  703. {
  704. smartlist_t *elts = smartlist_create();
  705. char *s;
  706. for (i = 0; i < sk_SSL_CIPHER_num(session->ciphers); ++i) {
  707. SSL_CIPHER *cipher = sk_SSL_CIPHER_value(session->ciphers, i);
  708. const char *ciphername = SSL_CIPHER_get_name(cipher);
  709. smartlist_add(elts, (char*)ciphername);
  710. }
  711. s = smartlist_join_strings(elts, ":", 0, NULL);
  712. log_info(LD_NET, "Got a non-version-1 cipher list from %s. It is: '%s'",
  713. address, s);
  714. tor_free(s);
  715. smartlist_free(elts);
  716. }
  717. return 1;
  718. }
  719. /** Invoked when we're accepting a connection on <b>ssl</b>, and the connection
  720. * changes state. We use this:
  721. * <ul><li>To alter the state of the handshake partway through, so we
  722. * do not send or request extra certificates in v2 handshakes.</li>
  723. * <li>To detect renegotiation</li></ul>
  724. */
  725. static void
  726. tor_tls_server_info_callback(const SSL *ssl, int type, int val)
  727. {
  728. tor_tls_t *tls;
  729. (void) val;
  730. if (type != SSL_CB_ACCEPT_LOOP)
  731. return;
  732. if (ssl->state != SSL3_ST_SW_SRVR_HELLO_A)
  733. return;
  734. tls = tor_tls_get_by_ssl(ssl);
  735. if (tls) {
  736. /* Check whether we're watching for renegotiates. If so, this is one! */
  737. if (tls->negotiated_callback)
  738. tls->got_renegotiate = 1;
  739. } else {
  740. log_warn(LD_BUG, "Couldn't look up the tls for an SSL*. How odd!");
  741. }
  742. /* Now check the cipher list. */
  743. if (tor_tls_client_is_using_v2_ciphers(ssl, ADDR(tls))) {
  744. /*XXXX_TLS keep this from happening more than once! */
  745. /* Yes, we're casting away the const from ssl. This is very naughty of us.
  746. * Let's hope openssl doesn't notice! */
  747. /* Set SSL_MODE_NO_AUTO_CHAIN to keep from sending back any extra certs. */
  748. SSL_set_mode((SSL*) ssl, SSL_MODE_NO_AUTO_CHAIN);
  749. /* Don't send a hello request. */
  750. SSL_set_verify((SSL*) ssl, SSL_VERIFY_NONE, NULL);
  751. if (tls) {
  752. tls->wasV2Handshake = 1;
  753. } else {
  754. log_warn(LD_BUG, "Couldn't look up the tls for an SSL*. How odd!");
  755. }
  756. }
  757. }
  758. #endif
  759. /** Replace *<b>ciphers</b> with a new list of SSL ciphersuites: specifically,
  760. * a list designed to mimic a common web browser. Some of the ciphers in the
  761. * list won't actually be implemented by OpenSSL: that's okay so long as the
  762. * server doesn't select them, and the server won't select anything besides
  763. * what's in SERVER_CIPHER_LIST.
  764. *
  765. * [If the server <b>does</b> select a bogus cipher, we won't crash or
  766. * anything; we'll just fail later when we try to look up the cipher in
  767. * ssl->cipher_list_by_id.]
  768. */
  769. static void
  770. rectify_client_ciphers(STACK_OF(SSL_CIPHER) **ciphers)
  771. {
  772. #ifdef V2_HANDSHAKE_CLIENT
  773. if (PREDICT_UNLIKELY(!CLIENT_CIPHER_STACK)) {
  774. /* We need to set CLIENT_CIPHER_STACK to an array of the ciphers
  775. * we want.*/
  776. int i = 0, j = 0;
  777. /* First, create a dummy SSL_CIPHER for every cipher. */
  778. CLIENT_CIPHER_DUMMIES =
  779. tor_malloc_zero(sizeof(SSL_CIPHER)*N_CLIENT_CIPHERS);
  780. for (i=0; i < N_CLIENT_CIPHERS; ++i) {
  781. CLIENT_CIPHER_DUMMIES[i].valid = 1;
  782. CLIENT_CIPHER_DUMMIES[i].id = CLIENT_CIPHER_INFO_LIST[i].id | (3<<24);
  783. CLIENT_CIPHER_DUMMIES[i].name = CLIENT_CIPHER_INFO_LIST[i].name;
  784. }
  785. CLIENT_CIPHER_STACK = sk_SSL_CIPHER_new_null();
  786. tor_assert(CLIENT_CIPHER_STACK);
  787. log_debug(LD_NET, "List was: %s", CLIENT_CIPHER_LIST);
  788. for (j = 0; j < sk_SSL_CIPHER_num(*ciphers); ++j) {
  789. SSL_CIPHER *cipher = sk_SSL_CIPHER_value(*ciphers, j);
  790. log_debug(LD_NET, "Cipher %d: %lx %s", j, cipher->id, cipher->name);
  791. }
  792. /* Then copy as many ciphers as we can from the good list, inserting
  793. * dummies as needed. */
  794. j=0;
  795. for (i = 0; i < N_CLIENT_CIPHERS; ) {
  796. SSL_CIPHER *cipher = NULL;
  797. if (j < sk_SSL_CIPHER_num(*ciphers))
  798. cipher = sk_SSL_CIPHER_value(*ciphers, j);
  799. if (cipher && ((cipher->id >> 24) & 0xff) != 3) {
  800. log_debug(LD_NET, "Skipping v2 cipher %s", cipher->name);
  801. ++j;
  802. } else if (cipher &&
  803. (cipher->id & 0xffff) == CLIENT_CIPHER_INFO_LIST[i].id) {
  804. log_debug(LD_NET, "Found cipher %s", cipher->name);
  805. sk_SSL_CIPHER_push(CLIENT_CIPHER_STACK, cipher);
  806. ++j;
  807. ++i;
  808. } else {
  809. log_debug(LD_NET, "Inserting fake %s", CLIENT_CIPHER_DUMMIES[i].name);
  810. sk_SSL_CIPHER_push(CLIENT_CIPHER_STACK, &CLIENT_CIPHER_DUMMIES[i]);
  811. ++i;
  812. }
  813. }
  814. }
  815. sk_SSL_CIPHER_free(*ciphers);
  816. *ciphers = sk_SSL_CIPHER_dup(CLIENT_CIPHER_STACK);
  817. tor_assert(*ciphers);
  818. #else
  819. (void)ciphers;
  820. #endif
  821. }
  822. /** Create a new TLS object from a file descriptor, and a flag to
  823. * determine whether it is functioning as a server.
  824. */
  825. tor_tls_t *
  826. tor_tls_new(int sock, int isServer)
  827. {
  828. BIO *bio = NULL;
  829. tor_tls_t *result = tor_malloc_zero(sizeof(tor_tls_t));
  830. tor_assert(global_tls_context); /* make sure somebody made it first */
  831. if (!(result->ssl = SSL_new(global_tls_context->ctx))) {
  832. tls_log_errors(NULL, LOG_WARN, "generating TLS context");
  833. tor_free(result);
  834. return NULL;
  835. }
  836. #ifdef SSL_set_tlsext_host_name
  837. /* Browsers use the TLS hostname extension, so we should too. */
  838. if (!isServer) {
  839. char *fake_hostname = crypto_random_hostname(4,25, "www.",".com");
  840. SSL_set_tlsext_host_name(result->ssl, fake_hostname);
  841. tor_free(fake_hostname);
  842. }
  843. #endif
  844. if (!SSL_set_cipher_list(result->ssl,
  845. isServer ? SERVER_CIPHER_LIST : CLIENT_CIPHER_LIST)) {
  846. tls_log_errors(NULL, LOG_WARN, "setting ciphers");
  847. #ifdef SSL_set_tlsext_host_name
  848. SSL_set_tlsext_host_name(result->ssl, NULL);
  849. #endif
  850. SSL_free(result->ssl);
  851. tor_free(result);
  852. return NULL;
  853. }
  854. if (!isServer)
  855. rectify_client_ciphers(&result->ssl->cipher_list);
  856. result->socket = sock;
  857. bio = BIO_new_socket(sock, BIO_NOCLOSE);
  858. if (! bio) {
  859. tls_log_errors(NULL, LOG_WARN, "opening BIO");
  860. #ifdef SSL_set_tlsext_host_name
  861. SSL_set_tlsext_host_name(result->ssl, NULL);
  862. #endif
  863. SSL_free(result->ssl);
  864. tor_free(result);
  865. return NULL;
  866. }
  867. HT_INSERT(tlsmap, &tlsmap_root, result);
  868. SSL_set_bio(result->ssl, bio, bio);
  869. tor_tls_context_incref(global_tls_context);
  870. result->context = global_tls_context;
  871. result->state = TOR_TLS_ST_HANDSHAKE;
  872. result->isServer = isServer;
  873. result->wantwrite_n = 0;
  874. result->last_write_count = BIO_number_written(bio);
  875. result->last_read_count = BIO_number_read(bio);
  876. if (result->last_write_count || result->last_read_count) {
  877. log_warn(LD_NET, "Newly created BIO has read count %lu, write count %lu",
  878. result->last_read_count, result->last_write_count);
  879. }
  880. #ifdef V2_HANDSHAKE_SERVER
  881. if (isServer) {
  882. SSL_set_info_callback(result->ssl, tor_tls_server_info_callback);
  883. }
  884. #endif
  885. /* Not expected to get called. */
  886. tls_log_errors(NULL, LOG_WARN, "generating TLS context");
  887. return result;
  888. }
  889. /** Make future log messages about <b>tls</b> display the address
  890. * <b>address</b>.
  891. */
  892. void
  893. tor_tls_set_logged_address(tor_tls_t *tls, const char *address)
  894. {
  895. tor_assert(tls);
  896. tor_free(tls->address);
  897. tls->address = tor_strdup(address);
  898. }
  899. /** Set <b>cb</b> to be called with argument <b>arg</b> whenever <b>tls</b>
  900. * next gets a client-side renegotiate in the middle of a read. Do not
  901. * invoke this function until <em>after</em> initial handshaking is done!
  902. */
  903. void
  904. tor_tls_set_renegotiate_callback(tor_tls_t *tls,
  905. void (*cb)(tor_tls_t *, void *arg),
  906. void *arg)
  907. {
  908. tls->negotiated_callback = cb;
  909. tls->callback_arg = arg;
  910. tls->got_renegotiate = 0;
  911. #ifdef V2_HANDSHAKE_SERVER
  912. if (cb) {
  913. SSL_set_info_callback(tls->ssl, tor_tls_server_info_callback);
  914. } else {
  915. SSL_set_info_callback(tls->ssl, NULL);
  916. }
  917. #endif
  918. }
  919. /** If this version of openssl requires it, turn on renegotiation on
  920. * <b>tls</b>.
  921. */
  922. static void
  923. tor_tls_unblock_renegotiation(tor_tls_t *tls)
  924. {
  925. /* Yes, we know what we are doing here. No, we do not treat a renegotiation
  926. * as authenticating any earlier-received data. */
  927. if (use_unsafe_renegotiation_flag) {
  928. tls->ssl->s3->flags |= SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
  929. }
  930. if (use_unsafe_renegotiation_op) {
  931. SSL_set_options(tls->ssl,
  932. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
  933. }
  934. }
  935. /** If this version of openssl supports it, turn off renegotiation on
  936. * <b>tls</b>. (Our protocol never requires this for security, but it's nice
  937. * to use belt-and-suspenders here.)
  938. */
  939. void
  940. tor_tls_block_renegotiation(tor_tls_t *tls)
  941. {
  942. tls->ssl->s3->flags &= ~SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
  943. }
  944. /** Return whether this tls initiated the connect (client) or
  945. * received it (server). */
  946. int
  947. tor_tls_is_server(tor_tls_t *tls)
  948. {
  949. tor_assert(tls);
  950. return tls->isServer;
  951. }
  952. /** Release resources associated with a TLS object. Does not close the
  953. * underlying file descriptor.
  954. */
  955. void
  956. tor_tls_free(tor_tls_t *tls)
  957. {
  958. tor_tls_t *removed;
  959. tor_assert(tls && tls->ssl);
  960. removed = HT_REMOVE(tlsmap, &tlsmap_root, tls);
  961. if (!removed) {
  962. log_warn(LD_BUG, "Freeing a TLS that was not in the ssl->tls map.");
  963. }
  964. #ifdef SSL_set_tlsext_host_name
  965. SSL_set_tlsext_host_name(tls->ssl, NULL);
  966. #endif
  967. SSL_free(tls->ssl);
  968. tls->ssl = NULL;
  969. tls->negotiated_callback = NULL;
  970. if (tls->context)
  971. tor_tls_context_decref(tls->context);
  972. tor_free(tls->address);
  973. tor_free(tls);
  974. }
  975. /** Underlying function for TLS reading. Reads up to <b>len</b>
  976. * characters from <b>tls</b> into <b>cp</b>. On success, returns the
  977. * number of characters read. On failure, returns TOR_TLS_ERROR,
  978. * TOR_TLS_CLOSE, TOR_TLS_WANTREAD, or TOR_TLS_WANTWRITE.
  979. */
  980. int
  981. tor_tls_read(tor_tls_t *tls, char *cp, size_t len)
  982. {
  983. int r, err;
  984. tor_assert(tls);
  985. tor_assert(tls->ssl);
  986. tor_assert(tls->state == TOR_TLS_ST_OPEN);
  987. tor_assert(len<INT_MAX);
  988. r = SSL_read(tls->ssl, cp, (int)len);
  989. if (r > 0) {
  990. #ifdef V2_HANDSHAKE_SERVER
  991. if (tls->got_renegotiate) {
  992. /* Renegotiation happened! */
  993. log_info(LD_NET, "Got a TLS renegotiation from %s", ADDR(tls));
  994. if (tls->negotiated_callback)
  995. tls->negotiated_callback(tls, tls->callback_arg);
  996. tls->got_renegotiate = 0;
  997. }
  998. #endif
  999. return r;
  1000. }
  1001. err = tor_tls_get_error(tls, r, CATCH_ZERO, "reading", LOG_DEBUG);
  1002. if (err == _TOR_TLS_ZERORETURN || err == TOR_TLS_CLOSE) {
  1003. log_debug(LD_NET,"read returned r=%d; TLS is closed",r);
  1004. tls->state = TOR_TLS_ST_CLOSED;
  1005. return TOR_TLS_CLOSE;
  1006. } else {
  1007. tor_assert(err != TOR_TLS_DONE);
  1008. log_debug(LD_NET,"read returned r=%d, err=%d",r,err);
  1009. return err;
  1010. }
  1011. }
  1012. /** Underlying function for TLS writing. Write up to <b>n</b>
  1013. * characters from <b>cp</b> onto <b>tls</b>. On success, returns the
  1014. * number of characters written. On failure, returns TOR_TLS_ERROR,
  1015. * TOR_TLS_WANTREAD, or TOR_TLS_WANTWRITE.
  1016. */
  1017. int
  1018. tor_tls_write(tor_tls_t *tls, const char *cp, size_t n)
  1019. {
  1020. int r, err;
  1021. tor_assert(tls);
  1022. tor_assert(tls->ssl);
  1023. tor_assert(tls->state == TOR_TLS_ST_OPEN);
  1024. tor_assert(n < INT_MAX);
  1025. if (n == 0)
  1026. return 0;
  1027. if (tls->wantwrite_n) {
  1028. /* if WANTWRITE last time, we must use the _same_ n as before */
  1029. tor_assert(n >= tls->wantwrite_n);
  1030. log_debug(LD_NET,"resuming pending-write, (%d to flush, reusing %d)",
  1031. (int)n, (int)tls->wantwrite_n);
  1032. n = tls->wantwrite_n;
  1033. tls->wantwrite_n = 0;
  1034. }
  1035. r = SSL_write(tls->ssl, cp, (int)n);
  1036. err = tor_tls_get_error(tls, r, 0, "writing", LOG_INFO);
  1037. if (err == TOR_TLS_DONE) {
  1038. return r;
  1039. }
  1040. if (err == TOR_TLS_WANTWRITE || err == TOR_TLS_WANTREAD) {
  1041. tls->wantwrite_n = n;
  1042. }
  1043. return err;
  1044. }
  1045. /** Perform initial handshake on <b>tls</b>. When finished, returns
  1046. * TOR_TLS_DONE. On failure, returns TOR_TLS_ERROR, TOR_TLS_WANTREAD,
  1047. * or TOR_TLS_WANTWRITE.
  1048. */
  1049. int
  1050. tor_tls_handshake(tor_tls_t *tls)
  1051. {
  1052. int r;
  1053. tor_assert(tls);
  1054. tor_assert(tls->ssl);
  1055. tor_assert(tls->state == TOR_TLS_ST_HANDSHAKE);
  1056. check_no_tls_errors();
  1057. if (tls->isServer) {
  1058. r = SSL_accept(tls->ssl);
  1059. } else {
  1060. r = SSL_connect(tls->ssl);
  1061. }
  1062. /* We need to call this here and not earlier, since OpenSSL has a penchant
  1063. * for clearing its flags when you say accept or connect. */
  1064. tor_tls_unblock_renegotiation(tls);
  1065. r = tor_tls_get_error(tls,r,0, "handshaking", LOG_INFO);
  1066. if (ERR_peek_error() != 0) {
  1067. tls_log_errors(tls, tls->isServer ? LOG_INFO : LOG_WARN,
  1068. "handshaking");
  1069. return TOR_TLS_ERROR_MISC;
  1070. }
  1071. if (r == TOR_TLS_DONE) {
  1072. tls->state = TOR_TLS_ST_OPEN;
  1073. if (tls->isServer) {
  1074. SSL_set_info_callback(tls->ssl, NULL);
  1075. SSL_set_verify(tls->ssl, SSL_VERIFY_PEER, always_accept_verify_cb);
  1076. /* There doesn't seem to be a clear OpenSSL API to clear mode flags. */
  1077. tls->ssl->mode &= ~SSL_MODE_NO_AUTO_CHAIN;
  1078. #ifdef V2_HANDSHAKE_SERVER
  1079. if (tor_tls_client_is_using_v2_ciphers(tls->ssl, ADDR(tls))) {
  1080. /* This check is redundant, but back when we did it in the callback,
  1081. * we might have not been able to look up the tor_tls_t if the code
  1082. * was buggy. Fixing that. */
  1083. if (!tls->wasV2Handshake) {
  1084. log_warn(LD_BUG, "For some reason, wasV2Handshake didn't"
  1085. " get set. Fixing that.");
  1086. }
  1087. tls->wasV2Handshake = 1;
  1088. log_debug(LD_NET, "Completed V2 TLS handshake with client; waiting "
  1089. "for renegotiation.");
  1090. } else {
  1091. tls->wasV2Handshake = 0;
  1092. }
  1093. #endif
  1094. } else {
  1095. #ifdef V2_HANDSHAKE_CLIENT
  1096. /* If we got no ID cert, we're a v2 handshake. */
  1097. X509 *cert = SSL_get_peer_certificate(tls->ssl);
  1098. STACK_OF(X509) *chain = SSL_get_peer_cert_chain(tls->ssl);
  1099. int n_certs = sk_X509_num(chain);
  1100. if (n_certs > 1 || (n_certs == 1 && cert != sk_X509_value(chain, 0)))
  1101. tls->wasV2Handshake = 0;
  1102. else {
  1103. log_debug(LD_NET, "Server sent back a single certificate; looks like "
  1104. "a v2 handshake on %p.", tls);
  1105. tls->wasV2Handshake = 1;
  1106. }
  1107. if (cert)
  1108. X509_free(cert);
  1109. #endif
  1110. if (SSL_set_cipher_list(tls->ssl, SERVER_CIPHER_LIST) == 0) {
  1111. tls_log_errors(NULL, LOG_WARN, "re-setting ciphers");
  1112. r = TOR_TLS_ERROR_MISC;
  1113. }
  1114. }
  1115. }
  1116. return r;
  1117. }
  1118. /** Client only: Renegotiate a TLS session. When finished, returns
  1119. * TOR_TLS_DONE. On failure, returns TOR_TLS_ERROR, TOR_TLS_WANTREAD, or
  1120. * TOR_TLS_WANTWRITE.
  1121. */
  1122. int
  1123. tor_tls_renegotiate(tor_tls_t *tls)
  1124. {
  1125. int r;
  1126. tor_assert(tls);
  1127. /* We could do server-initiated renegotiation too, but that would be tricky.
  1128. * Instead of "SSL_renegotiate, then SSL_do_handshake until done" */
  1129. tor_assert(!tls->isServer);
  1130. if (tls->state != TOR_TLS_ST_RENEGOTIATE) {
  1131. int r = SSL_renegotiate(tls->ssl);
  1132. if (r <= 0) {
  1133. return tor_tls_get_error(tls, r, 0, "renegotiating", LOG_WARN);
  1134. }
  1135. tls->state = TOR_TLS_ST_RENEGOTIATE;
  1136. }
  1137. r = SSL_do_handshake(tls->ssl);
  1138. if (r == 1) {
  1139. tls->state = TOR_TLS_ST_OPEN;
  1140. return TOR_TLS_DONE;
  1141. } else
  1142. return tor_tls_get_error(tls, r, 0, "renegotiating handshake", LOG_INFO);
  1143. }
  1144. /** Shut down an open tls connection <b>tls</b>. When finished, returns
  1145. * TOR_TLS_DONE. On failure, returns TOR_TLS_ERROR, TOR_TLS_WANTREAD,
  1146. * or TOR_TLS_WANTWRITE.
  1147. */
  1148. int
  1149. tor_tls_shutdown(tor_tls_t *tls)
  1150. {
  1151. int r, err;
  1152. char buf[128];
  1153. tor_assert(tls);
  1154. tor_assert(tls->ssl);
  1155. while (1) {
  1156. if (tls->state == TOR_TLS_ST_SENTCLOSE) {
  1157. /* If we've already called shutdown once to send a close message,
  1158. * we read until the other side has closed too.
  1159. */
  1160. do {
  1161. r = SSL_read(tls->ssl, buf, 128);
  1162. } while (r>0);
  1163. err = tor_tls_get_error(tls, r, CATCH_ZERO, "reading to shut down",
  1164. LOG_INFO);
  1165. if (err == _TOR_TLS_ZERORETURN) {
  1166. tls->state = TOR_TLS_ST_GOTCLOSE;
  1167. /* fall through... */
  1168. } else {
  1169. return err;
  1170. }
  1171. }
  1172. r = SSL_shutdown(tls->ssl);
  1173. if (r == 1) {
  1174. /* If shutdown returns 1, the connection is entirely closed. */
  1175. tls->state = TOR_TLS_ST_CLOSED;
  1176. return TOR_TLS_DONE;
  1177. }
  1178. err = tor_tls_get_error(tls, r, CATCH_SYSCALL|CATCH_ZERO, "shutting down",
  1179. LOG_INFO);
  1180. if (err == _TOR_TLS_SYSCALL) {
  1181. /* The underlying TCP connection closed while we were shutting down. */
  1182. tls->state = TOR_TLS_ST_CLOSED;
  1183. return TOR_TLS_DONE;
  1184. } else if (err == _TOR_TLS_ZERORETURN) {
  1185. /* The TLS connection says that it sent a shutdown record, but
  1186. * isn't done shutting down yet. Make sure that this hasn't
  1187. * happened before, then go back to the start of the function
  1188. * and try to read.
  1189. */
  1190. if (tls->state == TOR_TLS_ST_GOTCLOSE ||
  1191. tls->state == TOR_TLS_ST_SENTCLOSE) {
  1192. log(LOG_WARN, LD_NET,
  1193. "TLS returned \"half-closed\" value while already half-closed");
  1194. return TOR_TLS_ERROR_MISC;
  1195. }
  1196. tls->state = TOR_TLS_ST_SENTCLOSE;
  1197. /* fall through ... */
  1198. } else {
  1199. return err;
  1200. }
  1201. } /* end loop */
  1202. }
  1203. /** Return true iff this TLS connection is authenticated.
  1204. */
  1205. int
  1206. tor_tls_peer_has_cert(tor_tls_t *tls)
  1207. {
  1208. X509 *cert;
  1209. cert = SSL_get_peer_certificate(tls->ssl);
  1210. tls_log_errors(tls, LOG_WARN, "getting peer certificate");
  1211. if (!cert)
  1212. return 0;
  1213. X509_free(cert);
  1214. return 1;
  1215. }
  1216. /** Warn that a certificate lifetime extends through a certain range. */
  1217. static void
  1218. log_cert_lifetime(X509 *cert, const char *problem)
  1219. {
  1220. BIO *bio = NULL;
  1221. BUF_MEM *buf;
  1222. char *s1=NULL, *s2=NULL;
  1223. char mytime[33];
  1224. time_t now = time(NULL);
  1225. struct tm tm;
  1226. if (problem)
  1227. log_warn(LD_GENERAL,
  1228. "Certificate %s: is your system clock set incorrectly?",
  1229. problem);
  1230. if (!(bio = BIO_new(BIO_s_mem()))) {
  1231. log_warn(LD_GENERAL, "Couldn't allocate BIO!"); goto end;
  1232. }
  1233. if (!(ASN1_TIME_print(bio, X509_get_notBefore(cert)))) {
  1234. tls_log_errors(NULL, LOG_WARN, "printing certificate lifetime");
  1235. goto end;
  1236. }
  1237. BIO_get_mem_ptr(bio, &buf);
  1238. s1 = tor_strndup(buf->data, buf->length);
  1239. (void)BIO_reset(bio);
  1240. if (!(ASN1_TIME_print(bio, X509_get_notAfter(cert)))) {
  1241. tls_log_errors(NULL, LOG_WARN, "printing certificate lifetime");
  1242. goto end;
  1243. }
  1244. BIO_get_mem_ptr(bio, &buf);
  1245. s2 = tor_strndup(buf->data, buf->length);
  1246. strftime(mytime, 32, "%b %d %H:%M:%S %Y GMT", tor_gmtime_r(&now, &tm));
  1247. log_warn(LD_GENERAL,
  1248. "(certificate lifetime runs from %s through %s. Your time is %s.)",
  1249. s1,s2,mytime);
  1250. end:
  1251. /* Not expected to get invoked */
  1252. tls_log_errors(NULL, LOG_WARN, "getting certificate lifetime");
  1253. if (bio)
  1254. BIO_free(bio);
  1255. if (s1)
  1256. tor_free(s1);
  1257. if (s2)
  1258. tor_free(s2);
  1259. }
  1260. /** Helper function: try to extract a link certificate and an identity
  1261. * certificate from <b>tls</b>, and store them in *<b>cert_out</b> and
  1262. * *<b>id_cert_out</b> respectively. Log all messages at level
  1263. * <b>severity</b>.
  1264. *
  1265. * Note that a reference is added to cert_out, so it needs to be
  1266. * freed. id_cert_out doesn't. */
  1267. static void
  1268. try_to_extract_certs_from_tls(int severity, tor_tls_t *tls,
  1269. X509 **cert_out, X509 **id_cert_out)
  1270. {
  1271. X509 *cert = NULL, *id_cert = NULL;
  1272. STACK_OF(X509) *chain = NULL;
  1273. int num_in_chain, i;
  1274. *cert_out = *id_cert_out = NULL;
  1275. if (!(cert = SSL_get_peer_certificate(tls->ssl)))
  1276. return;
  1277. *cert_out = cert;
  1278. if (!(chain = SSL_get_peer_cert_chain(tls->ssl)))
  1279. return;
  1280. num_in_chain = sk_X509_num(chain);
  1281. /* 1 means we're receiving (server-side), and it's just the id_cert.
  1282. * 2 means we're connecting (client-side), and it's both the link
  1283. * cert and the id_cert.
  1284. */
  1285. if (num_in_chain < 1) {
  1286. log_fn(severity,LD_PROTOCOL,
  1287. "Unexpected number of certificates in chain (%d)",
  1288. num_in_chain);
  1289. return;
  1290. }
  1291. for (i=0; i<num_in_chain; ++i) {
  1292. id_cert = sk_X509_value(chain, i);
  1293. if (X509_cmp(id_cert, cert) != 0)
  1294. break;
  1295. }
  1296. *id_cert_out = id_cert;
  1297. }
  1298. /** If the provided tls connection is authenticated and has a
  1299. * certificate chain that is currently valid and signed, then set
  1300. * *<b>identity_key</b> to the identity certificate's key and return
  1301. * 0. Else, return -1 and log complaints with log-level <b>severity</b>.
  1302. */
  1303. int
  1304. tor_tls_verify(int severity, tor_tls_t *tls, crypto_pk_env_t **identity_key)
  1305. {
  1306. X509 *cert = NULL, *id_cert = NULL;
  1307. EVP_PKEY *id_pkey = NULL;
  1308. RSA *rsa;
  1309. int r = -1;
  1310. *identity_key = NULL;
  1311. try_to_extract_certs_from_tls(severity, tls, &cert, &id_cert);
  1312. if (!cert)
  1313. goto done;
  1314. if (!id_cert) {
  1315. log_fn(severity,LD_PROTOCOL,"No distinct identity certificate found");
  1316. goto done;
  1317. }
  1318. if (!(id_pkey = X509_get_pubkey(id_cert)) ||
  1319. X509_verify(cert, id_pkey) <= 0) {
  1320. log_fn(severity,LD_PROTOCOL,"X509_verify on cert and pkey returned <= 0");
  1321. tls_log_errors(tls, severity,"verifying certificate");
  1322. goto done;
  1323. }
  1324. rsa = EVP_PKEY_get1_RSA(id_pkey);
  1325. if (!rsa)
  1326. goto done;
  1327. *identity_key = _crypto_new_pk_env_rsa(rsa);
  1328. r = 0;
  1329. done:
  1330. if (cert)
  1331. X509_free(cert);
  1332. if (id_pkey)
  1333. EVP_PKEY_free(id_pkey);
  1334. /* This should never get invoked, but let's make sure in case OpenSSL
  1335. * acts unexpectedly. */
  1336. tls_log_errors(tls, LOG_WARN, "finishing tor_tls_verify");
  1337. return r;
  1338. }
  1339. /** Check whether the certificate set on the connection <b>tls</b> is
  1340. * expired or not-yet-valid, give or take <b>tolerance</b>
  1341. * seconds. Return 0 for valid, -1 for failure.
  1342. *
  1343. * NOTE: you should call tor_tls_verify before tor_tls_check_lifetime.
  1344. */
  1345. int
  1346. tor_tls_check_lifetime(tor_tls_t *tls, int tolerance)
  1347. {
  1348. time_t now, t;
  1349. X509 *cert;
  1350. int r = -1;
  1351. now = time(NULL);
  1352. if (!(cert = SSL_get_peer_certificate(tls->ssl)))
  1353. goto done;
  1354. t = now + tolerance;
  1355. if (X509_cmp_time(X509_get_notBefore(cert), &t) > 0) {
  1356. log_cert_lifetime(cert, "not yet valid");
  1357. goto done;
  1358. }
  1359. t = now - tolerance;
  1360. if (X509_cmp_time(X509_get_notAfter(cert), &t) < 0) {
  1361. log_cert_lifetime(cert, "already expired");
  1362. goto done;
  1363. }
  1364. r = 0;
  1365. done:
  1366. if (cert)
  1367. X509_free(cert);
  1368. /* Not expected to get invoked */
  1369. tls_log_errors(tls, LOG_WARN, "checking certificate lifetime");
  1370. return r;
  1371. }
  1372. /** Return the number of bytes available for reading from <b>tls</b>.
  1373. */
  1374. int
  1375. tor_tls_get_pending_bytes(tor_tls_t *tls)
  1376. {
  1377. tor_assert(tls);
  1378. return SSL_pending(tls->ssl);
  1379. }
  1380. /** If <b>tls</b> requires that the next write be of a particular size,
  1381. * return that size. Otherwise, return 0. */
  1382. size_t
  1383. tor_tls_get_forced_write_size(tor_tls_t *tls)
  1384. {
  1385. return tls->wantwrite_n;
  1386. }
  1387. /** Sets n_read and n_written to the number of bytes read and written,
  1388. * respectively, on the raw socket used by <b>tls</b> since the last time this
  1389. * function was called on <b>tls</b>. */
  1390. void
  1391. tor_tls_get_n_raw_bytes(tor_tls_t *tls, size_t *n_read, size_t *n_written)
  1392. {
  1393. BIO *wbio, *tmpbio;
  1394. unsigned long r, w;
  1395. r = BIO_number_read(SSL_get_rbio(tls->ssl));
  1396. /* We want the number of bytes actually for real written. Unfortunately,
  1397. * sometimes OpenSSL replaces the wbio on tls->ssl with a buffering bio,
  1398. * which makes the answer turn out wrong. Let's cope with that. Note
  1399. * that this approach will fail if we ever replace tls->ssl's BIOs with
  1400. * buffering bios for reasons of our own. As an alternative, we could
  1401. * save the original BIO for tls->ssl in the tor_tls_t structure, but
  1402. * that would be tempting fate. */
  1403. wbio = SSL_get_wbio(tls->ssl);
  1404. if (wbio->method == BIO_f_buffer() && (tmpbio = BIO_next(wbio)) != NULL)
  1405. wbio = tmpbio;
  1406. w = BIO_number_written(wbio);
  1407. /* We are ok with letting these unsigned ints go "negative" here:
  1408. * If we wrapped around, this should still give us the right answer, unless
  1409. * we wrapped around by more than ULONG_MAX since the last time we called
  1410. * this function.
  1411. */
  1412. *n_read = (size_t)(r - tls->last_read_count);
  1413. *n_written = (size_t)(w - tls->last_write_count);
  1414. if (*n_read > INT_MAX || *n_written > INT_MAX) {
  1415. log_warn(LD_BUG, "Preposterously large value in tor_tls_get_n_raw_bytes. "
  1416. "r=%lu, last_read=%lu, w=%lu, last_written=%lu",
  1417. r, tls->last_read_count, w, tls->last_write_count);
  1418. }
  1419. tls->last_read_count = r;
  1420. tls->last_write_count = w;
  1421. }
  1422. /** Implement check_no_tls_errors: If there are any pending OpenSSL
  1423. * errors, log an error message. */
  1424. void
  1425. _check_no_tls_errors(const char *fname, int line)
  1426. {
  1427. if (ERR_peek_error() == 0)
  1428. return;
  1429. log(LOG_WARN, LD_CRYPTO, "Unhandled OpenSSL errors found at %s:%d: ",
  1430. tor_fix_source_file(fname), line);
  1431. tls_log_errors(NULL, LOG_WARN, NULL);
  1432. }
  1433. /** Return true iff the initial TLS connection at <b>tls</b> did not use a v2
  1434. * TLS handshake. Output is undefined if the handshake isn't finished. */
  1435. int
  1436. tor_tls_used_v1_handshake(tor_tls_t *tls)
  1437. {
  1438. if (tls->isServer) {
  1439. #ifdef V2_HANDSHAKE_SERVER
  1440. return ! tls->wasV2Handshake;
  1441. #endif
  1442. } else {
  1443. #ifdef V2_HANDSHAKE_CLIENT
  1444. return ! tls->wasV2Handshake;
  1445. #endif
  1446. }
  1447. return 1;
  1448. }
  1449. /** Examine the amount of memory used and available for buffers in <b>tls</b>.
  1450. * Set *<b>rbuf_capacity</b> to the amount of storage allocated for the read
  1451. * buffer and *<b>rbuf_bytes</b> to the amount actually used.
  1452. * Set *<b>wbuf_capacity</b> to the amount of storage allocated for the write
  1453. * buffer and *<b>wbuf_bytes</b> to the amount actually used. */
  1454. void
  1455. tor_tls_get_buffer_sizes(tor_tls_t *tls,
  1456. size_t *rbuf_capacity, size_t *rbuf_bytes,
  1457. size_t *wbuf_capacity, size_t *wbuf_bytes)
  1458. {
  1459. if (tls->ssl->s3->rbuf.buf)
  1460. *rbuf_capacity = tls->ssl->s3->rbuf.len;
  1461. else
  1462. *rbuf_capacity = 0;
  1463. if (tls->ssl->s3->wbuf.buf)
  1464. *wbuf_capacity = tls->ssl->s3->wbuf.len;
  1465. else
  1466. *wbuf_capacity = 0;
  1467. *rbuf_bytes = tls->ssl->s3->rbuf.left;
  1468. *wbuf_bytes = tls->ssl->s3->wbuf.left;
  1469. }