test_tortls.c 76 KB

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