test_tortls.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836
  1. /* Copyright (c) 2010-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TORTLS_PRIVATE
  4. #define LOG_PRIVATE
  5. #include "orconfig.h"
  6. #ifdef _WIN32
  7. #include <winsock2.h>
  8. #endif
  9. #ifdef __GNUC__
  10. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  11. #endif
  12. #if __GNUC__ && GCC_VERSION >= 402
  13. #if GCC_VERSION >= 406
  14. #pragma GCC diagnostic push
  15. #endif
  16. /* Some versions of OpenSSL declare SSL_get_selected_srtp_profile twice in
  17. * srtp.h. Suppress the GCC warning so we can build with -Wredundant-decl. */
  18. #pragma GCC diagnostic ignored "-Wredundant-decls"
  19. #endif
  20. #include <openssl/opensslv.h>
  21. #include <openssl/ssl.h>
  22. #include <openssl/ssl3.h>
  23. #include <openssl/err.h>
  24. #include <openssl/asn1t.h>
  25. #include <openssl/x509.h>
  26. #include <openssl/rsa.h>
  27. #include <openssl/evp.h>
  28. #include <openssl/bn.h>
  29. #if __GNUC__ && GCC_VERSION >= 402
  30. #if GCC_VERSION >= 406
  31. #pragma GCC diagnostic pop
  32. #else
  33. #pragma GCC diagnostic warning "-Wredundant-decls"
  34. #endif
  35. #endif
  36. #include "or.h"
  37. #include "torlog.h"
  38. #include "config.h"
  39. #include "tortls.h"
  40. #include "test.h"
  41. #include "log_test_helpers.h"
  42. #define NS_MODULE tortls
  43. extern tor_tls_context_t *server_tls_context;
  44. extern tor_tls_context_t *client_tls_context;
  45. #if OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,1,0) \
  46. && !defined(LIBRESSL_VERSION_NUMBER)
  47. #define OPENSSL_OPAQUE
  48. #define SSL_STATE_STR "before SSL initialization"
  49. #else
  50. #define SSL_STATE_STR "before/accept initialization"
  51. #endif
  52. #ifndef OPENSSL_OPAQUE
  53. static SSL_METHOD *
  54. give_me_a_test_method(void)
  55. {
  56. SSL_METHOD *method = tor_malloc_zero(sizeof(SSL_METHOD));
  57. memcpy(method, TLSv1_method(), sizeof(SSL_METHOD));
  58. return method;
  59. }
  60. static int
  61. fake_num_ciphers(void)
  62. {
  63. return 0;
  64. }
  65. #endif
  66. static void
  67. test_tortls_errno_to_tls_error(void *data)
  68. {
  69. (void) data;
  70. tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ECONNRESET)),OP_EQ,
  71. TOR_TLS_ERROR_CONNRESET);
  72. tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ETIMEDOUT)),OP_EQ,
  73. TOR_TLS_ERROR_TIMEOUT);
  74. tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(EHOSTUNREACH)),OP_EQ,
  75. TOR_TLS_ERROR_NO_ROUTE);
  76. tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ENETUNREACH)),OP_EQ,
  77. TOR_TLS_ERROR_NO_ROUTE);
  78. tt_int_op(tor_errno_to_tls_error(SOCK_ERRNO(ECONNREFUSED)),OP_EQ,
  79. TOR_TLS_ERROR_CONNREFUSED);
  80. tt_int_op(tor_errno_to_tls_error(0),OP_EQ,TOR_TLS_ERROR_MISC);
  81. done:
  82. (void)1;
  83. }
  84. static void
  85. test_tortls_err_to_string(void *data)
  86. {
  87. (void) data;
  88. tt_str_op(tor_tls_err_to_string(1),OP_EQ,"[Not an error.]");
  89. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_MISC),OP_EQ,"misc error");
  90. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_IO),OP_EQ,"unexpected close");
  91. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_CONNREFUSED),OP_EQ,
  92. "connection refused");
  93. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_CONNRESET),OP_EQ,
  94. "connection reset");
  95. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_NO_ROUTE),OP_EQ,
  96. "host unreachable");
  97. tt_str_op(tor_tls_err_to_string(TOR_TLS_ERROR_TIMEOUT),OP_EQ,
  98. "connection timed out");
  99. tt_str_op(tor_tls_err_to_string(TOR_TLS_CLOSE),OP_EQ,"closed");
  100. tt_str_op(tor_tls_err_to_string(TOR_TLS_WANTREAD),OP_EQ,"want to read");
  101. tt_str_op(tor_tls_err_to_string(TOR_TLS_WANTWRITE),OP_EQ,"want to write");
  102. tt_str_op(tor_tls_err_to_string(-100),OP_EQ,"(unknown error code)");
  103. done:
  104. (void)1;
  105. }
  106. static int
  107. mock_tls_cert_matches_key(const tor_tls_t *tls, const tor_x509_cert_t *cert)
  108. {
  109. (void) tls;
  110. (void) cert; // XXXX look at this.
  111. return 1;
  112. }
  113. static void
  114. test_tortls_tor_tls_new(void *data)
  115. {
  116. (void) data;
  117. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  118. crypto_pk_t *key1 = NULL, *key2 = NULL;
  119. SSL_METHOD *method = NULL;
  120. key1 = pk_generate(2);
  121. key2 = pk_generate(3);
  122. tor_tls_t *tls = NULL;
  123. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  124. key1, key2, 86400), OP_EQ, 0);
  125. tls = tor_tls_new(-1, 0);
  126. tt_want(tls);
  127. tor_tls_free(tls); tls = NULL;
  128. SSL_CTX_free(client_tls_context->ctx);
  129. client_tls_context->ctx = NULL;
  130. tls = tor_tls_new(-1, 0);
  131. tt_assert(!tls);
  132. #ifndef OPENSSL_OPAQUE
  133. method = give_me_a_test_method();
  134. SSL_CTX *ctx = SSL_CTX_new(method);
  135. method->num_ciphers = fake_num_ciphers;
  136. client_tls_context->ctx = ctx;
  137. tls = tor_tls_new(-1, 0);
  138. tt_assert(!tls);
  139. #endif
  140. done:
  141. UNMOCK(tor_tls_cert_matches_key);
  142. crypto_pk_free(key1);
  143. crypto_pk_free(key2);
  144. tor_tls_free(tls);
  145. tor_free(method);
  146. tor_tls_free_all();
  147. }
  148. #define NS_MODULE tortls
  149. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  150. const char *funcname, const char *suffix,
  151. const char *format, va_list ap));
  152. static void
  153. NS(logv)(int severity, log_domain_mask_t domain,
  154. const char *funcname, const char *suffix, const char *format,
  155. va_list ap)
  156. {
  157. (void) severity;
  158. (void) domain;
  159. (void) funcname;
  160. (void) suffix;
  161. (void) format;
  162. (void) ap; // XXXX look at this.
  163. CALLED(logv)++;
  164. }
  165. static void
  166. test_tortls_tor_tls_get_error(void *data)
  167. {
  168. (void) data;
  169. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  170. crypto_pk_t *key1 = NULL, *key2 = NULL;
  171. key1 = pk_generate(2);
  172. key2 = pk_generate(3);
  173. tor_tls_t *tls = NULL;
  174. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  175. key1, key2, 86400), OP_EQ, 0);
  176. tls = tor_tls_new(-1, 0);
  177. NS_MOCK(logv);
  178. tt_int_op(CALLED(logv), OP_EQ, 0);
  179. tor_tls_get_error(tls, 0, 0,
  180. (const char *)"test", 0, 0);
  181. tt_int_op(CALLED(logv), OP_EQ, 1);
  182. done:
  183. UNMOCK(tor_tls_cert_matches_key);
  184. NS_UNMOCK(logv);
  185. crypto_pk_free(key1);
  186. crypto_pk_free(key2);
  187. tor_tls_free(tls);
  188. }
  189. static void
  190. test_tortls_get_state_description(void *ignored)
  191. {
  192. (void)ignored;
  193. tor_tls_t *tls;
  194. char *buf;
  195. SSL_CTX *ctx;
  196. SSL_library_init();
  197. SSL_load_error_strings();
  198. ctx = SSL_CTX_new(SSLv23_method());
  199. buf = tor_malloc_zero(1000);
  200. tls = tor_malloc_zero(sizeof(tor_tls_t));
  201. tor_tls_get_state_description(NULL, buf, 20);
  202. tt_str_op(buf, OP_EQ, "(No SSL object)");
  203. SSL_free(tls->ssl);
  204. tls->ssl = NULL;
  205. tor_tls_get_state_description(tls, buf, 20);
  206. tt_str_op(buf, OP_EQ, "(No SSL object)");
  207. tls->ssl = SSL_new(ctx);
  208. tor_tls_get_state_description(tls, buf, 200);
  209. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in HANDSHAKE");
  210. tls->state = TOR_TLS_ST_OPEN;
  211. tor_tls_get_state_description(tls, buf, 200);
  212. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in OPEN");
  213. tls->state = TOR_TLS_ST_GOTCLOSE;
  214. tor_tls_get_state_description(tls, buf, 200);
  215. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in GOTCLOSE");
  216. tls->state = TOR_TLS_ST_SENTCLOSE;
  217. tor_tls_get_state_description(tls, buf, 200);
  218. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in SENTCLOSE");
  219. tls->state = TOR_TLS_ST_CLOSED;
  220. tor_tls_get_state_description(tls, buf, 200);
  221. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in CLOSED");
  222. tls->state = TOR_TLS_ST_RENEGOTIATE;
  223. tor_tls_get_state_description(tls, buf, 200);
  224. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in RENEGOTIATE");
  225. tls->state = TOR_TLS_ST_BUFFEREVENT;
  226. tor_tls_get_state_description(tls, buf, 200);
  227. tt_str_op(buf, OP_EQ, SSL_STATE_STR);
  228. tls->state = 7;
  229. tor_tls_get_state_description(tls, buf, 200);
  230. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in unknown TLS state");
  231. done:
  232. SSL_CTX_free(ctx);
  233. SSL_free(tls->ssl);
  234. tor_free(buf);
  235. tor_free(tls);
  236. }
  237. extern int tor_tls_object_ex_data_index;
  238. static void
  239. test_tortls_get_by_ssl(void *ignored)
  240. {
  241. (void)ignored;
  242. tor_tls_t *tls;
  243. tor_tls_t *res;
  244. SSL_CTX *ctx;
  245. SSL *ssl;
  246. SSL_library_init();
  247. SSL_load_error_strings();
  248. tor_tls_allocate_tor_tls_object_ex_data_index();
  249. ctx = SSL_CTX_new(SSLv23_method());
  250. tls = tor_malloc_zero(sizeof(tor_tls_t));
  251. tls->magic = TOR_TLS_MAGIC;
  252. ssl = SSL_new(ctx);
  253. res = tor_tls_get_by_ssl(ssl);
  254. tt_assert(!res);
  255. SSL_set_ex_data(ssl, tor_tls_object_ex_data_index, tls);
  256. res = tor_tls_get_by_ssl(ssl);
  257. tt_assert(res == tls);
  258. done:
  259. SSL_free(ssl);
  260. SSL_CTX_free(ctx);
  261. tor_free(tls);
  262. }
  263. static void
  264. test_tortls_allocate_tor_tls_object_ex_data_index(void *ignored)
  265. {
  266. (void)ignored;
  267. int first;
  268. tor_tls_allocate_tor_tls_object_ex_data_index();
  269. first = tor_tls_object_ex_data_index;
  270. tor_tls_allocate_tor_tls_object_ex_data_index();
  271. tt_int_op(first, OP_EQ, tor_tls_object_ex_data_index);
  272. done:
  273. (void)0;
  274. }
  275. static void
  276. test_tortls_log_one_error(void *ignored)
  277. {
  278. (void)ignored;
  279. tor_tls_t *tls;
  280. SSL_CTX *ctx;
  281. SSL *ssl = NULL;
  282. SSL_library_init();
  283. SSL_load_error_strings();
  284. ctx = SSL_CTX_new(SSLv23_method());
  285. tls = tor_malloc_zero(sizeof(tor_tls_t));
  286. int previous_log = setup_capture_of_logs(LOG_INFO);
  287. tor_tls_log_one_error(NULL, 0, LOG_WARN, 0, "something");
  288. expect_log_msg("TLS error while something: "
  289. "(null) (in (null):(null):---)\n");
  290. mock_clean_saved_logs();
  291. tor_tls_log_one_error(tls, 0, LOG_WARN, 0, NULL);
  292. expect_log_msg("TLS error: (null) "
  293. "(in (null):(null):---)\n");
  294. mock_clean_saved_logs();
  295. tls->address = tor_strdup("127.hello");
  296. tor_tls_log_one_error(tls, 0, LOG_WARN, 0, NULL);
  297. expect_log_msg("TLS error with 127.hello: "
  298. "(null) (in (null):(null):---)\n");
  299. tor_free(tls->address);
  300. mock_clean_saved_logs();
  301. tls->address = tor_strdup("127.hello");
  302. tor_tls_log_one_error(tls, 0, LOG_WARN, 0, "blarg");
  303. expect_log_msg("TLS error while blarg with "
  304. "127.hello: (null) (in (null):(null):---)\n");
  305. mock_clean_saved_logs();
  306. tor_tls_log_one_error(tls, ERR_PACK(1, 2, 3), LOG_WARN, 0, NULL);
  307. expect_log_msg("TLS error with 127.hello: "
  308. "BN lib (in unknown library:(null):---)\n");
  309. mock_clean_saved_logs();
  310. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_HTTP_REQUEST),
  311. LOG_WARN, 0, NULL);
  312. expect_log_severity(LOG_INFO);
  313. mock_clean_saved_logs();
  314. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_HTTPS_PROXY_REQUEST),
  315. LOG_WARN, 0, NULL);
  316. expect_log_severity(LOG_INFO);
  317. mock_clean_saved_logs();
  318. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_RECORD_LENGTH_MISMATCH),
  319. LOG_WARN, 0, NULL);
  320. expect_log_severity(LOG_INFO);
  321. #ifndef OPENSSL_1_1_API
  322. mock_clean_saved_logs();
  323. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_RECORD_TOO_LARGE),
  324. LOG_WARN, 0, NULL);
  325. expect_log_severity(LOG_INFO);
  326. #endif
  327. mock_clean_saved_logs();
  328. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_UNKNOWN_PROTOCOL),
  329. LOG_WARN, 0, NULL);
  330. expect_log_severity(LOG_INFO);
  331. mock_clean_saved_logs();
  332. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_UNSUPPORTED_PROTOCOL),
  333. LOG_WARN, 0, NULL);
  334. expect_log_severity(LOG_INFO);
  335. tls->ssl = SSL_new(ctx);
  336. mock_clean_saved_logs();
  337. tor_tls_log_one_error(tls, 0, LOG_WARN, 0, NULL);
  338. expect_log_msg("TLS error with 127.hello: (null)"
  339. " (in (null):(null):" SSL_STATE_STR ")\n");
  340. done:
  341. teardown_capture_of_logs(previous_log);
  342. SSL_free(ssl);
  343. SSL_CTX_free(ctx);
  344. if (tls && tls->ssl)
  345. SSL_free(tls->ssl);
  346. if (tls)
  347. tor_free(tls->address);
  348. tor_free(tls);
  349. }
  350. #ifndef OPENSSL_OPAQUE
  351. static void
  352. test_tortls_get_error(void *ignored)
  353. {
  354. (void)ignored;
  355. tor_tls_t *tls;
  356. int ret;
  357. SSL_CTX *ctx;
  358. SSL_library_init();
  359. SSL_load_error_strings();
  360. ctx = SSL_CTX_new(SSLv23_method());
  361. int previous_log = setup_capture_of_logs(LOG_INFO);
  362. tls = tor_malloc_zero(sizeof(tor_tls_t));
  363. tls->ssl = SSL_new(ctx);
  364. SSL_set_bio(tls->ssl, BIO_new(BIO_s_mem()), NULL);
  365. ret = tor_tls_get_error(tls, 0, 0, "something", LOG_WARN, 0);
  366. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_IO);
  367. expect_log_msg("TLS error: unexpected close while"
  368. " something (before/accept initialization)\n");
  369. mock_clean_saved_logs();
  370. ret = tor_tls_get_error(tls, 2, 0, "something", LOG_WARN, 0);
  371. tt_int_op(ret, OP_EQ, 0);
  372. expect_no_log_entry();
  373. mock_clean_saved_logs();
  374. ret = tor_tls_get_error(tls, 0, 1, "something", LOG_WARN, 0);
  375. tt_int_op(ret, OP_EQ, -11);
  376. expect_no_log_entry();
  377. mock_clean_saved_logs();
  378. ERR_clear_error();
  379. ERR_put_error(ERR_LIB_BN, 2, -1, "somewhere.c", 99);
  380. ret = tor_tls_get_error(tls, 0, 0, "something", LOG_WARN, 0);
  381. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  382. expect_log_msg("TLS error while something: (null)"
  383. " (in bignum routines:(null):before/accept initialization)\n");
  384. mock_clean_saved_logs();
  385. ERR_clear_error();
  386. tls->ssl->rwstate = SSL_READING;
  387. SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_READ;
  388. ret = tor_tls_get_error(tls, -1, 0, "something", LOG_WARN, 0);
  389. tt_int_op(ret, OP_EQ, TOR_TLS_WANTREAD);
  390. expect_no_log_entry();
  391. mock_clean_saved_logs();
  392. ERR_clear_error();
  393. tls->ssl->rwstate = SSL_READING;
  394. SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_WRITE;
  395. ret = tor_tls_get_error(tls, -1, 0, "something", LOG_WARN, 0);
  396. tt_int_op(ret, OP_EQ, TOR_TLS_WANTWRITE);
  397. expect_no_log_entry();
  398. mock_clean_saved_logs();
  399. ERR_clear_error();
  400. tls->ssl->rwstate = 0;
  401. tls->ssl->shutdown = SSL_RECEIVED_SHUTDOWN;
  402. tls->ssl->s3->warn_alert =SSL_AD_CLOSE_NOTIFY;
  403. ret = tor_tls_get_error(tls, 0, 0, "something", LOG_WARN, 0);
  404. tt_int_op(ret, OP_EQ, TOR_TLS_CLOSE);
  405. expect_log_entry();
  406. mock_clean_saved_logs();
  407. ret = tor_tls_get_error(tls, 0, 2, "something", LOG_WARN, 0);
  408. tt_int_op(ret, OP_EQ, -10);
  409. expect_no_log_entry();
  410. mock_clean_saved_logs();
  411. ERR_put_error(ERR_LIB_SYS, 2, -1, "somewhere.c", 99);
  412. ret = tor_tls_get_error(tls, -1, 0, "something", LOG_WARN, 0);
  413. tt_int_op(ret, OP_EQ, -9);
  414. expect_log_msg("TLS error while something: (null) (in system library:"
  415. "connect:before/accept initialization)\n");
  416. done:
  417. teardown_capture_of_logs(previous_log);
  418. SSL_free(tls->ssl);
  419. tor_free(tls);
  420. SSL_CTX_free(ctx);
  421. }
  422. #endif
  423. static void
  424. test_tortls_always_accept_verify_cb(void *ignored)
  425. {
  426. (void)ignored;
  427. int ret;
  428. ret = always_accept_verify_cb(0, NULL);
  429. tt_int_op(ret, OP_EQ, 1);
  430. done:
  431. (void)0;
  432. }
  433. #ifndef OPENSSL_OPAQUE
  434. static void
  435. test_tortls_x509_cert_free(void *ignored)
  436. {
  437. (void)ignored;
  438. tor_x509_cert_t *cert;
  439. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  440. tor_x509_cert_free(cert);
  441. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  442. cert->cert = tor_malloc_zero(sizeof(X509));
  443. cert->encoded = tor_malloc_zero(1);
  444. tor_x509_cert_free(cert);
  445. }
  446. #endif
  447. static void
  448. test_tortls_x509_cert_get_id_digests(void *ignored)
  449. {
  450. (void)ignored;
  451. tor_x509_cert_t *cert;
  452. common_digests_t *d;
  453. const common_digests_t *res;
  454. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  455. d = tor_malloc_zero(sizeof(common_digests_t));
  456. d->d[0][0] = 42;
  457. res = tor_x509_cert_get_id_digests(cert);
  458. tt_assert(!res);
  459. cert->pkey_digests_set = 1;
  460. cert->pkey_digests = *d;
  461. res = tor_x509_cert_get_id_digests(cert);
  462. tt_int_op(res->d[0][0], OP_EQ, 42);
  463. done:
  464. tor_free(cert);
  465. tor_free(d);
  466. }
  467. #ifndef OPENSSL_OPAQUE
  468. static int
  469. fixed_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  470. {
  471. (void) a; (void) b;
  472. return 1;
  473. }
  474. static void
  475. fake_x509_free(X509 *cert)
  476. {
  477. if (cert) {
  478. if (cert->cert_info) {
  479. if (cert->cert_info->key) {
  480. if (cert->cert_info->key->pkey) {
  481. tor_free(cert->cert_info->key->pkey);
  482. }
  483. tor_free(cert->cert_info->key);
  484. }
  485. tor_free(cert->cert_info);
  486. }
  487. tor_free(cert);
  488. }
  489. }
  490. static void
  491. test_tortls_cert_matches_key(void *ignored)
  492. {
  493. (void)ignored;
  494. int res;
  495. tor_tls_t *tls;
  496. tor_x509_cert_t *cert;
  497. X509 *one = NULL, *two = NULL;
  498. EVP_PKEY_ASN1_METHOD *meth = EVP_PKEY_asn1_new(999, 0, NULL, NULL);
  499. EVP_PKEY_asn1_set_public(meth, NULL, NULL, fixed_pub_cmp, NULL, NULL, NULL);
  500. tls = tor_malloc_zero(sizeof(tor_tls_t));
  501. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  502. one = tor_malloc_zero(sizeof(X509));
  503. one->references = 1;
  504. two = tor_malloc_zero(sizeof(X509));
  505. two->references = 1;
  506. res = tor_tls_cert_matches_key(tls, cert);
  507. tt_int_op(res, OP_EQ, 0);
  508. tls->ssl = tor_malloc_zero(sizeof(SSL));
  509. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  510. tls->ssl->session->peer = one;
  511. res = tor_tls_cert_matches_key(tls, cert);
  512. tt_int_op(res, OP_EQ, 0);
  513. cert->cert = two;
  514. res = tor_tls_cert_matches_key(tls, cert);
  515. tt_int_op(res, OP_EQ, 0);
  516. one->cert_info = tor_malloc_zero(sizeof(X509_CINF));
  517. one->cert_info->key = tor_malloc_zero(sizeof(X509_PUBKEY));
  518. one->cert_info->key->pkey = tor_malloc_zero(sizeof(EVP_PKEY));
  519. one->cert_info->key->pkey->references = 1;
  520. one->cert_info->key->pkey->ameth = meth;
  521. one->cert_info->key->pkey->type = 1;
  522. two->cert_info = tor_malloc_zero(sizeof(X509_CINF));
  523. two->cert_info->key = tor_malloc_zero(sizeof(X509_PUBKEY));
  524. two->cert_info->key->pkey = tor_malloc_zero(sizeof(EVP_PKEY));
  525. two->cert_info->key->pkey->references = 1;
  526. two->cert_info->key->pkey->ameth = meth;
  527. two->cert_info->key->pkey->type = 2;
  528. res = tor_tls_cert_matches_key(tls, cert);
  529. tt_int_op(res, OP_EQ, 0);
  530. one->cert_info->key->pkey->type = 1;
  531. two->cert_info->key->pkey->type = 1;
  532. res = tor_tls_cert_matches_key(tls, cert);
  533. tt_int_op(res, OP_EQ, 1);
  534. done:
  535. EVP_PKEY_asn1_free(meth);
  536. tor_free(tls->ssl->session);
  537. tor_free(tls->ssl);
  538. tor_free(tls);
  539. tor_free(cert);
  540. fake_x509_free(one);
  541. fake_x509_free(two);
  542. }
  543. static void
  544. test_tortls_cert_get_key(void *ignored)
  545. {
  546. (void)ignored;
  547. tor_x509_cert_t *cert = NULL;
  548. crypto_pk_t *res = NULL;
  549. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  550. X509 *key = NULL;
  551. key = tor_malloc_zero(sizeof(X509));
  552. key->references = 1;
  553. res = tor_tls_cert_get_key(cert);
  554. tt_assert(!res);
  555. cert->cert = key;
  556. key->cert_info = tor_malloc_zero(sizeof(X509_CINF));
  557. key->cert_info->key = tor_malloc_zero(sizeof(X509_PUBKEY));
  558. key->cert_info->key->pkey = tor_malloc_zero(sizeof(EVP_PKEY));
  559. key->cert_info->key->pkey->references = 1;
  560. key->cert_info->key->pkey->type = 2;
  561. res = tor_tls_cert_get_key(cert);
  562. tt_assert(!res);
  563. done:
  564. fake_x509_free(key);
  565. tor_free(cert);
  566. crypto_pk_free(res);
  567. }
  568. #endif
  569. static void
  570. test_tortls_get_my_client_auth_key(void *ignored)
  571. {
  572. (void)ignored;
  573. crypto_pk_t *ret;
  574. crypto_pk_t *expected;
  575. tor_tls_context_t *ctx;
  576. RSA *k = RSA_new();
  577. ctx = tor_malloc_zero(sizeof(tor_tls_context_t));
  578. expected = crypto_new_pk_from_rsa_(k);
  579. ctx->auth_key = expected;
  580. client_tls_context = NULL;
  581. ret = tor_tls_get_my_client_auth_key();
  582. tt_assert(!ret);
  583. client_tls_context = ctx;
  584. ret = tor_tls_get_my_client_auth_key();
  585. tt_assert(ret == expected);
  586. done:
  587. RSA_free(k);
  588. tor_free(expected);
  589. tor_free(ctx);
  590. }
  591. static void
  592. test_tortls_get_my_certs(void *ignored)
  593. {
  594. (void)ignored;
  595. int ret;
  596. tor_tls_context_t *ctx;
  597. const tor_x509_cert_t *link_cert_out = NULL;
  598. const tor_x509_cert_t *id_cert_out = NULL;
  599. ctx = tor_malloc_zero(sizeof(tor_tls_context_t));
  600. client_tls_context = NULL;
  601. ret = tor_tls_get_my_certs(0, NULL, NULL);
  602. tt_int_op(ret, OP_EQ, -1);
  603. server_tls_context = NULL;
  604. ret = tor_tls_get_my_certs(1, NULL, NULL);
  605. tt_int_op(ret, OP_EQ, -1);
  606. client_tls_context = ctx;
  607. ret = tor_tls_get_my_certs(0, NULL, NULL);
  608. tt_int_op(ret, OP_EQ, 0);
  609. client_tls_context = ctx;
  610. ret = tor_tls_get_my_certs(0, &link_cert_out, &id_cert_out);
  611. tt_int_op(ret, OP_EQ, 0);
  612. server_tls_context = ctx;
  613. ret = tor_tls_get_my_certs(1, &link_cert_out, &id_cert_out);
  614. tt_int_op(ret, OP_EQ, 0);
  615. done:
  616. (void)1;
  617. }
  618. #ifndef OPENSSL_OPAQUE
  619. static void
  620. test_tortls_get_ciphersuite_name(void *ignored)
  621. {
  622. (void)ignored;
  623. const char *ret;
  624. tor_tls_t *ctx;
  625. ctx = tor_malloc_zero(sizeof(tor_tls_t));
  626. ctx->ssl = tor_malloc_zero(sizeof(SSL));
  627. ret = tor_tls_get_ciphersuite_name(ctx);
  628. tt_str_op(ret, OP_EQ, "(NONE)");
  629. done:
  630. tor_free(ctx->ssl);
  631. tor_free(ctx);
  632. }
  633. static SSL_CIPHER *
  634. get_cipher_by_name(const char *name)
  635. {
  636. int i;
  637. const SSL_METHOD *method = SSLv23_method();
  638. int num = method->num_ciphers();
  639. for (i = 0; i < num; ++i) {
  640. const SSL_CIPHER *cipher = method->get_cipher(i);
  641. const char *ciphername = SSL_CIPHER_get_name(cipher);
  642. if (!strcmp(ciphername, name)) {
  643. return (SSL_CIPHER *)cipher;
  644. }
  645. }
  646. return NULL;
  647. }
  648. static SSL_CIPHER *
  649. get_cipher_by_id(uint16_t id)
  650. {
  651. int i;
  652. const SSL_METHOD *method = SSLv23_method();
  653. int num = method->num_ciphers();
  654. for (i = 0; i < num; ++i) {
  655. const SSL_CIPHER *cipher = method->get_cipher(i);
  656. if (id == (SSL_CIPHER_get_id(cipher) & 0xffff)) {
  657. return (SSL_CIPHER *)cipher;
  658. }
  659. }
  660. return NULL;
  661. }
  662. extern uint16_t v2_cipher_list[];
  663. static void
  664. test_tortls_classify_client_ciphers(void *ignored)
  665. {
  666. (void)ignored;
  667. int i;
  668. int ret;
  669. SSL_CTX *ctx;
  670. SSL *ssl;
  671. tor_tls_t *tls;
  672. STACK_OF(SSL_CIPHER) *ciphers;
  673. SSL_CIPHER *tmp_cipher;
  674. SSL_library_init();
  675. SSL_load_error_strings();
  676. tor_tls_allocate_tor_tls_object_ex_data_index();
  677. tls = tor_malloc_zero(sizeof(tor_tls_t));
  678. tls->magic = TOR_TLS_MAGIC;
  679. ctx = SSL_CTX_new(TLSv1_method());
  680. ssl = SSL_new(ctx);
  681. tls->ssl = ssl;
  682. ciphers = sk_SSL_CIPHER_new_null();
  683. ret = tor_tls_classify_client_ciphers(ssl, NULL);
  684. tt_int_op(ret, OP_EQ, -1);
  685. SSL_set_ex_data(ssl, tor_tls_object_ex_data_index, tls);
  686. tls->client_cipher_list_type = 42;
  687. ret = tor_tls_classify_client_ciphers(ssl, NULL);
  688. tt_int_op(ret, OP_EQ, 42);
  689. tls->client_cipher_list_type = 0;
  690. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  691. tt_int_op(ret, OP_EQ, 1);
  692. tt_int_op(tls->client_cipher_list_type, OP_EQ, 1);
  693. tls->client_cipher_list_type = 0;
  694. ret = tor_tls_classify_client_ciphers(ssl, SSL_get_ciphers(ssl));
  695. tt_int_op(ret, OP_EQ, 3);
  696. tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
  697. SSL_CIPHER *one = get_cipher_by_name(TLS1_TXT_DHE_RSA_WITH_AES_128_SHA),
  698. *two = get_cipher_by_name(TLS1_TXT_DHE_RSA_WITH_AES_256_SHA),
  699. *three = get_cipher_by_name(SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA),
  700. *four = NULL;
  701. sk_SSL_CIPHER_push(ciphers, one);
  702. sk_SSL_CIPHER_push(ciphers, two);
  703. sk_SSL_CIPHER_push(ciphers, three);
  704. sk_SSL_CIPHER_push(ciphers, four);
  705. tls->client_cipher_list_type = 0;
  706. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  707. tt_int_op(ret, OP_EQ, 1);
  708. tt_int_op(tls->client_cipher_list_type, OP_EQ, 1);
  709. sk_SSL_CIPHER_zero(ciphers);
  710. one = get_cipher_by_name("ECDH-RSA-AES256-GCM-SHA384");
  711. one->id = 0x00ff;
  712. two = get_cipher_by_name("ECDH-RSA-AES128-GCM-SHA256");
  713. two->id = 0x0000;
  714. sk_SSL_CIPHER_push(ciphers, one);
  715. tls->client_cipher_list_type = 0;
  716. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  717. tt_int_op(ret, OP_EQ, 3);
  718. tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
  719. sk_SSL_CIPHER_push(ciphers, two);
  720. tls->client_cipher_list_type = 0;
  721. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  722. tt_int_op(ret, OP_EQ, 3);
  723. tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
  724. one->id = 0xC00A;
  725. tls->client_cipher_list_type = 0;
  726. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  727. tt_int_op(ret, OP_EQ, 3);
  728. tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
  729. sk_SSL_CIPHER_zero(ciphers);
  730. for (i=0; v2_cipher_list[i]; i++) {
  731. tmp_cipher = get_cipher_by_id(v2_cipher_list[i]);
  732. tt_assert(tmp_cipher);
  733. sk_SSL_CIPHER_push(ciphers, tmp_cipher);
  734. }
  735. tls->client_cipher_list_type = 0;
  736. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  737. tt_int_op(ret, OP_EQ, 2);
  738. tt_int_op(tls->client_cipher_list_type, OP_EQ, 2);
  739. done:
  740. sk_SSL_CIPHER_free(ciphers);
  741. SSL_free(tls->ssl);
  742. tor_free(tls);
  743. SSL_CTX_free(ctx);
  744. }
  745. #endif
  746. static void
  747. test_tortls_client_is_using_v2_ciphers(void *ignored)
  748. {
  749. (void)ignored;
  750. #ifdef HAVE_SSL_GET_CLIENT_CIPHERS
  751. tt_skip();
  752. done:
  753. (void)1;
  754. #else
  755. int ret;
  756. SSL_CTX *ctx;
  757. SSL *ssl;
  758. SSL_SESSION *sess;
  759. STACK_OF(SSL_CIPHER) *ciphers;
  760. SSL_library_init();
  761. SSL_load_error_strings();
  762. ctx = SSL_CTX_new(TLSv1_method());
  763. ssl = SSL_new(ctx);
  764. sess = SSL_SESSION_new();
  765. ret = tor_tls_client_is_using_v2_ciphers(ssl);
  766. tt_int_op(ret, OP_EQ, -1);
  767. ssl->session = sess;
  768. ret = tor_tls_client_is_using_v2_ciphers(ssl);
  769. tt_int_op(ret, OP_EQ, 0);
  770. ciphers = sk_SSL_CIPHER_new_null();
  771. SSL_CIPHER *one = get_cipher_by_name("ECDH-RSA-AES256-GCM-SHA384");
  772. one->id = 0x00ff;
  773. sk_SSL_CIPHER_push(ciphers, one);
  774. sess->ciphers = ciphers;
  775. ret = tor_tls_client_is_using_v2_ciphers(ssl);
  776. tt_int_op(ret, OP_EQ, 1);
  777. done:
  778. SSL_free(ssl);
  779. SSL_CTX_free(ctx);
  780. #endif
  781. }
  782. #ifndef OPENSSL_OPAQUE
  783. static X509 *fixed_try_to_extract_certs_from_tls_cert_out_result = NULL;
  784. static X509 *fixed_try_to_extract_certs_from_tls_id_cert_out_result = NULL;
  785. static void
  786. fixed_try_to_extract_certs_from_tls(int severity, tor_tls_t *tls,
  787. X509 **cert_out, X509 **id_cert_out)
  788. {
  789. (void) severity;
  790. (void) tls;
  791. *cert_out = fixed_try_to_extract_certs_from_tls_cert_out_result;
  792. *id_cert_out = fixed_try_to_extract_certs_from_tls_id_cert_out_result;
  793. }
  794. #endif
  795. #ifndef OPENSSL_OPAQUE
  796. static const char* notCompletelyValidCertString =
  797. "-----BEGIN CERTIFICATE-----\n"
  798. "MIICVjCCAb8CAg37MA0GCSqGSIb3DQEBBQUAMIGbMQswCQYDVQQGEwJKUDEOMAwG\n"
  799. "A1UECBMFVG9reW8xEDAOBgNVBAcTB0NodW8ta3UxETAPBgNVBAoTCEZyYW5rNERE\n"
  800. "MRgwFgYDVQQLEw9XZWJDZXJ0IFN1cHBvcnQxGDAWBgNVBAMTD0ZyYW5rNEREIFdl\n"
  801. "YiBDQTEjMCEGCSqGSIb3DQEJARYUc3VwcG9ydEBmcmFuazRkZC5jb20wHhcNMTIw\n"
  802. "ODIyMDUyNzIzWhcNMTcwODIxMDUyNzIzWjBKMQswCQYDVQQGEwJKUDEOMAwGA1UE\n"
  803. "CAwFVG9reW8xETAPBgNVBAoMCEZyYW5rNEREMRgwFgYDVQQDDA93d3cuZXhhbXBs\n"
  804. "ZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMYBBrx5PlP0WNI/ZdzD\n"
  805. "+6Pktmurn+F2kQYbtc7XQh8/LTBvCo+P6iZoLEmUA9e7EXLRxgU1CVqeAi7QcAn9\n"
  806. "MwBlc8ksFJHB0rtf9pmf8Oza9E0Bynlq/4/Kb1x+d+AyhL7oK9tQwB24uHOueHi1\n"
  807. "C/iVv8CSWKiYe6hzN1txYe8rAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAASPdjigJ\n"
  808. "kXCqKWpnZ/Oc75EUcMi6HztaW8abUMlYXPIgkV2F7YanHOB7K4f7OOLjiz8DTPFf\n"
  809. "jC9UeuErhaA/zzWi8ewMTFZW/WshOrm3fNvcMrMLKtH534JKvcdMg6qIdjTFINIr\n"
  810. "evnAhf0cwULaebn+lMs8Pdl7y37+sfluVok=\n"
  811. "-----END CERTIFICATE-----\n";
  812. #endif
  813. static const char* validCertString = "-----BEGIN CERTIFICATE-----\n"
  814. "MIIDpTCCAY0CAg3+MA0GCSqGSIb3DQEBBQUAMF4xCzAJBgNVBAYTAlVTMREwDwYD\n"
  815. "VQQIDAhJbGxpbm9pczEQMA4GA1UEBwwHQ2hpY2FnbzEUMBIGA1UECgwLVG9yIFRl\n"
  816. "c3RpbmcxFDASBgNVBAMMC1RvciBUZXN0aW5nMB4XDTE1MDkwNjEzMzk1OVoXDTQz\n"
  817. "MDEyMjEzMzk1OVowVjELMAkGA1UEBhMCVVMxEDAOBgNVBAcMB0NoaWNhZ28xFDAS\n"
  818. "BgNVBAoMC1RvciBUZXN0aW5nMR8wHQYDVQQDDBZ0ZXN0aW5nLnRvcnByb2plY3Qu\n"
  819. "b3JnMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDoT6uyVVhWyOF3wkHjjYbd\n"
  820. "nKaykyRv4JVtKQdZ4OpEErmX1zw4MmyzpQNV6iR4bQnWiyLfzyVJMZDIC/WILBfX\n"
  821. "w2Pza/yuLgUvDc3twMuhOACzOQVO8PrEF/aVv2+hbCCy2udXvKhnYn+CCXl3ozc8\n"
  822. "XcKYvujTXDyvGWY3xwAjlQIDAQABMA0GCSqGSIb3DQEBBQUAA4ICAQCUvnhzQWuQ\n"
  823. "MrN+pERkE+zcTI/9dGS90rUMMLgu8VDNqTa0TUQh8uO0EQ6uDvI8Js6e8tgwS0BR\n"
  824. "UBahqb7ZHv+rejGCBr5OudqD+x4STiiuPNJVs86JTLN8SpM9CHjIBH5WCCN2KOy3\n"
  825. "mevNoRcRRyYJzSFULCunIK6FGulszigMYGscrO4oiTkZiHPh9KvWT40IMiHfL+Lw\n"
  826. "EtEWiLex6064LcA2YQ1AMuSZyCexks63lcfaFmQbkYOKqXa1oLkIRuDsOaSVjTfe\n"
  827. "vec+X6jvf12cFTKS5WIeqkKF2Irt+dJoiHEGTe5RscUMN/f+gqHPzfFz5dR23sxo\n"
  828. "g+HC6MZHlFkLAOx3wW6epPS8A/m1mw3zMPoTnb2U2YYt8T0dJMMlUn/7Y1sEAa+a\n"
  829. "dSTMaeUf6VnJ//11m454EZl1to9Z7oJOgqmFffSrdD4BGIWe8f7hhW6L1Enmqe/J\n"
  830. "BKL3wbzZh80O1W0bndAwhnEEhlzneFY84cbBo9pmVxpODHkUcStpr5Z7pBDrcL21\n"
  831. "Ss/aB/1YrsVXhdvJdOGxl3Mnl9dUY57CympLGlT8f0pPS6GAKOelECOhFMHmJd8L\n"
  832. "dj3XQSmKtYHevZ6IvuMXSlB/fJvSjSlkCuLo5+kJoaqPuRu+i/S1qxeRy3CBwmnE\n"
  833. "LdSNdcX4N79GQJ996PA8+mUCQG7YRtK+WA==\n"
  834. "-----END CERTIFICATE-----\n";
  835. static const char* caCertString = "-----BEGIN CERTIFICATE-----\n"
  836. "MIIFjzCCA3egAwIBAgIJAKd5WgyfPMYRMA0GCSqGSIb3DQEBCwUAMF4xCzAJBgNV\n"
  837. "BAYTAlVTMREwDwYDVQQIDAhJbGxpbm9pczEQMA4GA1UEBwwHQ2hpY2FnbzEUMBIG\n"
  838. "A1UECgwLVG9yIFRlc3RpbmcxFDASBgNVBAMMC1RvciBUZXN0aW5nMB4XDTE1MDkw\n"
  839. "NjEzMzc0MVoXDTQzMDEyMjEzMzc0MVowXjELMAkGA1UEBhMCVVMxETAPBgNVBAgM\n"
  840. "CElsbGlub2lzMRAwDgYDVQQHDAdDaGljYWdvMRQwEgYDVQQKDAtUb3IgVGVzdGlu\n"
  841. "ZzEUMBIGA1UEAwwLVG9yIFRlc3RpbmcwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAw\n"
  842. "ggIKAoICAQCpLMUEiLW5leUgBZoEJms2V7lZRhIAjnJBhVMHD0e3UubNknmaQoxf\n"
  843. "ARz3rvqOaRd0JlV+qM9qE0DjiYcCVP1cAfqAo9d83uS1vwY3YMVJzADlaIiHfyVW\n"
  844. "uEgBy0vvkeUBqaua24dYlcwsemOiXYLu41yM1wkcGHW1AhBNHppY6cznb8TyLgNM\n"
  845. "2x3SGUdzc5XMyAFx51faKGBA3wjs+Hg1PLY7d30nmCgEOBavpm5I1disM/0k+Mcy\n"
  846. "YmAKEo/iHJX/rQzO4b9znP69juLlR8PDBUJEVIG/CYb6+uw8MjjUyiWXYoqfVmN2\n"
  847. "hm/lH8b6rXw1a2Aa3VTeD0DxaWeacMYHY/i01fd5n7hCoDTRNdSw5KJ0L3Z0SKTu\n"
  848. "0lzffKzDaIfyZGlpW5qdouACkWYzsaitQOePVE01PIdO30vUfzNTFDfy42ccx3Di\n"
  849. "59UCu+IXB+eMtrBfsok0Qc63vtF1linJgjHW1z/8ujk8F7/qkOfODhk4l7wngc2A\n"
  850. "EmwWFIFoGaiTEZHB9qteXr4unbXZ0AHpM02uGGwZEGohjFyebEb73M+J57WKKAFb\n"
  851. "PqbLcGUksL1SHNBNAJcVLttX55sO4nbidOS/kA3m+F1R04MBTyQF9qA6YDDHqdI3\n"
  852. "h/3pw0Z4fxVouTYT4/NfRnX4JTP4u+7Mpcoof28VME0qWqD1LnRhFQIDAQABo1Aw\n"
  853. "TjAdBgNVHQ4EFgQUMoAgIXH7pZ3QMRwTjT+DM9Yo/v0wHwYDVR0jBBgwFoAUMoAg\n"
  854. "IXH7pZ3QMRwTjT+DM9Yo/v0wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOC\n"
  855. "AgEAUJxacjXR9sT+Xs6ISFiUsyd0T6WVKMnV46xrYJHirGfx+krWHrjxMY+ZtxYD\n"
  856. "DBDGlo11Qc4v6QrclNf5QUBfIiGQsP9Cm6hHcQ+Tpg9HHCgSqG1YNPwCPReCR4br\n"
  857. "BLvLfrfkcBL2IWM0PdQdCze+59DBfipsULD2mEn9fjYRXQEwb2QWtQ9qRc20Yb/x\n"
  858. "Q4b/+CvUodLkaq7B8MHz0BV8HHcBoph6DYaRmO/N+hPauIuSp6XyaGYcEefGKVKj\n"
  859. "G2+fcsdyXsoijNdL8vNKwm4j2gVwCBnw16J00yfFoV46YcbfqEdJB2je0XSvwXqt\n"
  860. "14AOTngxso2h9k9HLtrfpO1ZG/B5AcCMs1lzbZ2fp5DPHtjvvmvA2RJqgo3yjw4W\n"
  861. "4DHAuTglYFlC3mDHNfNtcGP20JvepcQNzNP2UzwcpOc94hfKikOFw+gf9Vf1qd0y\n"
  862. "h/Sk6OZHn2+JVUPiWHIQV98Vtoh4RmUZDJD+b55ia3fQGTGzt4z1XFzQYSva5sfs\n"
  863. "wocS/papthqWldQU7x+3wofNd5CNU1x6WKXG/yw30IT/4F8ADJD6GeygNT8QJYvt\n"
  864. "u/8lAkbOy6B9xGmSvr0Kk1oq9P2NshA6kalxp1Oz/DTNDdL4AeBXV3JmM6WWCjGn\n"
  865. "Yy1RT69d0rwYc5u/vnqODz1IjvT90smsrkBumGt791FAFeg=\n"
  866. "-----END CERTIFICATE-----\n";
  867. static X509 *
  868. read_cert_from(const char *str)
  869. {
  870. BIO *bio = BIO_new(BIO_s_mem());
  871. BIO_write(bio, str, (int) strlen(str));
  872. X509 *res = PEM_read_bio_X509(bio, NULL, NULL, NULL);
  873. BIO_free(bio);
  874. return res;
  875. }
  876. #ifndef OPENSSL_OPAQUE
  877. static void
  878. test_tortls_verify(void *ignored)
  879. {
  880. (void)ignored;
  881. int ret;
  882. tor_tls_t *tls;
  883. crypto_pk_t *k = NULL;
  884. X509 *cert1 = NULL, *cert2 = NULL, *invalidCert = NULL,
  885. *validCert = NULL, *caCert = NULL;
  886. cert1 = tor_malloc_zero(sizeof(X509));
  887. cert1->references = 10;
  888. cert2 = tor_malloc_zero(sizeof(X509));
  889. cert2->references = 10;
  890. validCert = read_cert_from(validCertString);
  891. caCert = read_cert_from(caCertString);
  892. invalidCert = read_cert_from(notCompletelyValidCertString);
  893. tls = tor_malloc_zero(sizeof(tor_tls_t));
  894. ret = tor_tls_verify(LOG_WARN, tls, &k);
  895. tt_int_op(ret, OP_EQ, -1);
  896. MOCK(try_to_extract_certs_from_tls, fixed_try_to_extract_certs_from_tls);
  897. fixed_try_to_extract_certs_from_tls_cert_out_result = cert1;
  898. ret = tor_tls_verify(LOG_WARN, tls, &k);
  899. tt_int_op(ret, OP_EQ, -1);
  900. fixed_try_to_extract_certs_from_tls_id_cert_out_result = cert2;
  901. ret = tor_tls_verify(LOG_WARN, tls, &k);
  902. tt_int_op(ret, OP_EQ, -1);
  903. fixed_try_to_extract_certs_from_tls_cert_out_result = invalidCert;
  904. fixed_try_to_extract_certs_from_tls_id_cert_out_result = invalidCert;
  905. ret = tor_tls_verify(LOG_WARN, tls, &k);
  906. tt_int_op(ret, OP_EQ, -1);
  907. fixed_try_to_extract_certs_from_tls_cert_out_result = validCert;
  908. fixed_try_to_extract_certs_from_tls_id_cert_out_result = caCert;
  909. ret = tor_tls_verify(LOG_WARN, tls, &k);
  910. tt_int_op(ret, OP_EQ, 0);
  911. tt_assert(k);
  912. done:
  913. UNMOCK(try_to_extract_certs_from_tls);
  914. tor_free(cert1);
  915. tor_free(cert2);
  916. tor_free(tls);
  917. tor_free(k);
  918. }
  919. #endif
  920. #ifndef OPENSSL_OPAQUE
  921. static void
  922. test_tortls_check_lifetime(void *ignored)
  923. {
  924. (void)ignored;
  925. int ret;
  926. tor_tls_t *tls;
  927. X509 *validCert = read_cert_from(validCertString);
  928. time_t now = time(NULL);
  929. tls = tor_malloc_zero(sizeof(tor_tls_t));
  930. ret = tor_tls_check_lifetime(LOG_WARN, tls, 0, 0);
  931. tt_int_op(ret, OP_EQ, -1);
  932. tls->ssl = tor_malloc_zero(sizeof(SSL));
  933. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  934. tls->ssl->session->peer = validCert;
  935. ret = tor_tls_check_lifetime(LOG_WARN, tls, 0, 0);
  936. tt_int_op(ret, OP_EQ, 0);
  937. ASN1_STRING_free(validCert->cert_info->validity->notBefore);
  938. validCert->cert_info->validity->notBefore = ASN1_TIME_set(NULL, now-10);
  939. ASN1_STRING_free(validCert->cert_info->validity->notAfter);
  940. validCert->cert_info->validity->notAfter = ASN1_TIME_set(NULL, now+60);
  941. ret = tor_tls_check_lifetime(LOG_WARN, tls, 0, -1000);
  942. tt_int_op(ret, OP_EQ, -1);
  943. ret = tor_tls_check_lifetime(LOG_WARN, tls, -1000, 0);
  944. tt_int_op(ret, OP_EQ, -1);
  945. done:
  946. tor_free(tls->ssl->session);
  947. tor_free(tls->ssl);
  948. tor_free(tls);
  949. X509_free(validCert);
  950. }
  951. #endif
  952. #ifndef OPENSSL_OPAQUE
  953. static int fixed_ssl_pending_result = 0;
  954. static int
  955. fixed_ssl_pending(const SSL *ignored)
  956. {
  957. (void)ignored;
  958. return fixed_ssl_pending_result;
  959. }
  960. static void
  961. test_tortls_get_pending_bytes(void *ignored)
  962. {
  963. (void)ignored;
  964. int ret;
  965. tor_tls_t *tls;
  966. SSL_METHOD *method;
  967. tls = tor_malloc_zero(sizeof(tor_tls_t));
  968. tls->ssl = tor_malloc_zero(sizeof(SSL));
  969. method = tor_malloc_zero(sizeof(SSL_METHOD));
  970. method->ssl_pending = fixed_ssl_pending;
  971. tls->ssl->method = method;
  972. fixed_ssl_pending_result = 42;
  973. ret = tor_tls_get_pending_bytes(tls);
  974. tt_int_op(ret, OP_EQ, 42);
  975. done:
  976. tor_free(method);
  977. tor_free(tls->ssl);
  978. tor_free(tls);
  979. }
  980. #endif
  981. static void
  982. test_tortls_get_forced_write_size(void *ignored)
  983. {
  984. (void)ignored;
  985. long ret;
  986. tor_tls_t *tls;
  987. tls = tor_malloc_zero(sizeof(tor_tls_t));
  988. tls->wantwrite_n = 43;
  989. ret = tor_tls_get_forced_write_size(tls);
  990. tt_int_op(ret, OP_EQ, 43);
  991. done:
  992. tor_free(tls);
  993. }
  994. extern uint64_t total_bytes_written_over_tls;
  995. extern uint64_t total_bytes_written_by_tls;
  996. static void
  997. test_tortls_get_write_overhead_ratio(void *ignored)
  998. {
  999. (void)ignored;
  1000. double ret;
  1001. total_bytes_written_over_tls = 0;
  1002. ret = tls_get_write_overhead_ratio();
  1003. tt_int_op(ret, OP_EQ, 1.0);
  1004. total_bytes_written_by_tls = 10;
  1005. total_bytes_written_over_tls = 1;
  1006. ret = tls_get_write_overhead_ratio();
  1007. tt_int_op(ret, OP_EQ, 10.0);
  1008. total_bytes_written_by_tls = 10;
  1009. total_bytes_written_over_tls = 2;
  1010. ret = tls_get_write_overhead_ratio();
  1011. tt_int_op(ret, OP_EQ, 5.0);
  1012. done:
  1013. (void)0;
  1014. }
  1015. static void
  1016. test_tortls_used_v1_handshake(void *ignored)
  1017. {
  1018. (void)ignored;
  1019. int ret;
  1020. tor_tls_t *tls;
  1021. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1022. // These tests assume both V2 handshake server and client are enabled
  1023. tls->wasV2Handshake = 0;
  1024. ret = tor_tls_used_v1_handshake(tls);
  1025. tt_int_op(ret, OP_EQ, 1);
  1026. tls->wasV2Handshake = 1;
  1027. ret = tor_tls_used_v1_handshake(tls);
  1028. tt_int_op(ret, OP_EQ, 0);
  1029. done:
  1030. tor_free(tls);
  1031. }
  1032. static void
  1033. test_tortls_get_num_server_handshakes(void *ignored)
  1034. {
  1035. (void)ignored;
  1036. int ret;
  1037. tor_tls_t *tls;
  1038. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1039. tls->server_handshake_count = 3;
  1040. ret = tor_tls_get_num_server_handshakes(tls);
  1041. tt_int_op(ret, OP_EQ, 3);
  1042. done:
  1043. tor_free(tls);
  1044. }
  1045. static void
  1046. test_tortls_server_got_renegotiate(void *ignored)
  1047. {
  1048. (void)ignored;
  1049. int ret;
  1050. tor_tls_t *tls;
  1051. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1052. tls->got_renegotiate = 1;
  1053. ret = tor_tls_server_got_renegotiate(tls);
  1054. tt_int_op(ret, OP_EQ, 1);
  1055. done:
  1056. tor_free(tls);
  1057. }
  1058. #ifndef OPENSSL_OPAQUE
  1059. static void
  1060. test_tortls_SSL_SESSION_get_master_key(void *ignored)
  1061. {
  1062. (void)ignored;
  1063. size_t ret;
  1064. tor_tls_t *tls;
  1065. uint8_t *out;
  1066. out = tor_malloc_zero(1);
  1067. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1068. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1069. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  1070. tls->ssl->session->master_key_length = 1;
  1071. #ifndef HAVE_SSL_SESSION_GET_MASTER_KEY
  1072. tls->ssl->session->master_key[0] = 43;
  1073. ret = SSL_SESSION_get_master_key(tls->ssl->session, out, 0);
  1074. tt_int_op(ret, OP_EQ, 1);
  1075. tt_int_op(out[0], OP_EQ, 0);
  1076. ret = SSL_SESSION_get_master_key(tls->ssl->session, out, 1);
  1077. tt_int_op(ret, OP_EQ, 1);
  1078. tt_int_op(out[0], OP_EQ, 43);
  1079. done:
  1080. #endif
  1081. tor_free(tls->ssl->session);
  1082. tor_free(tls->ssl);
  1083. tor_free(tls);
  1084. tor_free(out);
  1085. }
  1086. #endif
  1087. #ifndef OPENSSL_OPAQUE
  1088. static void
  1089. test_tortls_get_tlssecrets(void *ignored)
  1090. {
  1091. (void)ignored;
  1092. int ret;
  1093. uint8_t *secret_out = tor_malloc_zero(DIGEST256_LEN);;
  1094. tor_tls_t *tls;
  1095. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1096. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1097. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  1098. tls->ssl->session->master_key_length = 1;
  1099. tls->ssl->s3 = tor_malloc_zero(sizeof(SSL3_STATE));
  1100. ret = tor_tls_get_tlssecrets(tls, secret_out);
  1101. tt_int_op(ret, OP_EQ, 0);
  1102. done:
  1103. tor_free(secret_out);
  1104. tor_free(tls->ssl->s3);
  1105. tor_free(tls->ssl->session);
  1106. tor_free(tls->ssl);
  1107. tor_free(tls);
  1108. }
  1109. #endif
  1110. #ifndef OPENSSL_OPAQUE
  1111. static void
  1112. test_tortls_get_buffer_sizes(void *ignored)
  1113. {
  1114. (void)ignored;
  1115. int ret;
  1116. tor_tls_t *tls;
  1117. size_t rbuf_c=-1, rbuf_b=-1, wbuf_c=-1, wbuf_b=-1;
  1118. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1119. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1120. tls->ssl->s3 = tor_malloc_zero(sizeof(SSL3_STATE));
  1121. tls->ssl->s3->rbuf.buf = NULL;
  1122. tls->ssl->s3->rbuf.len = 1;
  1123. tls->ssl->s3->rbuf.offset = 0;
  1124. tls->ssl->s3->rbuf.left = 42;
  1125. tls->ssl->s3->wbuf.buf = NULL;
  1126. tls->ssl->s3->wbuf.len = 2;
  1127. tls->ssl->s3->wbuf.offset = 0;
  1128. tls->ssl->s3->wbuf.left = 43;
  1129. ret = tor_tls_get_buffer_sizes(tls, &rbuf_c, &rbuf_b, &wbuf_c, &wbuf_b);
  1130. #if OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,1,0)
  1131. tt_int_op(ret, OP_EQ, -1);
  1132. #else
  1133. tt_int_op(ret, OP_EQ, 0);
  1134. tt_int_op(rbuf_c, OP_EQ, 0);
  1135. tt_int_op(wbuf_c, OP_EQ, 0);
  1136. tt_int_op(rbuf_b, OP_EQ, 42);
  1137. tt_int_op(wbuf_b, OP_EQ, 43);
  1138. tls->ssl->s3->rbuf.buf = tor_malloc_zero(1);
  1139. tls->ssl->s3->wbuf.buf = tor_malloc_zero(1);
  1140. ret = tor_tls_get_buffer_sizes(tls, &rbuf_c, &rbuf_b, &wbuf_c, &wbuf_b);
  1141. tt_int_op(ret, OP_EQ, 0);
  1142. tt_int_op(rbuf_c, OP_EQ, 1);
  1143. tt_int_op(wbuf_c, OP_EQ, 2);
  1144. #endif
  1145. done:
  1146. tor_free(tls->ssl->s3->rbuf.buf);
  1147. tor_free(tls->ssl->s3->wbuf.buf);
  1148. tor_free(tls->ssl->s3);
  1149. tor_free(tls->ssl);
  1150. tor_free(tls);
  1151. }
  1152. #endif
  1153. static void
  1154. test_tortls_evaluate_ecgroup_for_tls(void *ignored)
  1155. {
  1156. (void)ignored;
  1157. int ret;
  1158. ret = evaluate_ecgroup_for_tls(NULL);
  1159. tt_int_op(ret, OP_EQ, 1);
  1160. ret = evaluate_ecgroup_for_tls("foobar");
  1161. tt_int_op(ret, OP_EQ, 0);
  1162. ret = evaluate_ecgroup_for_tls("P256");
  1163. tt_int_op(ret, OP_EQ, 1);
  1164. ret = evaluate_ecgroup_for_tls("P224");
  1165. // tt_int_op(ret, OP_EQ, 1); This varies between machines
  1166. done:
  1167. (void)0;
  1168. }
  1169. #ifndef OPENSSL_OPAQUE
  1170. typedef struct cert_pkey_st_local
  1171. {
  1172. X509 *x509;
  1173. EVP_PKEY *privatekey;
  1174. const EVP_MD *digest;
  1175. } CERT_PKEY_local;
  1176. typedef struct sess_cert_st_local
  1177. {
  1178. STACK_OF(X509) *cert_chain;
  1179. int peer_cert_type;
  1180. CERT_PKEY_local *peer_key;
  1181. CERT_PKEY_local peer_pkeys[8];
  1182. int references;
  1183. } SESS_CERT_local;
  1184. static void
  1185. test_tortls_try_to_extract_certs_from_tls(void *ignored)
  1186. {
  1187. (void)ignored;
  1188. tor_tls_t *tls;
  1189. X509 *cert = NULL, *id_cert = NULL, *c1 = NULL, *c2 = NULL;
  1190. SESS_CERT_local *sess = NULL;
  1191. c1 = read_cert_from(validCertString);
  1192. c2 = read_cert_from(caCertString);
  1193. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1194. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1195. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  1196. sess = tor_malloc_zero(sizeof(SESS_CERT_local));
  1197. tls->ssl->session->sess_cert = (void *)sess;
  1198. try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
  1199. tt_assert(!cert);
  1200. tt_assert(!id_cert);
  1201. tls->ssl->session->peer = c1;
  1202. try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
  1203. tt_assert(cert == c1);
  1204. tt_assert(!id_cert);
  1205. X509_free(cert); /* decrease refcnt */
  1206. sess->cert_chain = sk_X509_new_null();
  1207. try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
  1208. tt_assert(cert == c1);
  1209. tt_assert(!id_cert);
  1210. X509_free(cert); /* decrease refcnt */
  1211. sk_X509_push(sess->cert_chain, c1);
  1212. sk_X509_push(sess->cert_chain, c2);
  1213. try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
  1214. tt_assert(cert == c1);
  1215. tt_assert(id_cert);
  1216. X509_free(cert); /* decrease refcnt */
  1217. done:
  1218. sk_X509_free(sess->cert_chain);
  1219. tor_free(sess);
  1220. tor_free(tls->ssl->session);
  1221. tor_free(tls->ssl);
  1222. tor_free(tls);
  1223. X509_free(c1);
  1224. X509_free(c2);
  1225. }
  1226. #endif
  1227. #ifndef OPENSSL_OPAQUE
  1228. static void
  1229. test_tortls_get_peer_cert(void *ignored)
  1230. {
  1231. (void)ignored;
  1232. tor_x509_cert_t *ret;
  1233. tor_tls_t *tls;
  1234. X509 *cert = NULL;
  1235. cert = read_cert_from(validCertString);
  1236. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1237. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1238. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  1239. ret = tor_tls_get_peer_cert(tls);
  1240. tt_assert(!ret);
  1241. tls->ssl->session->peer = cert;
  1242. ret = tor_tls_get_peer_cert(tls);
  1243. tt_assert(ret);
  1244. tt_assert(ret->cert == cert);
  1245. done:
  1246. tor_x509_cert_free(ret);
  1247. tor_free(tls->ssl->session);
  1248. tor_free(tls->ssl);
  1249. tor_free(tls);
  1250. X509_free(cert);
  1251. }
  1252. #endif
  1253. #ifndef OPENSSL_OPAQUE
  1254. static void
  1255. test_tortls_peer_has_cert(void *ignored)
  1256. {
  1257. (void)ignored;
  1258. int ret;
  1259. tor_tls_t *tls;
  1260. X509 *cert = NULL;
  1261. cert = read_cert_from(validCertString);
  1262. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1263. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1264. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  1265. ret = tor_tls_peer_has_cert(tls);
  1266. tt_assert(!ret);
  1267. tls->ssl->session->peer = cert;
  1268. ret = tor_tls_peer_has_cert(tls);
  1269. tt_assert(ret);
  1270. done:
  1271. tor_free(tls->ssl->session);
  1272. tor_free(tls->ssl);
  1273. tor_free(tls);
  1274. X509_free(cert);
  1275. }
  1276. #endif
  1277. static void
  1278. test_tortls_is_server(void *ignored)
  1279. {
  1280. (void)ignored;
  1281. tor_tls_t *tls;
  1282. int ret;
  1283. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1284. tls->isServer = 1;
  1285. ret = tor_tls_is_server(tls);
  1286. tt_int_op(ret, OP_EQ, 1);
  1287. done:
  1288. tor_free(tls);
  1289. }
  1290. #ifndef OPENSSL_OPAQUE
  1291. static void
  1292. test_tortls_session_secret_cb(void *ignored)
  1293. {
  1294. (void)ignored;
  1295. tor_tls_t *tls;
  1296. SSL_CTX *ctx;
  1297. STACK_OF(SSL_CIPHER) *ciphers = NULL;
  1298. SSL_CIPHER *one;
  1299. SSL_library_init();
  1300. SSL_load_error_strings();
  1301. tor_tls_allocate_tor_tls_object_ex_data_index();
  1302. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1303. tls->magic = TOR_TLS_MAGIC;
  1304. ctx = SSL_CTX_new(TLSv1_method());
  1305. tls->ssl = SSL_new(ctx);
  1306. SSL_set_ex_data(tls->ssl, tor_tls_object_ex_data_index, tls);
  1307. SSL_set_session_secret_cb(tls->ssl, tor_tls_session_secret_cb, NULL);
  1308. tor_tls_session_secret_cb(tls->ssl, NULL, NULL, NULL, NULL, NULL);
  1309. tt_assert(!tls->ssl->tls_session_secret_cb);
  1310. one = get_cipher_by_name("ECDH-RSA-AES256-GCM-SHA384");
  1311. one->id = 0x00ff;
  1312. ciphers = sk_SSL_CIPHER_new_null();
  1313. sk_SSL_CIPHER_push(ciphers, one);
  1314. tls->client_cipher_list_type = 0;
  1315. tor_tls_session_secret_cb(tls->ssl, NULL, NULL, ciphers, NULL, NULL);
  1316. tt_assert(!tls->ssl->tls_session_secret_cb);
  1317. done:
  1318. sk_SSL_CIPHER_free(ciphers);
  1319. SSL_free(tls->ssl);
  1320. SSL_CTX_free(ctx);
  1321. tor_free(tls);
  1322. }
  1323. #endif
  1324. #ifndef OPENSSL_OPAQUE
  1325. /* TODO: It seems block_renegotiation and unblock_renegotiation and
  1326. * using different blags. This might not be correct */
  1327. static void
  1328. test_tortls_block_renegotiation(void *ignored)
  1329. {
  1330. (void)ignored;
  1331. tor_tls_t *tls;
  1332. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1333. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1334. tls->ssl->s3 = tor_malloc_zero(sizeof(SSL3_STATE));
  1335. #ifndef SUPPORT_UNSAFE_RENEGOTIATION_FLAG
  1336. #define SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION 0
  1337. #endif
  1338. tls->ssl->s3->flags = SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
  1339. tor_tls_block_renegotiation(tls);
  1340. #ifndef OPENSSL_1_1_API
  1341. tt_assert(!(tls->ssl->s3->flags &
  1342. SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION));
  1343. #endif
  1344. done:
  1345. tor_free(tls->ssl->s3);
  1346. tor_free(tls->ssl);
  1347. tor_free(tls);
  1348. }
  1349. static void
  1350. test_tortls_unblock_renegotiation(void *ignored)
  1351. {
  1352. (void)ignored;
  1353. tor_tls_t *tls;
  1354. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1355. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1356. tor_tls_unblock_renegotiation(tls);
  1357. tt_uint_op(SSL_get_options(tls->ssl) &
  1358. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, OP_EQ,
  1359. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
  1360. done:
  1361. tor_free(tls->ssl);
  1362. tor_free(tls);
  1363. }
  1364. #endif
  1365. #ifndef OPENSSL_OPAQUE
  1366. static void
  1367. test_tortls_assert_renegotiation_unblocked(void *ignored)
  1368. {
  1369. (void)ignored;
  1370. tor_tls_t *tls;
  1371. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1372. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1373. tor_tls_unblock_renegotiation(tls);
  1374. tor_tls_assert_renegotiation_unblocked(tls);
  1375. /* No assertion here - this test will fail if tor_assert is turned on
  1376. * and things are bad. */
  1377. tor_free(tls->ssl);
  1378. tor_free(tls);
  1379. }
  1380. #endif
  1381. static void
  1382. test_tortls_set_logged_address(void *ignored)
  1383. {
  1384. (void)ignored;
  1385. tor_tls_t *tls;
  1386. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1387. tor_tls_set_logged_address(tls, "foo bar");
  1388. tt_str_op(tls->address, OP_EQ, "foo bar");
  1389. tor_tls_set_logged_address(tls, "foo bar 2");
  1390. tt_str_op(tls->address, OP_EQ, "foo bar 2");
  1391. done:
  1392. tor_free(tls->address);
  1393. tor_free(tls);
  1394. }
  1395. #ifndef OPENSSL_OPAQUE
  1396. static void
  1397. example_cb(tor_tls_t *t, void *arg)
  1398. {
  1399. (void)t;
  1400. (void)arg;
  1401. }
  1402. static void
  1403. test_tortls_set_renegotiate_callback(void *ignored)
  1404. {
  1405. (void)ignored;
  1406. tor_tls_t *tls;
  1407. const char *arg = "hello";
  1408. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1409. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1410. tor_tls_set_renegotiate_callback(tls, example_cb, (void*)arg);
  1411. tt_assert(tls->negotiated_callback == example_cb);
  1412. tt_assert(tls->callback_arg == arg);
  1413. tt_assert(!tls->got_renegotiate);
  1414. /* Assumes V2_HANDSHAKE_SERVER */
  1415. tt_assert(tls->ssl->info_callback == tor_tls_server_info_callback);
  1416. tor_tls_set_renegotiate_callback(tls, NULL, (void*)arg);
  1417. tt_assert(tls->ssl->info_callback == tor_tls_debug_state_callback);
  1418. done:
  1419. tor_free(tls->ssl);
  1420. tor_free(tls);
  1421. }
  1422. #endif
  1423. #ifndef OPENSSL_OPAQUE
  1424. static SSL_CIPHER *fixed_cipher1 = NULL;
  1425. static SSL_CIPHER *fixed_cipher2 = NULL;
  1426. static const SSL_CIPHER *
  1427. fake_get_cipher(unsigned ncipher)
  1428. {
  1429. switch (ncipher) {
  1430. case 1:
  1431. return fixed_cipher1;
  1432. case 2:
  1433. return fixed_cipher2;
  1434. default:
  1435. return NULL;
  1436. }
  1437. }
  1438. #endif
  1439. #ifndef OPENSSL_OPAQUE
  1440. static void
  1441. test_tortls_find_cipher_by_id(void *ignored)
  1442. {
  1443. (void)ignored;
  1444. int ret;
  1445. SSL *ssl;
  1446. SSL_CTX *ctx;
  1447. const SSL_METHOD *m = TLSv1_method();
  1448. SSL_METHOD *empty_method = tor_malloc_zero(sizeof(SSL_METHOD));
  1449. fixed_cipher1 = tor_malloc_zero(sizeof(SSL_CIPHER));
  1450. fixed_cipher2 = tor_malloc_zero(sizeof(SSL_CIPHER));
  1451. fixed_cipher2->id = 0xC00A;
  1452. SSL_library_init();
  1453. SSL_load_error_strings();
  1454. ctx = SSL_CTX_new(m);
  1455. ssl = SSL_new(ctx);
  1456. ret = find_cipher_by_id(ssl, NULL, 0xC00A);
  1457. tt_int_op(ret, OP_EQ, 1);
  1458. ret = find_cipher_by_id(ssl, m, 0xC00A);
  1459. tt_int_op(ret, OP_EQ, 1);
  1460. ret = find_cipher_by_id(ssl, m, 0xFFFF);
  1461. tt_int_op(ret, OP_EQ, 0);
  1462. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1463. tt_int_op(ret, OP_EQ, 1);
  1464. ret = find_cipher_by_id(ssl, empty_method, 0xFFFF);
  1465. #ifdef HAVE_SSL_CIPHER_FIND
  1466. tt_int_op(ret, OP_EQ, 0);
  1467. #else
  1468. tt_int_op(ret, OP_EQ, 1);
  1469. #endif
  1470. empty_method->get_cipher = fake_get_cipher;
  1471. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1472. tt_int_op(ret, OP_EQ, 1);
  1473. empty_method->get_cipher = m->get_cipher;
  1474. empty_method->num_ciphers = m->num_ciphers;
  1475. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1476. tt_int_op(ret, OP_EQ, 1);
  1477. empty_method->get_cipher = fake_get_cipher;
  1478. empty_method->num_ciphers = m->num_ciphers;
  1479. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1480. tt_int_op(ret, OP_EQ, 1);
  1481. empty_method->num_ciphers = fake_num_ciphers;
  1482. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1483. #ifdef HAVE_SSL_CIPHER_FIND
  1484. tt_int_op(ret, OP_EQ, 1);
  1485. #else
  1486. tt_int_op(ret, OP_EQ, 0);
  1487. #endif
  1488. done:
  1489. tor_free(empty_method);
  1490. SSL_free(ssl);
  1491. SSL_CTX_free(ctx);
  1492. tor_free(fixed_cipher1);
  1493. }
  1494. #endif
  1495. #ifndef OPENSSL_OPAQUE
  1496. static void
  1497. test_tortls_debug_state_callback(void *ignored)
  1498. {
  1499. (void)ignored;
  1500. SSL *ssl;
  1501. char *buf = tor_malloc_zero(1000);
  1502. int n;
  1503. int previous_log = setup_capture_of_logs(LOG_DEBUG);
  1504. ssl = tor_malloc_zero(sizeof(SSL));
  1505. tor_tls_debug_state_callback(ssl, 32, 45);
  1506. n = tor_snprintf(buf, 1000, "SSL %p is now in state unknown"
  1507. " state [type=32,val=45].\n", ssl);
  1508. /* tor's snprintf returns -1 on error */
  1509. tt_int_op(n, OP_NE, -1);
  1510. expect_log_msg(buf);
  1511. done:
  1512. teardown_capture_of_logs(previous_log);
  1513. tor_free(buf);
  1514. tor_free(ssl);
  1515. }
  1516. #endif
  1517. #ifndef OPENSSL_OPAQUE
  1518. static void
  1519. test_tortls_server_info_callback(void *ignored)
  1520. {
  1521. (void)ignored;
  1522. tor_tls_t *tls;
  1523. SSL_CTX *ctx;
  1524. SSL *ssl;
  1525. int previous_log = setup_capture_of_logs(LOG_WARN);
  1526. SSL_library_init();
  1527. SSL_load_error_strings();
  1528. ctx = SSL_CTX_new(TLSv1_method());
  1529. ssl = SSL_new(ctx);
  1530. tor_tls_allocate_tor_tls_object_ex_data_index();
  1531. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1532. tls->magic = TOR_TLS_MAGIC;
  1533. tls->ssl = ssl;
  1534. tor_tls_server_info_callback(NULL, 0, 0);
  1535. SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_A);
  1536. mock_clean_saved_logs();
  1537. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1538. expect_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
  1539. SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_B);
  1540. mock_clean_saved_logs();
  1541. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1542. expect_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
  1543. SSL_set_state(ssl, 99);
  1544. mock_clean_saved_logs();
  1545. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1546. expect_no_log_entry();
  1547. SSL_set_ex_data(tls->ssl, tor_tls_object_ex_data_index, tls);
  1548. SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_B);
  1549. tls->negotiated_callback = 0;
  1550. tls->server_handshake_count = 120;
  1551. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1552. tt_int_op(tls->server_handshake_count, OP_EQ, 121);
  1553. tls->server_handshake_count = 127;
  1554. tls->negotiated_callback = (void *)1;
  1555. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1556. tt_int_op(tls->server_handshake_count, OP_EQ, 127);
  1557. tt_int_op(tls->got_renegotiate, OP_EQ, 1);
  1558. tls->ssl->session = SSL_SESSION_new();
  1559. tls->wasV2Handshake = 0;
  1560. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1561. tt_int_op(tls->wasV2Handshake, OP_EQ, 0);
  1562. done:
  1563. teardown_capture_of_logs(previous_log);
  1564. SSL_free(ssl);
  1565. SSL_CTX_free(ctx);
  1566. tor_free(tls);
  1567. }
  1568. #endif
  1569. #ifndef OPENSSL_OPAQUE
  1570. static int fixed_ssl_read_result_index;
  1571. static int fixed_ssl_read_result[5];
  1572. static int fixed_ssl_shutdown_result;
  1573. static int
  1574. fixed_ssl_read(SSL *s, void *buf, int len)
  1575. {
  1576. (void)s;
  1577. (void)buf;
  1578. (void)len;
  1579. return fixed_ssl_read_result[fixed_ssl_read_result_index++];
  1580. }
  1581. static int
  1582. fixed_ssl_shutdown(SSL *s)
  1583. {
  1584. (void)s;
  1585. return fixed_ssl_shutdown_result;
  1586. }
  1587. #ifndef LIBRESSL_VERSION_NUMBER
  1588. static int fixed_ssl_state_to_set;
  1589. static tor_tls_t *fixed_tls;
  1590. static int
  1591. setting_version_ssl_shutdown(SSL *s)
  1592. {
  1593. s->version = SSL2_VERSION;
  1594. return fixed_ssl_shutdown_result;
  1595. }
  1596. static int
  1597. setting_version_and_state_ssl_shutdown(SSL *s)
  1598. {
  1599. fixed_tls->state = fixed_ssl_state_to_set;
  1600. s->version = SSL2_VERSION;
  1601. return fixed_ssl_shutdown_result;
  1602. }
  1603. #endif
  1604. static int
  1605. dummy_handshake_func(SSL *s)
  1606. {
  1607. (void)s;
  1608. return 1;
  1609. }
  1610. static void
  1611. test_tortls_shutdown(void *ignored)
  1612. {
  1613. (void)ignored;
  1614. int ret;
  1615. tor_tls_t *tls;
  1616. SSL_METHOD *method = give_me_a_test_method();
  1617. int previous_log = setup_capture_of_logs(LOG_WARN);
  1618. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1619. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1620. tls->ssl->method = method;
  1621. method->ssl_read = fixed_ssl_read;
  1622. method->ssl_shutdown = fixed_ssl_shutdown;
  1623. ret = tor_tls_shutdown(tls);
  1624. tt_int_op(ret, OP_EQ, -9);
  1625. tls->state = TOR_TLS_ST_SENTCLOSE;
  1626. fixed_ssl_read_result_index = 0;
  1627. fixed_ssl_read_result[0] = 10;
  1628. fixed_ssl_read_result[1] = -1;
  1629. ret = tor_tls_shutdown(tls);
  1630. tt_int_op(ret, OP_EQ, -9);
  1631. #ifndef LIBRESSL_VERSION_NUMBER
  1632. tls->ssl->handshake_func = dummy_handshake_func;
  1633. fixed_ssl_read_result_index = 0;
  1634. fixed_ssl_read_result[0] = 10;
  1635. fixed_ssl_read_result[1] = 42;
  1636. fixed_ssl_read_result[2] = 0;
  1637. fixed_ssl_shutdown_result = 1;
  1638. ERR_clear_error();
  1639. tls->ssl->version = SSL2_VERSION;
  1640. ret = tor_tls_shutdown(tls);
  1641. tt_int_op(ret, OP_EQ, TOR_TLS_DONE);
  1642. tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_CLOSED);
  1643. fixed_ssl_read_result_index = 0;
  1644. fixed_ssl_read_result[0] = 10;
  1645. fixed_ssl_read_result[1] = 42;
  1646. fixed_ssl_read_result[2] = 0;
  1647. fixed_ssl_shutdown_result = 0;
  1648. ERR_clear_error();
  1649. tls->ssl->version = 0;
  1650. ret = tor_tls_shutdown(tls);
  1651. tt_int_op(ret, OP_EQ, TOR_TLS_DONE);
  1652. tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_CLOSED);
  1653. fixed_ssl_read_result_index = 0;
  1654. fixed_ssl_read_result[0] = 10;
  1655. fixed_ssl_read_result[1] = 42;
  1656. fixed_ssl_read_result[2] = 0;
  1657. fixed_ssl_shutdown_result = 0;
  1658. ERR_clear_error();
  1659. tls->ssl->version = 0;
  1660. method->ssl_shutdown = setting_version_ssl_shutdown;
  1661. ret = tor_tls_shutdown(tls);
  1662. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1663. fixed_ssl_read_result_index = 0;
  1664. fixed_ssl_read_result[0] = 10;
  1665. fixed_ssl_read_result[1] = 42;
  1666. fixed_ssl_read_result[2] = 0;
  1667. fixed_ssl_shutdown_result = 0;
  1668. fixed_tls = tls;
  1669. fixed_ssl_state_to_set = TOR_TLS_ST_GOTCLOSE;
  1670. ERR_clear_error();
  1671. tls->ssl->version = 0;
  1672. method->ssl_shutdown = setting_version_and_state_ssl_shutdown;
  1673. ret = tor_tls_shutdown(tls);
  1674. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1675. fixed_ssl_read_result_index = 0;
  1676. fixed_ssl_read_result[0] = 10;
  1677. fixed_ssl_read_result[1] = 42;
  1678. fixed_ssl_read_result[2] = 0;
  1679. fixed_ssl_read_result[3] = -1;
  1680. fixed_ssl_shutdown_result = 0;
  1681. fixed_tls = tls;
  1682. fixed_ssl_state_to_set = 0;
  1683. ERR_clear_error();
  1684. tls->ssl->version = 0;
  1685. method->ssl_shutdown = setting_version_and_state_ssl_shutdown;
  1686. ret = tor_tls_shutdown(tls);
  1687. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1688. #endif
  1689. done:
  1690. teardown_capture_of_logs(previous_log);
  1691. tor_free(method);
  1692. tor_free(tls->ssl);
  1693. tor_free(tls);
  1694. }
  1695. static int negotiated_callback_called;
  1696. static void
  1697. negotiated_callback_setter(tor_tls_t *t, void *arg)
  1698. {
  1699. (void)t;
  1700. (void)arg;
  1701. negotiated_callback_called++;
  1702. }
  1703. static void
  1704. test_tortls_read(void *ignored)
  1705. {
  1706. (void)ignored;
  1707. int ret;
  1708. tor_tls_t *tls;
  1709. char buf[100];
  1710. SSL_METHOD *method = give_me_a_test_method();
  1711. int previous_log = setup_capture_of_logs(LOG_WARN);
  1712. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1713. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1714. tls->state = TOR_TLS_ST_OPEN;
  1715. ret = tor_tls_read(tls, buf, 10);
  1716. tt_int_op(ret, OP_EQ, -9);
  1717. /* These tests assume that V2_HANDSHAKE_SERVER is set */
  1718. tls->ssl->handshake_func = dummy_handshake_func;
  1719. tls->ssl->method = method;
  1720. method->ssl_read = fixed_ssl_read;
  1721. fixed_ssl_read_result_index = 0;
  1722. fixed_ssl_read_result[0] = 42;
  1723. tls->state = TOR_TLS_ST_OPEN;
  1724. ERR_clear_error();
  1725. ret = tor_tls_read(tls, buf, 10);
  1726. tt_int_op(ret, OP_EQ, 42);
  1727. tls->state = TOR_TLS_ST_OPEN;
  1728. tls->got_renegotiate = 1;
  1729. fixed_ssl_read_result_index = 0;
  1730. ERR_clear_error();
  1731. ret = tor_tls_read(tls, buf, 10);
  1732. tt_int_op(tls->got_renegotiate, OP_EQ, 0);
  1733. tls->state = TOR_TLS_ST_OPEN;
  1734. tls->got_renegotiate = 1;
  1735. negotiated_callback_called = 0;
  1736. tls->negotiated_callback = negotiated_callback_setter;
  1737. fixed_ssl_read_result_index = 0;
  1738. ERR_clear_error();
  1739. ret = tor_tls_read(tls, buf, 10);
  1740. tt_int_op(negotiated_callback_called, OP_EQ, 1);
  1741. #ifndef LIBRESSL_VERSION_NUMBER
  1742. fixed_ssl_read_result_index = 0;
  1743. fixed_ssl_read_result[0] = 0;
  1744. tls->ssl->version = SSL2_VERSION;
  1745. ERR_clear_error();
  1746. ret = tor_tls_read(tls, buf, 10);
  1747. tt_int_op(ret, OP_EQ, TOR_TLS_CLOSE);
  1748. tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_CLOSED);
  1749. #endif
  1750. // TODO: fill up
  1751. done:
  1752. teardown_capture_of_logs(previous_log);
  1753. tor_free(tls->ssl);
  1754. tor_free(tls);
  1755. tor_free(method);
  1756. }
  1757. static int fixed_ssl_write_result;
  1758. static int
  1759. fixed_ssl_write(SSL *s, const void *buf, int len)
  1760. {
  1761. (void)s;
  1762. (void)buf;
  1763. (void)len;
  1764. return fixed_ssl_write_result;
  1765. }
  1766. static void
  1767. test_tortls_write(void *ignored)
  1768. {
  1769. (void)ignored;
  1770. int ret;
  1771. tor_tls_t *tls;
  1772. SSL_METHOD *method = give_me_a_test_method();
  1773. char buf[100];
  1774. int previous_log = setup_capture_of_logs(LOG_WARN);
  1775. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1776. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1777. tls->state = TOR_TLS_ST_OPEN;
  1778. ret = tor_tls_write(tls, buf, 0);
  1779. tt_int_op(ret, OP_EQ, 0);
  1780. ret = tor_tls_write(tls, buf, 10);
  1781. tt_int_op(ret, OP_EQ, -9);
  1782. tls->ssl->method = method;
  1783. tls->wantwrite_n = 1;
  1784. ret = tor_tls_write(tls, buf, 10);
  1785. tt_int_op(tls->wantwrite_n, OP_EQ, 0);
  1786. method->ssl_write = fixed_ssl_write;
  1787. tls->ssl->handshake_func = dummy_handshake_func;
  1788. fixed_ssl_write_result = 1;
  1789. ERR_clear_error();
  1790. ret = tor_tls_write(tls, buf, 10);
  1791. tt_int_op(ret, OP_EQ, 1);
  1792. fixed_ssl_write_result = -1;
  1793. ERR_clear_error();
  1794. tls->ssl->rwstate = SSL_READING;
  1795. SSL_set_bio(tls->ssl, BIO_new(BIO_s_mem()), NULL);
  1796. SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_READ;
  1797. ret = tor_tls_write(tls, buf, 10);
  1798. tt_int_op(ret, OP_EQ, TOR_TLS_WANTREAD);
  1799. ERR_clear_error();
  1800. tls->ssl->rwstate = SSL_READING;
  1801. SSL_set_bio(tls->ssl, BIO_new(BIO_s_mem()), NULL);
  1802. SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_WRITE;
  1803. ret = tor_tls_write(tls, buf, 10);
  1804. tt_int_op(ret, OP_EQ, TOR_TLS_WANTWRITE);
  1805. done:
  1806. teardown_capture_of_logs(previous_log);
  1807. BIO_free(tls->ssl->rbio);
  1808. tor_free(tls->ssl);
  1809. tor_free(tls);
  1810. tor_free(method);
  1811. }
  1812. #endif
  1813. #ifndef OPENSSL_OPAQUE
  1814. static int fixed_ssl_accept_result;
  1815. static int fixed_ssl_connect_result;
  1816. static int
  1817. setting_error_ssl_accept(SSL *ssl)
  1818. {
  1819. (void)ssl;
  1820. ERR_put_error(ERR_LIB_BN, 2, -1, "somewhere.c", 99);
  1821. ERR_put_error(ERR_LIB_SYS, 2, -1, "somewhere.c", 99);
  1822. return fixed_ssl_accept_result;
  1823. }
  1824. static int
  1825. setting_error_ssl_connect(SSL *ssl)
  1826. {
  1827. (void)ssl;
  1828. ERR_put_error(ERR_LIB_BN, 2, -1, "somewhere.c", 99);
  1829. ERR_put_error(ERR_LIB_SYS, 2, -1, "somewhere.c", 99);
  1830. return fixed_ssl_connect_result;
  1831. }
  1832. static int
  1833. fixed_ssl_accept(SSL *ssl)
  1834. {
  1835. (void) ssl;
  1836. return fixed_ssl_accept_result;
  1837. }
  1838. static void
  1839. test_tortls_handshake(void *ignored)
  1840. {
  1841. (void)ignored;
  1842. int ret;
  1843. tor_tls_t *tls;
  1844. SSL_CTX *ctx;
  1845. SSL_METHOD *method = give_me_a_test_method();
  1846. int previous_log = setup_capture_of_logs(LOG_INFO);
  1847. SSL_library_init();
  1848. SSL_load_error_strings();
  1849. ctx = SSL_CTX_new(TLSv1_method());
  1850. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1851. tls->ssl = SSL_new(ctx);
  1852. tls->state = TOR_TLS_ST_HANDSHAKE;
  1853. ret = tor_tls_handshake(tls);
  1854. tt_int_op(ret, OP_EQ, -9);
  1855. tls->isServer = 1;
  1856. tls->state = TOR_TLS_ST_HANDSHAKE;
  1857. ret = tor_tls_handshake(tls);
  1858. tt_int_op(ret, OP_EQ, -9);
  1859. tls->ssl->method = method;
  1860. method->ssl_accept = fixed_ssl_accept;
  1861. fixed_ssl_accept_result = 2;
  1862. ERR_clear_error();
  1863. tls->state = TOR_TLS_ST_HANDSHAKE;
  1864. ret = tor_tls_handshake(tls);
  1865. tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_OPEN);
  1866. method->ssl_accept = setting_error_ssl_accept;
  1867. fixed_ssl_accept_result = 1;
  1868. ERR_clear_error();
  1869. mock_clean_saved_logs();
  1870. tls->state = TOR_TLS_ST_HANDSHAKE;
  1871. ret = tor_tls_handshake(tls);
  1872. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1873. expect_log_entry();
  1874. /* This fails on jessie. Investigate why! */
  1875. #if 0
  1876. expect_log_msg("TLS error while handshaking: (null) (in bignum routines:"
  1877. "(null):SSLv3 write client hello B)\n");
  1878. expect_log_msg("TLS error while handshaking: (null) (in system library:"
  1879. "connect:SSLv3 write client hello B)\n");
  1880. #endif
  1881. expect_log_severity(LOG_INFO);
  1882. tls->isServer = 0;
  1883. method->ssl_connect = setting_error_ssl_connect;
  1884. fixed_ssl_connect_result = 1;
  1885. ERR_clear_error();
  1886. mock_clean_saved_logs();
  1887. tls->state = TOR_TLS_ST_HANDSHAKE;
  1888. ret = tor_tls_handshake(tls);
  1889. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1890. expect_log_entry();
  1891. #if 0
  1892. /* See above */
  1893. expect_log_msg("TLS error while handshaking: "
  1894. "(null) (in bignum routines:(null):SSLv3 write client hello B)\n");
  1895. expect_log_msg("TLS error while handshaking: "
  1896. "(null) (in system library:connect:SSLv3 write client hello B)\n");
  1897. #endif
  1898. expect_log_severity(LOG_WARN);
  1899. done:
  1900. teardown_capture_of_logs(previous_log);
  1901. SSL_free(tls->ssl);
  1902. SSL_CTX_free(ctx);
  1903. tor_free(tls);
  1904. tor_free(method);
  1905. }
  1906. #endif
  1907. #ifndef OPENSSL_OPAQUE
  1908. static void
  1909. test_tortls_finish_handshake(void *ignored)
  1910. {
  1911. (void)ignored;
  1912. int ret;
  1913. tor_tls_t *tls;
  1914. SSL_CTX *ctx;
  1915. SSL_METHOD *method = give_me_a_test_method();
  1916. SSL_library_init();
  1917. SSL_load_error_strings();
  1918. X509 *c1 = read_cert_from(validCertString);
  1919. SESS_CERT_local *sess = NULL;
  1920. ctx = SSL_CTX_new(method);
  1921. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1922. tls->ssl = SSL_new(ctx);
  1923. tls->state = TOR_TLS_ST_OPEN;
  1924. ret = tor_tls_finish_handshake(tls);
  1925. tt_int_op(ret, OP_EQ, 0);
  1926. tls->isServer = 1;
  1927. tls->wasV2Handshake = 0;
  1928. ret = tor_tls_finish_handshake(tls);
  1929. tt_int_op(ret, OP_EQ, 0);
  1930. tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
  1931. tls->wasV2Handshake = 1;
  1932. ret = tor_tls_finish_handshake(tls);
  1933. tt_int_op(ret, OP_EQ, 0);
  1934. tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
  1935. tls->wasV2Handshake = 1;
  1936. tls->ssl->session = SSL_SESSION_new();
  1937. ret = tor_tls_finish_handshake(tls);
  1938. tt_int_op(ret, OP_EQ, 0);
  1939. tt_int_op(tls->wasV2Handshake, OP_EQ, 0);
  1940. tls->isServer = 0;
  1941. sess = tor_malloc_zero(sizeof(SESS_CERT_local));
  1942. tls->ssl->session->sess_cert = (void *)sess;
  1943. sess->cert_chain = sk_X509_new_null();
  1944. sk_X509_push(sess->cert_chain, c1);
  1945. tls->ssl->session->peer = c1;
  1946. tls->wasV2Handshake = 0;
  1947. ret = tor_tls_finish_handshake(tls);
  1948. tt_int_op(ret, OP_EQ, 0);
  1949. tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
  1950. method->num_ciphers = fake_num_ciphers;
  1951. ret = tor_tls_finish_handshake(tls);
  1952. tt_int_op(ret, OP_EQ, -9);
  1953. done:
  1954. if (sess)
  1955. sk_X509_free(sess->cert_chain);
  1956. if (tls->ssl && tls->ssl->session) {
  1957. tor_free(tls->ssl->session->sess_cert);
  1958. }
  1959. SSL_free(tls->ssl);
  1960. tor_free(tls);
  1961. SSL_CTX_free(ctx);
  1962. tor_free(method);
  1963. }
  1964. #endif
  1965. static int fixed_crypto_pk_new_result_index;
  1966. static crypto_pk_t *fixed_crypto_pk_new_result[5];
  1967. static crypto_pk_t *
  1968. fixed_crypto_pk_new(void)
  1969. {
  1970. return fixed_crypto_pk_new_result[fixed_crypto_pk_new_result_index++];
  1971. }
  1972. #ifndef OPENSSL_OPAQUE
  1973. static int fixed_crypto_pk_generate_key_with_bits_result_index;
  1974. static int fixed_crypto_pk_generate_key_with_bits_result[5];
  1975. static int fixed_tor_tls_create_certificate_result_index;
  1976. static X509 *fixed_tor_tls_create_certificate_result[5];
  1977. static int fixed_tor_x509_cert_new_result_index;
  1978. static tor_x509_cert_t *fixed_tor_x509_cert_new_result[5];
  1979. static int
  1980. fixed_crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits)
  1981. {
  1982. (void)env;
  1983. (void)bits;
  1984. return fixed_crypto_pk_generate_key_with_bits_result[
  1985. fixed_crypto_pk_generate_key_with_bits_result_index++];
  1986. }
  1987. static X509 *
  1988. fixed_tor_tls_create_certificate(crypto_pk_t *rsa,
  1989. crypto_pk_t *rsa_sign,
  1990. const char *cname,
  1991. const char *cname_sign,
  1992. unsigned int cert_lifetime)
  1993. {
  1994. (void)rsa;
  1995. (void)rsa_sign;
  1996. (void)cname;
  1997. (void)cname_sign;
  1998. (void)cert_lifetime;
  1999. return fixed_tor_tls_create_certificate_result[
  2000. fixed_tor_tls_create_certificate_result_index++];
  2001. }
  2002. static tor_x509_cert_t *
  2003. fixed_tor_x509_cert_new(X509 *x509_cert)
  2004. {
  2005. (void) x509_cert;
  2006. return fixed_tor_x509_cert_new_result[
  2007. fixed_tor_x509_cert_new_result_index++];
  2008. }
  2009. static void
  2010. test_tortls_context_new(void *ignored)
  2011. {
  2012. (void)ignored;
  2013. tor_tls_context_t *ret;
  2014. crypto_pk_t *pk1, *pk2, *pk3, *pk4, *pk5, *pk6, *pk7, *pk8, *pk9, *pk10,
  2015. *pk11, *pk12, *pk13, *pk14, *pk15, *pk16, *pk17, *pk18;
  2016. pk1 = crypto_pk_new();
  2017. pk2 = crypto_pk_new();
  2018. pk3 = crypto_pk_new();
  2019. pk4 = crypto_pk_new();
  2020. pk5 = crypto_pk_new();
  2021. pk6 = crypto_pk_new();
  2022. pk7 = crypto_pk_new();
  2023. pk8 = crypto_pk_new();
  2024. pk9 = crypto_pk_new();
  2025. pk10 = crypto_pk_new();
  2026. pk11 = crypto_pk_new();
  2027. pk12 = crypto_pk_new();
  2028. pk13 = crypto_pk_new();
  2029. pk14 = crypto_pk_new();
  2030. pk15 = crypto_pk_new();
  2031. pk16 = crypto_pk_new();
  2032. pk17 = crypto_pk_new();
  2033. pk18 = crypto_pk_new();
  2034. fixed_crypto_pk_new_result_index = 0;
  2035. fixed_crypto_pk_new_result[0] = NULL;
  2036. MOCK(crypto_pk_new, fixed_crypto_pk_new);
  2037. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2038. tt_assert(!ret);
  2039. MOCK(crypto_pk_generate_key_with_bits,
  2040. fixed_crypto_pk_generate_key_with_bits);
  2041. fixed_crypto_pk_new_result_index = 0;
  2042. fixed_crypto_pk_new_result[0] = pk1;
  2043. fixed_crypto_pk_new_result[1] = NULL;
  2044. fixed_crypto_pk_generate_key_with_bits_result[0] = -1;
  2045. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2046. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2047. tt_assert(!ret);
  2048. fixed_crypto_pk_new_result_index = 0;
  2049. fixed_crypto_pk_new_result[0] = pk2;
  2050. fixed_crypto_pk_new_result[1] = NULL;
  2051. fixed_crypto_pk_generate_key_with_bits_result[0] = 0;
  2052. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2053. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2054. tt_assert(!ret);
  2055. fixed_crypto_pk_new_result_index = 0;
  2056. fixed_crypto_pk_new_result[0] = pk3;
  2057. fixed_crypto_pk_new_result[1] = pk4;
  2058. fixed_crypto_pk_new_result[2] = NULL;
  2059. fixed_crypto_pk_generate_key_with_bits_result[0] = 0;
  2060. fixed_crypto_pk_generate_key_with_bits_result[1] = -1;
  2061. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2062. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2063. tt_assert(!ret);
  2064. MOCK(tor_tls_create_certificate, fixed_tor_tls_create_certificate);
  2065. fixed_crypto_pk_new_result_index = 0;
  2066. fixed_crypto_pk_new_result[0] = pk5;
  2067. fixed_crypto_pk_new_result[1] = pk6;
  2068. fixed_crypto_pk_new_result[2] = NULL;
  2069. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2070. fixed_crypto_pk_generate_key_with_bits_result[1] = 0;
  2071. fixed_tor_tls_create_certificate_result_index = 0;
  2072. fixed_tor_tls_create_certificate_result[0] = NULL;
  2073. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2074. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2075. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2076. tt_assert(!ret);
  2077. fixed_crypto_pk_new_result_index = 0;
  2078. fixed_crypto_pk_new_result[0] = pk7;
  2079. fixed_crypto_pk_new_result[1] = pk8;
  2080. fixed_crypto_pk_new_result[2] = NULL;
  2081. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2082. fixed_tor_tls_create_certificate_result_index = 0;
  2083. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2084. fixed_tor_tls_create_certificate_result[1] = NULL;
  2085. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2086. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2087. tt_assert(!ret);
  2088. fixed_crypto_pk_new_result_index = 0;
  2089. fixed_crypto_pk_new_result[0] = pk9;
  2090. fixed_crypto_pk_new_result[1] = pk10;
  2091. fixed_crypto_pk_new_result[2] = NULL;
  2092. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2093. fixed_tor_tls_create_certificate_result_index = 0;
  2094. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2095. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2096. fixed_tor_tls_create_certificate_result[2] = NULL;
  2097. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2098. tt_assert(!ret);
  2099. MOCK(tor_x509_cert_new, fixed_tor_x509_cert_new);
  2100. fixed_crypto_pk_new_result_index = 0;
  2101. fixed_crypto_pk_new_result[0] = pk11;
  2102. fixed_crypto_pk_new_result[1] = pk12;
  2103. fixed_crypto_pk_new_result[2] = NULL;
  2104. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2105. fixed_tor_tls_create_certificate_result_index = 0;
  2106. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2107. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2108. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2109. fixed_tor_x509_cert_new_result_index = 0;
  2110. fixed_tor_x509_cert_new_result[0] = NULL;
  2111. fixed_tor_x509_cert_new_result[1] = NULL;
  2112. fixed_tor_x509_cert_new_result[2] = NULL;
  2113. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2114. tt_assert(!ret);
  2115. fixed_crypto_pk_new_result_index = 0;
  2116. fixed_crypto_pk_new_result[0] = pk13;
  2117. fixed_crypto_pk_new_result[1] = pk14;
  2118. fixed_crypto_pk_new_result[2] = NULL;
  2119. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2120. fixed_tor_tls_create_certificate_result_index = 0;
  2121. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2122. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2123. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2124. fixed_tor_x509_cert_new_result_index = 0;
  2125. fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2126. fixed_tor_x509_cert_new_result[1] = NULL;
  2127. fixed_tor_x509_cert_new_result[2] = NULL;
  2128. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2129. tt_assert(!ret);
  2130. fixed_crypto_pk_new_result_index = 0;
  2131. fixed_crypto_pk_new_result[0] = pk15;
  2132. fixed_crypto_pk_new_result[1] = pk16;
  2133. fixed_crypto_pk_new_result[2] = NULL;
  2134. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2135. fixed_tor_tls_create_certificate_result_index = 0;
  2136. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2137. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2138. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2139. fixed_tor_x509_cert_new_result_index = 0;
  2140. fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2141. fixed_tor_x509_cert_new_result[1] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2142. fixed_tor_x509_cert_new_result[2] = NULL;
  2143. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2144. tt_assert(!ret);
  2145. fixed_crypto_pk_new_result_index = 0;
  2146. fixed_crypto_pk_new_result[0] = pk17;
  2147. fixed_crypto_pk_new_result[1] = pk18;
  2148. fixed_crypto_pk_new_result[2] = NULL;
  2149. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2150. fixed_tor_tls_create_certificate_result_index = 0;
  2151. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2152. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2153. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2154. fixed_tor_x509_cert_new_result_index = 0;
  2155. fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2156. fixed_tor_x509_cert_new_result[1] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2157. fixed_tor_x509_cert_new_result[2] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2158. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2159. tt_assert(!ret);
  2160. done:
  2161. UNMOCK(tor_x509_cert_new);
  2162. UNMOCK(tor_tls_create_certificate);
  2163. UNMOCK(crypto_pk_generate_key_with_bits);
  2164. UNMOCK(crypto_pk_new);
  2165. }
  2166. #endif
  2167. static int fixed_crypto_pk_get_evp_pkey_result_index = 0;
  2168. static EVP_PKEY *fixed_crypto_pk_get_evp_pkey_result[5];
  2169. static EVP_PKEY *
  2170. fixed_crypto_pk_get_evp_pkey_(crypto_pk_t *env, int private)
  2171. {
  2172. (void) env;
  2173. (void) private;
  2174. return fixed_crypto_pk_get_evp_pkey_result[
  2175. fixed_crypto_pk_get_evp_pkey_result_index++];
  2176. }
  2177. static void
  2178. test_tortls_create_certificate(void *ignored)
  2179. {
  2180. (void)ignored;
  2181. X509 *ret;
  2182. crypto_pk_t *pk1, *pk2;
  2183. pk1 = crypto_pk_new();
  2184. pk2 = crypto_pk_new();
  2185. MOCK(crypto_pk_get_evp_pkey_, fixed_crypto_pk_get_evp_pkey_);
  2186. fixed_crypto_pk_get_evp_pkey_result_index = 0;
  2187. fixed_crypto_pk_get_evp_pkey_result[0] = NULL;
  2188. ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
  2189. tt_assert(!ret);
  2190. fixed_crypto_pk_get_evp_pkey_result_index = 0;
  2191. fixed_crypto_pk_get_evp_pkey_result[0] = EVP_PKEY_new();
  2192. fixed_crypto_pk_get_evp_pkey_result[1] = NULL;
  2193. ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
  2194. tt_assert(!ret);
  2195. fixed_crypto_pk_get_evp_pkey_result_index = 0;
  2196. fixed_crypto_pk_get_evp_pkey_result[0] = EVP_PKEY_new();
  2197. fixed_crypto_pk_get_evp_pkey_result[1] = EVP_PKEY_new();
  2198. ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
  2199. tt_assert(!ret);
  2200. done:
  2201. UNMOCK(crypto_pk_get_evp_pkey_);
  2202. crypto_pk_free(pk1);
  2203. crypto_pk_free(pk2);
  2204. }
  2205. static void
  2206. test_tortls_cert_new(void *ignored)
  2207. {
  2208. (void)ignored;
  2209. tor_x509_cert_t *ret;
  2210. X509 *cert = read_cert_from(validCertString);
  2211. ret = tor_x509_cert_new(NULL);
  2212. tt_assert(!ret);
  2213. ret = tor_x509_cert_new(cert);
  2214. tt_assert(ret);
  2215. tor_x509_cert_free(ret);
  2216. ret = NULL;
  2217. #if 0
  2218. cert = read_cert_from(validCertString);
  2219. /* XXX this doesn't do what you think: it alters a copy of the pubkey. */
  2220. X509_get_pubkey(cert)->type = EVP_PKEY_DSA;
  2221. ret = tor_x509_cert_new(cert);
  2222. tt_assert(ret);
  2223. #endif
  2224. #ifndef OPENSSL_OPAQUE
  2225. cert = read_cert_from(validCertString);
  2226. X509_CINF_free(cert->cert_info);
  2227. cert->cert_info = NULL;
  2228. ret = tor_x509_cert_new(cert);
  2229. tt_assert(ret);
  2230. #endif
  2231. done:
  2232. tor_x509_cert_free(ret);
  2233. }
  2234. static void
  2235. test_tortls_cert_is_valid(void *ignored)
  2236. {
  2237. (void)ignored;
  2238. int ret;
  2239. tor_x509_cert_t *cert = NULL, *scert = NULL;
  2240. scert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2241. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2242. tt_int_op(ret, OP_EQ, 0);
  2243. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2244. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2245. tt_int_op(ret, OP_EQ, 0);
  2246. tor_free(scert);
  2247. tor_free(cert);
  2248. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2249. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2250. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2251. tt_int_op(ret, OP_EQ, 1);
  2252. #ifndef OPENSSL_OPAQUE
  2253. tor_x509_cert_free(cert);
  2254. tor_x509_cert_free(scert);
  2255. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2256. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2257. ASN1_TIME_free(cert->cert->cert_info->validity->notAfter);
  2258. cert->cert->cert_info->validity->notAfter =
  2259. ASN1_TIME_set(NULL, time(NULL)-1000000);
  2260. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2261. tt_int_op(ret, OP_EQ, 0);
  2262. tor_x509_cert_free(cert);
  2263. tor_x509_cert_free(scert);
  2264. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2265. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2266. X509_PUBKEY_free(cert->cert->cert_info->key);
  2267. cert->cert->cert_info->key = NULL;
  2268. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 1);
  2269. tt_int_op(ret, OP_EQ, 0);
  2270. #endif
  2271. #if 0
  2272. tor_x509_cert_free(cert);
  2273. tor_x509_cert_free(scert);
  2274. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2275. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2276. /* This doesn't actually change the key in the cert. XXXXXX */
  2277. BN_one(EVP_PKEY_get1_RSA(X509_get_pubkey(cert->cert))->n);
  2278. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 1);
  2279. tt_int_op(ret, OP_EQ, 0);
  2280. tor_x509_cert_free(cert);
  2281. tor_x509_cert_free(scert);
  2282. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2283. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2284. /* This doesn't actually change the key in the cert. XXXXXX */
  2285. X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
  2286. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 1);
  2287. tt_int_op(ret, OP_EQ, 0);
  2288. tor_x509_cert_free(cert);
  2289. tor_x509_cert_free(scert);
  2290. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2291. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2292. /* This doesn't actually change the key in the cert. XXXXXX */
  2293. X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
  2294. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2295. tt_int_op(ret, OP_EQ, 1);
  2296. tor_x509_cert_free(cert);
  2297. tor_x509_cert_free(scert);
  2298. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2299. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2300. /* This doesn't actually change the key in the cert. XXXXXX */
  2301. X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
  2302. X509_get_pubkey(cert->cert)->ameth = NULL;
  2303. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2304. tt_int_op(ret, OP_EQ, 0);
  2305. #endif
  2306. done:
  2307. tor_x509_cert_free(cert);
  2308. tor_x509_cert_free(scert);
  2309. }
  2310. static void
  2311. test_tortls_context_init_one(void *ignored)
  2312. {
  2313. (void)ignored;
  2314. int ret;
  2315. tor_tls_context_t *old = NULL;
  2316. MOCK(crypto_pk_new, fixed_crypto_pk_new);
  2317. fixed_crypto_pk_new_result_index = 0;
  2318. fixed_crypto_pk_new_result[0] = NULL;
  2319. ret = tor_tls_context_init_one(&old, NULL, 0, 0, 0);
  2320. tt_int_op(ret, OP_EQ, -1);
  2321. done:
  2322. UNMOCK(crypto_pk_new);
  2323. }
  2324. #define LOCAL_TEST_CASE(name, flags) \
  2325. { #name, test_tortls_##name, (flags|TT_FORK), NULL, NULL }
  2326. #ifdef OPENSSL_OPAQUE
  2327. #define INTRUSIVE_TEST_CASE(name, flags) \
  2328. { #name, NULL, TT_SKIP, NULL, NULL }
  2329. #else
  2330. #define INTRUSIVE_TEST_CASE(name, flags) LOCAL_TEST_CASE(name, flags)
  2331. #endif
  2332. struct testcase_t tortls_tests[] = {
  2333. LOCAL_TEST_CASE(errno_to_tls_error, 0),
  2334. LOCAL_TEST_CASE(err_to_string, 0),
  2335. LOCAL_TEST_CASE(tor_tls_new, TT_FORK),
  2336. LOCAL_TEST_CASE(tor_tls_get_error, 0),
  2337. LOCAL_TEST_CASE(get_state_description, TT_FORK),
  2338. LOCAL_TEST_CASE(get_by_ssl, TT_FORK),
  2339. LOCAL_TEST_CASE(allocate_tor_tls_object_ex_data_index, TT_FORK),
  2340. LOCAL_TEST_CASE(log_one_error, TT_FORK),
  2341. INTRUSIVE_TEST_CASE(get_error, TT_FORK),
  2342. LOCAL_TEST_CASE(always_accept_verify_cb, 0),
  2343. INTRUSIVE_TEST_CASE(x509_cert_free, 0),
  2344. LOCAL_TEST_CASE(x509_cert_get_id_digests, 0),
  2345. INTRUSIVE_TEST_CASE(cert_matches_key, 0),
  2346. INTRUSIVE_TEST_CASE(cert_get_key, 0),
  2347. LOCAL_TEST_CASE(get_my_client_auth_key, TT_FORK),
  2348. LOCAL_TEST_CASE(get_my_certs, TT_FORK),
  2349. INTRUSIVE_TEST_CASE(get_ciphersuite_name, 0),
  2350. INTRUSIVE_TEST_CASE(classify_client_ciphers, 0),
  2351. LOCAL_TEST_CASE(client_is_using_v2_ciphers, 0),
  2352. INTRUSIVE_TEST_CASE(verify, 0),
  2353. INTRUSIVE_TEST_CASE(check_lifetime, 0),
  2354. INTRUSIVE_TEST_CASE(get_pending_bytes, 0),
  2355. LOCAL_TEST_CASE(get_forced_write_size, 0),
  2356. LOCAL_TEST_CASE(get_write_overhead_ratio, TT_FORK),
  2357. LOCAL_TEST_CASE(used_v1_handshake, TT_FORK),
  2358. LOCAL_TEST_CASE(get_num_server_handshakes, 0),
  2359. LOCAL_TEST_CASE(server_got_renegotiate, 0),
  2360. INTRUSIVE_TEST_CASE(SSL_SESSION_get_master_key, 0),
  2361. INTRUSIVE_TEST_CASE(get_tlssecrets, 0),
  2362. INTRUSIVE_TEST_CASE(get_buffer_sizes, 0),
  2363. LOCAL_TEST_CASE(evaluate_ecgroup_for_tls, 0),
  2364. INTRUSIVE_TEST_CASE(try_to_extract_certs_from_tls, 0),
  2365. INTRUSIVE_TEST_CASE(get_peer_cert, 0),
  2366. INTRUSIVE_TEST_CASE(peer_has_cert, 0),
  2367. INTRUSIVE_TEST_CASE(shutdown, 0),
  2368. INTRUSIVE_TEST_CASE(finish_handshake, 0),
  2369. INTRUSIVE_TEST_CASE(handshake, 0),
  2370. INTRUSIVE_TEST_CASE(write, 0),
  2371. INTRUSIVE_TEST_CASE(read, 0),
  2372. INTRUSIVE_TEST_CASE(server_info_callback, 0),
  2373. LOCAL_TEST_CASE(is_server, 0),
  2374. INTRUSIVE_TEST_CASE(assert_renegotiation_unblocked, 0),
  2375. INTRUSIVE_TEST_CASE(block_renegotiation, 0),
  2376. INTRUSIVE_TEST_CASE(unblock_renegotiation, 0),
  2377. INTRUSIVE_TEST_CASE(set_renegotiate_callback, 0),
  2378. LOCAL_TEST_CASE(set_logged_address, 0),
  2379. INTRUSIVE_TEST_CASE(find_cipher_by_id, 0),
  2380. INTRUSIVE_TEST_CASE(session_secret_cb, 0),
  2381. INTRUSIVE_TEST_CASE(debug_state_callback, 0),
  2382. INTRUSIVE_TEST_CASE(context_new, 0),
  2383. LOCAL_TEST_CASE(create_certificate, 0),
  2384. LOCAL_TEST_CASE(cert_new, 0),
  2385. LOCAL_TEST_CASE(cert_is_valid, 0),
  2386. LOCAL_TEST_CASE(context_init_one, 0),
  2387. END_OF_TESTCASES
  2388. };