test_tortls_openssl.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305
  1. /* Copyright (c) 2010-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TORTLS_PRIVATE
  4. #define TORTLS_OPENSSL_PRIVATE
  5. #define TOR_X509_PRIVATE
  6. #define LOG_PRIVATE
  7. #include "orconfig.h"
  8. #ifdef _WIN32
  9. #include <winsock2.h>
  10. #endif
  11. #include <math.h>
  12. #include "lib/cc/compat_compiler.h"
  13. /* Some versions of OpenSSL declare SSL_get_selected_srtp_profile twice in
  14. * srtp.h. Suppress the GCC warning so we can build with -Wredundant-decl. */
  15. DISABLE_GCC_WARNING(redundant-decls)
  16. #include <openssl/opensslv.h>
  17. #include <openssl/ssl.h>
  18. #include <openssl/ssl3.h>
  19. #include <openssl/err.h>
  20. #include <openssl/asn1t.h>
  21. #include <openssl/x509.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/evp.h>
  24. #include <openssl/bn.h>
  25. ENABLE_GCC_WARNING(redundant-decls)
  26. #include "core/or/or.h"
  27. #include "lib/log/log.h"
  28. #include "app/config/config.h"
  29. #include "lib/crypt_ops/compat_openssl.h"
  30. #include "lib/tls/x509.h"
  31. #include "lib/tls/x509_internal.h"
  32. #include "lib/tls/tortls.h"
  33. #include "lib/tls/tortls_st.h"
  34. #include "lib/tls/tortls_internal.h"
  35. #include "app/config/or_state_st.h"
  36. #include "test/test.h"
  37. #include "test/log_test_helpers.h"
  38. #include "test/test_tortls.h"
  39. #define NS_MODULE tortls
  40. #ifndef HAVE_SSL_STATE
  41. #define OPENSSL_OPAQUE
  42. #endif
  43. #if defined(OPENSSL_OPAQUE) && !defined(LIBRESSL_VERSION_NUMBER)
  44. #define SSL_STATE_STR "before SSL initialization"
  45. #else
  46. #define SSL_STATE_STR "before/accept initialization"
  47. #endif
  48. #ifndef OPENSSL_OPAQUE
  49. static SSL_METHOD *
  50. give_me_a_test_method(void)
  51. {
  52. SSL_METHOD *method = tor_malloc_zero(sizeof(SSL_METHOD));
  53. memcpy(method, TLSv1_method(), sizeof(SSL_METHOD));
  54. return method;
  55. }
  56. static int
  57. fake_num_ciphers(void)
  58. {
  59. return 0;
  60. }
  61. #endif /* !defined(OPENSSL_OPAQUE) */
  62. static int
  63. mock_tls_cert_matches_key(const tor_tls_t *tls, const tor_x509_cert_t *cert)
  64. {
  65. (void) tls;
  66. (void) cert; // XXXX look at this.
  67. return 1;
  68. }
  69. static void
  70. test_tortls_tor_tls_new(void *data)
  71. {
  72. (void) data;
  73. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  74. crypto_pk_t *key1 = NULL, *key2 = NULL;
  75. SSL_METHOD *method = NULL;
  76. key1 = pk_generate(2);
  77. key2 = pk_generate(3);
  78. tor_tls_t *tls = NULL;
  79. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  80. key1, key2, 86400), OP_EQ, 0);
  81. tls = tor_tls_new(-1, 0);
  82. tt_want(tls);
  83. tor_tls_free(tls); tls = NULL;
  84. SSL_CTX_free(client_tls_context->ctx);
  85. client_tls_context->ctx = NULL;
  86. tls = tor_tls_new(-1, 0);
  87. tt_ptr_op(tls, OP_EQ, NULL);
  88. #ifndef OPENSSL_OPAQUE
  89. method = give_me_a_test_method();
  90. SSL_CTX *ctx = SSL_CTX_new(method);
  91. method->num_ciphers = fake_num_ciphers;
  92. client_tls_context->ctx = ctx;
  93. tls = tor_tls_new(-1, 0);
  94. tt_ptr_op(tls, OP_EQ, NULL);
  95. #endif /* !defined(OPENSSL_OPAQUE) */
  96. done:
  97. UNMOCK(tor_tls_cert_matches_key);
  98. crypto_pk_free(key1);
  99. crypto_pk_free(key2);
  100. tor_tls_free(tls);
  101. tor_free(method);
  102. tor_tls_free_all();
  103. }
  104. #define NS_MODULE tortls
  105. static void
  106. library_init(void)
  107. {
  108. #ifdef OPENSSL_1_1_API
  109. OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
  110. #else
  111. SSL_library_init();
  112. SSL_load_error_strings();
  113. #endif
  114. }
  115. static void
  116. test_tortls_get_state_description(void *ignored)
  117. {
  118. (void)ignored;
  119. tor_tls_t *tls;
  120. char *buf;
  121. SSL_CTX *ctx;
  122. library_init();
  123. ctx = SSL_CTX_new(SSLv23_method());
  124. buf = tor_malloc_zero(1000);
  125. tls = tor_malloc_zero(sizeof(tor_tls_t));
  126. tor_tls_get_state_description(NULL, buf, 20);
  127. tt_str_op(buf, OP_EQ, "(No SSL object)");
  128. SSL_free(tls->ssl);
  129. tls->ssl = NULL;
  130. tor_tls_get_state_description(tls, buf, 20);
  131. tt_str_op(buf, OP_EQ, "(No SSL object)");
  132. tls->ssl = SSL_new(ctx);
  133. tor_tls_get_state_description(tls, buf, 200);
  134. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in HANDSHAKE");
  135. tls->state = TOR_TLS_ST_OPEN;
  136. tor_tls_get_state_description(tls, buf, 200);
  137. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in OPEN");
  138. tls->state = TOR_TLS_ST_GOTCLOSE;
  139. tor_tls_get_state_description(tls, buf, 200);
  140. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in GOTCLOSE");
  141. tls->state = TOR_TLS_ST_SENTCLOSE;
  142. tor_tls_get_state_description(tls, buf, 200);
  143. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in SENTCLOSE");
  144. tls->state = TOR_TLS_ST_CLOSED;
  145. tor_tls_get_state_description(tls, buf, 200);
  146. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in CLOSED");
  147. tls->state = TOR_TLS_ST_RENEGOTIATE;
  148. tor_tls_get_state_description(tls, buf, 200);
  149. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in RENEGOTIATE");
  150. tls->state = TOR_TLS_ST_BUFFEREVENT;
  151. tor_tls_get_state_description(tls, buf, 200);
  152. tt_str_op(buf, OP_EQ, SSL_STATE_STR);
  153. tls->state = 7;
  154. tor_tls_get_state_description(tls, buf, 200);
  155. tt_str_op(buf, OP_EQ, SSL_STATE_STR " in unknown TLS state");
  156. done:
  157. SSL_CTX_free(ctx);
  158. SSL_free(tls->ssl);
  159. tor_free(buf);
  160. tor_free(tls);
  161. }
  162. static void
  163. test_tortls_get_by_ssl(void *ignored)
  164. {
  165. (void)ignored;
  166. tor_tls_t *tls;
  167. tor_tls_t *res;
  168. SSL_CTX *ctx;
  169. SSL *ssl;
  170. library_init();
  171. tor_tls_allocate_tor_tls_object_ex_data_index();
  172. ctx = SSL_CTX_new(SSLv23_method());
  173. tls = tor_malloc_zero(sizeof(tor_tls_t));
  174. tls->magic = TOR_TLS_MAGIC;
  175. ssl = SSL_new(ctx);
  176. res = tor_tls_get_by_ssl(ssl);
  177. tt_assert(!res);
  178. SSL_set_ex_data(ssl, tor_tls_object_ex_data_index, tls);
  179. res = tor_tls_get_by_ssl(ssl);
  180. tt_assert(res == tls);
  181. done:
  182. SSL_free(ssl);
  183. SSL_CTX_free(ctx);
  184. tor_free(tls);
  185. }
  186. static void
  187. test_tortls_allocate_tor_tls_object_ex_data_index(void *ignored)
  188. {
  189. (void)ignored;
  190. int first;
  191. tor_tls_allocate_tor_tls_object_ex_data_index();
  192. first = tor_tls_object_ex_data_index;
  193. tor_tls_allocate_tor_tls_object_ex_data_index();
  194. tt_int_op(first, OP_EQ, tor_tls_object_ex_data_index);
  195. done:
  196. (void)0;
  197. }
  198. static void
  199. test_tortls_log_one_error(void *ignored)
  200. {
  201. (void)ignored;
  202. tor_tls_t *tls;
  203. SSL_CTX *ctx;
  204. SSL *ssl = NULL;
  205. library_init();
  206. ctx = SSL_CTX_new(SSLv23_method());
  207. tls = tor_malloc_zero(sizeof(tor_tls_t));
  208. setup_capture_of_logs(LOG_INFO);
  209. tor_tls_log_one_error(NULL, 0, LOG_WARN, 0, "something");
  210. expect_log_msg("TLS error while something: "
  211. "(null) (in (null):(null):---)\n");
  212. mock_clean_saved_logs();
  213. tor_tls_log_one_error(tls, 0, LOG_WARN, 0, NULL);
  214. expect_log_msg("TLS error: (null) "
  215. "(in (null):(null):---)\n");
  216. mock_clean_saved_logs();
  217. tls->address = tor_strdup("127.hello");
  218. tor_tls_log_one_error(tls, 0, LOG_WARN, 0, NULL);
  219. expect_log_msg("TLS error with 127.hello: "
  220. "(null) (in (null):(null):---)\n");
  221. tor_free(tls->address);
  222. mock_clean_saved_logs();
  223. tls->address = tor_strdup("127.hello");
  224. tor_tls_log_one_error(tls, 0, LOG_WARN, 0, "blarg");
  225. expect_log_msg("TLS error while blarg with "
  226. "127.hello: (null) (in (null):(null):---)\n");
  227. mock_clean_saved_logs();
  228. tor_tls_log_one_error(tls, ERR_PACK(1, 2, 3), LOG_WARN, 0, NULL);
  229. expect_log_msg("TLS error with 127.hello: "
  230. "BN lib (in unknown library:(null):---)\n");
  231. mock_clean_saved_logs();
  232. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_HTTP_REQUEST),
  233. LOG_WARN, 0, NULL);
  234. expect_log_severity(LOG_INFO);
  235. mock_clean_saved_logs();
  236. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_HTTPS_PROXY_REQUEST),
  237. LOG_WARN, 0, NULL);
  238. expect_log_severity(LOG_INFO);
  239. mock_clean_saved_logs();
  240. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_RECORD_LENGTH_MISMATCH),
  241. LOG_WARN, 0, NULL);
  242. expect_log_severity(LOG_INFO);
  243. #ifndef OPENSSL_1_1_API
  244. mock_clean_saved_logs();
  245. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_RECORD_TOO_LARGE),
  246. LOG_WARN, 0, NULL);
  247. expect_log_severity(LOG_INFO);
  248. #endif /* !defined(OPENSSL_1_1_API) */
  249. mock_clean_saved_logs();
  250. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_UNKNOWN_PROTOCOL),
  251. LOG_WARN, 0, NULL);
  252. expect_log_severity(LOG_INFO);
  253. mock_clean_saved_logs();
  254. tor_tls_log_one_error(tls, ERR_PACK(1, 2, SSL_R_UNSUPPORTED_PROTOCOL),
  255. LOG_WARN, 0, NULL);
  256. expect_log_severity(LOG_INFO);
  257. tls->ssl = SSL_new(ctx);
  258. mock_clean_saved_logs();
  259. tor_tls_log_one_error(tls, 0, LOG_WARN, 0, NULL);
  260. expect_log_msg("TLS error with 127.hello: (null)"
  261. " (in (null):(null):" SSL_STATE_STR ")\n");
  262. done:
  263. teardown_capture_of_logs();
  264. SSL_free(ssl);
  265. SSL_CTX_free(ctx);
  266. if (tls && tls->ssl)
  267. SSL_free(tls->ssl);
  268. if (tls)
  269. tor_free(tls->address);
  270. tor_free(tls);
  271. }
  272. #ifndef OPENSSL_OPAQUE
  273. static void
  274. test_tortls_get_error(void *ignored)
  275. {
  276. (void)ignored;
  277. tor_tls_t *tls;
  278. int ret;
  279. SSL_CTX *ctx;
  280. library_init();
  281. ctx = SSL_CTX_new(SSLv23_method());
  282. setup_capture_of_logs(LOG_INFO);
  283. tls = tor_malloc_zero(sizeof(tor_tls_t));
  284. tls->ssl = SSL_new(ctx);
  285. SSL_set_bio(tls->ssl, BIO_new(BIO_s_mem()), NULL);
  286. ret = tor_tls_get_error(tls, 0, 0, "something", LOG_WARN, 0);
  287. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_IO);
  288. expect_log_msg("TLS error: unexpected close while"
  289. " something (before/accept initialization)\n");
  290. mock_clean_saved_logs();
  291. ret = tor_tls_get_error(tls, 2, 0, "something", LOG_WARN, 0);
  292. tt_int_op(ret, OP_EQ, 0);
  293. expect_no_log_entry();
  294. mock_clean_saved_logs();
  295. ret = tor_tls_get_error(tls, 0, 1, "something", LOG_WARN, 0);
  296. tt_int_op(ret, OP_EQ, -11);
  297. expect_no_log_entry();
  298. mock_clean_saved_logs();
  299. ERR_clear_error();
  300. ERR_put_error(ERR_LIB_BN, 2, -1, "somewhere.c", 99);
  301. ret = tor_tls_get_error(tls, 0, 0, "something", LOG_WARN, 0);
  302. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  303. expect_log_msg("TLS error while something: (null)"
  304. " (in bignum routines:(null):before/accept initialization)\n");
  305. mock_clean_saved_logs();
  306. ERR_clear_error();
  307. tls->ssl->rwstate = SSL_READING;
  308. SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_READ;
  309. ret = tor_tls_get_error(tls, -1, 0, "something", LOG_WARN, 0);
  310. tt_int_op(ret, OP_EQ, TOR_TLS_WANTREAD);
  311. expect_no_log_entry();
  312. mock_clean_saved_logs();
  313. ERR_clear_error();
  314. tls->ssl->rwstate = SSL_READING;
  315. SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_WRITE;
  316. ret = tor_tls_get_error(tls, -1, 0, "something", LOG_WARN, 0);
  317. tt_int_op(ret, OP_EQ, TOR_TLS_WANTWRITE);
  318. expect_no_log_entry();
  319. mock_clean_saved_logs();
  320. ERR_clear_error();
  321. tls->ssl->rwstate = 0;
  322. tls->ssl->shutdown = SSL_RECEIVED_SHUTDOWN;
  323. tls->ssl->s3->warn_alert =SSL_AD_CLOSE_NOTIFY;
  324. ret = tor_tls_get_error(tls, 0, 0, "something", LOG_WARN, 0);
  325. tt_int_op(ret, OP_EQ, TOR_TLS_CLOSE);
  326. expect_log_entry();
  327. mock_clean_saved_logs();
  328. ret = tor_tls_get_error(tls, 0, 2, "something", LOG_WARN, 0);
  329. tt_int_op(ret, OP_EQ, -10);
  330. expect_no_log_entry();
  331. mock_clean_saved_logs();
  332. ERR_put_error(ERR_LIB_SYS, 2, -1, "somewhere.c", 99);
  333. ret = tor_tls_get_error(tls, -1, 0, "something", LOG_WARN, 0);
  334. tt_int_op(ret, OP_EQ, -9);
  335. expect_log_msg("TLS error while something: (null) (in system library:"
  336. "connect:before/accept initialization)\n");
  337. done:
  338. teardown_capture_of_logs();
  339. SSL_free(tls->ssl);
  340. tor_free(tls);
  341. SSL_CTX_free(ctx);
  342. }
  343. #endif /* !defined(OPENSSL_OPAQUE) */
  344. static void
  345. test_tortls_always_accept_verify_cb(void *ignored)
  346. {
  347. (void)ignored;
  348. int ret;
  349. ret = always_accept_verify_cb(0, NULL);
  350. tt_int_op(ret, OP_EQ, 1);
  351. done:
  352. (void)0;
  353. }
  354. #ifndef OPENSSL_OPAQUE
  355. static void
  356. test_tortls_x509_cert_free(void *ignored)
  357. {
  358. (void)ignored;
  359. tor_x509_cert_t *cert;
  360. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  361. tor_x509_cert_free(cert);
  362. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  363. cert->cert = X509_new();
  364. cert->encoded = tor_malloc_zero(1);
  365. tor_x509_cert_free(cert);
  366. }
  367. #endif /* !defined(OPENSSL_OPAQUE) */
  368. #ifndef OPENSSL_OPAQUE
  369. static int
  370. fixed_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  371. {
  372. (void) a; (void) b;
  373. return 1;
  374. }
  375. /*
  376. * Use only for the matching fake_x509_free() call
  377. */
  378. static X509 *
  379. fake_x509_malloc(void)
  380. {
  381. return tor_malloc_zero(sizeof(X509));
  382. }
  383. static void
  384. fake_x509_free(X509 *cert)
  385. {
  386. if (cert) {
  387. if (cert->cert_info) {
  388. if (cert->cert_info->key) {
  389. if (cert->cert_info->key->pkey) {
  390. tor_free(cert->cert_info->key->pkey);
  391. }
  392. tor_free(cert->cert_info->key);
  393. }
  394. tor_free(cert->cert_info);
  395. }
  396. tor_free(cert);
  397. }
  398. }
  399. static void
  400. test_tortls_cert_matches_key(void *ignored)
  401. {
  402. (void)ignored;
  403. int res;
  404. tor_tls_t *tls;
  405. tor_x509_cert_t *cert;
  406. X509 *one = NULL, *two = NULL;
  407. EVP_PKEY_ASN1_METHOD *meth = EVP_PKEY_asn1_new(999, 0, NULL, NULL);
  408. EVP_PKEY_asn1_set_public(meth, NULL, NULL, fixed_pub_cmp, NULL, NULL, NULL);
  409. tls = tor_malloc_zero(sizeof(tor_tls_t));
  410. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  411. one = fake_x509_malloc();
  412. one->references = 1;
  413. two = fake_x509_malloc();
  414. two->references = 1;
  415. res = tor_tls_cert_matches_key(tls, cert);
  416. tt_int_op(res, OP_EQ, 0);
  417. tls->ssl = tor_malloc_zero(sizeof(SSL));
  418. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  419. tls->ssl->session->peer = one;
  420. res = tor_tls_cert_matches_key(tls, cert);
  421. tt_int_op(res, OP_EQ, 0);
  422. cert->cert = two;
  423. res = tor_tls_cert_matches_key(tls, cert);
  424. tt_int_op(res, OP_EQ, 0);
  425. one->cert_info = tor_malloc_zero(sizeof(X509_CINF));
  426. one->cert_info->key = tor_malloc_zero(sizeof(X509_PUBKEY));
  427. one->cert_info->key->pkey = tor_malloc_zero(sizeof(EVP_PKEY));
  428. one->cert_info->key->pkey->references = 1;
  429. one->cert_info->key->pkey->ameth = meth;
  430. one->cert_info->key->pkey->type = 1;
  431. two->cert_info = tor_malloc_zero(sizeof(X509_CINF));
  432. two->cert_info->key = tor_malloc_zero(sizeof(X509_PUBKEY));
  433. two->cert_info->key->pkey = tor_malloc_zero(sizeof(EVP_PKEY));
  434. two->cert_info->key->pkey->references = 1;
  435. two->cert_info->key->pkey->ameth = meth;
  436. two->cert_info->key->pkey->type = 2;
  437. res = tor_tls_cert_matches_key(tls, cert);
  438. tt_int_op(res, OP_EQ, 0);
  439. one->cert_info->key->pkey->type = 1;
  440. two->cert_info->key->pkey->type = 1;
  441. res = tor_tls_cert_matches_key(tls, cert);
  442. tt_int_op(res, OP_EQ, 1);
  443. done:
  444. EVP_PKEY_asn1_free(meth);
  445. tor_free(tls->ssl->session);
  446. tor_free(tls->ssl);
  447. tor_free(tls);
  448. tor_free(cert);
  449. fake_x509_free(one);
  450. fake_x509_free(two);
  451. }
  452. static void
  453. test_tortls_cert_get_key(void *ignored)
  454. {
  455. (void)ignored;
  456. tor_x509_cert_t *cert = NULL;
  457. crypto_pk_t *res = NULL;
  458. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  459. X509 *key = NULL;
  460. key = fake_x509_malloc();
  461. key->references = 1;
  462. res = tor_tls_cert_get_key(cert);
  463. tt_assert(!res);
  464. cert->cert = key;
  465. key->cert_info = tor_malloc_zero(sizeof(X509_CINF));
  466. key->cert_info->key = tor_malloc_zero(sizeof(X509_PUBKEY));
  467. key->cert_info->key->pkey = tor_malloc_zero(sizeof(EVP_PKEY));
  468. key->cert_info->key->pkey->references = 1;
  469. key->cert_info->key->pkey->type = 2;
  470. res = tor_tls_cert_get_key(cert);
  471. tt_assert(!res);
  472. done:
  473. fake_x509_free(key);
  474. tor_free(cert);
  475. crypto_pk_free(res);
  476. }
  477. #endif /* !defined(OPENSSL_OPAQUE) */
  478. static void
  479. test_tortls_get_my_client_auth_key(void *ignored)
  480. {
  481. (void)ignored;
  482. crypto_pk_t *ret;
  483. crypto_pk_t *expected;
  484. tor_tls_context_t *ctx;
  485. RSA *k = RSA_new();
  486. ctx = tor_malloc_zero(sizeof(tor_tls_context_t));
  487. expected = crypto_new_pk_from_openssl_rsa_(k);
  488. ctx->auth_key = expected;
  489. client_tls_context = NULL;
  490. ret = tor_tls_get_my_client_auth_key();
  491. tt_assert(!ret);
  492. client_tls_context = ctx;
  493. ret = tor_tls_get_my_client_auth_key();
  494. tt_assert(ret == expected);
  495. done:
  496. tor_free(expected);
  497. tor_free(ctx);
  498. }
  499. #ifndef HAVE_SSL_GET_CLIENT_CIPHERS
  500. static SSL_CIPHER *
  501. get_cipher_by_name(const char *name)
  502. {
  503. int i;
  504. const SSL_METHOD *method = SSLv23_method();
  505. int num = method->num_ciphers();
  506. for (i = 0; i < num; ++i) {
  507. const SSL_CIPHER *cipher = method->get_cipher(i);
  508. const char *ciphername = SSL_CIPHER_get_name(cipher);
  509. if (!strcmp(ciphername, name)) {
  510. return (SSL_CIPHER *)cipher;
  511. }
  512. }
  513. return NULL;
  514. }
  515. #endif /* !defined(HAVE_SSL_GET_CLIENT_CIPHERS) */
  516. #ifndef OPENSSL_OPAQUE
  517. static void
  518. test_tortls_get_ciphersuite_name(void *ignored)
  519. {
  520. (void)ignored;
  521. const char *ret;
  522. tor_tls_t *ctx;
  523. ctx = tor_malloc_zero(sizeof(tor_tls_t));
  524. ctx->ssl = tor_malloc_zero(sizeof(SSL));
  525. ret = tor_tls_get_ciphersuite_name(ctx);
  526. tt_str_op(ret, OP_EQ, "(NONE)");
  527. done:
  528. tor_free(ctx->ssl);
  529. tor_free(ctx);
  530. }
  531. static SSL_CIPHER *
  532. get_cipher_by_id(uint16_t id)
  533. {
  534. int i;
  535. const SSL_METHOD *method = SSLv23_method();
  536. int num = method->num_ciphers();
  537. for (i = 0; i < num; ++i) {
  538. const SSL_CIPHER *cipher = method->get_cipher(i);
  539. if (id == (SSL_CIPHER_get_id(cipher) & 0xffff)) {
  540. return (SSL_CIPHER *)cipher;
  541. }
  542. }
  543. return NULL;
  544. }
  545. static void
  546. test_tortls_classify_client_ciphers(void *ignored)
  547. {
  548. (void)ignored;
  549. int i;
  550. int ret;
  551. SSL_CTX *ctx;
  552. SSL *ssl;
  553. tor_tls_t *tls;
  554. STACK_OF(SSL_CIPHER) *ciphers;
  555. SSL_CIPHER *tmp_cipher;
  556. library_init();
  557. tor_tls_allocate_tor_tls_object_ex_data_index();
  558. tls = tor_malloc_zero(sizeof(tor_tls_t));
  559. tls->magic = TOR_TLS_MAGIC;
  560. ctx = SSL_CTX_new(TLSv1_method());
  561. ssl = SSL_new(ctx);
  562. tls->ssl = ssl;
  563. ciphers = sk_SSL_CIPHER_new_null();
  564. ret = tor_tls_classify_client_ciphers(ssl, NULL);
  565. tt_int_op(ret, OP_EQ, -1);
  566. SSL_set_ex_data(ssl, tor_tls_object_ex_data_index, tls);
  567. tls->client_cipher_list_type = 42;
  568. ret = tor_tls_classify_client_ciphers(ssl, NULL);
  569. tt_int_op(ret, OP_EQ, 42);
  570. tls->client_cipher_list_type = 0;
  571. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  572. tt_int_op(ret, OP_EQ, 1);
  573. tt_int_op(tls->client_cipher_list_type, OP_EQ, 1);
  574. tls->client_cipher_list_type = 0;
  575. ret = tor_tls_classify_client_ciphers(ssl, SSL_get_ciphers(ssl));
  576. tt_int_op(ret, OP_EQ, 3);
  577. tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
  578. SSL_CIPHER *one = get_cipher_by_name(TLS1_TXT_DHE_RSA_WITH_AES_128_SHA),
  579. *two = get_cipher_by_name(TLS1_TXT_DHE_RSA_WITH_AES_256_SHA),
  580. *three = get_cipher_by_name(SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA),
  581. *four = NULL;
  582. sk_SSL_CIPHER_push(ciphers, one);
  583. sk_SSL_CIPHER_push(ciphers, two);
  584. sk_SSL_CIPHER_push(ciphers, three);
  585. sk_SSL_CIPHER_push(ciphers, four);
  586. tls->client_cipher_list_type = 0;
  587. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  588. tt_int_op(ret, OP_EQ, 1);
  589. tt_int_op(tls->client_cipher_list_type, OP_EQ, 1);
  590. sk_SSL_CIPHER_zero(ciphers);
  591. one = get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
  592. tt_assert(one);
  593. one->id = 0x00ff;
  594. two = get_cipher_by_name("ECDHE-RSA-AES128-GCM-SHA256");
  595. tt_assert(two);
  596. two->id = 0x0000;
  597. sk_SSL_CIPHER_push(ciphers, one);
  598. tls->client_cipher_list_type = 0;
  599. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  600. tt_int_op(ret, OP_EQ, 3);
  601. tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
  602. sk_SSL_CIPHER_push(ciphers, two);
  603. tls->client_cipher_list_type = 0;
  604. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  605. tt_int_op(ret, OP_EQ, 3);
  606. tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
  607. one->id = 0xC00A;
  608. tls->client_cipher_list_type = 0;
  609. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  610. tt_int_op(ret, OP_EQ, 3);
  611. tt_int_op(tls->client_cipher_list_type, OP_EQ, 3);
  612. sk_SSL_CIPHER_zero(ciphers);
  613. for (i=0; v2_cipher_list[i]; i++) {
  614. tmp_cipher = get_cipher_by_id(v2_cipher_list[i]);
  615. tt_assert(tmp_cipher);
  616. sk_SSL_CIPHER_push(ciphers, tmp_cipher);
  617. }
  618. tls->client_cipher_list_type = 0;
  619. ret = tor_tls_classify_client_ciphers(ssl, ciphers);
  620. tt_int_op(ret, OP_EQ, 2);
  621. tt_int_op(tls->client_cipher_list_type, OP_EQ, 2);
  622. done:
  623. sk_SSL_CIPHER_free(ciphers);
  624. SSL_free(tls->ssl);
  625. tor_free(tls);
  626. SSL_CTX_free(ctx);
  627. }
  628. #endif /* !defined(OPENSSL_OPAQUE) */
  629. static void
  630. test_tortls_client_is_using_v2_ciphers(void *ignored)
  631. {
  632. (void)ignored;
  633. #ifdef HAVE_SSL_GET_CLIENT_CIPHERS
  634. tt_skip();
  635. done:
  636. (void)1;
  637. #else
  638. int ret;
  639. SSL_CTX *ctx;
  640. SSL *ssl;
  641. SSL_SESSION *sess;
  642. STACK_OF(SSL_CIPHER) *ciphers;
  643. library_init();
  644. ctx = SSL_CTX_new(TLSv1_method());
  645. ssl = SSL_new(ctx);
  646. sess = SSL_SESSION_new();
  647. ret = tor_tls_client_is_using_v2_ciphers(ssl);
  648. tt_int_op(ret, OP_EQ, -1);
  649. ssl->session = sess;
  650. ret = tor_tls_client_is_using_v2_ciphers(ssl);
  651. tt_int_op(ret, OP_EQ, 0);
  652. ciphers = sk_SSL_CIPHER_new_null();
  653. SSL_CIPHER *one = get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
  654. tt_assert(one);
  655. one->id = 0x00ff;
  656. sk_SSL_CIPHER_push(ciphers, one);
  657. sess->ciphers = ciphers;
  658. ret = tor_tls_client_is_using_v2_ciphers(ssl);
  659. tt_int_op(ret, OP_EQ, 1);
  660. done:
  661. SSL_free(ssl);
  662. SSL_CTX_free(ctx);
  663. #endif /* defined(HAVE_SSL_GET_CLIENT_CIPHERS) */
  664. }
  665. #ifndef OPENSSL_OPAQUE
  666. static int fixed_ssl_pending_result = 0;
  667. static int
  668. fixed_ssl_pending(const SSL *ignored)
  669. {
  670. (void)ignored;
  671. return fixed_ssl_pending_result;
  672. }
  673. static void
  674. test_tortls_get_pending_bytes(void *ignored)
  675. {
  676. (void)ignored;
  677. int ret;
  678. tor_tls_t *tls;
  679. SSL_METHOD *method;
  680. tls = tor_malloc_zero(sizeof(tor_tls_t));
  681. tls->ssl = tor_malloc_zero(sizeof(SSL));
  682. method = tor_malloc_zero(sizeof(SSL_METHOD));
  683. method->ssl_pending = fixed_ssl_pending;
  684. tls->ssl->method = method;
  685. fixed_ssl_pending_result = 42;
  686. ret = tor_tls_get_pending_bytes(tls);
  687. tt_int_op(ret, OP_EQ, 42);
  688. done:
  689. tor_free(method);
  690. tor_free(tls->ssl);
  691. tor_free(tls);
  692. }
  693. #endif /* !defined(OPENSSL_OPAQUE) */
  694. #ifndef OPENSSL_OPAQUE
  695. static void
  696. test_tortls_SSL_SESSION_get_master_key(void *ignored)
  697. {
  698. (void)ignored;
  699. size_t ret;
  700. tor_tls_t *tls;
  701. uint8_t *out;
  702. out = tor_malloc_zero(1);
  703. tls = tor_malloc_zero(sizeof(tor_tls_t));
  704. tls->ssl = tor_malloc_zero(sizeof(SSL));
  705. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  706. tls->ssl->session->master_key_length = 1;
  707. #ifndef HAVE_SSL_SESSION_GET_MASTER_KEY
  708. tls->ssl->session->master_key[0] = 43;
  709. ret = SSL_SESSION_get_master_key(tls->ssl->session, out, 0);
  710. tt_int_op(ret, OP_EQ, 1);
  711. tt_int_op(out[0], OP_EQ, 0);
  712. ret = SSL_SESSION_get_master_key(tls->ssl->session, out, 1);
  713. tt_int_op(ret, OP_EQ, 1);
  714. tt_int_op(out[0], OP_EQ, 43);
  715. done:
  716. #endif /* !defined(HAVE_SSL_SESSION_GET_MASTER_KEY) */
  717. tor_free(tls->ssl->session);
  718. tor_free(tls->ssl);
  719. tor_free(tls);
  720. tor_free(out);
  721. }
  722. #endif /* !defined(OPENSSL_OPAQUE) */
  723. #ifndef OPENSSL_OPAQUE
  724. static void
  725. test_tortls_get_tlssecrets(void *ignored)
  726. {
  727. (void)ignored;
  728. int ret;
  729. uint8_t *secret_out = tor_malloc_zero(DIGEST256_LEN);
  730. tor_tls_t *tls;
  731. tls = tor_malloc_zero(sizeof(tor_tls_t));
  732. tls->ssl = tor_malloc_zero(sizeof(SSL));
  733. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  734. tls->ssl->session->master_key_length = 1;
  735. tls->ssl->s3 = tor_malloc_zero(sizeof(SSL3_STATE));
  736. ret = tor_tls_get_tlssecrets(tls, secret_out);
  737. tt_int_op(ret, OP_EQ, 0);
  738. done:
  739. tor_free(secret_out);
  740. tor_free(tls->ssl->s3);
  741. tor_free(tls->ssl->session);
  742. tor_free(tls->ssl);
  743. tor_free(tls);
  744. }
  745. #endif /* !defined(OPENSSL_OPAQUE) */
  746. #ifndef OPENSSL_OPAQUE
  747. static void
  748. test_tortls_get_buffer_sizes(void *ignored)
  749. {
  750. (void)ignored;
  751. int ret;
  752. tor_tls_t *tls;
  753. size_t rbuf_c=-1, rbuf_b=-1, wbuf_c=-1, wbuf_b=-1;
  754. tls = tor_malloc_zero(sizeof(tor_tls_t));
  755. tls->ssl = tor_malloc_zero(sizeof(SSL));
  756. tls->ssl->s3 = tor_malloc_zero(sizeof(SSL3_STATE));
  757. tls->ssl->s3->rbuf.buf = NULL;
  758. tls->ssl->s3->rbuf.len = 1;
  759. tls->ssl->s3->rbuf.offset = 0;
  760. tls->ssl->s3->rbuf.left = 42;
  761. tls->ssl->s3->wbuf.buf = NULL;
  762. tls->ssl->s3->wbuf.len = 2;
  763. tls->ssl->s3->wbuf.offset = 0;
  764. tls->ssl->s3->wbuf.left = 43;
  765. ret = tor_tls_get_buffer_sizes(tls, &rbuf_c, &rbuf_b, &wbuf_c, &wbuf_b);
  766. #if OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,1,0)
  767. tt_int_op(ret, OP_EQ, -1);
  768. #else
  769. tt_int_op(ret, OP_EQ, 0);
  770. tt_int_op(rbuf_c, OP_EQ, 0);
  771. tt_int_op(wbuf_c, OP_EQ, 0);
  772. tt_int_op(rbuf_b, OP_EQ, 42);
  773. tt_int_op(wbuf_b, OP_EQ, 43);
  774. tls->ssl->s3->rbuf.buf = tor_malloc_zero(1);
  775. tls->ssl->s3->wbuf.buf = tor_malloc_zero(1);
  776. ret = tor_tls_get_buffer_sizes(tls, &rbuf_c, &rbuf_b, &wbuf_c, &wbuf_b);
  777. tt_int_op(ret, OP_EQ, 0);
  778. tt_int_op(rbuf_c, OP_EQ, 1);
  779. tt_int_op(wbuf_c, OP_EQ, 2);
  780. #endif /* OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,1,0) */
  781. done:
  782. tor_free(tls->ssl->s3->rbuf.buf);
  783. tor_free(tls->ssl->s3->wbuf.buf);
  784. tor_free(tls->ssl->s3);
  785. tor_free(tls->ssl);
  786. tor_free(tls);
  787. }
  788. #endif /* !defined(OPENSSL_OPAQUE) */
  789. #ifndef OPENSSL_OPAQUE
  790. typedef struct cert_pkey_st_local
  791. {
  792. X509 *x509;
  793. EVP_PKEY *privatekey;
  794. const EVP_MD *digest;
  795. } CERT_PKEY_local;
  796. typedef struct sess_cert_st_local
  797. {
  798. STACK_OF(X509) *cert_chain;
  799. int peer_cert_type;
  800. CERT_PKEY_local *peer_key;
  801. CERT_PKEY_local peer_pkeys[8];
  802. int references;
  803. } SESS_CERT_local;
  804. static void
  805. test_tortls_try_to_extract_certs_from_tls(void *ignored)
  806. {
  807. (void)ignored;
  808. tor_tls_t *tls;
  809. X509 *cert = NULL, *id_cert = NULL, *c1 = NULL, *c2 = NULL;
  810. SESS_CERT_local *sess = NULL;
  811. c1 = read_cert_from(validCertString);
  812. c2 = read_cert_from(caCertString);
  813. tls = tor_malloc_zero(sizeof(tor_tls_t));
  814. tls->ssl = tor_malloc_zero(sizeof(SSL));
  815. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  816. sess = tor_malloc_zero(sizeof(SESS_CERT_local));
  817. tls->ssl->session->sess_cert = (void *)sess;
  818. try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
  819. tt_assert(!cert);
  820. tt_assert(!id_cert);
  821. tls->ssl->session->peer = c1;
  822. try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
  823. tt_assert(cert == c1);
  824. tt_assert(!id_cert);
  825. X509_free(cert); /* decrease refcnt */
  826. sess->cert_chain = sk_X509_new_null();
  827. try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
  828. tt_assert(cert == c1);
  829. tt_assert(!id_cert);
  830. X509_free(cert); /* decrease refcnt */
  831. sk_X509_push(sess->cert_chain, c1);
  832. sk_X509_push(sess->cert_chain, c2);
  833. try_to_extract_certs_from_tls(LOG_WARN, tls, &cert, &id_cert);
  834. tt_assert(cert == c1);
  835. tt_assert(id_cert);
  836. X509_free(cert); /* decrease refcnt */
  837. done:
  838. sk_X509_free(sess->cert_chain);
  839. tor_free(sess);
  840. tor_free(tls->ssl->session);
  841. tor_free(tls->ssl);
  842. tor_free(tls);
  843. X509_free(c1);
  844. X509_free(c2);
  845. }
  846. #endif /* !defined(OPENSSL_OPAQUE) */
  847. #ifndef OPENSSL_OPAQUE
  848. static void
  849. test_tortls_get_peer_cert(void *ignored)
  850. {
  851. (void)ignored;
  852. tor_x509_cert_t *ret;
  853. tor_tls_t *tls;
  854. X509 *cert = NULL;
  855. cert = read_cert_from(validCertString);
  856. tls = tor_malloc_zero(sizeof(tor_tls_t));
  857. tls->ssl = tor_malloc_zero(sizeof(SSL));
  858. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  859. ret = tor_tls_get_peer_cert(tls);
  860. tt_assert(!ret);
  861. tls->ssl->session->peer = cert;
  862. ret = tor_tls_get_peer_cert(tls);
  863. tt_assert(ret);
  864. tt_assert(ret->cert == cert);
  865. done:
  866. tor_x509_cert_free(ret);
  867. tor_free(tls->ssl->session);
  868. tor_free(tls->ssl);
  869. tor_free(tls);
  870. X509_free(cert);
  871. }
  872. #endif /* !defined(OPENSSL_OPAQUE) */
  873. #ifndef OPENSSL_OPAQUE
  874. static void
  875. test_tortls_peer_has_cert(void *ignored)
  876. {
  877. (void)ignored;
  878. int ret;
  879. tor_tls_t *tls;
  880. X509 *cert = NULL;
  881. cert = read_cert_from(validCertString);
  882. tls = tor_malloc_zero(sizeof(tor_tls_t));
  883. tls->ssl = tor_malloc_zero(sizeof(SSL));
  884. tls->ssl->session = tor_malloc_zero(sizeof(SSL_SESSION));
  885. ret = tor_tls_peer_has_cert(tls);
  886. tt_assert(!ret);
  887. tls->ssl->session->peer = cert;
  888. ret = tor_tls_peer_has_cert(tls);
  889. tt_assert(ret);
  890. done:
  891. tor_free(tls->ssl->session);
  892. tor_free(tls->ssl);
  893. tor_free(tls);
  894. X509_free(cert);
  895. }
  896. #endif /* !defined(OPENSSL_OPAQUE) */
  897. static void
  898. test_tortls_get_write_overhead_ratio(void *ignored)
  899. {
  900. (void)ignored;
  901. double ret;
  902. total_bytes_written_over_tls = 0;
  903. ret = tls_get_write_overhead_ratio();
  904. tt_double_op(fabs(ret - 1.0), OP_LT, 1E-12);
  905. total_bytes_written_by_tls = 10;
  906. total_bytes_written_over_tls = 1;
  907. ret = tls_get_write_overhead_ratio();
  908. tt_double_op(fabs(ret - 10.0), OP_LT, 1E-12);
  909. total_bytes_written_by_tls = 10;
  910. total_bytes_written_over_tls = 2;
  911. ret = tls_get_write_overhead_ratio();
  912. tt_double_op(fabs(ret - 5.0), OP_LT, 1E-12);
  913. done:
  914. (void)0;
  915. }
  916. static void
  917. test_tortls_is_server(void *ignored)
  918. {
  919. (void)ignored;
  920. tor_tls_t *tls;
  921. int ret;
  922. tls = tor_malloc_zero(sizeof(tor_tls_t));
  923. tls->isServer = 1;
  924. ret = tor_tls_is_server(tls);
  925. tt_int_op(ret, OP_EQ, 1);
  926. done:
  927. tor_free(tls);
  928. }
  929. #ifndef OPENSSL_OPAQUE
  930. static void
  931. test_tortls_session_secret_cb(void *ignored)
  932. {
  933. (void)ignored;
  934. tor_tls_t *tls;
  935. SSL_CTX *ctx;
  936. STACK_OF(SSL_CIPHER) *ciphers = NULL;
  937. SSL_CIPHER *one;
  938. library_init();
  939. tor_tls_allocate_tor_tls_object_ex_data_index();
  940. tls = tor_malloc_zero(sizeof(tor_tls_t));
  941. tls->magic = TOR_TLS_MAGIC;
  942. ctx = SSL_CTX_new(TLSv1_method());
  943. tls->ssl = SSL_new(ctx);
  944. SSL_set_ex_data(tls->ssl, tor_tls_object_ex_data_index, tls);
  945. SSL_set_session_secret_cb(tls->ssl, tor_tls_session_secret_cb, NULL);
  946. tor_tls_session_secret_cb(tls->ssl, NULL, NULL, NULL, NULL, NULL);
  947. tt_assert(!tls->ssl->tls_session_secret_cb);
  948. one = get_cipher_by_name("ECDHE-RSA-AES256-GCM-SHA384");
  949. one->id = 0x00ff;
  950. ciphers = sk_SSL_CIPHER_new_null();
  951. sk_SSL_CIPHER_push(ciphers, one);
  952. tls->client_cipher_list_type = 0;
  953. tor_tls_session_secret_cb(tls->ssl, NULL, NULL, ciphers, NULL, NULL);
  954. tt_assert(!tls->ssl->tls_session_secret_cb);
  955. done:
  956. sk_SSL_CIPHER_free(ciphers);
  957. SSL_free(tls->ssl);
  958. SSL_CTX_free(ctx);
  959. tor_free(tls);
  960. }
  961. #endif /* !defined(OPENSSL_OPAQUE) */
  962. #ifndef OPENSSL_OPAQUE
  963. /* TODO: It seems block_renegotiation and unblock_renegotiation and
  964. * using different blags. This might not be correct */
  965. static void
  966. test_tortls_block_renegotiation(void *ignored)
  967. {
  968. (void)ignored;
  969. tor_tls_t *tls;
  970. tls = tor_malloc_zero(sizeof(tor_tls_t));
  971. tls->ssl = tor_malloc_zero(sizeof(SSL));
  972. tls->ssl->s3 = tor_malloc_zero(sizeof(SSL3_STATE));
  973. #ifndef SUPPORT_UNSAFE_RENEGOTIATION_FLAG
  974. #define SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION 0
  975. #endif
  976. tls->ssl->s3->flags = SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
  977. tor_tls_block_renegotiation(tls);
  978. #ifndef OPENSSL_1_1_API
  979. tt_assert(!(tls->ssl->s3->flags &
  980. SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION));
  981. #endif
  982. done:
  983. tor_free(tls->ssl->s3);
  984. tor_free(tls->ssl);
  985. tor_free(tls);
  986. }
  987. static void
  988. test_tortls_unblock_renegotiation(void *ignored)
  989. {
  990. (void)ignored;
  991. tor_tls_t *tls;
  992. tls = tor_malloc_zero(sizeof(tor_tls_t));
  993. tls->ssl = tor_malloc_zero(sizeof(SSL));
  994. tor_tls_unblock_renegotiation(tls);
  995. tt_uint_op(SSL_get_options(tls->ssl) &
  996. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, OP_EQ,
  997. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
  998. done:
  999. tor_free(tls->ssl);
  1000. tor_free(tls);
  1001. }
  1002. #endif /* !defined(OPENSSL_OPAQUE) */
  1003. #ifndef OPENSSL_OPAQUE
  1004. static void
  1005. test_tortls_assert_renegotiation_unblocked(void *ignored)
  1006. {
  1007. (void)ignored;
  1008. tor_tls_t *tls;
  1009. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1010. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1011. tor_tls_unblock_renegotiation(tls);
  1012. tor_tls_assert_renegotiation_unblocked(tls);
  1013. /* No assertion here - this test will fail if tor_assert is turned on
  1014. * and things are bad. */
  1015. tor_free(tls->ssl);
  1016. tor_free(tls);
  1017. }
  1018. #endif /* !defined(OPENSSL_OPAQUE) */
  1019. static void
  1020. test_tortls_set_logged_address(void *ignored)
  1021. {
  1022. (void)ignored;
  1023. tor_tls_t *tls;
  1024. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1025. tor_tls_set_logged_address(tls, "foo bar");
  1026. tt_str_op(tls->address, OP_EQ, "foo bar");
  1027. tor_tls_set_logged_address(tls, "foo bar 2");
  1028. tt_str_op(tls->address, OP_EQ, "foo bar 2");
  1029. done:
  1030. tor_free(tls->address);
  1031. tor_free(tls);
  1032. }
  1033. #ifndef OPENSSL_OPAQUE
  1034. static void
  1035. example_cb(tor_tls_t *t, void *arg)
  1036. {
  1037. (void)t;
  1038. (void)arg;
  1039. }
  1040. static void
  1041. test_tortls_set_renegotiate_callback(void *ignored)
  1042. {
  1043. (void)ignored;
  1044. tor_tls_t *tls;
  1045. const char *arg = "hello";
  1046. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1047. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1048. tor_tls_set_renegotiate_callback(tls, example_cb, (void*)arg);
  1049. tt_assert(tls->negotiated_callback == example_cb);
  1050. tt_assert(tls->callback_arg == arg);
  1051. tt_assert(!tls->got_renegotiate);
  1052. /* Assumes V2_HANDSHAKE_SERVER */
  1053. tt_assert(tls->ssl->info_callback == tor_tls_server_info_callback);
  1054. tor_tls_set_renegotiate_callback(tls, NULL, (void*)arg);
  1055. tt_assert(tls->ssl->info_callback == tor_tls_debug_state_callback);
  1056. done:
  1057. tor_free(tls->ssl);
  1058. tor_free(tls);
  1059. }
  1060. #endif /* !defined(OPENSSL_OPAQUE) */
  1061. #ifndef OPENSSL_OPAQUE
  1062. static SSL_CIPHER *fixed_cipher1 = NULL;
  1063. static SSL_CIPHER *fixed_cipher2 = NULL;
  1064. static const SSL_CIPHER *
  1065. fake_get_cipher(unsigned ncipher)
  1066. {
  1067. switch (ncipher) {
  1068. case 1:
  1069. return fixed_cipher1;
  1070. case 2:
  1071. return fixed_cipher2;
  1072. default:
  1073. return NULL;
  1074. }
  1075. }
  1076. #endif /* !defined(OPENSSL_OPAQUE) */
  1077. #ifndef OPENSSL_OPAQUE
  1078. static void
  1079. test_tortls_find_cipher_by_id(void *ignored)
  1080. {
  1081. (void)ignored;
  1082. int ret;
  1083. SSL *ssl;
  1084. SSL_CTX *ctx;
  1085. const SSL_METHOD *m = TLSv1_method();
  1086. SSL_METHOD *empty_method = tor_malloc_zero(sizeof(SSL_METHOD));
  1087. fixed_cipher1 = tor_malloc_zero(sizeof(SSL_CIPHER));
  1088. fixed_cipher2 = tor_malloc_zero(sizeof(SSL_CIPHER));
  1089. fixed_cipher2->id = 0xC00A;
  1090. library_init();
  1091. ctx = SSL_CTX_new(m);
  1092. ssl = SSL_new(ctx);
  1093. ret = find_cipher_by_id(ssl, NULL, 0xC00A);
  1094. tt_int_op(ret, OP_EQ, 1);
  1095. ret = find_cipher_by_id(ssl, m, 0xC00A);
  1096. tt_int_op(ret, OP_EQ, 1);
  1097. ret = find_cipher_by_id(ssl, m, 0xFFFF);
  1098. tt_int_op(ret, OP_EQ, 0);
  1099. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1100. tt_int_op(ret, OP_EQ, 1);
  1101. ret = find_cipher_by_id(ssl, empty_method, 0xFFFF);
  1102. #ifdef HAVE_SSL_CIPHER_FIND
  1103. tt_int_op(ret, OP_EQ, 0);
  1104. #else
  1105. tt_int_op(ret, OP_EQ, 1);
  1106. #endif
  1107. empty_method->get_cipher = fake_get_cipher;
  1108. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1109. tt_int_op(ret, OP_EQ, 1);
  1110. empty_method->get_cipher = m->get_cipher;
  1111. empty_method->num_ciphers = m->num_ciphers;
  1112. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1113. tt_int_op(ret, OP_EQ, 1);
  1114. empty_method->get_cipher = fake_get_cipher;
  1115. empty_method->num_ciphers = m->num_ciphers;
  1116. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1117. tt_int_op(ret, OP_EQ, 1);
  1118. empty_method->num_ciphers = fake_num_ciphers;
  1119. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1120. #ifdef HAVE_SSL_CIPHER_FIND
  1121. tt_int_op(ret, OP_EQ, 1);
  1122. #else
  1123. tt_int_op(ret, OP_EQ, 0);
  1124. #endif
  1125. done:
  1126. tor_free(empty_method);
  1127. SSL_free(ssl);
  1128. SSL_CTX_free(ctx);
  1129. tor_free(fixed_cipher1);
  1130. }
  1131. #endif /* !defined(OPENSSL_OPAQUE) */
  1132. #ifndef OPENSSL_OPAQUE
  1133. static void
  1134. test_tortls_debug_state_callback(void *ignored)
  1135. {
  1136. (void)ignored;
  1137. SSL *ssl;
  1138. char *buf = tor_malloc_zero(1000);
  1139. int n;
  1140. setup_capture_of_logs(LOG_DEBUG);
  1141. ssl = tor_malloc_zero(sizeof(SSL));
  1142. tor_tls_debug_state_callback(ssl, 32, 45);
  1143. n = tor_snprintf(buf, 1000, "SSL %p is now in state unknown"
  1144. " state [type=32,val=45].\n", ssl);
  1145. /* tor's snprintf returns -1 on error */
  1146. tt_int_op(n, OP_NE, -1);
  1147. expect_log_msg(buf);
  1148. done:
  1149. teardown_capture_of_logs();
  1150. tor_free(buf);
  1151. tor_free(ssl);
  1152. }
  1153. #endif /* !defined(OPENSSL_OPAQUE) */
  1154. #ifndef OPENSSL_OPAQUE
  1155. static void
  1156. test_tortls_server_info_callback(void *ignored)
  1157. {
  1158. (void)ignored;
  1159. tor_tls_t *tls;
  1160. SSL_CTX *ctx;
  1161. SSL *ssl;
  1162. library_init();
  1163. ctx = SSL_CTX_new(TLSv1_method());
  1164. ssl = SSL_new(ctx);
  1165. tor_tls_allocate_tor_tls_object_ex_data_index();
  1166. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1167. tls->magic = TOR_TLS_MAGIC;
  1168. tls->ssl = ssl;
  1169. setup_full_capture_of_logs(LOG_WARN);
  1170. SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_A);
  1171. mock_clean_saved_logs();
  1172. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1173. expect_single_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
  1174. SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_B);
  1175. mock_clean_saved_logs();
  1176. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1177. expect_single_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
  1178. SSL_set_state(ssl, 99);
  1179. mock_clean_saved_logs();
  1180. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1181. expect_no_log_entry();
  1182. teardown_capture_of_logs();
  1183. SSL_set_ex_data(tls->ssl, tor_tls_object_ex_data_index, tls);
  1184. SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_B);
  1185. tls->negotiated_callback = 0;
  1186. tls->server_handshake_count = 120;
  1187. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1188. tt_int_op(tls->server_handshake_count, OP_EQ, 121);
  1189. tls->server_handshake_count = 127;
  1190. tls->negotiated_callback = (void *)1;
  1191. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1192. tt_int_op(tls->server_handshake_count, OP_EQ, 127);
  1193. tt_int_op(tls->got_renegotiate, OP_EQ, 1);
  1194. tls->ssl->session = SSL_SESSION_new();
  1195. tls->wasV2Handshake = 0;
  1196. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1197. tt_int_op(tls->wasV2Handshake, OP_EQ, 0);
  1198. done:
  1199. teardown_capture_of_logs();
  1200. SSL_free(ssl);
  1201. SSL_CTX_free(ctx);
  1202. tor_free(tls);
  1203. }
  1204. #endif /* !defined(OPENSSL_OPAQUE) */
  1205. #ifndef OPENSSL_OPAQUE
  1206. static int fixed_ssl_read_result_index;
  1207. static int fixed_ssl_read_result[5];
  1208. static int fixed_ssl_shutdown_result;
  1209. static int
  1210. fixed_ssl_read(SSL *s, void *buf, int len)
  1211. {
  1212. (void)s;
  1213. (void)buf;
  1214. (void)len;
  1215. return fixed_ssl_read_result[fixed_ssl_read_result_index++];
  1216. }
  1217. static int
  1218. fixed_ssl_shutdown(SSL *s)
  1219. {
  1220. (void)s;
  1221. return fixed_ssl_shutdown_result;
  1222. }
  1223. #ifndef LIBRESSL_VERSION_NUMBER
  1224. static int fixed_ssl_state_to_set;
  1225. static tor_tls_t *fixed_tls;
  1226. static int
  1227. setting_version_ssl_shutdown(SSL *s)
  1228. {
  1229. s->version = SSL2_VERSION;
  1230. return fixed_ssl_shutdown_result;
  1231. }
  1232. static int
  1233. setting_version_and_state_ssl_shutdown(SSL *s)
  1234. {
  1235. fixed_tls->state = fixed_ssl_state_to_set;
  1236. s->version = SSL2_VERSION;
  1237. return fixed_ssl_shutdown_result;
  1238. }
  1239. #endif /* !defined(LIBRESSL_VERSION_NUMBER) */
  1240. static int
  1241. dummy_handshake_func(SSL *s)
  1242. {
  1243. (void)s;
  1244. return 1;
  1245. }
  1246. static int negotiated_callback_called;
  1247. static void
  1248. negotiated_callback_setter(tor_tls_t *t, void *arg)
  1249. {
  1250. (void)t;
  1251. (void)arg;
  1252. negotiated_callback_called++;
  1253. }
  1254. static void
  1255. test_tortls_read(void *ignored)
  1256. {
  1257. (void)ignored;
  1258. int ret;
  1259. tor_tls_t *tls;
  1260. char buf[100];
  1261. SSL_METHOD *method = give_me_a_test_method();
  1262. setup_capture_of_logs(LOG_WARN);
  1263. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1264. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1265. tls->state = TOR_TLS_ST_OPEN;
  1266. ret = tor_tls_read(tls, buf, 10);
  1267. tt_int_op(ret, OP_EQ, -9);
  1268. /* These tests assume that V2_HANDSHAKE_SERVER is set */
  1269. tls->ssl->handshake_func = dummy_handshake_func;
  1270. tls->ssl->method = method;
  1271. method->ssl_read = fixed_ssl_read;
  1272. fixed_ssl_read_result_index = 0;
  1273. fixed_ssl_read_result[0] = 42;
  1274. tls->state = TOR_TLS_ST_OPEN;
  1275. ERR_clear_error();
  1276. ret = tor_tls_read(tls, buf, 10);
  1277. tt_int_op(ret, OP_EQ, 42);
  1278. tls->state = TOR_TLS_ST_OPEN;
  1279. tls->got_renegotiate = 1;
  1280. fixed_ssl_read_result_index = 0;
  1281. ERR_clear_error();
  1282. ret = tor_tls_read(tls, buf, 10);
  1283. tt_int_op(tls->got_renegotiate, OP_EQ, 0);
  1284. tls->state = TOR_TLS_ST_OPEN;
  1285. tls->got_renegotiate = 1;
  1286. negotiated_callback_called = 0;
  1287. tls->negotiated_callback = negotiated_callback_setter;
  1288. fixed_ssl_read_result_index = 0;
  1289. ERR_clear_error();
  1290. ret = tor_tls_read(tls, buf, 10);
  1291. tt_int_op(negotiated_callback_called, OP_EQ, 1);
  1292. #ifndef LIBRESSL_VERSION_NUMBER
  1293. fixed_ssl_read_result_index = 0;
  1294. fixed_ssl_read_result[0] = 0;
  1295. tls->ssl->version = SSL2_VERSION;
  1296. ERR_clear_error();
  1297. ret = tor_tls_read(tls, buf, 10);
  1298. tt_int_op(ret, OP_EQ, TOR_TLS_CLOSE);
  1299. tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_CLOSED);
  1300. #endif /* !defined(LIBRESSL_VERSION_NUMBER) */
  1301. // TODO: fill up
  1302. done:
  1303. teardown_capture_of_logs();
  1304. tor_free(tls->ssl);
  1305. tor_free(tls);
  1306. tor_free(method);
  1307. }
  1308. static int fixed_ssl_write_result;
  1309. static int
  1310. fixed_ssl_write(SSL *s, const void *buf, int len)
  1311. {
  1312. (void)s;
  1313. (void)buf;
  1314. (void)len;
  1315. return fixed_ssl_write_result;
  1316. }
  1317. static void
  1318. test_tortls_write(void *ignored)
  1319. {
  1320. (void)ignored;
  1321. int ret;
  1322. tor_tls_t *tls;
  1323. SSL_METHOD *method = give_me_a_test_method();
  1324. char buf[100];
  1325. setup_capture_of_logs(LOG_WARN);
  1326. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1327. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1328. tls->state = TOR_TLS_ST_OPEN;
  1329. ret = tor_tls_write(tls, buf, 0);
  1330. tt_int_op(ret, OP_EQ, 0);
  1331. ret = tor_tls_write(tls, buf, 10);
  1332. tt_int_op(ret, OP_EQ, -9);
  1333. tls->ssl->method = method;
  1334. tls->wantwrite_n = 1;
  1335. ret = tor_tls_write(tls, buf, 10);
  1336. tt_int_op(tls->wantwrite_n, OP_EQ, 0);
  1337. method->ssl_write = fixed_ssl_write;
  1338. tls->ssl->handshake_func = dummy_handshake_func;
  1339. fixed_ssl_write_result = 1;
  1340. ERR_clear_error();
  1341. ret = tor_tls_write(tls, buf, 10);
  1342. tt_int_op(ret, OP_EQ, 1);
  1343. fixed_ssl_write_result = -1;
  1344. ERR_clear_error();
  1345. tls->ssl->rwstate = SSL_READING;
  1346. SSL_set_bio(tls->ssl, BIO_new(BIO_s_mem()), NULL);
  1347. SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_READ;
  1348. ret = tor_tls_write(tls, buf, 10);
  1349. tt_int_op(ret, OP_EQ, TOR_TLS_WANTREAD);
  1350. ERR_clear_error();
  1351. tls->ssl->rwstate = SSL_READING;
  1352. SSL_set_bio(tls->ssl, BIO_new(BIO_s_mem()), NULL);
  1353. SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_WRITE;
  1354. ret = tor_tls_write(tls, buf, 10);
  1355. tt_int_op(ret, OP_EQ, TOR_TLS_WANTWRITE);
  1356. done:
  1357. teardown_capture_of_logs();
  1358. BIO_free(tls->ssl->rbio);
  1359. tor_free(tls->ssl);
  1360. tor_free(tls);
  1361. tor_free(method);
  1362. }
  1363. #endif /* !defined(OPENSSL_OPAQUE) */
  1364. #ifndef OPENSSL_OPAQUE
  1365. static int fixed_ssl_accept_result;
  1366. static int fixed_ssl_connect_result;
  1367. static int
  1368. setting_error_ssl_accept(SSL *ssl)
  1369. {
  1370. (void)ssl;
  1371. ERR_put_error(ERR_LIB_BN, 2, -1, "somewhere.c", 99);
  1372. ERR_put_error(ERR_LIB_SYS, 2, -1, "somewhere.c", 99);
  1373. return fixed_ssl_accept_result;
  1374. }
  1375. static int
  1376. setting_error_ssl_connect(SSL *ssl)
  1377. {
  1378. (void)ssl;
  1379. ERR_put_error(ERR_LIB_BN, 2, -1, "somewhere.c", 99);
  1380. ERR_put_error(ERR_LIB_SYS, 2, -1, "somewhere.c", 99);
  1381. return fixed_ssl_connect_result;
  1382. }
  1383. static int
  1384. fixed_ssl_accept(SSL *ssl)
  1385. {
  1386. (void) ssl;
  1387. return fixed_ssl_accept_result;
  1388. }
  1389. static void
  1390. test_tortls_handshake(void *ignored)
  1391. {
  1392. (void)ignored;
  1393. int ret;
  1394. tor_tls_t *tls;
  1395. SSL_CTX *ctx;
  1396. SSL_METHOD *method = give_me_a_test_method();
  1397. setup_capture_of_logs(LOG_INFO);
  1398. SSL_library_init();
  1399. SSL_load_error_strings();
  1400. ctx = SSL_CTX_new(TLSv1_method());
  1401. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1402. tls->ssl = SSL_new(ctx);
  1403. tls->state = TOR_TLS_ST_HANDSHAKE;
  1404. ret = tor_tls_handshake(tls);
  1405. tt_int_op(ret, OP_EQ, -9);
  1406. tls->isServer = 1;
  1407. tls->state = TOR_TLS_ST_HANDSHAKE;
  1408. ret = tor_tls_handshake(tls);
  1409. tt_int_op(ret, OP_EQ, -9);
  1410. tls->ssl->method = method;
  1411. method->ssl_accept = fixed_ssl_accept;
  1412. fixed_ssl_accept_result = 2;
  1413. ERR_clear_error();
  1414. tls->state = TOR_TLS_ST_HANDSHAKE;
  1415. ret = tor_tls_handshake(tls);
  1416. tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_OPEN);
  1417. method->ssl_accept = setting_error_ssl_accept;
  1418. fixed_ssl_accept_result = 1;
  1419. ERR_clear_error();
  1420. mock_clean_saved_logs();
  1421. tls->state = TOR_TLS_ST_HANDSHAKE;
  1422. ret = tor_tls_handshake(tls);
  1423. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1424. expect_log_entry();
  1425. /* This fails on jessie. Investigate why! */
  1426. #if 0
  1427. expect_log_msg("TLS error while handshaking: (null) (in bignum routines:"
  1428. "(null):SSLv3 write client hello B)\n");
  1429. expect_log_msg("TLS error while handshaking: (null) (in system library:"
  1430. "connect:SSLv3 write client hello B)\n");
  1431. #endif /* 0 */
  1432. expect_log_severity(LOG_INFO);
  1433. tls->isServer = 0;
  1434. method->ssl_connect = setting_error_ssl_connect;
  1435. fixed_ssl_connect_result = 1;
  1436. ERR_clear_error();
  1437. mock_clean_saved_logs();
  1438. tls->state = TOR_TLS_ST_HANDSHAKE;
  1439. ret = tor_tls_handshake(tls);
  1440. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1441. expect_log_entry();
  1442. #if 0
  1443. /* See above */
  1444. expect_log_msg("TLS error while handshaking: "
  1445. "(null) (in bignum routines:(null):SSLv3 write client hello B)\n");
  1446. expect_log_msg("TLS error while handshaking: "
  1447. "(null) (in system library:connect:SSLv3 write client hello B)\n");
  1448. #endif /* 0 */
  1449. expect_log_severity(LOG_WARN);
  1450. done:
  1451. teardown_capture_of_logs();
  1452. SSL_free(tls->ssl);
  1453. SSL_CTX_free(ctx);
  1454. tor_free(tls);
  1455. tor_free(method);
  1456. }
  1457. #endif /* !defined(OPENSSL_OPAQUE) */
  1458. #ifndef OPENSSL_OPAQUE
  1459. static void
  1460. test_tortls_finish_handshake(void *ignored)
  1461. {
  1462. (void)ignored;
  1463. int ret;
  1464. tor_tls_t *tls;
  1465. SSL_CTX *ctx;
  1466. SSL_METHOD *method = give_me_a_test_method();
  1467. SSL_library_init();
  1468. SSL_load_error_strings();
  1469. X509 *c1 = read_cert_from(validCertString);
  1470. SESS_CERT_local *sess = NULL;
  1471. ctx = SSL_CTX_new(method);
  1472. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1473. tls->ssl = SSL_new(ctx);
  1474. tls->state = TOR_TLS_ST_OPEN;
  1475. ret = tor_tls_finish_handshake(tls);
  1476. tt_int_op(ret, OP_EQ, 0);
  1477. tls->isServer = 1;
  1478. tls->wasV2Handshake = 0;
  1479. setup_full_capture_of_logs(LOG_WARN);
  1480. ret = tor_tls_finish_handshake(tls);
  1481. tt_int_op(ret, OP_EQ, 0);
  1482. tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
  1483. expect_single_log_msg_containing("For some reason, wasV2Handshake didn't "
  1484. "get set.");
  1485. teardown_capture_of_logs();
  1486. tls->wasV2Handshake = 1;
  1487. ret = tor_tls_finish_handshake(tls);
  1488. tt_int_op(ret, OP_EQ, 0);
  1489. tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
  1490. tls->wasV2Handshake = 1;
  1491. tls->ssl->session = SSL_SESSION_new();
  1492. ret = tor_tls_finish_handshake(tls);
  1493. tt_int_op(ret, OP_EQ, 0);
  1494. tt_int_op(tls->wasV2Handshake, OP_EQ, 0);
  1495. tls->isServer = 0;
  1496. sess = tor_malloc_zero(sizeof(SESS_CERT_local));
  1497. tls->ssl->session->sess_cert = (void *)sess;
  1498. sess->cert_chain = sk_X509_new_null();
  1499. sk_X509_push(sess->cert_chain, c1);
  1500. tls->ssl->session->peer = c1;
  1501. tls->wasV2Handshake = 0;
  1502. ret = tor_tls_finish_handshake(tls);
  1503. tt_int_op(ret, OP_EQ, 0);
  1504. tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
  1505. method->num_ciphers = fake_num_ciphers;
  1506. ret = tor_tls_finish_handshake(tls);
  1507. tt_int_op(ret, OP_EQ, -9);
  1508. done:
  1509. if (sess)
  1510. sk_X509_free(sess->cert_chain);
  1511. if (tls->ssl && tls->ssl->session) {
  1512. tor_free(tls->ssl->session->sess_cert);
  1513. }
  1514. SSL_free(tls->ssl);
  1515. tor_free(tls);
  1516. SSL_CTX_free(ctx);
  1517. tor_free(method);
  1518. teardown_capture_of_logs();
  1519. }
  1520. #endif /* !defined(OPENSSL_OPAQUE) */
  1521. static int fixed_crypto_pk_new_result_index;
  1522. static crypto_pk_t *fixed_crypto_pk_new_result[5];
  1523. static crypto_pk_t *
  1524. fixed_crypto_pk_new(void)
  1525. {
  1526. return fixed_crypto_pk_new_result[fixed_crypto_pk_new_result_index++];
  1527. }
  1528. #ifndef OPENSSL_OPAQUE
  1529. static int fixed_crypto_pk_generate_key_with_bits_result_index;
  1530. static int fixed_crypto_pk_generate_key_with_bits_result[5];
  1531. static int fixed_tor_tls_create_certificate_result_index;
  1532. static X509 *fixed_tor_tls_create_certificate_result[5];
  1533. static int fixed_tor_x509_cert_new_result_index;
  1534. static tor_x509_cert_t *fixed_tor_x509_cert_new_result[5];
  1535. static int
  1536. fixed_crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits)
  1537. {
  1538. (void)env;
  1539. (void)bits;
  1540. return fixed_crypto_pk_generate_key_with_bits_result[
  1541. fixed_crypto_pk_generate_key_with_bits_result_index++];
  1542. }
  1543. static X509 *
  1544. fixed_tor_tls_create_certificate(crypto_pk_t *rsa,
  1545. crypto_pk_t *rsa_sign,
  1546. const char *cname,
  1547. const char *cname_sign,
  1548. unsigned int cert_lifetime)
  1549. {
  1550. (void)rsa;
  1551. (void)rsa_sign;
  1552. (void)cname;
  1553. (void)cname_sign;
  1554. (void)cert_lifetime;
  1555. return fixed_tor_tls_create_certificate_result[
  1556. fixed_tor_tls_create_certificate_result_index++];
  1557. }
  1558. static tor_x509_cert_t *
  1559. fixed_tor_x509_cert_new(tor_x509_cert_impl_t *x509_cert)
  1560. {
  1561. (void) x509_cert;
  1562. return fixed_tor_x509_cert_new_result[
  1563. fixed_tor_x509_cert_new_result_index++];
  1564. }
  1565. static void
  1566. test_tortls_context_new(void *ignored)
  1567. {
  1568. (void)ignored;
  1569. tor_tls_context_t *ret;
  1570. crypto_pk_t *pk1, *pk2, *pk3, *pk4, *pk5, *pk6, *pk7, *pk8, *pk9, *pk10,
  1571. *pk11, *pk12, *pk13, *pk14, *pk15, *pk16, *pk17, *pk18;
  1572. pk1 = crypto_pk_new();
  1573. pk2 = crypto_pk_new();
  1574. pk3 = crypto_pk_new();
  1575. pk4 = crypto_pk_new();
  1576. pk5 = crypto_pk_new();
  1577. pk6 = crypto_pk_new();
  1578. pk7 = crypto_pk_new();
  1579. pk8 = crypto_pk_new();
  1580. pk9 = crypto_pk_new();
  1581. pk10 = crypto_pk_new();
  1582. pk11 = crypto_pk_new();
  1583. pk12 = crypto_pk_new();
  1584. pk13 = crypto_pk_new();
  1585. pk14 = crypto_pk_new();
  1586. pk15 = crypto_pk_new();
  1587. pk16 = crypto_pk_new();
  1588. pk17 = crypto_pk_new();
  1589. pk18 = crypto_pk_new();
  1590. fixed_crypto_pk_new_result_index = 0;
  1591. fixed_crypto_pk_new_result[0] = NULL;
  1592. MOCK(crypto_pk_new, fixed_crypto_pk_new);
  1593. ret = tor_tls_context_new(NULL, 0, 0, 0);
  1594. tt_assert(!ret);
  1595. /* note: we already override this in testing_common.c, so we
  1596. * run this unit test in a subprocess. */
  1597. MOCK(crypto_pk_generate_key_with_bits,
  1598. fixed_crypto_pk_generate_key_with_bits);
  1599. fixed_crypto_pk_new_result_index = 0;
  1600. fixed_crypto_pk_new_result[0] = pk1;
  1601. fixed_crypto_pk_new_result[1] = NULL;
  1602. fixed_crypto_pk_generate_key_with_bits_result[0] = -1;
  1603. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  1604. ret = tor_tls_context_new(NULL, 0, 0, 0);
  1605. tt_assert(!ret);
  1606. fixed_crypto_pk_new_result_index = 0;
  1607. fixed_crypto_pk_new_result[0] = pk2;
  1608. fixed_crypto_pk_new_result[1] = NULL;
  1609. fixed_crypto_pk_generate_key_with_bits_result[0] = 0;
  1610. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  1611. ret = tor_tls_context_new(NULL, 0, 0, 0);
  1612. tt_assert(!ret);
  1613. fixed_crypto_pk_new_result_index = 0;
  1614. fixed_crypto_pk_new_result[0] = pk3;
  1615. fixed_crypto_pk_new_result[1] = pk4;
  1616. fixed_crypto_pk_new_result[2] = NULL;
  1617. fixed_crypto_pk_generate_key_with_bits_result[0] = 0;
  1618. fixed_crypto_pk_generate_key_with_bits_result[1] = -1;
  1619. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  1620. ret = tor_tls_context_new(NULL, 0, 0, 0);
  1621. tt_assert(!ret);
  1622. MOCK(tor_tls_create_certificate, fixed_tor_tls_create_certificate);
  1623. fixed_crypto_pk_new_result_index = 0;
  1624. fixed_crypto_pk_new_result[0] = pk5;
  1625. fixed_crypto_pk_new_result[1] = pk6;
  1626. fixed_crypto_pk_new_result[2] = NULL;
  1627. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  1628. fixed_crypto_pk_generate_key_with_bits_result[1] = 0;
  1629. fixed_tor_tls_create_certificate_result_index = 0;
  1630. fixed_tor_tls_create_certificate_result[0] = NULL;
  1631. fixed_tor_tls_create_certificate_result[1] = X509_new();
  1632. fixed_tor_tls_create_certificate_result[2] = X509_new();
  1633. ret = tor_tls_context_new(NULL, 0, 0, 0);
  1634. tt_assert(!ret);
  1635. fixed_crypto_pk_new_result_index = 0;
  1636. fixed_crypto_pk_new_result[0] = pk7;
  1637. fixed_crypto_pk_new_result[1] = pk8;
  1638. fixed_crypto_pk_new_result[2] = NULL;
  1639. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  1640. fixed_tor_tls_create_certificate_result_index = 0;
  1641. fixed_tor_tls_create_certificate_result[0] = X509_new();
  1642. fixed_tor_tls_create_certificate_result[1] = NULL;
  1643. fixed_tor_tls_create_certificate_result[2] = X509_new();
  1644. ret = tor_tls_context_new(NULL, 0, 0, 0);
  1645. tt_assert(!ret);
  1646. fixed_crypto_pk_new_result_index = 0;
  1647. fixed_crypto_pk_new_result[0] = pk9;
  1648. fixed_crypto_pk_new_result[1] = pk10;
  1649. fixed_crypto_pk_new_result[2] = NULL;
  1650. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  1651. fixed_tor_tls_create_certificate_result_index = 0;
  1652. fixed_tor_tls_create_certificate_result[0] = X509_new();
  1653. fixed_tor_tls_create_certificate_result[1] = X509_new();
  1654. fixed_tor_tls_create_certificate_result[2] = NULL;
  1655. ret = tor_tls_context_new(NULL, 0, 0, 0);
  1656. tt_assert(!ret);
  1657. MOCK(tor_x509_cert_new, fixed_tor_x509_cert_new);
  1658. fixed_crypto_pk_new_result_index = 0;
  1659. fixed_crypto_pk_new_result[0] = pk11;
  1660. fixed_crypto_pk_new_result[1] = pk12;
  1661. fixed_crypto_pk_new_result[2] = NULL;
  1662. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  1663. fixed_tor_tls_create_certificate_result_index = 0;
  1664. fixed_tor_tls_create_certificate_result[0] = X509_new();
  1665. fixed_tor_tls_create_certificate_result[1] = X509_new();
  1666. fixed_tor_tls_create_certificate_result[2] = X509_new();
  1667. fixed_tor_x509_cert_new_result_index = 0;
  1668. fixed_tor_x509_cert_new_result[0] = NULL;
  1669. fixed_tor_x509_cert_new_result[1] = NULL;
  1670. fixed_tor_x509_cert_new_result[2] = NULL;
  1671. ret = tor_tls_context_new(NULL, 0, 0, 0);
  1672. tt_assert(!ret);
  1673. fixed_crypto_pk_new_result_index = 0;
  1674. fixed_crypto_pk_new_result[0] = pk13;
  1675. fixed_crypto_pk_new_result[1] = pk14;
  1676. fixed_crypto_pk_new_result[2] = NULL;
  1677. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  1678. fixed_tor_tls_create_certificate_result_index = 0;
  1679. fixed_tor_tls_create_certificate_result[0] = X509_new();
  1680. fixed_tor_tls_create_certificate_result[1] = X509_new();
  1681. fixed_tor_tls_create_certificate_result[2] = X509_new();
  1682. fixed_tor_x509_cert_new_result_index = 0;
  1683. fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  1684. fixed_tor_x509_cert_new_result[1] = NULL;
  1685. fixed_tor_x509_cert_new_result[2] = NULL;
  1686. ret = tor_tls_context_new(NULL, 0, 0, 0);
  1687. tt_assert(!ret);
  1688. fixed_crypto_pk_new_result_index = 0;
  1689. fixed_crypto_pk_new_result[0] = pk15;
  1690. fixed_crypto_pk_new_result[1] = pk16;
  1691. fixed_crypto_pk_new_result[2] = NULL;
  1692. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  1693. fixed_tor_tls_create_certificate_result_index = 0;
  1694. fixed_tor_tls_create_certificate_result[0] = X509_new();
  1695. fixed_tor_tls_create_certificate_result[1] = X509_new();
  1696. fixed_tor_tls_create_certificate_result[2] = X509_new();
  1697. fixed_tor_x509_cert_new_result_index = 0;
  1698. fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  1699. fixed_tor_x509_cert_new_result[1] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  1700. fixed_tor_x509_cert_new_result[2] = NULL;
  1701. ret = tor_tls_context_new(NULL, 0, 0, 0);
  1702. tt_assert(!ret);
  1703. fixed_crypto_pk_new_result_index = 0;
  1704. fixed_crypto_pk_new_result[0] = pk17;
  1705. fixed_crypto_pk_new_result[1] = pk18;
  1706. fixed_crypto_pk_new_result[2] = NULL;
  1707. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  1708. fixed_tor_tls_create_certificate_result_index = 0;
  1709. fixed_tor_tls_create_certificate_result[0] = X509_new();
  1710. fixed_tor_tls_create_certificate_result[1] = X509_new();
  1711. fixed_tor_tls_create_certificate_result[2] = X509_new();
  1712. fixed_tor_x509_cert_new_result_index = 0;
  1713. fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  1714. fixed_tor_x509_cert_new_result[1] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  1715. fixed_tor_x509_cert_new_result[2] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  1716. ret = tor_tls_context_new(NULL, 0, 0, 0);
  1717. tt_assert(!ret);
  1718. done:
  1719. UNMOCK(tor_x509_cert_new);
  1720. UNMOCK(tor_tls_create_certificate);
  1721. UNMOCK(crypto_pk_generate_key_with_bits);
  1722. UNMOCK(crypto_pk_new);
  1723. }
  1724. #endif /* !defined(OPENSSL_OPAQUE) */
  1725. static int fixed_crypto_pk_get_evp_pkey_result_index = 0;
  1726. static EVP_PKEY *fixed_crypto_pk_get_evp_pkey_result[5];
  1727. static EVP_PKEY *
  1728. fixed_crypto_pk_get_evp_pkey_(crypto_pk_t *env, int private)
  1729. {
  1730. (void) env;
  1731. (void) private;
  1732. return fixed_crypto_pk_get_evp_pkey_result[
  1733. fixed_crypto_pk_get_evp_pkey_result_index++];
  1734. }
  1735. static void
  1736. test_tortls_create_certificate(void *ignored)
  1737. {
  1738. (void)ignored;
  1739. X509 *ret;
  1740. crypto_pk_t *pk1, *pk2;
  1741. pk1 = crypto_pk_new();
  1742. pk2 = crypto_pk_new();
  1743. MOCK(crypto_pk_get_openssl_evp_pkey_, fixed_crypto_pk_get_evp_pkey_);
  1744. fixed_crypto_pk_get_evp_pkey_result_index = 0;
  1745. fixed_crypto_pk_get_evp_pkey_result[0] = NULL;
  1746. ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
  1747. tt_assert(!ret);
  1748. fixed_crypto_pk_get_evp_pkey_result_index = 0;
  1749. fixed_crypto_pk_get_evp_pkey_result[0] = EVP_PKEY_new();
  1750. fixed_crypto_pk_get_evp_pkey_result[1] = NULL;
  1751. ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
  1752. tt_assert(!ret);
  1753. fixed_crypto_pk_get_evp_pkey_result_index = 0;
  1754. fixed_crypto_pk_get_evp_pkey_result[0] = EVP_PKEY_new();
  1755. fixed_crypto_pk_get_evp_pkey_result[1] = EVP_PKEY_new();
  1756. ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
  1757. tt_assert(!ret);
  1758. done:
  1759. UNMOCK(crypto_pk_get_openssl_evp_pkey_);
  1760. crypto_pk_free(pk1);
  1761. crypto_pk_free(pk2);
  1762. }
  1763. static void
  1764. test_tortls_cert_new(void *ignored)
  1765. {
  1766. (void)ignored;
  1767. tor_x509_cert_t *ret;
  1768. X509 *cert = read_cert_from(validCertString);
  1769. ret = tor_x509_cert_new(NULL);
  1770. tt_assert(!ret);
  1771. ret = tor_x509_cert_new(cert);
  1772. tt_assert(ret);
  1773. tor_x509_cert_free(ret);
  1774. ret = NULL;
  1775. #if 0
  1776. cert = read_cert_from(validCertString);
  1777. /* XXX this doesn't do what you think: it alters a copy of the pubkey. */
  1778. X509_get_pubkey(cert)->type = EVP_PKEY_DSA;
  1779. ret = tor_x509_cert_new(cert);
  1780. tt_assert(ret);
  1781. #endif /* 0 */
  1782. #ifndef OPENSSL_OPAQUE
  1783. cert = read_cert_from(validCertString);
  1784. X509_CINF_free(cert->cert_info);
  1785. cert->cert_info = NULL;
  1786. ret = tor_x509_cert_new(cert);
  1787. tt_assert(ret);
  1788. #endif /* !defined(OPENSSL_OPAQUE) */
  1789. done:
  1790. tor_x509_cert_free(ret);
  1791. }
  1792. static void
  1793. test_tortls_cert_is_valid(void *ignored)
  1794. {
  1795. (void)ignored;
  1796. int ret;
  1797. tor_x509_cert_t *cert = NULL, *scert = NULL;
  1798. scert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  1799. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, time(NULL), 0);
  1800. tt_int_op(ret, OP_EQ, 0);
  1801. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  1802. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, time(NULL), 0);
  1803. tt_int_op(ret, OP_EQ, 0);
  1804. tor_free(scert);
  1805. tor_free(cert);
  1806. cert = tor_x509_cert_new(read_cert_from(validCertString));
  1807. scert = tor_x509_cert_new(read_cert_from(caCertString));
  1808. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, time(NULL), 0);
  1809. tt_int_op(ret, OP_EQ, 1);
  1810. #ifndef OPENSSL_OPAQUE
  1811. tor_x509_cert_free(cert);
  1812. tor_x509_cert_free(scert);
  1813. cert = tor_x509_cert_new(read_cert_from(validCertString));
  1814. scert = tor_x509_cert_new(read_cert_from(caCertString));
  1815. ASN1_TIME_free(cert->cert->cert_info->validity->notAfter);
  1816. cert->cert->cert_info->validity->notAfter =
  1817. ASN1_TIME_set(NULL, time(NULL)-1000000);
  1818. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, time(NULL), 0);
  1819. tt_int_op(ret, OP_EQ, 0);
  1820. tor_x509_cert_free(cert);
  1821. tor_x509_cert_free(scert);
  1822. cert = tor_x509_cert_new(read_cert_from(validCertString));
  1823. scert = tor_x509_cert_new(read_cert_from(caCertString));
  1824. X509_PUBKEY_free(cert->cert->cert_info->key);
  1825. cert->cert->cert_info->key = NULL;
  1826. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, time(NULL), 1);
  1827. tt_int_op(ret, OP_EQ, 0);
  1828. #endif /* !defined(OPENSSL_OPAQUE) */
  1829. #if 0
  1830. tor_x509_cert_free(cert);
  1831. tor_x509_cert_free(scert);
  1832. cert = tor_x509_cert_new(read_cert_from(validCertString));
  1833. scert = tor_x509_cert_new(read_cert_from(caCertString));
  1834. /* This doesn't actually change the key in the cert. XXXXXX */
  1835. BN_one(EVP_PKEY_get1_RSA(X509_get_pubkey(cert->cert))->n);
  1836. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, time(NULL), 1);
  1837. tt_int_op(ret, OP_EQ, 0);
  1838. tor_x509_cert_free(cert);
  1839. tor_x509_cert_free(scert);
  1840. cert = tor_x509_cert_new(read_cert_from(validCertString));
  1841. scert = tor_x509_cert_new(read_cert_from(caCertString));
  1842. /* This doesn't actually change the key in the cert. XXXXXX */
  1843. X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
  1844. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, time(NULL), 1);
  1845. tt_int_op(ret, OP_EQ, 0);
  1846. tor_x509_cert_free(cert);
  1847. tor_x509_cert_free(scert);
  1848. cert = tor_x509_cert_new(read_cert_from(validCertString));
  1849. scert = tor_x509_cert_new(read_cert_from(caCertString));
  1850. /* This doesn't actually change the key in the cert. XXXXXX */
  1851. X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
  1852. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, time(NULL), 0);
  1853. tt_int_op(ret, OP_EQ, 1);
  1854. tor_x509_cert_free(cert);
  1855. tor_x509_cert_free(scert);
  1856. cert = tor_x509_cert_new(read_cert_from(validCertString));
  1857. scert = tor_x509_cert_new(read_cert_from(caCertString));
  1858. /* This doesn't actually change the key in the cert. XXXXXX */
  1859. X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
  1860. X509_get_pubkey(cert->cert)->ameth = NULL;
  1861. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, time(NULL), 0);
  1862. tt_int_op(ret, OP_EQ, 0);
  1863. #endif /* 0 */
  1864. done:
  1865. tor_x509_cert_free(cert);
  1866. tor_x509_cert_free(scert);
  1867. }
  1868. static void
  1869. test_tortls_context_init_one(void *ignored)
  1870. {
  1871. (void)ignored;
  1872. int ret;
  1873. tor_tls_context_t *old = NULL;
  1874. MOCK(crypto_pk_new, fixed_crypto_pk_new);
  1875. fixed_crypto_pk_new_result_index = 0;
  1876. fixed_crypto_pk_new_result[0] = NULL;
  1877. ret = tor_tls_context_init_one(&old, NULL, 0, 0, 0);
  1878. tt_int_op(ret, OP_EQ, -1);
  1879. done:
  1880. UNMOCK(crypto_pk_new);
  1881. }
  1882. #define LOCAL_TEST_CASE(name, flags) \
  1883. { #name, test_tortls_##name, (flags|TT_FORK), NULL, NULL }
  1884. #ifdef OPENSSL_OPAQUE
  1885. #define INTRUSIVE_TEST_CASE(name, flags) \
  1886. { #name, NULL, TT_SKIP, NULL, NULL }
  1887. #else
  1888. #define INTRUSIVE_TEST_CASE(name, flags) LOCAL_TEST_CASE(name, flags)
  1889. #endif /* defined(OPENSSL_OPAQUE) */
  1890. struct testcase_t tortls_openssl_tests[] = {
  1891. LOCAL_TEST_CASE(tor_tls_new, TT_FORK),
  1892. LOCAL_TEST_CASE(get_state_description, TT_FORK),
  1893. LOCAL_TEST_CASE(get_by_ssl, TT_FORK),
  1894. LOCAL_TEST_CASE(allocate_tor_tls_object_ex_data_index, TT_FORK),
  1895. LOCAL_TEST_CASE(log_one_error, TT_FORK),
  1896. INTRUSIVE_TEST_CASE(get_error, TT_FORK),
  1897. LOCAL_TEST_CASE(always_accept_verify_cb, 0),
  1898. INTRUSIVE_TEST_CASE(x509_cert_free, 0),
  1899. INTRUSIVE_TEST_CASE(cert_matches_key, 0),
  1900. INTRUSIVE_TEST_CASE(cert_get_key, 0),
  1901. LOCAL_TEST_CASE(get_my_client_auth_key, TT_FORK),
  1902. INTRUSIVE_TEST_CASE(get_ciphersuite_name, 0),
  1903. INTRUSIVE_TEST_CASE(classify_client_ciphers, 0),
  1904. LOCAL_TEST_CASE(client_is_using_v2_ciphers, 0),
  1905. INTRUSIVE_TEST_CASE(get_pending_bytes, 0),
  1906. INTRUSIVE_TEST_CASE(SSL_SESSION_get_master_key, 0),
  1907. INTRUSIVE_TEST_CASE(get_tlssecrets, 0),
  1908. INTRUSIVE_TEST_CASE(get_buffer_sizes, 0),
  1909. INTRUSIVE_TEST_CASE(try_to_extract_certs_from_tls, 0),
  1910. INTRUSIVE_TEST_CASE(get_peer_cert, 0),
  1911. INTRUSIVE_TEST_CASE(peer_has_cert, 0),
  1912. INTRUSIVE_TEST_CASE(shutdown, 0),
  1913. INTRUSIVE_TEST_CASE(finish_handshake, 0),
  1914. INTRUSIVE_TEST_CASE(handshake, 0),
  1915. INTRUSIVE_TEST_CASE(write, 0),
  1916. INTRUSIVE_TEST_CASE(read, 0),
  1917. INTRUSIVE_TEST_CASE(server_info_callback, 0),
  1918. LOCAL_TEST_CASE(get_write_overhead_ratio, TT_FORK),
  1919. LOCAL_TEST_CASE(is_server, 0),
  1920. INTRUSIVE_TEST_CASE(assert_renegotiation_unblocked, 0),
  1921. INTRUSIVE_TEST_CASE(block_renegotiation, 0),
  1922. INTRUSIVE_TEST_CASE(unblock_renegotiation, 0),
  1923. INTRUSIVE_TEST_CASE(set_renegotiate_callback, 0),
  1924. LOCAL_TEST_CASE(set_logged_address, 0),
  1925. INTRUSIVE_TEST_CASE(find_cipher_by_id, 0),
  1926. INTRUSIVE_TEST_CASE(session_secret_cb, 0),
  1927. INTRUSIVE_TEST_CASE(debug_state_callback, 0),
  1928. INTRUSIVE_TEST_CASE(context_new, TT_FORK /* redundant */),
  1929. LOCAL_TEST_CASE(create_certificate, 0),
  1930. LOCAL_TEST_CASE(cert_new, 0),
  1931. LOCAL_TEST_CASE(cert_is_valid, 0),
  1932. LOCAL_TEST_CASE(context_init_one, 0),
  1933. END_OF_TESTCASES
  1934. };