test_tortls.c 78 KB

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