test_tortls.c 78 KB

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