test_tortls.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834
  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. #ifndef SUPPORT_UNSAFE_RENEGOTIATION_FLAG
  1334. #define SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION 0
  1335. #endif
  1336. tls->ssl->s3->flags = SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
  1337. tor_tls_block_renegotiation(tls);
  1338. #ifndef OPENSSL_1_1_API
  1339. tt_assert(!(tls->ssl->s3->flags &
  1340. SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION));
  1341. #endif
  1342. done:
  1343. tor_free(tls->ssl->s3);
  1344. tor_free(tls->ssl);
  1345. tor_free(tls);
  1346. }
  1347. static void
  1348. test_tortls_unblock_renegotiation(void *ignored)
  1349. {
  1350. (void)ignored;
  1351. tor_tls_t *tls;
  1352. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1353. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1354. tor_tls_unblock_renegotiation(tls);
  1355. tt_uint_op(SSL_get_options(tls->ssl) &
  1356. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, OP_EQ,
  1357. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
  1358. done:
  1359. tor_free(tls->ssl);
  1360. tor_free(tls);
  1361. }
  1362. #endif
  1363. #ifndef OPENSSL_OPAQUE
  1364. static void
  1365. test_tortls_assert_renegotiation_unblocked(void *ignored)
  1366. {
  1367. (void)ignored;
  1368. tor_tls_t *tls;
  1369. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1370. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1371. tor_tls_unblock_renegotiation(tls);
  1372. tor_tls_assert_renegotiation_unblocked(tls);
  1373. /* No assertion here - this test will fail if tor_assert is turned on
  1374. * and things are bad. */
  1375. tor_free(tls->ssl);
  1376. tor_free(tls);
  1377. }
  1378. #endif
  1379. static void
  1380. test_tortls_set_logged_address(void *ignored)
  1381. {
  1382. (void)ignored;
  1383. tor_tls_t *tls;
  1384. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1385. tor_tls_set_logged_address(tls, "foo bar");
  1386. tt_str_op(tls->address, OP_EQ, "foo bar");
  1387. tor_tls_set_logged_address(tls, "foo bar 2");
  1388. tt_str_op(tls->address, OP_EQ, "foo bar 2");
  1389. done:
  1390. tor_free(tls->address);
  1391. tor_free(tls);
  1392. }
  1393. #ifndef OPENSSL_OPAQUE
  1394. static void
  1395. example_cb(tor_tls_t *t, void *arg)
  1396. {
  1397. (void)t;
  1398. (void)arg;
  1399. }
  1400. static void
  1401. test_tortls_set_renegotiate_callback(void *ignored)
  1402. {
  1403. (void)ignored;
  1404. tor_tls_t *tls;
  1405. const char *arg = "hello";
  1406. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1407. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1408. tor_tls_set_renegotiate_callback(tls, example_cb, (void*)arg);
  1409. tt_assert(tls->negotiated_callback == example_cb);
  1410. tt_assert(tls->callback_arg == arg);
  1411. tt_assert(!tls->got_renegotiate);
  1412. /* Assumes V2_HANDSHAKE_SERVER */
  1413. tt_assert(tls->ssl->info_callback == tor_tls_server_info_callback);
  1414. tor_tls_set_renegotiate_callback(tls, NULL, (void*)arg);
  1415. tt_assert(tls->ssl->info_callback == tor_tls_debug_state_callback);
  1416. done:
  1417. tor_free(tls->ssl);
  1418. tor_free(tls);
  1419. }
  1420. #endif
  1421. #ifndef OPENSSL_OPAQUE
  1422. static SSL_CIPHER *fixed_cipher1 = NULL;
  1423. static SSL_CIPHER *fixed_cipher2 = NULL;
  1424. static const SSL_CIPHER *
  1425. fake_get_cipher(unsigned ncipher)
  1426. {
  1427. switch (ncipher) {
  1428. case 1:
  1429. return fixed_cipher1;
  1430. case 2:
  1431. return fixed_cipher2;
  1432. default:
  1433. return NULL;
  1434. }
  1435. }
  1436. #endif
  1437. #ifndef OPENSSL_OPAQUE
  1438. static void
  1439. test_tortls_find_cipher_by_id(void *ignored)
  1440. {
  1441. (void)ignored;
  1442. int ret;
  1443. SSL *ssl;
  1444. SSL_CTX *ctx;
  1445. const SSL_METHOD *m = TLSv1_method();
  1446. SSL_METHOD *empty_method = tor_malloc_zero(sizeof(SSL_METHOD));
  1447. fixed_cipher1 = tor_malloc_zero(sizeof(SSL_CIPHER));
  1448. fixed_cipher2 = tor_malloc_zero(sizeof(SSL_CIPHER));
  1449. fixed_cipher2->id = 0xC00A;
  1450. SSL_library_init();
  1451. SSL_load_error_strings();
  1452. ctx = SSL_CTX_new(m);
  1453. ssl = SSL_new(ctx);
  1454. ret = find_cipher_by_id(ssl, NULL, 0xC00A);
  1455. tt_int_op(ret, OP_EQ, 1);
  1456. ret = find_cipher_by_id(ssl, m, 0xC00A);
  1457. tt_int_op(ret, OP_EQ, 1);
  1458. ret = find_cipher_by_id(ssl, m, 0xFFFF);
  1459. tt_int_op(ret, OP_EQ, 0);
  1460. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1461. tt_int_op(ret, OP_EQ, 1);
  1462. ret = find_cipher_by_id(ssl, empty_method, 0xFFFF);
  1463. #ifdef HAVE_SSL_CIPHER_FIND
  1464. tt_int_op(ret, OP_EQ, 0);
  1465. #else
  1466. tt_int_op(ret, OP_EQ, 1);
  1467. #endif
  1468. empty_method->get_cipher = fake_get_cipher;
  1469. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1470. tt_int_op(ret, OP_EQ, 1);
  1471. empty_method->get_cipher = m->get_cipher;
  1472. empty_method->num_ciphers = m->num_ciphers;
  1473. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1474. tt_int_op(ret, OP_EQ, 1);
  1475. empty_method->get_cipher = fake_get_cipher;
  1476. empty_method->num_ciphers = m->num_ciphers;
  1477. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1478. tt_int_op(ret, OP_EQ, 1);
  1479. empty_method->num_ciphers = fake_num_ciphers;
  1480. ret = find_cipher_by_id(ssl, empty_method, 0xC00A);
  1481. #ifdef HAVE_SSL_CIPHER_FIND
  1482. tt_int_op(ret, OP_EQ, 1);
  1483. #else
  1484. tt_int_op(ret, OP_EQ, 0);
  1485. #endif
  1486. done:
  1487. tor_free(empty_method);
  1488. SSL_free(ssl);
  1489. SSL_CTX_free(ctx);
  1490. tor_free(fixed_cipher1);
  1491. }
  1492. #endif
  1493. #ifndef OPENSSL_OPAQUE
  1494. static void
  1495. test_tortls_debug_state_callback(void *ignored)
  1496. {
  1497. (void)ignored;
  1498. SSL *ssl;
  1499. char *buf = tor_malloc_zero(1000);
  1500. int n;
  1501. int previous_log = setup_capture_of_logs(LOG_DEBUG);
  1502. ssl = tor_malloc_zero(sizeof(SSL));
  1503. tor_tls_debug_state_callback(ssl, 32, 45);
  1504. n = tor_snprintf(buf, 1000, "SSL %p is now in state unknown"
  1505. " state [type=32,val=45].\n", ssl);
  1506. /* tor's snprintf returns -1 on error */
  1507. tt_int_op(n, OP_NE, -1);
  1508. expect_log_msg(buf);
  1509. done:
  1510. teardown_capture_of_logs(previous_log);
  1511. tor_free(buf);
  1512. tor_free(ssl);
  1513. }
  1514. #endif
  1515. #ifndef OPENSSL_OPAQUE
  1516. static void
  1517. test_tortls_server_info_callback(void *ignored)
  1518. {
  1519. (void)ignored;
  1520. tor_tls_t *tls;
  1521. SSL_CTX *ctx;
  1522. SSL *ssl;
  1523. int previous_log = setup_capture_of_logs(LOG_WARN);
  1524. SSL_library_init();
  1525. SSL_load_error_strings();
  1526. ctx = SSL_CTX_new(TLSv1_method());
  1527. ssl = SSL_new(ctx);
  1528. tor_tls_allocate_tor_tls_object_ex_data_index();
  1529. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1530. tls->magic = TOR_TLS_MAGIC;
  1531. tls->ssl = ssl;
  1532. tor_tls_server_info_callback(NULL, 0, 0);
  1533. SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_A);
  1534. mock_clean_saved_logs();
  1535. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1536. expect_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
  1537. SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_B);
  1538. mock_clean_saved_logs();
  1539. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1540. expect_log_msg("Couldn't look up the tls for an SSL*. How odd!\n");
  1541. SSL_set_state(ssl, 99);
  1542. mock_clean_saved_logs();
  1543. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1544. expect_no_log_entry();
  1545. SSL_set_ex_data(tls->ssl, tor_tls_object_ex_data_index, tls);
  1546. SSL_set_state(ssl, SSL3_ST_SW_SRVR_HELLO_B);
  1547. tls->negotiated_callback = 0;
  1548. tls->server_handshake_count = 120;
  1549. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1550. tt_int_op(tls->server_handshake_count, OP_EQ, 121);
  1551. tls->server_handshake_count = 127;
  1552. tls->negotiated_callback = (void *)1;
  1553. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1554. tt_int_op(tls->server_handshake_count, OP_EQ, 127);
  1555. tt_int_op(tls->got_renegotiate, OP_EQ, 1);
  1556. tls->ssl->session = SSL_SESSION_new();
  1557. tls->wasV2Handshake = 0;
  1558. tor_tls_server_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 0);
  1559. tt_int_op(tls->wasV2Handshake, OP_EQ, 0);
  1560. done:
  1561. teardown_capture_of_logs(previous_log);
  1562. SSL_free(ssl);
  1563. SSL_CTX_free(ctx);
  1564. tor_free(tls);
  1565. }
  1566. #endif
  1567. #ifndef OPENSSL_OPAQUE
  1568. static int fixed_ssl_read_result_index;
  1569. static int fixed_ssl_read_result[5];
  1570. static int fixed_ssl_shutdown_result;
  1571. static int
  1572. fixed_ssl_read(SSL *s, void *buf, int len)
  1573. {
  1574. (void)s;
  1575. (void)buf;
  1576. (void)len;
  1577. return fixed_ssl_read_result[fixed_ssl_read_result_index++];
  1578. }
  1579. static int
  1580. fixed_ssl_shutdown(SSL *s)
  1581. {
  1582. (void)s;
  1583. return fixed_ssl_shutdown_result;
  1584. }
  1585. #ifndef LIBRESSL_VERSION_NUMBER
  1586. static int fixed_ssl_state_to_set;
  1587. static tor_tls_t *fixed_tls;
  1588. static int
  1589. setting_version_ssl_shutdown(SSL *s)
  1590. {
  1591. s->version = SSL2_VERSION;
  1592. return fixed_ssl_shutdown_result;
  1593. }
  1594. static int
  1595. setting_version_and_state_ssl_shutdown(SSL *s)
  1596. {
  1597. fixed_tls->state = fixed_ssl_state_to_set;
  1598. s->version = SSL2_VERSION;
  1599. return fixed_ssl_shutdown_result;
  1600. }
  1601. #endif
  1602. static int
  1603. dummy_handshake_func(SSL *s)
  1604. {
  1605. (void)s;
  1606. return 1;
  1607. }
  1608. static void
  1609. test_tortls_shutdown(void *ignored)
  1610. {
  1611. (void)ignored;
  1612. int ret;
  1613. tor_tls_t *tls;
  1614. SSL_METHOD *method = give_me_a_test_method();
  1615. int previous_log = setup_capture_of_logs(LOG_WARN);
  1616. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1617. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1618. tls->ssl->method = method;
  1619. method->ssl_read = fixed_ssl_read;
  1620. method->ssl_shutdown = fixed_ssl_shutdown;
  1621. ret = tor_tls_shutdown(tls);
  1622. tt_int_op(ret, OP_EQ, -9);
  1623. tls->state = TOR_TLS_ST_SENTCLOSE;
  1624. fixed_ssl_read_result_index = 0;
  1625. fixed_ssl_read_result[0] = 10;
  1626. fixed_ssl_read_result[1] = -1;
  1627. ret = tor_tls_shutdown(tls);
  1628. tt_int_op(ret, OP_EQ, -9);
  1629. #ifndef LIBRESSL_VERSION_NUMBER
  1630. tls->ssl->handshake_func = dummy_handshake_func;
  1631. fixed_ssl_read_result_index = 0;
  1632. fixed_ssl_read_result[0] = 10;
  1633. fixed_ssl_read_result[1] = 42;
  1634. fixed_ssl_read_result[2] = 0;
  1635. fixed_ssl_shutdown_result = 1;
  1636. ERR_clear_error();
  1637. tls->ssl->version = SSL2_VERSION;
  1638. ret = tor_tls_shutdown(tls);
  1639. tt_int_op(ret, OP_EQ, TOR_TLS_DONE);
  1640. tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_CLOSED);
  1641. fixed_ssl_read_result_index = 0;
  1642. fixed_ssl_read_result[0] = 10;
  1643. fixed_ssl_read_result[1] = 42;
  1644. fixed_ssl_read_result[2] = 0;
  1645. fixed_ssl_shutdown_result = 0;
  1646. ERR_clear_error();
  1647. tls->ssl->version = 0;
  1648. ret = tor_tls_shutdown(tls);
  1649. tt_int_op(ret, OP_EQ, TOR_TLS_DONE);
  1650. tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_CLOSED);
  1651. fixed_ssl_read_result_index = 0;
  1652. fixed_ssl_read_result[0] = 10;
  1653. fixed_ssl_read_result[1] = 42;
  1654. fixed_ssl_read_result[2] = 0;
  1655. fixed_ssl_shutdown_result = 0;
  1656. ERR_clear_error();
  1657. tls->ssl->version = 0;
  1658. method->ssl_shutdown = setting_version_ssl_shutdown;
  1659. ret = tor_tls_shutdown(tls);
  1660. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1661. fixed_ssl_read_result_index = 0;
  1662. fixed_ssl_read_result[0] = 10;
  1663. fixed_ssl_read_result[1] = 42;
  1664. fixed_ssl_read_result[2] = 0;
  1665. fixed_ssl_shutdown_result = 0;
  1666. fixed_tls = tls;
  1667. fixed_ssl_state_to_set = TOR_TLS_ST_GOTCLOSE;
  1668. ERR_clear_error();
  1669. tls->ssl->version = 0;
  1670. method->ssl_shutdown = setting_version_and_state_ssl_shutdown;
  1671. ret = tor_tls_shutdown(tls);
  1672. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1673. fixed_ssl_read_result_index = 0;
  1674. fixed_ssl_read_result[0] = 10;
  1675. fixed_ssl_read_result[1] = 42;
  1676. fixed_ssl_read_result[2] = 0;
  1677. fixed_ssl_read_result[3] = -1;
  1678. fixed_ssl_shutdown_result = 0;
  1679. fixed_tls = tls;
  1680. fixed_ssl_state_to_set = 0;
  1681. ERR_clear_error();
  1682. tls->ssl->version = 0;
  1683. method->ssl_shutdown = setting_version_and_state_ssl_shutdown;
  1684. ret = tor_tls_shutdown(tls);
  1685. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1686. #endif
  1687. done:
  1688. teardown_capture_of_logs(previous_log);
  1689. tor_free(method);
  1690. tor_free(tls->ssl);
  1691. tor_free(tls);
  1692. }
  1693. static int negotiated_callback_called;
  1694. static void
  1695. negotiated_callback_setter(tor_tls_t *t, void *arg)
  1696. {
  1697. (void)t;
  1698. (void)arg;
  1699. negotiated_callback_called++;
  1700. }
  1701. static void
  1702. test_tortls_read(void *ignored)
  1703. {
  1704. (void)ignored;
  1705. int ret;
  1706. tor_tls_t *tls;
  1707. char buf[100];
  1708. SSL_METHOD *method = give_me_a_test_method();
  1709. int previous_log = setup_capture_of_logs(LOG_WARN);
  1710. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1711. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1712. tls->state = TOR_TLS_ST_OPEN;
  1713. ret = tor_tls_read(tls, buf, 10);
  1714. tt_int_op(ret, OP_EQ, -9);
  1715. /* These tests assume that V2_HANDSHAKE_SERVER is set */
  1716. tls->ssl->handshake_func = dummy_handshake_func;
  1717. tls->ssl->method = method;
  1718. method->ssl_read = fixed_ssl_read;
  1719. fixed_ssl_read_result_index = 0;
  1720. fixed_ssl_read_result[0] = 42;
  1721. tls->state = TOR_TLS_ST_OPEN;
  1722. ERR_clear_error();
  1723. ret = tor_tls_read(tls, buf, 10);
  1724. tt_int_op(ret, OP_EQ, 42);
  1725. tls->state = TOR_TLS_ST_OPEN;
  1726. tls->got_renegotiate = 1;
  1727. fixed_ssl_read_result_index = 0;
  1728. ERR_clear_error();
  1729. ret = tor_tls_read(tls, buf, 10);
  1730. tt_int_op(tls->got_renegotiate, OP_EQ, 0);
  1731. tls->state = TOR_TLS_ST_OPEN;
  1732. tls->got_renegotiate = 1;
  1733. negotiated_callback_called = 0;
  1734. tls->negotiated_callback = negotiated_callback_setter;
  1735. fixed_ssl_read_result_index = 0;
  1736. ERR_clear_error();
  1737. ret = tor_tls_read(tls, buf, 10);
  1738. tt_int_op(negotiated_callback_called, OP_EQ, 1);
  1739. #ifndef LIBRESSL_VERSION_NUMBER
  1740. fixed_ssl_read_result_index = 0;
  1741. fixed_ssl_read_result[0] = 0;
  1742. tls->ssl->version = SSL2_VERSION;
  1743. ERR_clear_error();
  1744. ret = tor_tls_read(tls, buf, 10);
  1745. tt_int_op(ret, OP_EQ, TOR_TLS_CLOSE);
  1746. tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_CLOSED);
  1747. #endif
  1748. // TODO: fill up
  1749. done:
  1750. teardown_capture_of_logs(previous_log);
  1751. tor_free(tls->ssl);
  1752. tor_free(tls);
  1753. tor_free(method);
  1754. }
  1755. static int fixed_ssl_write_result;
  1756. static int
  1757. fixed_ssl_write(SSL *s, const void *buf, int len)
  1758. {
  1759. (void)s;
  1760. (void)buf;
  1761. (void)len;
  1762. return fixed_ssl_write_result;
  1763. }
  1764. static void
  1765. test_tortls_write(void *ignored)
  1766. {
  1767. (void)ignored;
  1768. int ret;
  1769. tor_tls_t *tls;
  1770. SSL_METHOD *method = give_me_a_test_method();
  1771. char buf[100];
  1772. int previous_log = setup_capture_of_logs(LOG_WARN);
  1773. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1774. tls->ssl = tor_malloc_zero(sizeof(SSL));
  1775. tls->state = TOR_TLS_ST_OPEN;
  1776. ret = tor_tls_write(tls, buf, 0);
  1777. tt_int_op(ret, OP_EQ, 0);
  1778. ret = tor_tls_write(tls, buf, 10);
  1779. tt_int_op(ret, OP_EQ, -9);
  1780. tls->ssl->method = method;
  1781. tls->wantwrite_n = 1;
  1782. ret = tor_tls_write(tls, buf, 10);
  1783. tt_int_op(tls->wantwrite_n, OP_EQ, 0);
  1784. method->ssl_write = fixed_ssl_write;
  1785. tls->ssl->handshake_func = dummy_handshake_func;
  1786. fixed_ssl_write_result = 1;
  1787. ERR_clear_error();
  1788. ret = tor_tls_write(tls, buf, 10);
  1789. tt_int_op(ret, OP_EQ, 1);
  1790. fixed_ssl_write_result = -1;
  1791. ERR_clear_error();
  1792. tls->ssl->rwstate = SSL_READING;
  1793. SSL_set_bio(tls->ssl, BIO_new(BIO_s_mem()), NULL);
  1794. SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_READ;
  1795. ret = tor_tls_write(tls, buf, 10);
  1796. tt_int_op(ret, OP_EQ, TOR_TLS_WANTREAD);
  1797. ERR_clear_error();
  1798. tls->ssl->rwstate = SSL_READING;
  1799. SSL_set_bio(tls->ssl, BIO_new(BIO_s_mem()), NULL);
  1800. SSL_get_rbio(tls->ssl)->flags = BIO_FLAGS_WRITE;
  1801. ret = tor_tls_write(tls, buf, 10);
  1802. tt_int_op(ret, OP_EQ, TOR_TLS_WANTWRITE);
  1803. done:
  1804. teardown_capture_of_logs(previous_log);
  1805. BIO_free(tls->ssl->rbio);
  1806. tor_free(tls->ssl);
  1807. tor_free(tls);
  1808. tor_free(method);
  1809. }
  1810. #endif
  1811. #ifndef OPENSSL_OPAQUE
  1812. static int fixed_ssl_accept_result;
  1813. static int fixed_ssl_connect_result;
  1814. static int
  1815. setting_error_ssl_accept(SSL *ssl)
  1816. {
  1817. (void)ssl;
  1818. ERR_put_error(ERR_LIB_BN, 2, -1, "somewhere.c", 99);
  1819. ERR_put_error(ERR_LIB_SYS, 2, -1, "somewhere.c", 99);
  1820. return fixed_ssl_accept_result;
  1821. }
  1822. static int
  1823. setting_error_ssl_connect(SSL *ssl)
  1824. {
  1825. (void)ssl;
  1826. ERR_put_error(ERR_LIB_BN, 2, -1, "somewhere.c", 99);
  1827. ERR_put_error(ERR_LIB_SYS, 2, -1, "somewhere.c", 99);
  1828. return fixed_ssl_connect_result;
  1829. }
  1830. static int
  1831. fixed_ssl_accept(SSL *ssl)
  1832. {
  1833. (void) ssl;
  1834. return fixed_ssl_accept_result;
  1835. }
  1836. static void
  1837. test_tortls_handshake(void *ignored)
  1838. {
  1839. (void)ignored;
  1840. int ret;
  1841. tor_tls_t *tls;
  1842. SSL_CTX *ctx;
  1843. SSL_METHOD *method = give_me_a_test_method();
  1844. int previous_log = setup_capture_of_logs(LOG_INFO);
  1845. SSL_library_init();
  1846. SSL_load_error_strings();
  1847. ctx = SSL_CTX_new(TLSv1_method());
  1848. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1849. tls->ssl = SSL_new(ctx);
  1850. tls->state = TOR_TLS_ST_HANDSHAKE;
  1851. ret = tor_tls_handshake(tls);
  1852. tt_int_op(ret, OP_EQ, -9);
  1853. tls->isServer = 1;
  1854. tls->state = TOR_TLS_ST_HANDSHAKE;
  1855. ret = tor_tls_handshake(tls);
  1856. tt_int_op(ret, OP_EQ, -9);
  1857. tls->ssl->method = method;
  1858. method->ssl_accept = fixed_ssl_accept;
  1859. fixed_ssl_accept_result = 2;
  1860. ERR_clear_error();
  1861. tls->state = TOR_TLS_ST_HANDSHAKE;
  1862. ret = tor_tls_handshake(tls);
  1863. tt_int_op(tls->state, OP_EQ, TOR_TLS_ST_OPEN);
  1864. method->ssl_accept = setting_error_ssl_accept;
  1865. fixed_ssl_accept_result = 1;
  1866. ERR_clear_error();
  1867. mock_clean_saved_logs();
  1868. tls->state = TOR_TLS_ST_HANDSHAKE;
  1869. ret = tor_tls_handshake(tls);
  1870. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1871. expect_log_entry();
  1872. /* This fails on jessie. Investigate why! */
  1873. #if 0
  1874. expect_log_msg("TLS error while handshaking: (null) (in bignum routines:"
  1875. "(null):SSLv3 write client hello B)\n");
  1876. expect_log_msg("TLS error while handshaking: (null) (in system library:"
  1877. "connect:SSLv3 write client hello B)\n");
  1878. #endif
  1879. expect_log_severity(LOG_INFO);
  1880. tls->isServer = 0;
  1881. method->ssl_connect = setting_error_ssl_connect;
  1882. fixed_ssl_connect_result = 1;
  1883. ERR_clear_error();
  1884. mock_clean_saved_logs();
  1885. tls->state = TOR_TLS_ST_HANDSHAKE;
  1886. ret = tor_tls_handshake(tls);
  1887. tt_int_op(ret, OP_EQ, TOR_TLS_ERROR_MISC);
  1888. expect_log_entry();
  1889. #if 0
  1890. /* See above */
  1891. expect_log_msg("TLS error while handshaking: "
  1892. "(null) (in bignum routines:(null):SSLv3 write client hello B)\n");
  1893. expect_log_msg("TLS error while handshaking: "
  1894. "(null) (in system library:connect:SSLv3 write client hello B)\n");
  1895. #endif
  1896. expect_log_severity(LOG_WARN);
  1897. done:
  1898. teardown_capture_of_logs(previous_log);
  1899. SSL_free(tls->ssl);
  1900. SSL_CTX_free(ctx);
  1901. tor_free(tls);
  1902. tor_free(method);
  1903. }
  1904. #endif
  1905. #ifndef OPENSSL_OPAQUE
  1906. static void
  1907. test_tortls_finish_handshake(void *ignored)
  1908. {
  1909. (void)ignored;
  1910. int ret;
  1911. tor_tls_t *tls;
  1912. SSL_CTX *ctx;
  1913. SSL_METHOD *method = give_me_a_test_method();
  1914. SSL_library_init();
  1915. SSL_load_error_strings();
  1916. X509 *c1 = read_cert_from(validCertString);
  1917. SESS_CERT_local *sess = NULL;
  1918. ctx = SSL_CTX_new(method);
  1919. tls = tor_malloc_zero(sizeof(tor_tls_t));
  1920. tls->ssl = SSL_new(ctx);
  1921. tls->state = TOR_TLS_ST_OPEN;
  1922. ret = tor_tls_finish_handshake(tls);
  1923. tt_int_op(ret, OP_EQ, 0);
  1924. tls->isServer = 1;
  1925. tls->wasV2Handshake = 0;
  1926. ret = tor_tls_finish_handshake(tls);
  1927. tt_int_op(ret, OP_EQ, 0);
  1928. tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
  1929. tls->wasV2Handshake = 1;
  1930. ret = tor_tls_finish_handshake(tls);
  1931. tt_int_op(ret, OP_EQ, 0);
  1932. tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
  1933. tls->wasV2Handshake = 1;
  1934. tls->ssl->session = SSL_SESSION_new();
  1935. ret = tor_tls_finish_handshake(tls);
  1936. tt_int_op(ret, OP_EQ, 0);
  1937. tt_int_op(tls->wasV2Handshake, OP_EQ, 0);
  1938. tls->isServer = 0;
  1939. sess = tor_malloc_zero(sizeof(SESS_CERT_local));
  1940. tls->ssl->session->sess_cert = (void *)sess;
  1941. sess->cert_chain = sk_X509_new_null();
  1942. sk_X509_push(sess->cert_chain, c1);
  1943. tls->ssl->session->peer = c1;
  1944. tls->wasV2Handshake = 0;
  1945. ret = tor_tls_finish_handshake(tls);
  1946. tt_int_op(ret, OP_EQ, 0);
  1947. tt_int_op(tls->wasV2Handshake, OP_EQ, 1);
  1948. method->num_ciphers = fake_num_ciphers;
  1949. ret = tor_tls_finish_handshake(tls);
  1950. tt_int_op(ret, OP_EQ, -9);
  1951. done:
  1952. if (sess)
  1953. sk_X509_free(sess->cert_chain);
  1954. if (tls->ssl && tls->ssl->session) {
  1955. tor_free(tls->ssl->session->sess_cert);
  1956. }
  1957. SSL_free(tls->ssl);
  1958. tor_free(tls);
  1959. SSL_CTX_free(ctx);
  1960. tor_free(method);
  1961. }
  1962. #endif
  1963. static int fixed_crypto_pk_new_result_index;
  1964. static crypto_pk_t *fixed_crypto_pk_new_result[5];
  1965. static crypto_pk_t *
  1966. fixed_crypto_pk_new(void)
  1967. {
  1968. return fixed_crypto_pk_new_result[fixed_crypto_pk_new_result_index++];
  1969. }
  1970. #ifndef OPENSSL_OPAQUE
  1971. static int fixed_crypto_pk_generate_key_with_bits_result_index;
  1972. static int fixed_crypto_pk_generate_key_with_bits_result[5];
  1973. static int fixed_tor_tls_create_certificate_result_index;
  1974. static X509 *fixed_tor_tls_create_certificate_result[5];
  1975. static int fixed_tor_x509_cert_new_result_index;
  1976. static tor_x509_cert_t *fixed_tor_x509_cert_new_result[5];
  1977. static int
  1978. fixed_crypto_pk_generate_key_with_bits(crypto_pk_t *env, int bits)
  1979. {
  1980. (void)env;
  1981. (void)bits;
  1982. return fixed_crypto_pk_generate_key_with_bits_result[
  1983. fixed_crypto_pk_generate_key_with_bits_result_index++];
  1984. }
  1985. static X509 *
  1986. fixed_tor_tls_create_certificate(crypto_pk_t *rsa,
  1987. crypto_pk_t *rsa_sign,
  1988. const char *cname,
  1989. const char *cname_sign,
  1990. unsigned int cert_lifetime)
  1991. {
  1992. (void)rsa;
  1993. (void)rsa_sign;
  1994. (void)cname;
  1995. (void)cname_sign;
  1996. (void)cert_lifetime;
  1997. return fixed_tor_tls_create_certificate_result[
  1998. fixed_tor_tls_create_certificate_result_index++];
  1999. }
  2000. static tor_x509_cert_t *
  2001. fixed_tor_x509_cert_new(X509 *x509_cert)
  2002. {
  2003. (void) x509_cert;
  2004. return fixed_tor_x509_cert_new_result[
  2005. fixed_tor_x509_cert_new_result_index++];
  2006. }
  2007. static void
  2008. test_tortls_context_new(void *ignored)
  2009. {
  2010. (void)ignored;
  2011. tor_tls_context_t *ret;
  2012. crypto_pk_t *pk1, *pk2, *pk3, *pk4, *pk5, *pk6, *pk7, *pk8, *pk9, *pk10,
  2013. *pk11, *pk12, *pk13, *pk14, *pk15, *pk16, *pk17, *pk18;
  2014. pk1 = crypto_pk_new();
  2015. pk2 = crypto_pk_new();
  2016. pk3 = crypto_pk_new();
  2017. pk4 = crypto_pk_new();
  2018. pk5 = crypto_pk_new();
  2019. pk6 = crypto_pk_new();
  2020. pk7 = crypto_pk_new();
  2021. pk8 = crypto_pk_new();
  2022. pk9 = crypto_pk_new();
  2023. pk10 = crypto_pk_new();
  2024. pk11 = crypto_pk_new();
  2025. pk12 = crypto_pk_new();
  2026. pk13 = crypto_pk_new();
  2027. pk14 = crypto_pk_new();
  2028. pk15 = crypto_pk_new();
  2029. pk16 = crypto_pk_new();
  2030. pk17 = crypto_pk_new();
  2031. pk18 = crypto_pk_new();
  2032. fixed_crypto_pk_new_result_index = 0;
  2033. fixed_crypto_pk_new_result[0] = NULL;
  2034. MOCK(crypto_pk_new, fixed_crypto_pk_new);
  2035. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2036. tt_assert(!ret);
  2037. MOCK(crypto_pk_generate_key_with_bits,
  2038. fixed_crypto_pk_generate_key_with_bits);
  2039. fixed_crypto_pk_new_result_index = 0;
  2040. fixed_crypto_pk_new_result[0] = pk1;
  2041. fixed_crypto_pk_new_result[1] = NULL;
  2042. fixed_crypto_pk_generate_key_with_bits_result[0] = -1;
  2043. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2044. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2045. tt_assert(!ret);
  2046. fixed_crypto_pk_new_result_index = 0;
  2047. fixed_crypto_pk_new_result[0] = pk2;
  2048. fixed_crypto_pk_new_result[1] = NULL;
  2049. fixed_crypto_pk_generate_key_with_bits_result[0] = 0;
  2050. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2051. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2052. tt_assert(!ret);
  2053. fixed_crypto_pk_new_result_index = 0;
  2054. fixed_crypto_pk_new_result[0] = pk3;
  2055. fixed_crypto_pk_new_result[1] = pk4;
  2056. fixed_crypto_pk_new_result[2] = NULL;
  2057. fixed_crypto_pk_generate_key_with_bits_result[0] = 0;
  2058. fixed_crypto_pk_generate_key_with_bits_result[1] = -1;
  2059. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2060. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2061. tt_assert(!ret);
  2062. MOCK(tor_tls_create_certificate, fixed_tor_tls_create_certificate);
  2063. fixed_crypto_pk_new_result_index = 0;
  2064. fixed_crypto_pk_new_result[0] = pk5;
  2065. fixed_crypto_pk_new_result[1] = pk6;
  2066. fixed_crypto_pk_new_result[2] = NULL;
  2067. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2068. fixed_crypto_pk_generate_key_with_bits_result[1] = 0;
  2069. fixed_tor_tls_create_certificate_result_index = 0;
  2070. fixed_tor_tls_create_certificate_result[0] = NULL;
  2071. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2072. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2073. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2074. tt_assert(!ret);
  2075. fixed_crypto_pk_new_result_index = 0;
  2076. fixed_crypto_pk_new_result[0] = pk7;
  2077. fixed_crypto_pk_new_result[1] = pk8;
  2078. fixed_crypto_pk_new_result[2] = NULL;
  2079. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2080. fixed_tor_tls_create_certificate_result_index = 0;
  2081. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2082. fixed_tor_tls_create_certificate_result[1] = NULL;
  2083. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2084. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2085. tt_assert(!ret);
  2086. fixed_crypto_pk_new_result_index = 0;
  2087. fixed_crypto_pk_new_result[0] = pk9;
  2088. fixed_crypto_pk_new_result[1] = pk10;
  2089. fixed_crypto_pk_new_result[2] = NULL;
  2090. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2091. fixed_tor_tls_create_certificate_result_index = 0;
  2092. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2093. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2094. fixed_tor_tls_create_certificate_result[2] = NULL;
  2095. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2096. tt_assert(!ret);
  2097. MOCK(tor_x509_cert_new, fixed_tor_x509_cert_new);
  2098. fixed_crypto_pk_new_result_index = 0;
  2099. fixed_crypto_pk_new_result[0] = pk11;
  2100. fixed_crypto_pk_new_result[1] = pk12;
  2101. fixed_crypto_pk_new_result[2] = NULL;
  2102. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2103. fixed_tor_tls_create_certificate_result_index = 0;
  2104. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2105. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2106. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2107. fixed_tor_x509_cert_new_result_index = 0;
  2108. fixed_tor_x509_cert_new_result[0] = NULL;
  2109. fixed_tor_x509_cert_new_result[1] = NULL;
  2110. fixed_tor_x509_cert_new_result[2] = NULL;
  2111. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2112. tt_assert(!ret);
  2113. fixed_crypto_pk_new_result_index = 0;
  2114. fixed_crypto_pk_new_result[0] = pk13;
  2115. fixed_crypto_pk_new_result[1] = pk14;
  2116. fixed_crypto_pk_new_result[2] = NULL;
  2117. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2118. fixed_tor_tls_create_certificate_result_index = 0;
  2119. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2120. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2121. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2122. fixed_tor_x509_cert_new_result_index = 0;
  2123. fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2124. fixed_tor_x509_cert_new_result[1] = NULL;
  2125. fixed_tor_x509_cert_new_result[2] = NULL;
  2126. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2127. tt_assert(!ret);
  2128. fixed_crypto_pk_new_result_index = 0;
  2129. fixed_crypto_pk_new_result[0] = pk15;
  2130. fixed_crypto_pk_new_result[1] = pk16;
  2131. fixed_crypto_pk_new_result[2] = NULL;
  2132. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2133. fixed_tor_tls_create_certificate_result_index = 0;
  2134. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2135. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2136. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2137. fixed_tor_x509_cert_new_result_index = 0;
  2138. fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2139. fixed_tor_x509_cert_new_result[1] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2140. fixed_tor_x509_cert_new_result[2] = NULL;
  2141. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2142. tt_assert(!ret);
  2143. fixed_crypto_pk_new_result_index = 0;
  2144. fixed_crypto_pk_new_result[0] = pk17;
  2145. fixed_crypto_pk_new_result[1] = pk18;
  2146. fixed_crypto_pk_new_result[2] = NULL;
  2147. fixed_crypto_pk_generate_key_with_bits_result_index = 0;
  2148. fixed_tor_tls_create_certificate_result_index = 0;
  2149. fixed_tor_tls_create_certificate_result[0] = tor_malloc_zero(sizeof(X509));
  2150. fixed_tor_tls_create_certificate_result[1] = tor_malloc_zero(sizeof(X509));
  2151. fixed_tor_tls_create_certificate_result[2] = tor_malloc_zero(sizeof(X509));
  2152. fixed_tor_x509_cert_new_result_index = 0;
  2153. fixed_tor_x509_cert_new_result[0] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2154. fixed_tor_x509_cert_new_result[1] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2155. fixed_tor_x509_cert_new_result[2] = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2156. ret = tor_tls_context_new(NULL, 0, 0, 0);
  2157. tt_assert(!ret);
  2158. done:
  2159. UNMOCK(tor_x509_cert_new);
  2160. UNMOCK(tor_tls_create_certificate);
  2161. UNMOCK(crypto_pk_generate_key_with_bits);
  2162. UNMOCK(crypto_pk_new);
  2163. }
  2164. #endif
  2165. static int fixed_crypto_pk_get_evp_pkey_result_index = 0;
  2166. static EVP_PKEY *fixed_crypto_pk_get_evp_pkey_result[5];
  2167. static EVP_PKEY *
  2168. fixed_crypto_pk_get_evp_pkey_(crypto_pk_t *env, int private)
  2169. {
  2170. (void) env;
  2171. (void) private;
  2172. return fixed_crypto_pk_get_evp_pkey_result[
  2173. fixed_crypto_pk_get_evp_pkey_result_index++];
  2174. }
  2175. static void
  2176. test_tortls_create_certificate(void *ignored)
  2177. {
  2178. (void)ignored;
  2179. X509 *ret;
  2180. crypto_pk_t *pk1, *pk2;
  2181. pk1 = crypto_pk_new();
  2182. pk2 = crypto_pk_new();
  2183. MOCK(crypto_pk_get_evp_pkey_, fixed_crypto_pk_get_evp_pkey_);
  2184. fixed_crypto_pk_get_evp_pkey_result_index = 0;
  2185. fixed_crypto_pk_get_evp_pkey_result[0] = NULL;
  2186. ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
  2187. tt_assert(!ret);
  2188. fixed_crypto_pk_get_evp_pkey_result_index = 0;
  2189. fixed_crypto_pk_get_evp_pkey_result[0] = EVP_PKEY_new();
  2190. fixed_crypto_pk_get_evp_pkey_result[1] = NULL;
  2191. ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
  2192. tt_assert(!ret);
  2193. fixed_crypto_pk_get_evp_pkey_result_index = 0;
  2194. fixed_crypto_pk_get_evp_pkey_result[0] = EVP_PKEY_new();
  2195. fixed_crypto_pk_get_evp_pkey_result[1] = EVP_PKEY_new();
  2196. ret = tor_tls_create_certificate(pk1, pk2, "hello", "hello2", 1);
  2197. tt_assert(!ret);
  2198. done:
  2199. UNMOCK(crypto_pk_get_evp_pkey_);
  2200. crypto_pk_free(pk1);
  2201. crypto_pk_free(pk2);
  2202. }
  2203. static void
  2204. test_tortls_cert_new(void *ignored)
  2205. {
  2206. (void)ignored;
  2207. tor_x509_cert_t *ret;
  2208. X509 *cert = read_cert_from(validCertString);
  2209. ret = tor_x509_cert_new(NULL);
  2210. tt_assert(!ret);
  2211. ret = tor_x509_cert_new(cert);
  2212. tt_assert(ret);
  2213. tor_x509_cert_free(ret);
  2214. ret = NULL;
  2215. #if 0
  2216. cert = read_cert_from(validCertString);
  2217. /* XXX this doesn't do what you think: it alters a copy of the pubkey. */
  2218. X509_get_pubkey(cert)->type = EVP_PKEY_DSA;
  2219. ret = tor_x509_cert_new(cert);
  2220. tt_assert(ret);
  2221. #endif
  2222. #ifndef OPENSSL_OPAQUE
  2223. cert = read_cert_from(validCertString);
  2224. X509_CINF_free(cert->cert_info);
  2225. cert->cert_info = NULL;
  2226. ret = tor_x509_cert_new(cert);
  2227. tt_assert(ret);
  2228. #endif
  2229. done:
  2230. tor_x509_cert_free(ret);
  2231. }
  2232. static void
  2233. test_tortls_cert_is_valid(void *ignored)
  2234. {
  2235. (void)ignored;
  2236. int ret;
  2237. tor_x509_cert_t *cert = NULL, *scert = NULL;
  2238. scert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2239. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2240. tt_int_op(ret, OP_EQ, 0);
  2241. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  2242. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2243. tt_int_op(ret, OP_EQ, 0);
  2244. tor_free(scert);
  2245. tor_free(cert);
  2246. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2247. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2248. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2249. tt_int_op(ret, OP_EQ, 1);
  2250. #ifndef OPENSSL_OPAQUE
  2251. tor_x509_cert_free(cert);
  2252. tor_x509_cert_free(scert);
  2253. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2254. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2255. ASN1_TIME_free(cert->cert->cert_info->validity->notAfter);
  2256. cert->cert->cert_info->validity->notAfter =
  2257. ASN1_TIME_set(NULL, time(NULL)-1000000);
  2258. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2259. tt_int_op(ret, OP_EQ, 0);
  2260. tor_x509_cert_free(cert);
  2261. tor_x509_cert_free(scert);
  2262. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2263. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2264. X509_PUBKEY_free(cert->cert->cert_info->key);
  2265. cert->cert->cert_info->key = NULL;
  2266. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 1);
  2267. tt_int_op(ret, OP_EQ, 0);
  2268. #endif
  2269. #if 0
  2270. tor_x509_cert_free(cert);
  2271. tor_x509_cert_free(scert);
  2272. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2273. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2274. /* This doesn't actually change the key in the cert. XXXXXX */
  2275. BN_one(EVP_PKEY_get1_RSA(X509_get_pubkey(cert->cert))->n);
  2276. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 1);
  2277. tt_int_op(ret, OP_EQ, 0);
  2278. tor_x509_cert_free(cert);
  2279. tor_x509_cert_free(scert);
  2280. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2281. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2282. /* This doesn't actually change the key in the cert. XXXXXX */
  2283. X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
  2284. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 1);
  2285. tt_int_op(ret, OP_EQ, 0);
  2286. tor_x509_cert_free(cert);
  2287. tor_x509_cert_free(scert);
  2288. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2289. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2290. /* This doesn't actually change the key in the cert. XXXXXX */
  2291. X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
  2292. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2293. tt_int_op(ret, OP_EQ, 1);
  2294. tor_x509_cert_free(cert);
  2295. tor_x509_cert_free(scert);
  2296. cert = tor_x509_cert_new(read_cert_from(validCertString));
  2297. scert = tor_x509_cert_new(read_cert_from(caCertString));
  2298. /* This doesn't actually change the key in the cert. XXXXXX */
  2299. X509_get_pubkey(cert->cert)->type = EVP_PKEY_EC;
  2300. X509_get_pubkey(cert->cert)->ameth = NULL;
  2301. ret = tor_tls_cert_is_valid(LOG_WARN, cert, scert, 0);
  2302. tt_int_op(ret, OP_EQ, 0);
  2303. #endif
  2304. done:
  2305. tor_x509_cert_free(cert);
  2306. tor_x509_cert_free(scert);
  2307. }
  2308. static void
  2309. test_tortls_context_init_one(void *ignored)
  2310. {
  2311. (void)ignored;
  2312. int ret;
  2313. tor_tls_context_t *old = NULL;
  2314. MOCK(crypto_pk_new, fixed_crypto_pk_new);
  2315. fixed_crypto_pk_new_result_index = 0;
  2316. fixed_crypto_pk_new_result[0] = NULL;
  2317. ret = tor_tls_context_init_one(&old, NULL, 0, 0, 0);
  2318. tt_int_op(ret, OP_EQ, -1);
  2319. done:
  2320. UNMOCK(crypto_pk_new);
  2321. }
  2322. #define LOCAL_TEST_CASE(name, flags) \
  2323. { #name, test_tortls_##name, (flags|TT_FORK), NULL, NULL }
  2324. #ifdef OPENSSL_OPAQUE
  2325. #define INTRUSIVE_TEST_CASE(name, flags) \
  2326. { #name, NULL, TT_SKIP, NULL, NULL }
  2327. #else
  2328. #define INTRUSIVE_TEST_CASE(name, flags) LOCAL_TEST_CASE(name, flags)
  2329. #endif
  2330. struct testcase_t tortls_tests[] = {
  2331. LOCAL_TEST_CASE(errno_to_tls_error, 0),
  2332. LOCAL_TEST_CASE(err_to_string, 0),
  2333. LOCAL_TEST_CASE(tor_tls_new, TT_FORK),
  2334. LOCAL_TEST_CASE(tor_tls_get_error, 0),
  2335. LOCAL_TEST_CASE(get_state_description, TT_FORK),
  2336. LOCAL_TEST_CASE(get_by_ssl, TT_FORK),
  2337. LOCAL_TEST_CASE(allocate_tor_tls_object_ex_data_index, TT_FORK),
  2338. LOCAL_TEST_CASE(log_one_error, TT_FORK),
  2339. INTRUSIVE_TEST_CASE(get_error, TT_FORK),
  2340. LOCAL_TEST_CASE(always_accept_verify_cb, 0),
  2341. INTRUSIVE_TEST_CASE(x509_cert_free, 0),
  2342. LOCAL_TEST_CASE(x509_cert_get_id_digests, 0),
  2343. INTRUSIVE_TEST_CASE(cert_matches_key, 0),
  2344. INTRUSIVE_TEST_CASE(cert_get_key, 0),
  2345. LOCAL_TEST_CASE(get_my_client_auth_key, TT_FORK),
  2346. LOCAL_TEST_CASE(get_my_certs, TT_FORK),
  2347. INTRUSIVE_TEST_CASE(get_ciphersuite_name, 0),
  2348. INTRUSIVE_TEST_CASE(classify_client_ciphers, 0),
  2349. LOCAL_TEST_CASE(client_is_using_v2_ciphers, 0),
  2350. INTRUSIVE_TEST_CASE(verify, 0),
  2351. INTRUSIVE_TEST_CASE(check_lifetime, 0),
  2352. INTRUSIVE_TEST_CASE(get_pending_bytes, 0),
  2353. LOCAL_TEST_CASE(get_forced_write_size, 0),
  2354. LOCAL_TEST_CASE(get_write_overhead_ratio, TT_FORK),
  2355. LOCAL_TEST_CASE(used_v1_handshake, TT_FORK),
  2356. LOCAL_TEST_CASE(get_num_server_handshakes, 0),
  2357. LOCAL_TEST_CASE(server_got_renegotiate, 0),
  2358. INTRUSIVE_TEST_CASE(SSL_SESSION_get_master_key, 0),
  2359. INTRUSIVE_TEST_CASE(get_tlssecrets, 0),
  2360. INTRUSIVE_TEST_CASE(get_buffer_sizes, 0),
  2361. LOCAL_TEST_CASE(evaluate_ecgroup_for_tls, 0),
  2362. INTRUSIVE_TEST_CASE(try_to_extract_certs_from_tls, 0),
  2363. INTRUSIVE_TEST_CASE(get_peer_cert, 0),
  2364. INTRUSIVE_TEST_CASE(peer_has_cert, 0),
  2365. INTRUSIVE_TEST_CASE(shutdown, 0),
  2366. INTRUSIVE_TEST_CASE(finish_handshake, 0),
  2367. INTRUSIVE_TEST_CASE(handshake, 0),
  2368. INTRUSIVE_TEST_CASE(write, 0),
  2369. INTRUSIVE_TEST_CASE(read, 0),
  2370. INTRUSIVE_TEST_CASE(server_info_callback, 0),
  2371. LOCAL_TEST_CASE(is_server, 0),
  2372. INTRUSIVE_TEST_CASE(assert_renegotiation_unblocked, 0),
  2373. INTRUSIVE_TEST_CASE(block_renegotiation, 0),
  2374. INTRUSIVE_TEST_CASE(unblock_renegotiation, 0),
  2375. INTRUSIVE_TEST_CASE(set_renegotiate_callback, 0),
  2376. LOCAL_TEST_CASE(set_logged_address, 0),
  2377. INTRUSIVE_TEST_CASE(find_cipher_by_id, 0),
  2378. INTRUSIVE_TEST_CASE(session_secret_cb, 0),
  2379. INTRUSIVE_TEST_CASE(debug_state_callback, 0),
  2380. INTRUSIVE_TEST_CASE(context_new, 0),
  2381. LOCAL_TEST_CASE(create_certificate, 0),
  2382. LOCAL_TEST_CASE(cert_new, 0),
  2383. LOCAL_TEST_CASE(cert_is_valid, 0),
  2384. LOCAL_TEST_CASE(context_init_one, 0),
  2385. END_OF_TESTCASES
  2386. };