ssltest.c.bak 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  1. /*
  2. * Copyright (c) 2007, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*
  31. * The testing of the crypto and ssl stuff goes here. Keeps the individual code
  32. * modules from being uncluttered with test code.
  33. *
  34. * This is test code - I make no apologies for the quality!
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <signal.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #include <sys/stat.h>
  42. #include <fcntl.h>
  43. #ifndef WIN32
  44. #include <pthread.h>
  45. #endif
  46. #include "ssl.h"
  47. #define DEFAULT_CERT "../ssl/test/axTLS.x509_512.cer"
  48. #define DEFAULT_KEY "../ssl/test/axTLS.key_512"
  49. //#define DEFAULT_SVR_OPTION SSL_DISPLAY_BYTES|SSL_DISPLAY_STATES
  50. #define DEFAULT_SVR_OPTION 0
  51. #define DEFAULT_CLNT_OPTION 0
  52. //#define DEFAULT_CLNT_OPTION SSL_DISPLAY_BYTES|SSL_DISPLAY_STATES
  53. static int g_port = 19001;
  54. /**************************************************************************
  55. * AES tests
  56. *
  57. * Run through a couple of the RFC3602 tests to verify that AES is correct.
  58. **************************************************************************/
  59. #define TEST1_SIZE 16
  60. #define TEST2_SIZE 32
  61. static int AES_test(BI_CTX *bi_ctx)
  62. {
  63. AES_CTX aes_key;
  64. int res = 1;
  65. uint8_t key[TEST1_SIZE];
  66. uint8_t iv[TEST1_SIZE];
  67. {
  68. /*
  69. Case #1: Encrypting 16 bytes (1 block) using AES-CBC
  70. Key : 0x06a9214036b8a15b512e03d534120006
  71. IV : 0x3dafba429d9eb430b422da802c9fac41
  72. Plaintext : "Single block msg"
  73. Ciphertext: 0xe353779c1079aeb82708942dbe77181a
  74. */
  75. char *in_str = "Single block msg";
  76. uint8_t ct[TEST1_SIZE];
  77. uint8_t enc_data[TEST1_SIZE];
  78. uint8_t dec_data[TEST1_SIZE];
  79. bigint *key_bi = bi_str_import(
  80. bi_ctx, "06A9214036B8A15B512E03D534120006");
  81. bigint *iv_bi = bi_str_import(
  82. bi_ctx, "3DAFBA429D9EB430B422DA802C9FAC41");
  83. bigint *ct_bi = bi_str_import(
  84. bi_ctx, "E353779C1079AEB82708942DBE77181A");
  85. bi_export(bi_ctx, key_bi, key, TEST1_SIZE);
  86. bi_export(bi_ctx, iv_bi, iv, TEST1_SIZE);
  87. bi_export(bi_ctx, ct_bi, ct, TEST1_SIZE);
  88. AES_set_key(&aes_key, key, iv, AES_MODE_128);
  89. AES_cbc_encrypt(&aes_key, (const uint8_t *)in_str,
  90. enc_data, sizeof(enc_data));
  91. if (memcmp(enc_data, ct, sizeof(ct)))
  92. {
  93. printf("Error: AES ENCRYPT #1 failed\n");
  94. goto end;
  95. }
  96. AES_set_key(&aes_key, key, iv, AES_MODE_128);
  97. AES_convert_key(&aes_key);
  98. AES_cbc_decrypt(&aes_key, enc_data, dec_data, sizeof(enc_data));
  99. if (memcmp(dec_data, in_str, sizeof(dec_data)))
  100. {
  101. printf("Error: AES DECRYPT #1 failed\n");
  102. goto end;
  103. }
  104. }
  105. {
  106. /*
  107. Case #2: Encrypting 32 bytes (2 blocks) using AES-CBC
  108. Key : 0xc286696d887c9aa0611bbb3e2025a45a
  109. IV : 0x562e17996d093d28ddb3ba695a2e6f58
  110. Plaintext : 0x000102030405060708090a0b0c0d0e0f
  111. 101112131415161718191a1b1c1d1e1f
  112. Ciphertext: 0xd296cd94c2cccf8a3a863028b5e1dc0a
  113. 7586602d253cfff91b8266bea6d61ab1
  114. */
  115. uint8_t in_data[TEST2_SIZE];
  116. uint8_t ct[TEST2_SIZE];
  117. uint8_t enc_data[TEST2_SIZE];
  118. uint8_t dec_data[TEST2_SIZE];
  119. bigint *in_bi = bi_str_import(bi_ctx,
  120. "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F");
  121. bigint *key_bi = bi_str_import(
  122. bi_ctx, "C286696D887C9AA0611BBB3E2025A45A");
  123. bigint *iv_bi = bi_str_import(
  124. bi_ctx, "562E17996D093D28DDB3BA695A2E6F58");
  125. bigint *ct_bi = bi_str_import(bi_ctx,
  126. "D296CD94C2CCCF8A3A863028B5E1DC0A7586602D253CFFF91B8266BEA6D61AB1");
  127. bi_export(bi_ctx, in_bi, in_data, TEST2_SIZE);
  128. bi_export(bi_ctx, key_bi, key, TEST1_SIZE);
  129. bi_export(bi_ctx, iv_bi, iv, TEST1_SIZE);
  130. bi_export(bi_ctx, ct_bi, ct, TEST2_SIZE);
  131. AES_set_key(&aes_key, key, iv, AES_MODE_128);
  132. AES_cbc_encrypt(&aes_key, (const uint8_t *)in_data,
  133. enc_data, sizeof(enc_data));
  134. if (memcmp(enc_data, ct, sizeof(ct)))
  135. {
  136. printf("Error: ENCRYPT #2 failed\n");
  137. goto end;
  138. }
  139. AES_set_key(&aes_key, key, iv, AES_MODE_128);
  140. AES_convert_key(&aes_key);
  141. AES_cbc_decrypt(&aes_key, enc_data, dec_data, sizeof(enc_data));
  142. if (memcmp(dec_data, in_data, sizeof(dec_data)))
  143. {
  144. printf("Error: DECRYPT #2 failed\n");
  145. goto end;
  146. }
  147. }
  148. res = 0;
  149. printf("All AES tests passed\n");
  150. end:
  151. return res;
  152. }
  153. /**************************************************************************
  154. * RC4 tests
  155. *
  156. * ARC4 tests vectors from OpenSSL (crypto/rc4/rc4test.c)
  157. **************************************************************************/
  158. static const uint8_t keys[7][30]=
  159. {
  160. {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
  161. {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
  162. {8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
  163. {4,0xef,0x01,0x23,0x45},
  164. {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
  165. {4,0xef,0x01,0x23,0x45},
  166. };
  167. static const uint8_t data_len[7]={8,8,8,20,28,10};
  168. static uint8_t data[7][30]=
  169. {
  170. {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xff},
  171. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
  172. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
  173. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  174. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  175. 0x00,0x00,0x00,0x00,0xff},
  176. {0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
  177. 0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
  178. 0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
  179. 0x12,0x34,0x56,0x78,0xff},
  180. {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
  181. {0},
  182. };
  183. static const uint8_t output[7][30]=
  184. {
  185. {0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96,0x00},
  186. {0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79,0x00},
  187. {0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a,0x00},
  188. {0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,
  189. 0xbd,0x61,0x5a,0x11,0x62,0xe1,0xc7,0xba,
  190. 0x36,0xb6,0x78,0x58,0x00},
  191. {0x66,0xa0,0x94,0x9f,0x8a,0xf7,0xd6,0x89,
  192. 0x1f,0x7f,0x83,0x2b,0xa8,0x33,0xc0,0x0c,
  193. 0x89,0x2e,0xbe,0x30,0x14,0x3c,0xe2,0x87,
  194. 0x40,0x01,0x1e,0xcf,0x00},
  195. {0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61,0x00},
  196. {0},
  197. };
  198. static int RC4_test(BI_CTX *bi_ctx)
  199. {
  200. int i, res = 1;
  201. RC4_CTX s;
  202. for (i = 0; i < 6; i++)
  203. {
  204. RC4_setup(&s, &keys[i][1], keys[i][0]);
  205. RC4_crypt(&s, data[i], data[i], data_len[i]);
  206. if (memcmp(data[i], output[i], data_len[i]))
  207. {
  208. printf("Error: RC4 CRYPT #%d failed\n", i);
  209. goto end;
  210. }
  211. }
  212. res = 0;
  213. printf("All RC4 tests passed\n");
  214. end:
  215. return res;
  216. }
  217. /**************************************************************************
  218. * SHA1 tests
  219. *
  220. * Run through a couple of the RFC3174 tests to verify that SHA1 is correct.
  221. **************************************************************************/
  222. static int SHA1_test(BI_CTX *bi_ctx)
  223. {
  224. SHA1_CTX ctx;
  225. uint8_t ct[SHA1_SIZE];
  226. uint8_t digest[SHA1_SIZE];
  227. int res = 1;
  228. {
  229. const char *in_str = "abc";
  230. bigint *ct_bi = bi_str_import(bi_ctx,
  231. "A9993E364706816ABA3E25717850C26C9CD0D89D");
  232. bi_export(bi_ctx, ct_bi, ct, SHA1_SIZE);
  233. SHA1_Init(&ctx);
  234. SHA1_Update(&ctx, (const uint8_t *)in_str, strlen(in_str));
  235. SHA1_Final(digest, &ctx);
  236. if (memcmp(digest, ct, sizeof(ct)))
  237. {
  238. printf("Error: SHA1 #1 failed\n");
  239. goto end;
  240. }
  241. }
  242. {
  243. const char *in_str =
  244. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  245. bigint *ct_bi = bi_str_import(bi_ctx,
  246. "84983E441C3BD26EBAAE4AA1F95129E5E54670F1");
  247. bi_export(bi_ctx, ct_bi, ct, SHA1_SIZE);
  248. SHA1_Init(&ctx);
  249. SHA1_Update(&ctx, (const uint8_t *)in_str, strlen(in_str));
  250. SHA1_Final(digest, &ctx);
  251. if (memcmp(digest, ct, sizeof(ct)))
  252. {
  253. printf("Error: SHA1 #2 failed\n");
  254. goto end;
  255. }
  256. }
  257. res = 0;
  258. printf("All SHA1 tests passed\n");
  259. end:
  260. return res;
  261. }
  262. /**************************************************************************
  263. * MD5 tests
  264. *
  265. * Run through a couple of the RFC1321 tests to verify that MD5 is correct.
  266. **************************************************************************/
  267. static int MD5_test(BI_CTX *bi_ctx)
  268. {
  269. MD5_CTX ctx;
  270. uint8_t ct[MD5_SIZE];
  271. uint8_t digest[MD5_SIZE];
  272. int res = 1;
  273. {
  274. const char *in_str = "abc";
  275. bigint *ct_bi = bi_str_import(bi_ctx,
  276. "900150983CD24FB0D6963F7D28E17F72");
  277. bi_export(bi_ctx, ct_bi, ct, MD5_SIZE);
  278. MD5_Init(&ctx);
  279. MD5_Update(&ctx, (const uint8_t *)in_str, strlen(in_str));
  280. MD5_Final(digest, &ctx);
  281. if (memcmp(digest, ct, sizeof(ct)))
  282. {
  283. printf("Error: MD5 #1 failed\n");
  284. goto end;
  285. }
  286. }
  287. {
  288. const char *in_str =
  289. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  290. bigint *ct_bi = bi_str_import(
  291. bi_ctx, "D174AB98D277D9F5A5611C2C9F419D9F");
  292. bi_export(bi_ctx, ct_bi, ct, MD5_SIZE);
  293. MD5_Init(&ctx);
  294. MD5_Update(&ctx, (const uint8_t *)in_str, strlen(in_str));
  295. MD5_Final(digest, &ctx);
  296. if (memcmp(digest, ct, sizeof(ct)))
  297. {
  298. printf("Error: MD5 #2 failed\n");
  299. goto end;
  300. }
  301. }
  302. res = 0;
  303. printf("All MD5 tests passed\n");
  304. end:
  305. return res;
  306. }
  307. /**************************************************************************
  308. * HMAC tests
  309. *
  310. * Run through a couple of the RFC2202 tests to verify that HMAC is correct.
  311. **************************************************************************/
  312. static int HMAC_test(BI_CTX *bi_ctx)
  313. {
  314. uint8_t key[SHA1_SIZE];
  315. uint8_t ct[SHA1_SIZE];
  316. uint8_t dgst[SHA1_SIZE];
  317. int res = 1;
  318. const char *key_str;
  319. const char *data_str = "Hi There";
  320. bigint *key_bi = bi_str_import(bi_ctx, "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B");
  321. bigint *ct_bi = bi_str_import(bi_ctx, "9294727A3638BB1C13F48EF8158BFC9D");
  322. bi_export(bi_ctx, key_bi, key, MD5_SIZE);
  323. bi_export(bi_ctx, ct_bi, ct, MD5_SIZE);
  324. hmac_md5((const uint8_t *)data_str, 8, key, MD5_SIZE, dgst);
  325. if (memcmp(dgst, ct, MD5_SIZE))
  326. {
  327. printf("HMAC MD5 #1 failed\n");
  328. goto end;
  329. }
  330. data_str = "what do ya want for nothing?";
  331. key_str = "Jefe";
  332. ct_bi = bi_str_import(bi_ctx, "750C783E6AB0B503EAA86E310A5DB738");
  333. bi_export(bi_ctx, ct_bi, ct, MD5_SIZE);
  334. hmac_md5((const uint8_t *)data_str, 28, (const uint8_t *)key_str, 4, dgst);
  335. if (memcmp(dgst, ct, MD5_SIZE))
  336. {
  337. printf("HMAC MD5 #2 failed\n");
  338. goto end;
  339. }
  340. data_str = "Hi There";
  341. key_bi = bi_str_import(bi_ctx, "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B");
  342. bi_export(bi_ctx, key_bi, key, SHA1_SIZE);
  343. ct_bi = bi_str_import(bi_ctx, "B617318655057264E28BC0B6FB378C8EF146BE00");
  344. bi_export(bi_ctx, ct_bi, ct, SHA1_SIZE);
  345. hmac_sha1((const uint8_t *)data_str, 8,
  346. (const uint8_t *)key, SHA1_SIZE, dgst);
  347. if (memcmp(dgst, ct, SHA1_SIZE))
  348. {
  349. printf("HMAC SHA1 #1 failed\n");
  350. goto end;
  351. }
  352. data_str = "what do ya want for nothing?";
  353. key_str = "Jefe";
  354. ct_bi = bi_str_import(bi_ctx, "EFFCDF6AE5EB2FA2D27416D5F184DF9C259A7C79");
  355. bi_export(bi_ctx, ct_bi, ct, SHA1_SIZE);
  356. hmac_sha1((const uint8_t *)data_str, 28, (const uint8_t *)key_str, 5, dgst);
  357. if (memcmp(dgst, ct, SHA1_SIZE))
  358. {
  359. printf("HMAC SHA1 failed\n");
  360. exit(1);
  361. }
  362. res = 0;
  363. printf("All HMAC tests passed\n");
  364. end:
  365. return res;
  366. }
  367. /**************************************************************************
  368. * BIGINT tests
  369. *
  370. **************************************************************************/
  371. static int BIGINT_test(BI_CTX *ctx)
  372. {
  373. int res = 1;
  374. bigint *bi_data, *bi_exp, *bi_res;
  375. const char *expnt, *plaintext, *mod;
  376. uint8_t compare[MAX_KEY_BYTE_SIZE];
  377. /**
  378. * 512 bit key
  379. */
  380. plaintext = /* 64 byte number */
  381. "01aaaaaaaaaabbbbbbbbbbbbbbbccccccccccccccdddddddddddddeeeeeeeeee";
  382. mod = "C30773C8ABE09FCC279EE0E5343370DE"
  383. "8B2FFDB6059271E3005A7CEEF0D35E0A"
  384. "1F9915D95E63560836CC2EB2C289270D"
  385. "BCAE8CAF6F5E907FC2759EE220071E1B";
  386. expnt = "A1E556CD1738E10DF539E35101334E97"
  387. "BE8D391C57A5C89A7AD9A2EA2ACA1B3D"
  388. "F3140F5091CC535CBAA47CEC4159EE1F"
  389. "B6A3661AFF1AB758426EAB158452A9B9";
  390. bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
  391. bi_exp = int_to_bi(ctx, 0x10001);
  392. bi_set_mod(ctx, bi_str_import(ctx, mod), 0);
  393. bi_res = bi_mod_power(ctx, bi_data, bi_exp);
  394. bi_data = bi_res; /* resuse again - see if we get the original */
  395. bi_exp = bi_str_import(ctx, expnt);
  396. bi_res = bi_mod_power(ctx, bi_data, bi_exp);
  397. bi_free_mod(ctx, 0);
  398. bi_export(ctx, bi_res, compare, 64);
  399. if (memcmp(plaintext, compare, 64) != 0)
  400. goto end;
  401. printf("All BIGINT tests passed\n");
  402. res = 0;
  403. end:
  404. return res;
  405. }
  406. /**************************************************************************
  407. * RSA tests
  408. *
  409. * Use the results from openssl to verify PKCS1 etc
  410. **************************************************************************/
  411. static int RSA_test(void)
  412. {
  413. int res = 1;
  414. const char *plaintext = /* 128 byte hex number */
  415. "1aaaaaaaaaabbbbbbbbbbbbbbbccccccccccccccdddddddddddddeeeeeeeeee2"
  416. "1aaaaaaaaaabbbbbbbbbbbbbbbccccccccccccccdddddddddddddeeeeeeeee2\012";
  417. uint8_t enc_data[128], dec_data[128];
  418. RSA_CTX *rsa_ctx = NULL;
  419. BI_CTX *bi_ctx;
  420. bigint *plaintext_bi;
  421. bigint *enc_data_bi, *dec_data_bi;
  422. uint8_t enc_data2[128], dec_data2[128];
  423. int size;
  424. int len;
  425. uint8_t *buf;
  426. /* extract the private key elements */
  427. len = get_file("../ssl/test/axTLS.key_1024", &buf);
  428. if (asn1_get_private_key(buf, len, &rsa_ctx) < 0)
  429. {
  430. goto end;
  431. }
  432. free(buf);
  433. bi_ctx = rsa_ctx->bi_ctx;
  434. plaintext_bi = bi_import(bi_ctx,
  435. (const uint8_t *)plaintext, strlen(plaintext));
  436. /* basic rsa encrypt */
  437. enc_data_bi = RSA_public(rsa_ctx, plaintext_bi);
  438. bi_export(bi_ctx, bi_copy(enc_data_bi), enc_data, sizeof(enc_data));
  439. /* basic rsa decrypt */
  440. dec_data_bi = RSA_private(rsa_ctx, enc_data_bi);
  441. bi_export(bi_ctx, dec_data_bi, dec_data, sizeof(dec_data));
  442. if (memcmp(dec_data, plaintext, strlen(plaintext)))
  443. {
  444. printf("Error: DECRYPT #1 failed\n");
  445. goto end;
  446. }
  447. RSA_encrypt(rsa_ctx, (const uint8_t *)"abc", 3, enc_data2, 0);
  448. size = RSA_decrypt(rsa_ctx, enc_data2, dec_data2, 1);
  449. if (memcmp("abc", dec_data2, 3))
  450. {
  451. printf("Error: ENCRYPT/DECRYPT #2 failed\n");
  452. goto end;
  453. }
  454. RSA_free(rsa_ctx);
  455. res = 0;
  456. printf("All RSA tests passed\n");
  457. end:
  458. return res;
  459. }
  460. /**************************************************************************
  461. * Cert Testing
  462. *
  463. **************************************************************************/
  464. static int cert_tests(void)
  465. {
  466. int res = -1, len;
  467. X509_CTX *x509_ctx;
  468. SSL_CTX *ssl_ctx;
  469. uint8_t *buf;
  470. /* check a bunch of 3rd party certificates */
  471. ssl_ctx = ssl_ctx_new(0, 0);
  472. len = get_file("../ssl/test/microsoft.x509_ca", &buf);
  473. if ((res = add_cert_auth(ssl_ctx, buf, len)) < 0)
  474. {
  475. printf("Cert #1\n");
  476. ssl_display_error(res);
  477. goto bad_cert;
  478. }
  479. ssl_ctx_free(ssl_ctx);
  480. free(buf);
  481. ssl_ctx = ssl_ctx_new(0, 0);
  482. len = get_file("../ssl/test/thawte.x509_ca", &buf);
  483. if ((res = add_cert_auth(ssl_ctx, buf, len)) < 0)
  484. {
  485. printf("Cert #2\n");
  486. ssl_display_error(res);
  487. goto bad_cert;
  488. }
  489. ssl_ctx_free(ssl_ctx);
  490. free(buf);
  491. ssl_ctx = ssl_ctx_new(0, 0);
  492. len = get_file("../ssl/test/deutsche_telecom.x509_ca", &buf);
  493. if ((res = add_cert_auth(ssl_ctx, buf, len)) < 0)
  494. {
  495. printf("Cert #3\n");
  496. ssl_display_error(res);
  497. goto bad_cert;
  498. }
  499. ssl_ctx_free(ssl_ctx);
  500. free(buf);
  501. ssl_ctx = ssl_ctx_new(0, 0);
  502. len = get_file("../ssl/test/equifax.x509_ca", &buf);
  503. if ((res = add_cert_auth(ssl_ctx, buf, len)) < 0)
  504. {
  505. printf("Cert #4\n");
  506. ssl_display_error(res);
  507. goto bad_cert;
  508. }
  509. ssl_ctx_free(ssl_ctx);
  510. free(buf);
  511. ssl_ctx = ssl_ctx_new(0, 0);
  512. len = get_file("../ssl/test/gnutls.cer", &buf);
  513. if ((res = add_cert(ssl_ctx, buf, len)) < 0)
  514. {
  515. printf("Cert #5\n");
  516. ssl_display_error(res);
  517. goto bad_cert;
  518. }
  519. ssl_ctx_free(ssl_ctx);
  520. free(buf);
  521. ssl_ctx = ssl_ctx_new(0, 0);
  522. len = get_file("../ssl/test/socgen.cer", &buf);
  523. if ((res = add_cert(ssl_ctx, buf, len)) < 0)
  524. {
  525. printf("Cert #6\n");
  526. ssl_display_error(res);
  527. goto bad_cert;
  528. }
  529. ssl_ctx_free(ssl_ctx);
  530. free(buf);
  531. ssl_ctx = ssl_ctx_new(0, 0);
  532. len = get_file("../ssl/test/verisign.x509_ca", &buf);
  533. if ((res = add_cert_auth(ssl_ctx, buf, len)) <0)
  534. {
  535. printf("Cert #7\n");
  536. ssl_display_error(res);
  537. goto bad_cert;
  538. }
  539. ssl_ctx_free(ssl_ctx);
  540. free(buf);
  541. if (get_file("../ssl/test/verisign.x509_my_cert", &buf) < 0 ||
  542. x509_new(buf, &len, &x509_ctx))
  543. {
  544. printf("Cert #8\n");
  545. ssl_display_error(res);
  546. goto bad_cert;
  547. }
  548. x509_free(x509_ctx);
  549. free(buf);
  550. ssl_ctx = ssl_ctx_new(0, 0);
  551. if ((res = ssl_obj_load(ssl_ctx,
  552. SSL_OBJ_X509_CERT, "../ssl/test/ms_iis.cer", NULL)) != SSL_OK)
  553. {
  554. ssl_display_error(res);
  555. goto bad_cert;
  556. }
  557. ssl_ctx_free(ssl_ctx);
  558. res = 0; /* all ok */
  559. printf("All Certificate tests passed\n");
  560. bad_cert:
  561. if (res)
  562. printf("Error: A certificate test failed\n");
  563. return res;
  564. }
  565. /**
  566. * init a server socket.
  567. */
  568. static int server_socket_init(int *port)
  569. {
  570. struct sockaddr_in serv_addr;
  571. int server_fd;
  572. char yes = 1;
  573. /* Create socket for incoming connections */
  574. if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  575. {
  576. return -1;
  577. }
  578. setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
  579. go_again:
  580. /* Construct local address structure */
  581. memset(&serv_addr, 0, sizeof(serv_addr)); /* Zero out structure */
  582. serv_addr.sin_family = AF_INET; /* Internet address family */
  583. serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
  584. serv_addr.sin_port = htons(*port); /* Local port */
  585. /* Bind to the local address */
  586. if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  587. {
  588. (*port)++;
  589. goto go_again;
  590. }
  591. /* Mark the socket so it will listen for incoming connections */
  592. if (listen(server_fd, 3000) < 0)
  593. {
  594. return -1;
  595. }
  596. return server_fd;
  597. }
  598. /**
  599. * init a client socket.
  600. */
  601. static int client_socket_init(uint16_t port)
  602. {
  603. struct sockaddr_in address;
  604. int client_fd;
  605. address.sin_family = AF_INET;
  606. address.sin_port = htons(port);
  607. address.sin_addr.s_addr = inet_addr("127.0.0.1");
  608. client_fd = socket(AF_INET, SOCK_STREAM, 0);
  609. if (connect(client_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
  610. {
  611. perror("socket");
  612. SOCKET_CLOSE(client_fd);
  613. client_fd = -1;
  614. }
  615. return client_fd;
  616. }
  617. /**************************************************************************
  618. * SSL Server Testing
  619. *
  620. **************************************************************************/
  621. typedef struct
  622. {
  623. /* not used as yet */
  624. int dummy;
  625. } SVR_CTX;
  626. typedef struct
  627. {
  628. const char *testname;
  629. const char *openssl_option;
  630. } client_t;
  631. static void do_client(client_t *clnt)
  632. {
  633. char openssl_buf[2048];
  634. /* make sure the main thread goes first */
  635. sleep(0);
  636. /* show the session ids in the reconnect test */
  637. if (strcmp(clnt->testname, "Session Reuse") == 0)
  638. {
  639. sprintf(openssl_buf, "echo \"hello client\" | openssl s_client "
  640. "-connect localhost:%d %s 2>&1 | grep \"Session-ID:\"",
  641. g_port, clnt->openssl_option);
  642. }
  643. else
  644. {
  645. sprintf(openssl_buf, "echo \"hello client\" | openssl s_client "
  646. #ifdef WIN32
  647. "-connect localhost:%d -quiet %s",
  648. #else
  649. "-connect localhost:%d -quiet %s > /dev/null 2>&1",
  650. #endif
  651. g_port, clnt->openssl_option);
  652. }
  653. system(openssl_buf);
  654. }
  655. static int SSL_server_test(
  656. const char *testname,
  657. const char *openssl_option,
  658. const char *device_cert,
  659. const char *product_cert,
  660. const char *private_key,
  661. const char *ca_cert,
  662. const char *password,
  663. int axolotls_option)
  664. {
  665. int server_fd, ret = 0;
  666. SSL_CTX *ssl_ctx = NULL;
  667. struct sockaddr_in client_addr;
  668. uint8_t *read_buf;
  669. socklen_t clnt_len = sizeof(client_addr);
  670. client_t client_data;
  671. #ifndef WIN32
  672. pthread_t thread;
  673. #endif
  674. g_port++;
  675. client_data.testname = testname;
  676. client_data.openssl_option = openssl_option;
  677. if ((server_fd = server_socket_init(&g_port)) < 0)
  678. goto error;
  679. if (private_key)
  680. {
  681. axolotls_option |= SSL_NO_DEFAULT_KEY;
  682. }
  683. if ((ssl_ctx = ssl_ctx_new(axolotls_option, SSL_DEFAULT_SVR_SESS)) == NULL)
  684. {
  685. ret = SSL_ERROR_INVALID_KEY;
  686. goto error;
  687. }
  688. if (private_key)
  689. {
  690. int obj_type = SSL_OBJ_RSA_KEY;
  691. if (strstr(private_key, ".p8"))
  692. obj_type = SSL_OBJ_PKCS8;
  693. else if (strstr(private_key, ".p12"))
  694. obj_type = SSL_OBJ_PKCS12;
  695. if (ssl_obj_load(ssl_ctx, obj_type, private_key, password))
  696. {
  697. ret = SSL_ERROR_INVALID_KEY;
  698. goto error;
  699. }
  700. }
  701. if (device_cert) /* test chaining */
  702. {
  703. if ((ret = ssl_obj_load(ssl_ctx,
  704. SSL_OBJ_X509_CERT, device_cert, NULL)) != SSL_OK)
  705. goto error;
  706. }
  707. if (product_cert) /* test chaining */
  708. {
  709. if ((ret = ssl_obj_load(ssl_ctx,
  710. SSL_OBJ_X509_CERT, product_cert, NULL)) != SSL_OK)
  711. goto error;
  712. }
  713. if (ca_cert) /* test adding certificate authorities */
  714. {
  715. if ((ret = ssl_obj_load(ssl_ctx,
  716. SSL_OBJ_X509_CACERT, ca_cert, NULL)) != SSL_OK)
  717. goto error;
  718. }
  719. #ifndef WIN32
  720. pthread_create(&thread, NULL,
  721. (void *(*)(void *))do_client, (void *)&client_data);
  722. pthread_detach(thread);
  723. #else
  724. CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)do_client,
  725. (LPVOID)&client_data, 0, NULL);
  726. #endif
  727. for (;;)
  728. {
  729. int client_fd, size = 0;
  730. SSL *ssl;
  731. /* Wait for a client to connect */
  732. if ((client_fd = accept(server_fd,
  733. (struct sockaddr *)&client_addr, &clnt_len)) < 0)
  734. {
  735. ret = SSL_ERROR_SOCK_SETUP_FAILURE;
  736. goto error;
  737. }
  738. /* we are ready to go */
  739. ssl = ssl_server_new(ssl_ctx, client_fd);
  740. while ((size = ssl_read(ssl, &read_buf)) == SSL_OK);
  741. SOCKET_CLOSE(client_fd);
  742. if (size < SSL_OK) /* got some alert or something nasty */
  743. {
  744. ret = size;
  745. if (ret == SSL_ERROR_CONN_LOST)
  746. {
  747. ret = SSL_OK;
  748. continue;
  749. }
  750. break; /* we've got a problem */
  751. }
  752. else /* looks more promising */
  753. {
  754. if (strstr("hello client", (char *)read_buf) == NULL)
  755. {
  756. printf("SSL server test \"%s\" passed\n", testname);
  757. TTY_FLUSH();
  758. ret = 0;
  759. break;
  760. }
  761. }
  762. ssl_free(ssl);
  763. }
  764. SOCKET_CLOSE(server_fd);
  765. error:
  766. ssl_ctx_free(ssl_ctx);
  767. return ret;
  768. }
  769. int SSL_server_tests(void)
  770. {
  771. int ret = -1;
  772. struct stat stat_buf;
  773. SVR_CTX svr_test_ctx;
  774. memset(&svr_test_ctx, 0, sizeof(SVR_CTX));
  775. printf("### starting server tests\n"); TTY_FLUSH();
  776. /* Go through the algorithms */
  777. /*
  778. * TLS1 client hello
  779. */
  780. if ((ret = SSL_server_test("TLSv1", "-cipher RC4-SHA -tls1",
  781. NULL, NULL, NULL, NULL, NULL, DEFAULT_SVR_OPTION)))
  782. goto cleanup;
  783. /*
  784. * AES128-SHA
  785. */
  786. if ((ret = SSL_server_test("AES256-SHA", "-cipher AES128-SHA",
  787. DEFAULT_CERT, NULL, DEFAULT_KEY, NULL, NULL,
  788. DEFAULT_SVR_OPTION)))
  789. goto cleanup;
  790. /*
  791. * AES256-SHA
  792. */
  793. if ((ret = SSL_server_test("AES256-SHA", "-cipher AES128-SHA",
  794. DEFAULT_CERT, NULL, DEFAULT_KEY, NULL, NULL,
  795. DEFAULT_SVR_OPTION)))
  796. goto cleanup;
  797. /*
  798. * RC4-SHA
  799. */
  800. if ((ret = SSL_server_test("RC4-SHA", "-cipher RC4-SHA",
  801. DEFAULT_CERT, NULL, DEFAULT_KEY, NULL, NULL,
  802. DEFAULT_SVR_OPTION)))
  803. goto cleanup;
  804. /*
  805. * RC4-MD5
  806. */
  807. if ((ret = SSL_server_test("RC4-MD5", "-cipher RC4-MD5",
  808. DEFAULT_CERT, NULL, DEFAULT_KEY, NULL, NULL,
  809. DEFAULT_SVR_OPTION)))
  810. goto cleanup;
  811. /*
  812. * Session Reuse
  813. * all the session id's should match for session resumption.
  814. */
  815. if ((ret = SSL_server_test("Session Reuse",
  816. "-cipher RC4-SHA -reconnect",
  817. DEFAULT_CERT, NULL, DEFAULT_KEY, NULL, NULL,
  818. DEFAULT_SVR_OPTION)))
  819. goto cleanup;
  820. /*
  821. * 512 bit RSA key
  822. */
  823. if ((ret = SSL_server_test("512 bit key", "-cipher RC4-SHA",
  824. "../ssl/test/axTLS.x509_512.cer", NULL,
  825. "../ssl/test/axTLS.key_512",
  826. NULL, NULL, DEFAULT_SVR_OPTION)))
  827. goto cleanup;
  828. /*
  829. * 1024 bit RSA key (check certificate chaining)
  830. */
  831. if ((ret = SSL_server_test("1024 bit key",
  832. "-cipher RC4-SHA",
  833. "../ssl/test/axTLS.x509_device.cer",
  834. "../ssl/test/axTLS.x509_512.cer",
  835. "../ssl/test/axTLS.device_key",
  836. NULL, NULL, DEFAULT_SVR_OPTION)))
  837. goto cleanup;
  838. /*
  839. * 2048 bit RSA key
  840. */
  841. if ((ret = SSL_server_test("2048 bit key",
  842. "-cipher RC4-SHA",
  843. "../ssl/test/axTLS.x509_2048.cer", NULL,
  844. "../ssl/test/axTLS.key_2048",
  845. NULL, NULL, DEFAULT_SVR_OPTION)))
  846. goto cleanup;
  847. /*
  848. * 4096 bit RSA key
  849. */
  850. if ((ret = SSL_server_test("4096 bit key",
  851. "-cipher RC4-SHA",
  852. "../ssl/test/axTLS.x509_4096.cer", NULL,
  853. "../ssl/test/axTLS.key_4096",
  854. NULL, NULL, DEFAULT_SVR_OPTION)))
  855. goto cleanup;
  856. /*
  857. * Client Verification
  858. */
  859. if ((ret = SSL_server_test("Client Verification",
  860. "-cipher RC4-SHA -tls1 "
  861. "-cert ../ssl/test/axTLS.x509_2048.pem "
  862. "-key ../ssl/test/axTLS.key_2048.pem ",
  863. NULL, NULL, NULL,
  864. "../ssl/test/axTLS.ca_x509.cer", NULL,
  865. DEFAULT_SVR_OPTION|SSL_CLIENT_AUTHENTICATION)))
  866. goto cleanup;
  867. /* this test should fail */
  868. if (stat("../ssl/test/axTLS.x509_bad_before.pem", &stat_buf) >= 0)
  869. {
  870. if ((ret = SSL_server_test("Bad Before Cert",
  871. "-cipher RC4-SHA -tls1 "
  872. "-cert ../ssl/test/axTLS.x509_bad_before.pem "
  873. "-key ../ssl/test/axTLS.key_512.pem ",
  874. NULL, NULL, NULL,
  875. "../ssl/test/axTLS.ca_x509.cer", NULL,
  876. DEFAULT_SVR_OPTION|SSL_CLIENT_AUTHENTICATION)) !=
  877. SSL_X509_ERROR(X509_VFY_ERROR_NOT_YET_VALID))
  878. goto cleanup;
  879. printf("SSL server test \"%s\" passed\n", "Bad Before Cert");
  880. TTY_FLUSH();
  881. ret = 0; /* is ok */
  882. }
  883. /* this test should fail */
  884. if ((ret = SSL_server_test("Bad After Cert",
  885. "-cipher RC4-SHA -tls1 "
  886. "-cert ../ssl/test/axTLS.x509_bad_after.pem "
  887. "-key ../ssl/test/axTLS.key_512.pem ",
  888. NULL, NULL, NULL,
  889. "../ssl/test/axTLS.ca_x509.cer", NULL,
  890. DEFAULT_SVR_OPTION|SSL_CLIENT_AUTHENTICATION)) !=
  891. SSL_X509_ERROR(X509_VFY_ERROR_EXPIRED))
  892. goto cleanup;
  893. printf("SSL server test \"%s\" passed\n", "Bad After Cert");
  894. TTY_FLUSH();
  895. /*
  896. * Key in PEM format
  897. */
  898. if ((ret = SSL_server_test("Key in PEM format",
  899. "-cipher RC4-SHA",
  900. "../ssl/test/axTLS.x509_512.cer", NULL,
  901. "../ssl/test/axTLS.key_512.pem", NULL,
  902. NULL, DEFAULT_SVR_OPTION)))
  903. goto cleanup;
  904. /*
  905. * Cert in PEM format
  906. */
  907. if ((ret = SSL_server_test("Cert in PEM format",
  908. "-cipher RC4-SHA",
  909. "../ssl/test/axTLS.x509_512.pem", NULL,
  910. "../ssl/test/axTLS.key_512.pem", NULL,
  911. NULL, DEFAULT_SVR_OPTION)))
  912. goto cleanup;
  913. /*
  914. * Cert chain in PEM format
  915. */
  916. if ((ret = SSL_server_test("Cert chain in PEM format",
  917. "-cipher RC4-SHA",
  918. "../ssl/test/axTLS.x509_device.pem",
  919. NULL, "../ssl/test/axTLS.device_key.pem",
  920. "../ssl/test/axTLS.ca_x509.pem", NULL, DEFAULT_SVR_OPTION)))
  921. goto cleanup;
  922. /*
  923. * AES128 Encrypted key
  924. */
  925. if ((ret = SSL_server_test("AES128 encrypted key",
  926. "-cipher RC4-SHA",
  927. "../ssl/test/axTLS.x509_aes128.pem", NULL,
  928. "../ssl/test/axTLS.key_aes128.pem",
  929. NULL, "abcd", DEFAULT_SVR_OPTION)))
  930. goto cleanup;
  931. /*
  932. * AES256 Encrypted key
  933. */
  934. if ((ret = SSL_server_test("AES256 encrypted key",
  935. "-cipher RC4-SHA",
  936. "../ssl/test/axTLS.x509_aes256.pem", NULL,
  937. "../ssl/test/axTLS.key_aes256.pem",
  938. NULL, "abcd", DEFAULT_SVR_OPTION)))
  939. goto cleanup;
  940. /*
  941. * AES128 Encrypted invalid key
  942. */
  943. if ((ret = SSL_server_test("AES128 encrypted invalid key",
  944. "-cipher RC4-SHA",
  945. "../ssl/test/axTLS.x509_aes128.pem", NULL,
  946. "../ssl/test/axTLS.key_aes128.pem",
  947. NULL, "xyz", DEFAULT_SVR_OPTION)) != SSL_ERROR_INVALID_KEY)
  948. goto cleanup;
  949. printf("SSL server test \"%s\" passed\n", "AES128 encrypted invalid key");
  950. TTY_FLUSH();
  951. /*
  952. * PKCS#8 key (encrypted)
  953. */
  954. if ((ret = SSL_server_test("pkcs#8 encrypted", "-cipher RC4-SHA",
  955. DEFAULT_CERT, NULL, "../ssl/test/axTLS.encrypted.p8",
  956. NULL, "abcd", DEFAULT_SVR_OPTION)))
  957. goto cleanup;
  958. /*
  959. * PKCS#8 key (unencrypted)
  960. */
  961. if ((ret = SSL_server_test("pkcs#8 unencrypted", "-cipher RC4-SHA",
  962. DEFAULT_CERT, NULL, "../ssl/test/axTLS.unencrypted.p8",
  963. NULL, NULL, DEFAULT_SVR_OPTION)))
  964. goto cleanup;
  965. /*
  966. * PKCS#12 key/certificate
  967. */
  968. if ((ret = SSL_server_test("pkcs#12 with CA", "-cipher RC4-SHA",
  969. NULL, NULL, "../ssl/test/axTLS.withCA.p12",
  970. NULL, "abcd", DEFAULT_SVR_OPTION)))
  971. goto cleanup;
  972. if ((ret = SSL_server_test("pkcs#12 no CA", "-cipher RC4-SHA",
  973. DEFAULT_CERT, NULL, "../ssl/test/axTLS.withoutCA.p12",
  974. NULL, "abcd", DEFAULT_SVR_OPTION)))
  975. goto cleanup;
  976. /*
  977. *
  978. */
  979. ret = 0;
  980. cleanup:
  981. if (ret)
  982. {
  983. printf("Error: A server test failed\n");
  984. ssl_display_error(ret);
  985. exit(1);
  986. }
  987. else
  988. {
  989. printf("All server tests passed\n"); TTY_FLUSH();
  990. }
  991. return ret;
  992. }
  993. /**************************************************************************
  994. * SSL Client Testing
  995. *
  996. **************************************************************************/
  997. typedef struct
  998. {
  999. uint8_t session_id[SSL_SESSION_ID_SIZE];
  1000. #ifndef WIN32
  1001. pthread_t server_thread;
  1002. #endif
  1003. int start_server;
  1004. int stop_server;
  1005. int do_reneg;
  1006. } CLNT_SESSION_RESUME_CTX;
  1007. typedef struct
  1008. {
  1009. const char *testname;
  1010. const char *openssl_option;
  1011. } server_t;
  1012. static void do_server(server_t *svr)
  1013. {
  1014. char openssl_buf[2048];
  1015. #ifndef WIN32
  1016. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1017. #endif
  1018. sprintf(openssl_buf, "openssl s_server -tls1 "
  1019. "-accept %d -quiet %s ", g_port, svr->openssl_option);
  1020. system(openssl_buf);
  1021. }
  1022. static int SSL_client_test(
  1023. const char *test,
  1024. SSL_CTX **ssl_ctx,
  1025. const char *openssl_option,
  1026. CLNT_SESSION_RESUME_CTX *sess_resume,
  1027. uint32_t client_options,
  1028. const char *private_key,
  1029. const char *password,
  1030. const char *cert)
  1031. {
  1032. server_t server_data;
  1033. SSL *ssl = NULL;
  1034. int client_fd = -1;
  1035. uint8_t *session_id = NULL;
  1036. int ret = 1;
  1037. #ifndef WIN32
  1038. pthread_t thread;
  1039. #endif
  1040. if (sess_resume == NULL || sess_resume->start_server)
  1041. {
  1042. g_port++;
  1043. server_data.openssl_option = openssl_option;
  1044. #ifndef WIN32
  1045. pthread_create(&thread, NULL,
  1046. (void *(*)(void *))do_server, (void *)&server_data);
  1047. pthread_detach(thread);
  1048. #else
  1049. CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)do_server,
  1050. (LPVOID)&server_data, 0, NULL);
  1051. #endif
  1052. }
  1053. usleep(200000); /* allow server to start */
  1054. if (*ssl_ctx == NULL)
  1055. {
  1056. if (private_key)
  1057. {
  1058. client_options |= SSL_NO_DEFAULT_KEY;
  1059. }
  1060. if ((*ssl_ctx = ssl_ctx_new(
  1061. client_options, SSL_DEFAULT_CLNT_SESS)) == NULL)
  1062. {
  1063. ret = SSL_ERROR_INVALID_KEY;
  1064. goto client_test_exit;
  1065. }
  1066. if (private_key)
  1067. {
  1068. int obj_type = SSL_OBJ_RSA_KEY;
  1069. if (strstr(private_key, ".p8"))
  1070. obj_type = SSL_OBJ_PKCS8;
  1071. else if (strstr(private_key, ".p12"))
  1072. obj_type = SSL_OBJ_PKCS12;
  1073. if (ssl_obj_load(*ssl_ctx, obj_type, private_key, password))
  1074. {
  1075. ret = SSL_ERROR_INVALID_KEY;
  1076. goto client_test_exit;
  1077. }
  1078. }
  1079. if (cert)
  1080. {
  1081. if ((ret = ssl_obj_load(*ssl_ctx,
  1082. SSL_OBJ_X509_CERT, cert, NULL)) != SSL_OK)
  1083. {
  1084. printf("could not add cert %s (%d)\n", cert, ret);
  1085. TTY_FLUSH();
  1086. goto client_test_exit;
  1087. }
  1088. }
  1089. if (ssl_obj_load(*ssl_ctx, SSL_OBJ_X509_CACERT,
  1090. "../ssl/test/axTLS.ca_x509.cer", NULL))
  1091. {
  1092. printf("could not add cert auth\n"); TTY_FLUSH();
  1093. goto client_test_exit;
  1094. }
  1095. }
  1096. if (sess_resume && !sess_resume->start_server)
  1097. {
  1098. session_id = sess_resume->session_id;
  1099. }
  1100. if ((client_fd = client_socket_init(g_port)) < 0)
  1101. {
  1102. printf("could not start socket on %d\n", g_port); TTY_FLUSH();
  1103. goto client_test_exit;
  1104. }
  1105. ssl = ssl_client_new(*ssl_ctx, client_fd, session_id, sizeof(session_id));
  1106. /* check the return status */
  1107. if ((ret = ssl_handshake_status(ssl)))
  1108. goto client_test_exit;
  1109. /* renegotiate client */
  1110. if (sess_resume && sess_resume->do_reneg)
  1111. {
  1112. if (ssl_renegotiate(ssl) < 0)
  1113. goto client_test_exit;
  1114. }
  1115. if (sess_resume)
  1116. {
  1117. memcpy(sess_resume->session_id,
  1118. ssl_get_session_id(ssl), SSL_SESSION_ID_SIZE);
  1119. }
  1120. if (IS_SET_SSL_FLAG(SSL_SERVER_VERIFY_LATER) &&
  1121. (ret = ssl_verify_cert(ssl)))
  1122. {
  1123. goto client_test_exit;
  1124. }
  1125. ssl_write(ssl, (uint8_t *)"hello world\n", 13);
  1126. if (sess_resume)
  1127. {
  1128. const uint8_t *sess_id = ssl_get_session_id(ssl);
  1129. int i;
  1130. printf(" Session-ID: ");
  1131. for (i = 0; i < SSL_SESSION_ID_SIZE; i++)
  1132. {
  1133. printf("%02X", sess_id[i]);
  1134. }
  1135. printf("\n");
  1136. TTY_FLUSH();
  1137. }
  1138. ret = 0;
  1139. client_test_exit:
  1140. ssl_free(ssl);
  1141. SOCKET_CLOSE(client_fd);
  1142. usleep(200000); /* allow openssl to say something */
  1143. if (sess_resume)
  1144. {
  1145. if (sess_resume->stop_server)
  1146. {
  1147. ssl_ctx_free(*ssl_ctx);
  1148. *ssl_ctx = NULL;
  1149. #ifndef WIN32
  1150. pthread_cancel(sess_resume->server_thread);
  1151. #endif
  1152. }
  1153. else if (sess_resume->start_server)
  1154. {
  1155. #ifndef WIN32
  1156. sess_resume->server_thread = thread;
  1157. #endif
  1158. }
  1159. }
  1160. else
  1161. {
  1162. ssl_ctx_free(*ssl_ctx);
  1163. *ssl_ctx = NULL;
  1164. #ifndef WIN32
  1165. pthread_cancel(thread);
  1166. #endif
  1167. }
  1168. if (ret == 0)
  1169. {
  1170. printf("SSL client test \"%s\" passed\n", test);
  1171. TTY_FLUSH();
  1172. }
  1173. return ret;
  1174. }
  1175. int SSL_client_tests(void)
  1176. {
  1177. int ret = -1;
  1178. SSL_CTX *ssl_ctx = NULL;
  1179. CLNT_SESSION_RESUME_CTX sess_resume;
  1180. memset(&sess_resume, 0, sizeof(CLNT_SESSION_RESUME_CTX));
  1181. sess_resume.start_server = 1;
  1182. printf("### starting client tests\n");
  1183. if ((ret = SSL_client_test("512 bit key",
  1184. &ssl_ctx,
  1185. "-cert ../ssl/test/axTLS.x509_512.pem "
  1186. "-key ../ssl/test/axTLS.key_512.pem", &sess_resume,
  1187. DEFAULT_CLNT_OPTION, NULL, NULL, NULL)))
  1188. goto cleanup;
  1189. /* all the session id's should match for session resumption */
  1190. sess_resume.start_server = 0;
  1191. if ((ret = SSL_client_test("Client session resumption #1",
  1192. &ssl_ctx, NULL, &sess_resume,
  1193. DEFAULT_CLNT_OPTION, NULL, NULL, NULL)))
  1194. goto cleanup;
  1195. sess_resume.do_reneg = 1;
  1196. if ((ret = SSL_client_test("Client renegotiation",
  1197. &ssl_ctx, NULL, &sess_resume,
  1198. DEFAULT_CLNT_OPTION, NULL, NULL, NULL)))
  1199. goto cleanup;
  1200. sess_resume.do_reneg = 0;
  1201. sess_resume.stop_server = 1;
  1202. if ((ret = SSL_client_test("Client session resumption #2",
  1203. &ssl_ctx, NULL, &sess_resume,
  1204. DEFAULT_CLNT_OPTION, NULL, NULL, NULL)))
  1205. goto cleanup;
  1206. if ((ret = SSL_client_test("1024 bit key",
  1207. &ssl_ctx,
  1208. "-cert ../ssl/test/axTLS.x509_1024.pem "
  1209. "-key ../ssl/test/axTLS.key_1024.pem", NULL,
  1210. DEFAULT_CLNT_OPTION, NULL, NULL, NULL)))
  1211. goto cleanup;
  1212. if ((ret = SSL_client_test("2048 bit key",
  1213. &ssl_ctx,
  1214. "-cert ../ssl/test/axTLS.x509_2048.pem "
  1215. "-key ../ssl/test/axTLS.key_2048.pem", NULL,
  1216. DEFAULT_CLNT_OPTION, NULL, NULL, NULL)))
  1217. goto cleanup;
  1218. if ((ret = SSL_client_test("4096 bit key",
  1219. &ssl_ctx,
  1220. "-cert ../ssl/test/axTLS.x509_4096.pem "
  1221. "-key ../ssl/test/axTLS.key_4096.pem", NULL,
  1222. DEFAULT_CLNT_OPTION, NULL, NULL, NULL)))
  1223. goto cleanup;
  1224. if ((ret = SSL_client_test("Server cert chaining",
  1225. &ssl_ctx,
  1226. "-cert ../ssl/test/axTLS.x509_device.pem "
  1227. "-key ../ssl/test/axTLS.device_key.pem "
  1228. "-CAfile ../ssl/test/axTLS.x509_512.pem ", NULL,
  1229. DEFAULT_CLNT_OPTION, NULL, NULL, NULL)))
  1230. goto cleanup;
  1231. /* Check the server can verify the client */
  1232. if ((ret = SSL_client_test("Client peer authentication",
  1233. &ssl_ctx,
  1234. "-cert ../ssl/test/axTLS.x509_2048.pem "
  1235. "-key ../ssl/test/axTLS.key_2048.pem "
  1236. "-CAfile ../ssl/test/axTLS.ca_x509.pem "
  1237. "-verify 1 ", NULL, DEFAULT_CLNT_OPTION,
  1238. "../ssl/test/axTLS.key_1024", NULL,
  1239. "../ssl/test/axTLS.x509_1024.cer")))
  1240. goto cleanup;
  1241. /* Should get an "ERROR" from openssl (as the handshake fails as soon as
  1242. * the certificate verification fails) */
  1243. if ((ret = SSL_client_test("Error: Expired cert (verify now)",
  1244. &ssl_ctx,
  1245. "-cert ../ssl/test/axTLS.x509_bad_after.pem "
  1246. "-key ../ssl/test/axTLS.key_512.pem", NULL,
  1247. DEFAULT_CLNT_OPTION, NULL, NULL, NULL)) !=
  1248. SSL_X509_ERROR(X509_VFY_ERROR_EXPIRED))
  1249. {
  1250. printf("*** Error: %d\n", ret);
  1251. goto cleanup;
  1252. }
  1253. printf("SSL client test \"Expired cert (verify now)\" passed\n");
  1254. /* There is no "ERROR" from openssl */
  1255. if ((ret = SSL_client_test("Error: Expired cert (verify later)",
  1256. &ssl_ctx,
  1257. "-cert ../ssl/test/axTLS.x509_bad_after.pem "
  1258. "-key ../ssl/test/axTLS.key_512.pem", NULL,
  1259. DEFAULT_CLNT_OPTION|SSL_SERVER_VERIFY_LATER, NULL,
  1260. NULL, NULL)) != SSL_X509_ERROR(X509_VFY_ERROR_EXPIRED))
  1261. {
  1262. printf("*** Error: %d\n", ret);
  1263. goto cleanup;
  1264. }
  1265. printf("SSL client test \"Expired cert (verify later)\" passed\n");
  1266. ret = 0;
  1267. cleanup:
  1268. if (ret)
  1269. {
  1270. ssl_display_error(ret);
  1271. printf("Error: A client test failed\n");
  1272. exit(1);
  1273. }
  1274. else
  1275. {
  1276. printf("All client tests passed\n"); TTY_FLUSH();
  1277. }
  1278. return ret;
  1279. }
  1280. /**************************************************************************
  1281. * SSL Basic Testing (test a big packet handshake)
  1282. *
  1283. **************************************************************************/
  1284. static uint8_t basic_buf[256*1024];
  1285. static void do_basic(void)
  1286. {
  1287. int client_fd;
  1288. SSL *ssl_clnt;
  1289. SSL_CTX *ssl_clnt_ctx = ssl_ctx_new(
  1290. DEFAULT_CLNT_OPTION, SSL_DEFAULT_CLNT_SESS);
  1291. usleep(200000); /* allow server to start */
  1292. if ((client_fd = client_socket_init(g_port)) < 0)
  1293. goto error;
  1294. if (ssl_obj_load(ssl_clnt_ctx, SSL_OBJ_X509_CACERT,
  1295. "../ssl/test/axTLS.ca_x509.cer", NULL))
  1296. goto error;
  1297. ssl_clnt = ssl_client_new(ssl_clnt_ctx, client_fd, NULL, 0);
  1298. /* check the return status */
  1299. if (ssl_handshake_status(ssl_clnt) < 0)
  1300. {
  1301. printf("YA YA\n");
  1302. ssl_display_error(ssl_handshake_status(ssl_clnt));
  1303. goto error;
  1304. }
  1305. ssl_write(ssl_clnt, basic_buf, sizeof(basic_buf));
  1306. ssl_free(ssl_clnt);
  1307. error:
  1308. ssl_ctx_free(ssl_clnt_ctx);
  1309. SOCKET_CLOSE(client_fd);
  1310. /* exit this thread */
  1311. }
  1312. static int SSL_basic_test(void)
  1313. {
  1314. int server_fd, client_fd, ret = 0, size = 0, offset = 0;
  1315. SSL_CTX *ssl_svr_ctx = NULL;
  1316. struct sockaddr_in client_addr;
  1317. uint8_t *read_buf;
  1318. socklen_t clnt_len = sizeof(client_addr);
  1319. SSL *ssl_svr;
  1320. #ifndef WIN32
  1321. pthread_t thread;
  1322. #endif
  1323. memset(basic_buf, 0xA5, sizeof(basic_buf)/2);
  1324. memset(&basic_buf[sizeof(basic_buf)/2], 0x5A, sizeof(basic_buf)/2);
  1325. if ((server_fd = server_socket_init(&g_port)) < 0)
  1326. goto error;
  1327. ssl_svr_ctx = ssl_ctx_new(DEFAULT_SVR_OPTION, SSL_DEFAULT_SVR_SESS);
  1328. #ifndef WIN32
  1329. pthread_create(&thread, NULL,
  1330. (void *(*)(void *))do_basic, NULL);
  1331. pthread_detach(thread);
  1332. #else
  1333. CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)do_basic, NULL, 0, NULL);
  1334. #endif
  1335. /* Wait for a client to connect */
  1336. if ((client_fd = accept(server_fd,
  1337. (struct sockaddr *) &client_addr, &clnt_len)) < 0)
  1338. {
  1339. ret = SSL_ERROR_SOCK_SETUP_FAILURE;
  1340. goto error;
  1341. }
  1342. /* we are ready to go */
  1343. ssl_svr = ssl_server_new(ssl_svr_ctx, client_fd);
  1344. do
  1345. {
  1346. while ((size = ssl_read(ssl_svr, &read_buf)) == SSL_OK);
  1347. if (size < SSL_OK) /* got some alert or something nasty */
  1348. {
  1349. printf("Server ");
  1350. ssl_display_error(size);
  1351. ret = size;
  1352. break;
  1353. }
  1354. else /* looks more promising */
  1355. {
  1356. if (memcmp(read_buf, &basic_buf[offset], size) != 0)
  1357. {
  1358. ret = SSL_NOT_OK;
  1359. break;
  1360. }
  1361. }
  1362. offset += size;
  1363. } while (offset < sizeof(basic_buf));
  1364. printf(ret == SSL_OK && offset == sizeof(basic_buf) ?
  1365. "SSL basic test passed\n" :
  1366. "SSL basic test failed\n");
  1367. TTY_FLUSH();
  1368. ssl_free(ssl_svr);
  1369. SOCKET_CLOSE(server_fd);
  1370. SOCKET_CLOSE(client_fd);
  1371. error:
  1372. ssl_ctx_free(ssl_svr_ctx);
  1373. return ret;
  1374. }
  1375. #if !defined(WIN32) && defined(CONFIG_SSL_CTX_MUTEXING)
  1376. /**************************************************************************
  1377. * Multi-Threading Tests
  1378. *
  1379. **************************************************************************/
  1380. #define NUM_THREADS 100
  1381. typedef struct
  1382. {
  1383. SSL_CTX *ssl_clnt_ctx;
  1384. int port;
  1385. int thread_id;
  1386. } multi_t;
  1387. void do_multi_clnt(multi_t *multi_data)
  1388. {
  1389. int res = 1, client_fd, i;
  1390. SSL *ssl = NULL;
  1391. char tmp[5];
  1392. if ((client_fd = client_socket_init(multi_data->port)) < 0)
  1393. goto client_test_exit;
  1394. sleep(1);
  1395. ssl = ssl_client_new(multi_data->ssl_clnt_ctx, client_fd, NULL, 0);
  1396. if ((res = ssl_handshake_status(ssl)))
  1397. {
  1398. printf("Client ");
  1399. ssl_display_error(res);
  1400. goto client_test_exit;
  1401. }
  1402. sprintf(tmp, "%d\n", multi_data->thread_id);
  1403. for (i = 0; i < 10; i++)
  1404. ssl_write(ssl, (uint8_t *)tmp, strlen(tmp)+1);
  1405. client_test_exit:
  1406. ssl_free(ssl);
  1407. SOCKET_CLOSE(client_fd);
  1408. free(multi_data);
  1409. }
  1410. void do_multi_svr(SSL *ssl)
  1411. {
  1412. uint8_t *read_buf;
  1413. int *res_ptr = malloc(sizeof(int));
  1414. int res;
  1415. for (;;)
  1416. {
  1417. res = ssl_read(ssl, &read_buf);
  1418. /* kill the client */
  1419. if (res != SSL_OK)
  1420. {
  1421. if (res == SSL_ERROR_CONN_LOST)
  1422. {
  1423. SOCKET_CLOSE(ssl->client_fd);
  1424. ssl_free(ssl);
  1425. break;
  1426. }
  1427. else if (res > 0)
  1428. {
  1429. /* do nothing */
  1430. }
  1431. else /* some problem */
  1432. {
  1433. printf("Server ");
  1434. ssl_display_error(res);
  1435. goto error;
  1436. }
  1437. }
  1438. }
  1439. res = SSL_OK;
  1440. error:
  1441. *res_ptr = res;
  1442. pthread_exit(res_ptr);
  1443. }
  1444. int multi_thread_test(void)
  1445. {
  1446. int server_fd = -1;
  1447. SSL_CTX *ssl_server_ctx;
  1448. SSL_CTX *ssl_clnt_ctx;
  1449. pthread_t clnt_threads[NUM_THREADS];
  1450. pthread_t svr_threads[NUM_THREADS];
  1451. int i, res = 0;
  1452. struct sockaddr_in client_addr;
  1453. socklen_t clnt_len = sizeof(client_addr);
  1454. printf("Do multi-threading test (takes a minute)\n");
  1455. ssl_server_ctx = ssl_ctx_new(DEFAULT_SVR_OPTION, SSL_DEFAULT_SVR_SESS);
  1456. ssl_clnt_ctx = ssl_ctx_new(DEFAULT_CLNT_OPTION, SSL_DEFAULT_CLNT_SESS);
  1457. if (ssl_obj_load(ssl_clnt_ctx, SSL_OBJ_X509_CACERT,
  1458. "../ssl/test/axTLS.ca_x509.cer", NULL))
  1459. goto error;
  1460. if ((server_fd = server_socket_init(&g_port)) < 0)
  1461. goto error;
  1462. for (i = 0; i < NUM_THREADS; i++)
  1463. {
  1464. multi_t *multi_data = (multi_t *)malloc(sizeof(multi_t));
  1465. multi_data->ssl_clnt_ctx = ssl_clnt_ctx;
  1466. multi_data->port = g_port;
  1467. multi_data->thread_id = i+1;
  1468. pthread_create(&clnt_threads[i], NULL,
  1469. (void *(*)(void *))do_multi_clnt, (void *)multi_data);
  1470. pthread_detach(clnt_threads[i]);
  1471. }
  1472. for (i = 0; i < NUM_THREADS; i++)
  1473. {
  1474. SSL *ssl_svr;
  1475. int client_fd = accept(server_fd,
  1476. (struct sockaddr *)&client_addr, &clnt_len);
  1477. if (client_fd < 0)
  1478. goto error;
  1479. ssl_svr = ssl_server_new(ssl_server_ctx, client_fd);
  1480. pthread_create(&svr_threads[i], NULL,
  1481. (void *(*)(void *))do_multi_svr, (void *)ssl_svr);
  1482. }
  1483. /* make sure we've run all of the threads */
  1484. for (i = 0; i < NUM_THREADS; i++)
  1485. {
  1486. void *thread_res;
  1487. pthread_join(svr_threads[i], &thread_res);
  1488. if (*((int *)thread_res) != 0)
  1489. res = 1;
  1490. free(thread_res);
  1491. }
  1492. if (res)
  1493. goto error;
  1494. printf("Multi-thread test passed (%d)\n", NUM_THREADS);
  1495. error:
  1496. ssl_ctx_free(ssl_server_ctx);
  1497. ssl_ctx_free(ssl_clnt_ctx);
  1498. SOCKET_CLOSE(server_fd);
  1499. return res;
  1500. }
  1501. #endif /* !defined(WIN32) && defined(CONFIG_SSL_CTX_MUTEXING) */
  1502. /**************************************************************************
  1503. * Header issue
  1504. *
  1505. **************************************************************************/
  1506. static void do_header_issue(void)
  1507. {
  1508. char axtls_buf[2048];
  1509. #ifndef WIN32
  1510. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1511. #endif
  1512. sprintf(axtls_buf, "./axssl s_client -connect localhost:%d", g_port);
  1513. system(axtls_buf);
  1514. }
  1515. static int header_issue(void)
  1516. {
  1517. FILE *f = fopen("../ssl/test/header_issue.dat", "r");
  1518. int server_fd = -1, client_fd = -1, ret = 1;
  1519. uint8_t buf[2048];
  1520. int size = 0;
  1521. struct sockaddr_in client_addr;
  1522. socklen_t clnt_len = sizeof(client_addr);
  1523. #ifndef WIN32
  1524. pthread_t thread;
  1525. #endif
  1526. if (f == NULL || (server_fd = server_socket_init(&g_port)) < 0)
  1527. goto error;
  1528. #ifndef WIN32
  1529. pthread_create(&thread, NULL,
  1530. (void *(*)(void *))do_header_issue, NULL);
  1531. pthread_detach(thread);
  1532. #else
  1533. CreateThread(NULL, 1024, (LPTHREAD_START_ROUTINE)do_header_issue,
  1534. NULL, 0, NULL);
  1535. #endif
  1536. if ((client_fd = accept(server_fd,
  1537. (struct sockaddr *) &client_addr, &clnt_len)) < 0)
  1538. {
  1539. ret = SSL_ERROR_SOCK_SETUP_FAILURE;
  1540. goto error;
  1541. }
  1542. size = fread(buf, 1, sizeof(buf), f);
  1543. SOCKET_WRITE(client_fd, buf, size);
  1544. usleep(200000);
  1545. ret = 0;
  1546. error:
  1547. fclose(f);
  1548. SOCKET_CLOSE(client_fd);
  1549. SOCKET_CLOSE(server_fd);
  1550. TTY_FLUSH();
  1551. system("killall axssl");
  1552. return ret;
  1553. }
  1554. /**************************************************************************
  1555. * main()
  1556. *
  1557. **************************************************************************/
  1558. int main(int argc, char *argv[])
  1559. {
  1560. int ret = 1;
  1561. BI_CTX *bi_ctx;
  1562. int fd;
  1563. #ifdef WIN32
  1564. WSADATA wsaData;
  1565. WORD wVersionRequested = MAKEWORD(2, 2);
  1566. WSAStartup(wVersionRequested, &wsaData);
  1567. fd = _open("test_result.txt", O_WRONLY|O_TEMPORARY|O_CREAT, _S_IWRITE);
  1568. dup2(fd, 2); /* write stderr to this file */
  1569. #else
  1570. fd = open("/dev/null", O_WRONLY); /* write stderr to /dev/null */
  1571. signal(SIGPIPE, SIG_IGN); /* ignore pipe errors */
  1572. dup2(fd, 2);
  1573. #endif
  1574. /* can't do testing in this mode */
  1575. #if defined CONFIG_SSL_GENERATE_X509_CERT
  1576. printf("Error: Must compile with default key/certificates\n");
  1577. exit(1);
  1578. #endif
  1579. bi_ctx = bi_initialize();
  1580. if (AES_test(bi_ctx))
  1581. {
  1582. printf("AES tests failed\n");
  1583. goto cleanup;
  1584. }
  1585. TTY_FLUSH();
  1586. if (RC4_test(bi_ctx))
  1587. {
  1588. printf("RC4 tests failed\n");
  1589. goto cleanup;
  1590. }
  1591. TTY_FLUSH();
  1592. if (MD5_test(bi_ctx))
  1593. {
  1594. printf("MD5 tests failed\n");
  1595. goto cleanup;
  1596. }
  1597. TTY_FLUSH();
  1598. if (SHA1_test(bi_ctx))
  1599. {
  1600. printf("SHA1 tests failed\n");
  1601. goto cleanup;
  1602. }
  1603. TTY_FLUSH();
  1604. if (HMAC_test(bi_ctx))
  1605. {
  1606. printf("HMAC tests failed\n");
  1607. goto cleanup;
  1608. }
  1609. TTY_FLUSH();
  1610. if (BIGINT_test(bi_ctx))
  1611. {
  1612. printf("BigInt tests failed!\n");
  1613. goto cleanup;
  1614. }
  1615. TTY_FLUSH();
  1616. bi_terminate(bi_ctx);
  1617. if (RSA_test())
  1618. {
  1619. printf("RSA tests failed\n");
  1620. goto cleanup;
  1621. }
  1622. TTY_FLUSH();
  1623. if (cert_tests())
  1624. {
  1625. printf("CERT tests failed\n");
  1626. goto cleanup;
  1627. }
  1628. TTY_FLUSH();
  1629. #if !defined(WIN32) && defined(CONFIG_SSL_CTX_MUTEXING)
  1630. if (multi_thread_test())
  1631. goto cleanup;
  1632. #endif
  1633. if (SSL_basic_test())
  1634. goto cleanup;
  1635. system("sh ../ssl/test/killopenssl.sh");
  1636. if (SSL_client_tests())
  1637. goto cleanup;
  1638. system("sh ../ssl/test/killopenssl.sh");
  1639. if (SSL_server_tests())
  1640. goto cleanup;
  1641. system("sh ../ssl/test/killopenssl.sh");
  1642. if (header_issue())
  1643. {
  1644. printf("Header tests failed\n"); TTY_FLUSH();
  1645. goto cleanup;
  1646. }
  1647. ret = 0; /* all ok */
  1648. printf("**** ALL TESTS PASSED ****\n"); TTY_FLUSH();
  1649. cleanup:
  1650. if (ret)
  1651. printf("Error: Some tests failed!\n");
  1652. close(fd);
  1653. return ret;
  1654. }