test_tortls.c 78 KB

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