test_tortls.c 77 KB

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