test_tortls_openssl.c 72 KB

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