test_tortls.c 78 KB

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