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