test_tortls.c 77 KB

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