test_tortls.c 78 KB

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