test_tortls_openssl.c 62 KB

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