test_tortls_openssl.c 69 KB

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