test_tortls.c 77 KB

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