test_tortls.c 77 KB

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