api.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2016 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*----------------------------------------------------------------------------*
  22. | Includes
  23. *----------------------------------------------------------------------------*/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #ifdef HAVE_ECC
  29. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  30. #endif
  31. #include <wolfssl/error-ssl.h>
  32. #include <stdlib.h>
  33. #include <wolfssl/ssl.h> /* compatibility layer */
  34. #include <wolfssl/test.h>
  35. #include <tests/unit.h>
  36. /* enable testing buffer load functions */
  37. #ifndef USE_CERT_BUFFERS_2048
  38. #define USE_CERT_BUFFERS_2048
  39. #endif
  40. #include <wolfssl/certs_test.h>
  41. /*----------------------------------------------------------------------------*
  42. | Constants
  43. *----------------------------------------------------------------------------*/
  44. #define TEST_SUCCESS (1)
  45. #define TEST_FAIL (0)
  46. #define testingFmt " %s:"
  47. #define resultFmt " %s\n"
  48. static const char* passed = "passed";
  49. static const char* failed = "failed";
  50. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  51. static const char* bogusFile = "/dev/null";
  52. #endif
  53. /*----------------------------------------------------------------------------*
  54. | Setup
  55. *----------------------------------------------------------------------------*/
  56. static int test_wolfSSL_Init(void)
  57. {
  58. int result;
  59. printf(testingFmt, "wolfSSL_Init()");
  60. result = wolfSSL_Init();
  61. printf(resultFmt, result == SSL_SUCCESS ? passed : failed);
  62. return result;
  63. }
  64. static int test_wolfSSL_Cleanup(void)
  65. {
  66. int result;
  67. printf(testingFmt, "wolfSSL_Cleanup()");
  68. result = wolfSSL_Cleanup();
  69. printf(resultFmt, result == SSL_SUCCESS ? passed : failed);
  70. return result;
  71. }
  72. /*----------------------------------------------------------------------------*
  73. | Method Allocators
  74. *----------------------------------------------------------------------------*/
  75. static void test_wolfSSL_Method_Allocators(void)
  76. {
  77. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  78. do { \
  79. WOLFSSL_METHOD *method; \
  80. condition(method = allocator()); \
  81. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  82. } while(0)
  83. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  84. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  85. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  86. TEST_METHOD_ALLOCATOR(a, AssertNull)
  87. #ifndef NO_OLD_TLS
  88. #ifdef WOLFSSL_ALLOW_SSLV3
  89. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  90. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  91. #endif
  92. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  93. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  94. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  95. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  96. #endif
  97. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  98. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  99. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  100. #ifdef WOLFSSL_DTLS
  101. #ifndef NO_OLD_TLS
  102. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  103. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  104. #endif
  105. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  106. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  107. #endif
  108. #ifdef OPENSSL_EXTRA
  109. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  110. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  111. #endif
  112. }
  113. /*----------------------------------------------------------------------------*
  114. | Context
  115. *----------------------------------------------------------------------------*/
  116. static void test_wolfSSL_CTX_new(WOLFSSL_METHOD *method)
  117. {
  118. WOLFSSL_CTX *ctx;
  119. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  120. AssertNotNull(method);
  121. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  122. wolfSSL_CTX_free(ctx);
  123. }
  124. static void test_wolfSSL_CTX_use_certificate_file(void)
  125. {
  126. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  127. WOLFSSL_CTX *ctx;
  128. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  129. /* invalid context */
  130. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCert,
  131. SSL_FILETYPE_PEM));
  132. /* invalid cert file */
  133. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  134. SSL_FILETYPE_PEM));
  135. /* invalid cert type */
  136. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCert, 9999));
  137. #ifdef NO_RSA
  138. /* rsa needed */
  139. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCert,SSL_FILETYPE_PEM));
  140. #else
  141. /* success */
  142. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM));
  143. #endif
  144. wolfSSL_CTX_free(ctx);
  145. #endif
  146. }
  147. static void test_wolfSSL_CTX_use_PrivateKey_file(void)
  148. {
  149. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  150. WOLFSSL_CTX *ctx;
  151. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  152. /* invalid context */
  153. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKey,
  154. SSL_FILETYPE_PEM));
  155. /* invalid key file */
  156. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  157. SSL_FILETYPE_PEM));
  158. /* invalid key type */
  159. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, 9999));
  160. /* success */
  161. #ifdef NO_RSA
  162. /* rsa needed */
  163. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
  164. #else
  165. /* success */
  166. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
  167. #endif
  168. wolfSSL_CTX_free(ctx);
  169. #endif
  170. }
  171. /* test both file and buffer versions along with unloading trusted peer certs */
  172. static void test_wolfSSL_CTX_trust_peer_cert(void)
  173. {
  174. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT)
  175. WOLFSSL_CTX *ctx;
  176. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  177. #if !defined(NO_FILESYSTEM)
  178. /* invalid file */
  179. assert(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  180. SSL_FILETYPE_PEM) != SSL_SUCCESS);
  181. assert(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  182. SSL_FILETYPE_PEM) != SSL_SUCCESS);
  183. assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCert,
  184. SSL_FILETYPE_ASN1) != SSL_SUCCESS);
  185. /* success */
  186. assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCert, SSL_FILETYPE_PEM)
  187. == SSL_SUCCESS);
  188. /* unload cert */
  189. assert(wolfSSL_CTX_Unload_trust_peers(NULL) != SSL_SUCCESS);
  190. assert(wolfSSL_CTX_Unload_trust_peers(ctx) == SSL_SUCCESS);
  191. #endif
  192. /* Test of loading certs from buffers */
  193. /* invalid buffer */
  194. assert(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  195. SSL_FILETYPE_ASN1) != SSL_SUCCESS);
  196. /* success */
  197. #ifdef USE_CERT_BUFFERS_1024
  198. assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  199. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1) == SSL_SUCCESS);
  200. #endif
  201. #ifdef USE_CERT_BUFFERS_2048
  202. assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  203. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1) == SSL_SUCCESS);
  204. #endif
  205. /* unload cert */
  206. assert(wolfSSL_CTX_Unload_trust_peers(NULL) != SSL_SUCCESS);
  207. assert(wolfSSL_CTX_Unload_trust_peers(ctx) == SSL_SUCCESS);
  208. wolfSSL_CTX_free(ctx);
  209. #endif
  210. }
  211. static void test_wolfSSL_CTX_load_verify_locations(void)
  212. {
  213. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  214. WOLFSSL_CTX *ctx;
  215. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  216. /* invalid context */
  217. AssertFalse(wolfSSL_CTX_load_verify_locations(NULL, caCert, 0));
  218. /* invalid ca file */
  219. AssertFalse(wolfSSL_CTX_load_verify_locations(ctx, NULL, 0));
  220. AssertFalse(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, 0));
  221. #ifndef WOLFSSL_TIRTOS
  222. /* invalid path */
  223. /* not working... investigate! */
  224. /* AssertFalse(wolfSSL_CTX_load_verify_locations(ctx, caCert, bogusFile)); */
  225. #endif
  226. /* success */
  227. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCert, 0));
  228. wolfSSL_CTX_free(ctx);
  229. #endif
  230. }
  231. static void test_wolfSSL_CTX_SetTmpDH_file(void)
  232. {
  233. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH)
  234. WOLFSSL_CTX *ctx;
  235. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  236. /* invalid context */
  237. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  238. dhParam, SSL_FILETYPE_PEM));
  239. /* invalid dhParam file */
  240. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  241. NULL, SSL_FILETYPE_PEM));
  242. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  243. bogusFile, SSL_FILETYPE_PEM));
  244. /* success */
  245. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParam,
  246. SSL_FILETYPE_PEM));
  247. wolfSSL_CTX_free(ctx);
  248. #endif
  249. }
  250. static void test_wolfSSL_CTX_SetTmpDH_buffer(void)
  251. {
  252. #if !defined(NO_CERTS) && !defined(NO_DH)
  253. WOLFSSL_CTX *ctx;
  254. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  255. /* invalid context */
  256. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  257. sizeof_dh_key_der_2048, SSL_FILETYPE_ASN1));
  258. /* invalid dhParam file */
  259. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  260. 0, SSL_FILETYPE_ASN1));
  261. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  262. sizeof_dsa_key_der_2048, SSL_FILETYPE_ASN1));
  263. /* success */
  264. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  265. sizeof_dh_key_der_2048, SSL_FILETYPE_ASN1));
  266. wolfSSL_CTX_free(ctx);
  267. #endif
  268. }
  269. /*----------------------------------------------------------------------------*
  270. | SSL
  271. *----------------------------------------------------------------------------*/
  272. static void test_server_wolfSSL_new(void)
  273. {
  274. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  275. WOLFSSL_CTX *ctx;
  276. WOLFSSL_CTX *ctx_nocert;
  277. WOLFSSL *ssl;
  278. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  279. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  280. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM));
  281. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
  282. /* invalid context */
  283. AssertNull(ssl = wolfSSL_new(NULL));
  284. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  285. /* success */
  286. AssertNotNull(ssl = wolfSSL_new(ctx));
  287. wolfSSL_free(ssl);
  288. wolfSSL_CTX_free(ctx);
  289. wolfSSL_CTX_free(ctx_nocert);
  290. #endif
  291. }
  292. static void test_client_wolfSSL_new(void)
  293. {
  294. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  295. WOLFSSL_CTX *ctx;
  296. WOLFSSL_CTX *ctx_nocert;
  297. WOLFSSL *ssl;
  298. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  299. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  300. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCert, 0));
  301. /* invalid context */
  302. AssertNull(ssl = wolfSSL_new(NULL));
  303. /* success */
  304. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  305. wolfSSL_free(ssl);
  306. /* success */
  307. AssertNotNull(ssl = wolfSSL_new(ctx));
  308. wolfSSL_free(ssl);
  309. wolfSSL_CTX_free(ctx);
  310. wolfSSL_CTX_free(ctx_nocert);
  311. #endif
  312. }
  313. static void test_wolfSSL_SetTmpDH_file(void)
  314. {
  315. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH)
  316. WOLFSSL_CTX *ctx;
  317. WOLFSSL *ssl;
  318. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  319. #ifndef NO_RSA
  320. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCert,
  321. SSL_FILETYPE_PEM));
  322. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey,
  323. SSL_FILETYPE_PEM));
  324. #else
  325. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCert,
  326. SSL_FILETYPE_PEM));
  327. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKey,
  328. SSL_FILETYPE_PEM));
  329. #endif
  330. AssertNotNull(ssl = wolfSSL_new(ctx));
  331. /* invalid ssl */
  332. AssertIntNE(SSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  333. dhParam, SSL_FILETYPE_PEM));
  334. /* invalid dhParam file */
  335. AssertIntNE(SSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  336. NULL, SSL_FILETYPE_PEM));
  337. AssertIntNE(SSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  338. bogusFile, SSL_FILETYPE_PEM));
  339. /* success */
  340. AssertIntEQ(SSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParam,
  341. SSL_FILETYPE_PEM));
  342. wolfSSL_free(ssl);
  343. wolfSSL_CTX_free(ctx);
  344. #endif
  345. }
  346. static void test_wolfSSL_SetTmpDH_buffer(void)
  347. {
  348. #if !defined(NO_CERTS) && !defined(NO_DH)
  349. WOLFSSL_CTX *ctx;
  350. WOLFSSL *ssl;
  351. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  352. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  353. sizeof_server_cert_der_2048, SSL_FILETYPE_ASN1));
  354. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  355. sizeof_server_key_der_2048, SSL_FILETYPE_ASN1));
  356. AssertNotNull(ssl = wolfSSL_new(ctx));
  357. /* invalid ssl */
  358. AssertIntNE(SSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  359. sizeof_dh_key_der_2048, SSL_FILETYPE_ASN1));
  360. /* invalid dhParam file */
  361. AssertIntNE(SSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  362. 0, SSL_FILETYPE_ASN1));
  363. AssertIntNE(SSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  364. sizeof_dsa_key_der_2048, SSL_FILETYPE_ASN1));
  365. /* success */
  366. AssertIntEQ(SSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  367. sizeof_dh_key_der_2048, SSL_FILETYPE_ASN1));
  368. wolfSSL_free(ssl);
  369. wolfSSL_CTX_free(ctx);
  370. printf("SUCCESS4\n");
  371. #endif
  372. }
  373. /*----------------------------------------------------------------------------*
  374. | IO
  375. *----------------------------------------------------------------------------*/
  376. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  377. !defined(NO_RSA) && !defined(SINGLE_THREADED)
  378. #define HAVE_IO_TESTS_DEPENDENCIES
  379. #endif
  380. /* helper functions */
  381. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  382. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  383. {
  384. SOCKET_T sockfd = 0;
  385. SOCKET_T clientfd = 0;
  386. word16 port = wolfSSLPort;
  387. WOLFSSL_METHOD* method = 0;
  388. WOLFSSL_CTX* ctx = 0;
  389. WOLFSSL* ssl = 0;
  390. char msg[] = "I hear you fa shizzle!";
  391. char input[1024];
  392. int idx;
  393. #ifdef WOLFSSL_TIRTOS
  394. fdOpenSession(Task_self());
  395. #endif
  396. ((func_args*)args)->return_code = TEST_FAIL;
  397. method = wolfSSLv23_server_method();
  398. ctx = wolfSSL_CTX_new(method);
  399. #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && \
  400. !defined(WOLFSSL_SNIFFER) && !defined(WOLFSSL_MDK_SHELL) && \
  401. !defined(WOLFSSL_TIRTOS)
  402. port = 0;
  403. #endif
  404. wolfSSL_CTX_set_verify(ctx,
  405. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  406. #ifdef OPENSSL_EXTRA
  407. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  408. #endif
  409. if (wolfSSL_CTX_load_verify_locations(ctx, cliCert, 0) != SSL_SUCCESS)
  410. {
  411. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  412. goto done;
  413. }
  414. if (wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
  415. != SSL_SUCCESS)
  416. {
  417. /*err_sys("can't load server cert chain file, "
  418. "Please run from wolfSSL home dir");*/
  419. goto done;
  420. }
  421. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
  422. != SSL_SUCCESS)
  423. {
  424. /*err_sys("can't load server key file, "
  425. "Please run from wolfSSL home dir");*/
  426. goto done;
  427. }
  428. ssl = wolfSSL_new(ctx);
  429. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 1);
  430. CloseSocket(sockfd);
  431. if (wolfSSL_set_fd(ssl, clientfd) != SSL_SUCCESS) {
  432. /*err_sys("SSL_set_fd failed");*/
  433. goto done;
  434. }
  435. #ifdef NO_PSK
  436. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  437. wolfSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
  438. #elif !defined(NO_DH)
  439. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  440. #endif
  441. #endif
  442. if (wolfSSL_accept(ssl) != SSL_SUCCESS)
  443. {
  444. int err = wolfSSL_get_error(ssl, 0);
  445. char buffer[WOLFSSL_MAX_ERROR_SZ];
  446. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
  447. /*err_sys("SSL_accept failed");*/
  448. goto done;
  449. }
  450. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  451. if (idx > 0) {
  452. input[idx] = 0;
  453. printf("Client message: %s\n", input);
  454. }
  455. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
  456. {
  457. /*err_sys("SSL_write failed");*/
  458. #ifdef WOLFSSL_TIRTOS
  459. return;
  460. #else
  461. return 0;
  462. #endif
  463. }
  464. #ifdef WOLFSSL_TIRTOS
  465. Task_yield();
  466. #endif
  467. done:
  468. wolfSSL_shutdown(ssl);
  469. wolfSSL_free(ssl);
  470. wolfSSL_CTX_free(ctx);
  471. CloseSocket(clientfd);
  472. ((func_args*)args)->return_code = TEST_SUCCESS;
  473. #ifdef WOLFSSL_TIRTOS
  474. fdCloseSession(Task_self());
  475. #endif
  476. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  477. && defined(HAVE_THREAD_LS)
  478. wc_ecc_fp_free(); /* free per thread cache */
  479. #endif
  480. #ifndef WOLFSSL_TIRTOS
  481. return 0;
  482. #endif
  483. }
  484. static void test_client_nofail(void* args)
  485. {
  486. SOCKET_T sockfd = 0;
  487. WOLFSSL_METHOD* method = 0;
  488. WOLFSSL_CTX* ctx = 0;
  489. WOLFSSL* ssl = 0;
  490. char msg[64] = "hello wolfssl!";
  491. char reply[1024];
  492. int input;
  493. int msgSz = (int)strlen(msg);
  494. #ifdef WOLFSSL_TIRTOS
  495. fdOpenSession(Task_self());
  496. #endif
  497. ((func_args*)args)->return_code = TEST_FAIL;
  498. method = wolfSSLv23_client_method();
  499. ctx = wolfSSL_CTX_new(method);
  500. #ifdef OPENSSL_EXTRA
  501. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  502. #endif
  503. if (wolfSSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
  504. {
  505. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  506. goto done2;
  507. }
  508. if (wolfSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM)
  509. != SSL_SUCCESS)
  510. {
  511. /*err_sys("can't load client cert file, "
  512. "Please run from wolfSSL home dir");*/
  513. goto done2;
  514. }
  515. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM)
  516. != SSL_SUCCESS)
  517. {
  518. /*err_sys("can't load client key file, "
  519. "Please run from wolfSSL home dir");*/
  520. goto done2;
  521. }
  522. ssl = wolfSSL_new(ctx);
  523. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, ssl);
  524. if (wolfSSL_set_fd(ssl, sockfd) != SSL_SUCCESS) {
  525. /*err_sys("SSL_set_fd failed");*/
  526. goto done2;
  527. }
  528. if (wolfSSL_connect(ssl) != SSL_SUCCESS)
  529. {
  530. int err = wolfSSL_get_error(ssl, 0);
  531. char buffer[WOLFSSL_MAX_ERROR_SZ];
  532. printf("err = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
  533. /*printf("SSL_connect failed");*/
  534. goto done2;
  535. }
  536. if (wolfSSL_write(ssl, msg, msgSz) != msgSz)
  537. {
  538. /*err_sys("SSL_write failed");*/
  539. goto done2;
  540. }
  541. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  542. if (input > 0)
  543. {
  544. reply[input] = 0;
  545. printf("Server response: %s\n", reply);
  546. }
  547. done2:
  548. wolfSSL_free(ssl);
  549. wolfSSL_CTX_free(ctx);
  550. CloseSocket(sockfd);
  551. ((func_args*)args)->return_code = TEST_SUCCESS;
  552. #ifdef WOLFSSL_TIRTOS
  553. fdCloseSession(Task_self());
  554. #endif
  555. return;
  556. }
  557. /* SNI / ALPN helper functions */
  558. #if defined(HAVE_SNI) || defined(HAVE_ALPN)
  559. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  560. {
  561. callback_functions* callbacks = ((func_args*)args)->callbacks;
  562. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  563. WOLFSSL* ssl = NULL;
  564. SOCKET_T sfd = 0;
  565. SOCKET_T cfd = 0;
  566. word16 port = wolfSSLPort;
  567. char msg[] = "I hear you fa shizzle!";
  568. int len = (int) XSTRLEN(msg);
  569. char input[1024];
  570. int idx;
  571. #ifdef WOLFSSL_TIRTOS
  572. fdOpenSession(Task_self());
  573. #endif
  574. ((func_args*)args)->return_code = TEST_FAIL;
  575. #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && \
  576. !defined(WOLFSSL_SNIFFER) && !defined(WOLFSSL_MDK_SHELL) && \
  577. !defined(WOLFSSL_TIRTOS)
  578. port = 0;
  579. #endif
  580. wolfSSL_CTX_set_verify(ctx,
  581. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  582. #ifdef OPENSSL_EXTRA
  583. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  584. #endif
  585. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, cliCert, 0));
  586. AssertIntEQ(SSL_SUCCESS,
  587. wolfSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM));
  588. AssertIntEQ(SSL_SUCCESS,
  589. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM));
  590. if (callbacks->ctx_ready)
  591. callbacks->ctx_ready(ctx);
  592. ssl = wolfSSL_new(ctx);
  593. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 1);
  594. CloseSocket(sfd);
  595. AssertIntEQ(SSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  596. #ifdef NO_PSK
  597. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  598. wolfSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
  599. #elif !defined(NO_DH)
  600. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  601. #endif
  602. #endif
  603. if (callbacks->ssl_ready)
  604. callbacks->ssl_ready(ssl);
  605. /* AssertIntEQ(SSL_SUCCESS, wolfSSL_accept(ssl)); */
  606. if (wolfSSL_accept(ssl) != SSL_SUCCESS) {
  607. int err = wolfSSL_get_error(ssl, 0);
  608. char buffer[WOLFSSL_MAX_ERROR_SZ];
  609. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
  610. } else {
  611. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  612. input[idx] = 0;
  613. printf("Client message: %s\n", input);
  614. }
  615. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  616. #ifdef WOLFSSL_TIRTOS
  617. Task_yield();
  618. #endif
  619. wolfSSL_shutdown(ssl);
  620. }
  621. if (callbacks->on_result)
  622. callbacks->on_result(ssl);
  623. wolfSSL_free(ssl);
  624. wolfSSL_CTX_free(ctx);
  625. CloseSocket(cfd);
  626. ((func_args*)args)->return_code = TEST_SUCCESS;
  627. #ifdef WOLFSSL_TIRTOS
  628. fdCloseSession(Task_self());
  629. #endif
  630. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  631. && defined(HAVE_THREAD_LS)
  632. wc_ecc_fp_free(); /* free per thread cache */
  633. #endif
  634. #ifndef WOLFSSL_TIRTOS
  635. return 0;
  636. #endif
  637. }
  638. static void run_wolfssl_client(void* args)
  639. {
  640. callback_functions* callbacks = ((func_args*)args)->callbacks;
  641. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  642. WOLFSSL* ssl = NULL;
  643. SOCKET_T sfd = 0;
  644. char msg[] = "hello wolfssl server!";
  645. int len = (int) XSTRLEN(msg);
  646. char input[1024];
  647. int idx;
  648. #ifdef WOLFSSL_TIRTOS
  649. fdOpenSession(Task_self());
  650. #endif
  651. ((func_args*)args)->return_code = TEST_FAIL;
  652. #ifdef OPENSSL_EXTRA
  653. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  654. #endif
  655. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, caCert, 0));
  656. AssertIntEQ(SSL_SUCCESS,
  657. wolfSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM));
  658. AssertIntEQ(SSL_SUCCESS,
  659. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM));
  660. if (callbacks->ctx_ready)
  661. callbacks->ctx_ready(ctx);
  662. ssl = wolfSSL_new(ctx);
  663. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, ssl);
  664. AssertIntEQ(SSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  665. if (callbacks->ssl_ready)
  666. callbacks->ssl_ready(ssl);
  667. if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
  668. int err = wolfSSL_get_error(ssl, 0);
  669. char buffer[WOLFSSL_MAX_ERROR_SZ];
  670. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer));
  671. } else {
  672. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  673. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  674. input[idx] = 0;
  675. printf("Server response: %s\n", input);
  676. }
  677. }
  678. if (callbacks->on_result)
  679. callbacks->on_result(ssl);
  680. wolfSSL_free(ssl);
  681. wolfSSL_CTX_free(ctx);
  682. CloseSocket(sfd);
  683. ((func_args*)args)->return_code = TEST_SUCCESS;
  684. #ifdef WOLFSSL_TIRTOS
  685. fdCloseSession(Task_self());
  686. #endif
  687. }
  688. #endif /* defined(HAVE_SNI) || defined(HAVE_ALPN) */
  689. #endif /* io tests dependencies */
  690. static void test_wolfSSL_read_write(void)
  691. {
  692. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  693. /* The unit testing for read and write shall happen simutaneously, since
  694. * one can't do anything with one without the other. (Except for a failure
  695. * test case.) This function will call all the others that will set up,
  696. * execute, and report their test findings.
  697. *
  698. * Set up the success case first. This function will become the template
  699. * for the other tests. This should eventually be renamed
  700. *
  701. * The success case isn't interesting, how can this fail?
  702. * - Do not give the client context a CA certificate. The connect should
  703. * fail. Do not need server for this?
  704. * - Using NULL for the ssl object on server. Do not need client for this.
  705. * - Using NULL for the ssl object on client. Do not need server for this.
  706. * - Good ssl objects for client and server. Client write() without server
  707. * read().
  708. * - Good ssl objects for client and server. Server write() without client
  709. * read().
  710. * - Forgetting the password callback?
  711. */
  712. tcp_ready ready;
  713. func_args client_args;
  714. func_args server_args;
  715. THREAD_TYPE serverThread;
  716. #ifdef WOLFSSL_TIRTOS
  717. fdOpenSession(Task_self());
  718. #endif
  719. StartTCP();
  720. InitTcpReady(&ready);
  721. server_args.signal = &ready;
  722. client_args.signal = &ready;
  723. start_thread(test_server_nofail, &server_args, &serverThread);
  724. wait_tcp_ready(&server_args);
  725. test_client_nofail(&client_args);
  726. join_thread(serverThread);
  727. AssertTrue(client_args.return_code);
  728. AssertTrue(server_args.return_code);
  729. FreeTcpReady(&ready);
  730. #ifdef WOLFSSL_TIRTOS
  731. fdOpenSession(Task_self());
  732. #endif
  733. #endif
  734. }
  735. /*----------------------------------------------------------------------------*
  736. | TLS extensions tests
  737. *----------------------------------------------------------------------------*/
  738. #if defined(HAVE_SNI) || defined(HAVE_ALPN)
  739. /* connection test runner */
  740. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  741. callback_functions* server_callbacks)
  742. {
  743. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  744. tcp_ready ready;
  745. func_args client_args;
  746. func_args server_args;
  747. THREAD_TYPE serverThread;
  748. StartTCP();
  749. client_args.callbacks = client_callbacks;
  750. server_args.callbacks = server_callbacks;
  751. #ifdef WOLFSSL_TIRTOS
  752. fdOpenSession(Task_self());
  753. #endif
  754. /* RUN Server side */
  755. InitTcpReady(&ready);
  756. server_args.signal = &ready;
  757. client_args.signal = &ready;
  758. start_thread(run_wolfssl_server, &server_args, &serverThread);
  759. wait_tcp_ready(&server_args);
  760. /* RUN Client side */
  761. run_wolfssl_client(&client_args);
  762. join_thread(serverThread);
  763. FreeTcpReady(&ready);
  764. #ifdef WOLFSSL_TIRTOS
  765. fdCloseSession(Task_self());
  766. #endif
  767. #else
  768. (void)client_callbacks;
  769. (void)server_callbacks;
  770. #endif
  771. }
  772. #endif /* defined(HAVE_SNI) || defined(HAVE_ALPN) */
  773. #ifdef HAVE_SNI
  774. static void test_wolfSSL_UseSNI_params(void)
  775. {
  776. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  777. WOLFSSL *ssl = wolfSSL_new(ctx);
  778. AssertNotNull(ctx);
  779. AssertNotNull(ssl);
  780. /* invalid [ctx|ssl] */
  781. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  782. AssertIntNE(SSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  783. /* invalid type */
  784. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  785. AssertIntNE(SSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  786. /* invalid data */
  787. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  788. AssertIntNE(SSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  789. /* success case */
  790. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  791. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  792. wolfSSL_free(ssl);
  793. wolfSSL_CTX_free(ctx);
  794. }
  795. /* BEGIN of connection tests callbacks */
  796. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  797. {
  798. AssertIntEQ(SSL_SUCCESS,
  799. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  800. }
  801. static void use_SNI_at_ssl(WOLFSSL* ssl)
  802. {
  803. AssertIntEQ(SSL_SUCCESS,
  804. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  805. }
  806. static void different_SNI_at_ssl(WOLFSSL* ssl)
  807. {
  808. AssertIntEQ(SSL_SUCCESS,
  809. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  810. }
  811. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  812. {
  813. use_SNI_at_ssl(ssl);
  814. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  815. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  816. }
  817. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  818. {
  819. use_SNI_at_ssl(ssl);
  820. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  821. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  822. }
  823. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  824. {
  825. use_SNI_at_ctx(ctx);
  826. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  827. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  828. }
  829. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  830. {
  831. use_SNI_at_ssl(ssl);
  832. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  833. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  834. }
  835. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  836. {
  837. use_SNI_at_ctx(ctx);
  838. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  839. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  840. }
  841. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  842. {
  843. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  844. }
  845. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  846. {
  847. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  848. }
  849. static void verify_SNI_no_matching(WOLFSSL* ssl)
  850. {
  851. byte type = WOLFSSL_SNI_HOST_NAME;
  852. char* request = (char*) &type; /* to be overwriten */
  853. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  854. AssertNotNull(request);
  855. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  856. AssertNull(request);
  857. }
  858. static void verify_SNI_real_matching(WOLFSSL* ssl)
  859. {
  860. byte type = WOLFSSL_SNI_HOST_NAME;
  861. char* request = NULL;
  862. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  863. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  864. AssertNotNull(request);
  865. AssertStrEQ("www.wolfssl.com", request);
  866. }
  867. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  868. {
  869. byte type = WOLFSSL_SNI_HOST_NAME;
  870. char* request = NULL;
  871. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  872. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  873. AssertNotNull(request);
  874. AssertStrEQ("ww2.wolfssl.com", request);
  875. }
  876. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  877. {
  878. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  879. }
  880. /* END of connection tests callbacks */
  881. static void test_wolfSSL_UseSNI_connection(void)
  882. {
  883. unsigned long i;
  884. callback_functions callbacks[] = {
  885. /* success case at ctx */
  886. {0, use_SNI_at_ctx, 0, 0},
  887. {0, use_SNI_at_ctx, 0, verify_SNI_real_matching},
  888. /* success case at ssl */
  889. {0, 0, use_SNI_at_ssl, 0},
  890. {0, 0, use_SNI_at_ssl, verify_SNI_real_matching},
  891. /* default missmatch behavior */
  892. {0, 0, different_SNI_at_ssl, verify_FATAL_ERROR_on_client},
  893. {0, 0, use_SNI_at_ssl, verify_UNKNOWN_SNI_on_server},
  894. /* continue on missmatch */
  895. {0, 0, different_SNI_at_ssl, 0},
  896. {0, 0, use_SNI_WITH_CONTINUE_at_ssl, verify_SNI_no_matching},
  897. /* fake answer on missmatch */
  898. {0, 0, different_SNI_at_ssl, 0},
  899. {0, 0, use_SNI_WITH_FAKE_ANSWER_at_ssl, verify_SNI_fake_matching},
  900. /* sni abort - success */
  901. {0, use_SNI_at_ctx, 0, 0},
  902. {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_real_matching},
  903. /* sni abort - abort when absent (ctx) */
  904. {0, 0, 0, verify_FATAL_ERROR_on_client},
  905. {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_ABSENT_on_server},
  906. /* sni abort - abort when absent (ssl) */
  907. {0, 0, 0, verify_FATAL_ERROR_on_client},
  908. {0, 0, use_MANDATORY_SNI_at_ssl, verify_SNI_ABSENT_on_server},
  909. /* sni abort - success when overwriten */
  910. {0, 0, 0, 0},
  911. {0, use_MANDATORY_SNI_at_ctx, use_SNI_at_ssl, verify_SNI_no_matching},
  912. /* sni abort - success when allowing missmatches */
  913. {0, 0, different_SNI_at_ssl, 0},
  914. {0, use_PSEUDO_MANDATORY_SNI_at_ctx, 0, verify_SNI_fake_matching},
  915. };
  916. for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
  917. callbacks[i ].method = wolfSSLv23_client_method;
  918. callbacks[i + 1].method = wolfSSLv23_server_method;
  919. test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
  920. }
  921. }
  922. static void test_wolfSSL_SNI_GetFromBuffer(void)
  923. {
  924. byte buffer[] = { /* www.paypal.com */
  925. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  926. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  927. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  928. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  929. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  930. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  931. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  932. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  933. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  934. };
  935. byte buffer2[] = { /* api.textmate.org */
  936. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  937. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  938. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  939. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  940. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  941. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  942. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  943. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  944. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  945. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  946. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  947. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  948. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  949. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  950. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  951. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  952. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  953. };
  954. byte buffer3[] = { /* no sni extension */
  955. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  956. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  957. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  958. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  959. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  960. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  961. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  962. };
  963. byte buffer4[] = { /* last extension has zero size */
  964. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  965. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  966. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  967. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  968. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  969. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  970. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  971. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  972. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  973. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  974. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  975. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  976. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  977. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  978. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  979. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  980. 0x12, 0x00, 0x00
  981. };
  982. byte buffer5[] = { /* SSL v2.0 client hello */
  983. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  984. /* dummy bytes bellow, just to pass size check */
  985. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  986. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  987. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  988. };
  989. byte result[32] = {0};
  990. word32 length = 32;
  991. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer4, sizeof(buffer4),
  992. 0, result, &length));
  993. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer3, sizeof(buffer3),
  994. 0, result, &length));
  995. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
  996. 1, result, &length));
  997. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  998. 0, result, &length));
  999. buffer[0] = 0x16;
  1000. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  1001. 0, result, &length));
  1002. buffer[1] = 0x03;
  1003. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buffer,
  1004. sizeof(buffer), 0, result, &length));
  1005. buffer[2] = 0x03;
  1006. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buffer,
  1007. sizeof(buffer), 0, result, &length));
  1008. buffer[4] = 0x64;
  1009. AssertIntEQ(SSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer, sizeof(buffer),
  1010. 0, result, &length));
  1011. result[length] = 0;
  1012. AssertStrEQ("www.paypal.com", (const char*) result);
  1013. length = 32;
  1014. AssertIntEQ(SSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buffer2, sizeof(buffer2),
  1015. 0, result, &length));
  1016. result[length] = 0;
  1017. AssertStrEQ("api.textmate.org", (const char*) result);
  1018. /* SSL v2.0 tests */
  1019. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buffer5,
  1020. sizeof(buffer5), 0, result, &length));
  1021. buffer5[2] = 0x02;
  1022. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  1023. sizeof(buffer5), 0, result, &length));
  1024. buffer5[2] = 0x01; buffer5[6] = 0x08;
  1025. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  1026. sizeof(buffer5), 0, result, &length));
  1027. buffer5[6] = 0x09; buffer5[8] = 0x01;
  1028. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buffer5,
  1029. sizeof(buffer5), 0, result, &length));
  1030. }
  1031. #endif /* HAVE_SNI */
  1032. static void test_wolfSSL_UseSNI(void)
  1033. {
  1034. #ifdef HAVE_SNI
  1035. test_wolfSSL_UseSNI_params();
  1036. test_wolfSSL_UseSNI_connection();
  1037. test_wolfSSL_SNI_GetFromBuffer();
  1038. #endif
  1039. }
  1040. static void test_wolfSSL_UseMaxFragment(void)
  1041. {
  1042. #ifdef HAVE_MAX_FRAGMENT
  1043. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1044. WOLFSSL *ssl = wolfSSL_new(ctx);
  1045. AssertNotNull(ctx);
  1046. AssertNotNull(ssl);
  1047. /* error cases */
  1048. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  1049. AssertIntNE(SSL_SUCCESS, wolfSSL_UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  1050. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, 0));
  1051. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, 6));
  1052. AssertIntNE(SSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, 0));
  1053. AssertIntNE(SSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, 6));
  1054. /* success case */
  1055. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  1056. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  1057. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  1058. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  1059. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  1060. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  1061. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  1062. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  1063. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  1064. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  1065. wolfSSL_free(ssl);
  1066. wolfSSL_CTX_free(ctx);
  1067. #endif
  1068. }
  1069. static void test_wolfSSL_UseTruncatedHMAC(void)
  1070. {
  1071. #ifdef HAVE_TRUNCATED_HMAC
  1072. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1073. WOLFSSL *ssl = wolfSSL_new(ctx);
  1074. AssertNotNull(ctx);
  1075. AssertNotNull(ssl);
  1076. /* error cases */
  1077. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  1078. AssertIntNE(SSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  1079. /* success case */
  1080. AssertIntEQ(SSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  1081. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  1082. wolfSSL_free(ssl);
  1083. wolfSSL_CTX_free(ctx);
  1084. #endif
  1085. }
  1086. static void test_wolfSSL_UseSupportedCurve(void)
  1087. {
  1088. #ifdef HAVE_SUPPORTED_CURVES
  1089. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1090. WOLFSSL *ssl = wolfSSL_new(ctx);
  1091. AssertNotNull(ctx);
  1092. AssertNotNull(ssl);
  1093. #ifndef NO_WOLFSSL_CLIENT
  1094. /* error cases */
  1095. AssertIntNE(SSL_SUCCESS,
  1096. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  1097. AssertIntNE(SSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  1098. AssertIntNE(SSL_SUCCESS,
  1099. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  1100. AssertIntNE(SSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  1101. /* success case */
  1102. AssertIntEQ(SSL_SUCCESS,
  1103. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  1104. AssertIntEQ(SSL_SUCCESS,
  1105. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  1106. #endif
  1107. wolfSSL_free(ssl);
  1108. wolfSSL_CTX_free(ctx);
  1109. #endif
  1110. }
  1111. #ifdef HAVE_ALPN
  1112. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  1113. {
  1114. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  1115. }
  1116. static void use_ALPN_all(WOLFSSL* ssl)
  1117. {
  1118. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  1119. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  1120. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  1121. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  1122. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  1123. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  1124. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  1125. }
  1126. static void use_ALPN_all_continue(WOLFSSL* ssl)
  1127. {
  1128. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  1129. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  1130. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  1131. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  1132. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  1133. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  1134. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  1135. }
  1136. static void use_ALPN_one(WOLFSSL* ssl)
  1137. {
  1138. /* spdy/2 */
  1139. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  1140. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  1141. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  1142. }
  1143. static void use_ALPN_unknown(WOLFSSL* ssl)
  1144. {
  1145. /* http/2.0 */
  1146. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  1147. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  1148. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  1149. }
  1150. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  1151. {
  1152. /* http/2.0 */
  1153. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  1154. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  1155. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  1156. }
  1157. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  1158. {
  1159. /* spdy/3 */
  1160. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  1161. char *proto;
  1162. word16 protoSz = 0;
  1163. AssertIntEQ(SSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  1164. /* check value */
  1165. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  1166. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  1167. }
  1168. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  1169. {
  1170. char *proto = NULL;
  1171. word16 protoSz = 0;
  1172. AssertIntEQ(SSL_ALPN_NOT_FOUND,
  1173. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  1174. /* check value */
  1175. AssertIntEQ(1, 0 == protoSz);
  1176. AssertIntEQ(1, NULL == proto);
  1177. }
  1178. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  1179. {
  1180. /* http/1.1 */
  1181. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  1182. char *proto;
  1183. word16 protoSz = 0;
  1184. AssertIntEQ(SSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  1185. /* check value */
  1186. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  1187. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  1188. }
  1189. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  1190. {
  1191. /* spdy/2 */
  1192. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  1193. char *proto;
  1194. word16 protoSz = 0;
  1195. AssertIntEQ(SSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  1196. /* check value */
  1197. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  1198. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  1199. }
  1200. static void verify_ALPN_client_list(WOLFSSL* ssl)
  1201. {
  1202. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  1203. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  1204. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  1205. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  1206. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  1207. char *clist = NULL;
  1208. word16 clistSz = 0;
  1209. AssertIntEQ(SSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  1210. &clistSz));
  1211. /* check value */
  1212. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  1213. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  1214. XFREE(clist, 0, DYNAMIC_TYPE_TLSX);
  1215. }
  1216. static void test_wolfSSL_UseALPN_connection(void)
  1217. {
  1218. unsigned long i;
  1219. callback_functions callbacks[] = {
  1220. /* success case same list */
  1221. {0, 0, use_ALPN_all, 0},
  1222. {0, 0, use_ALPN_all, verify_ALPN_matching_http1},
  1223. /* success case only one for server */
  1224. {0, 0, use_ALPN_all, 0},
  1225. {0, 0, use_ALPN_one, verify_ALPN_matching_spdy2},
  1226. /* success case only one for client */
  1227. {0, 0, use_ALPN_one, 0},
  1228. {0, 0, use_ALPN_all, verify_ALPN_matching_spdy2},
  1229. /* success case none for client */
  1230. {0, 0, 0, 0},
  1231. {0, 0, use_ALPN_all, 0},
  1232. /* success case missmatch behavior but option 'continue' set */
  1233. {0, 0, use_ALPN_all_continue, verify_ALPN_not_matching_continue},
  1234. {0, 0, use_ALPN_unknown_continue, 0},
  1235. /* success case read protocol send by client */
  1236. {0, 0, use_ALPN_all, 0},
  1237. {0, 0, use_ALPN_one, verify_ALPN_client_list},
  1238. /* missmatch behavior with same list
  1239. * the first and only this one must be taken */
  1240. {0, 0, use_ALPN_all, 0},
  1241. {0, 0, use_ALPN_all, verify_ALPN_not_matching_spdy3},
  1242. /* default missmatch behavior */
  1243. {0, 0, use_ALPN_all, 0},
  1244. {0, 0, use_ALPN_unknown, verify_ALPN_FATAL_ERROR_on_client},
  1245. };
  1246. for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
  1247. callbacks[i ].method = wolfSSLv23_client_method;
  1248. callbacks[i + 1].method = wolfSSLv23_server_method;
  1249. test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
  1250. }
  1251. }
  1252. static void test_wolfSSL_UseALPN_params(void)
  1253. {
  1254. /* "http/1.1" */
  1255. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  1256. /* "spdy/1" */
  1257. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  1258. /* "spdy/2" */
  1259. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  1260. /* "spdy/3" */
  1261. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  1262. char buff[256];
  1263. word32 idx;
  1264. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1265. WOLFSSL *ssl = wolfSSL_new(ctx);
  1266. AssertNotNull(ctx);
  1267. AssertNotNull(ssl);
  1268. /* error cases */
  1269. AssertIntNE(SSL_SUCCESS,
  1270. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  1271. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  1272. AssertIntNE(SSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  1273. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  1274. /* success case */
  1275. /* http1 only */
  1276. AssertIntEQ(SSL_SUCCESS,
  1277. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  1278. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  1279. /* http1, spdy1 */
  1280. memcpy(buff, http1, sizeof(http1));
  1281. idx = sizeof(http1);
  1282. buff[idx++] = ',';
  1283. memcpy(buff+idx, spdy1, sizeof(spdy1));
  1284. idx += sizeof(spdy1);
  1285. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  1286. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  1287. /* http1, spdy2, spdy1 */
  1288. memcpy(buff, http1, sizeof(http1));
  1289. idx = sizeof(http1);
  1290. buff[idx++] = ',';
  1291. memcpy(buff+idx, spdy2, sizeof(spdy2));
  1292. idx += sizeof(spdy2);
  1293. buff[idx++] = ',';
  1294. memcpy(buff+idx, spdy1, sizeof(spdy1));
  1295. idx += sizeof(spdy1);
  1296. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  1297. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  1298. /* spdy3, http1, spdy2, spdy1 */
  1299. memcpy(buff, spdy3, sizeof(spdy3));
  1300. idx = sizeof(spdy3);
  1301. buff[idx++] = ',';
  1302. memcpy(buff+idx, http1, sizeof(http1));
  1303. idx += sizeof(http1);
  1304. buff[idx++] = ',';
  1305. memcpy(buff+idx, spdy2, sizeof(spdy2));
  1306. idx += sizeof(spdy2);
  1307. buff[idx++] = ',';
  1308. memcpy(buff+idx, spdy1, sizeof(spdy1));
  1309. idx += sizeof(spdy1);
  1310. AssertIntEQ(SSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  1311. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  1312. wolfSSL_free(ssl);
  1313. wolfSSL_CTX_free(ctx);
  1314. }
  1315. #endif /* HAVE_ALPN */
  1316. static void test_wolfSSL_UseALPN(void)
  1317. {
  1318. #ifdef HAVE_ALPN
  1319. test_wolfSSL_UseALPN_connection();
  1320. test_wolfSSL_UseALPN_params();
  1321. #endif
  1322. }
  1323. /*----------------------------------------------------------------------------*
  1324. | Main
  1325. *----------------------------------------------------------------------------*/
  1326. void ApiTest(void)
  1327. {
  1328. printf(" Begin API Tests\n");
  1329. test_wolfSSL_Init();
  1330. test_wolfSSL_Method_Allocators();
  1331. test_wolfSSL_CTX_new(wolfSSLv23_server_method());
  1332. test_wolfSSL_CTX_use_certificate_file();
  1333. test_wolfSSL_CTX_use_PrivateKey_file();
  1334. test_wolfSSL_CTX_load_verify_locations();
  1335. test_wolfSSL_CTX_trust_peer_cert();
  1336. test_wolfSSL_CTX_SetTmpDH_file();
  1337. test_wolfSSL_CTX_SetTmpDH_buffer();
  1338. test_server_wolfSSL_new();
  1339. test_client_wolfSSL_new();
  1340. test_wolfSSL_SetTmpDH_file();
  1341. test_wolfSSL_SetTmpDH_buffer();
  1342. test_wolfSSL_read_write();
  1343. /* TLS extensions tests */
  1344. test_wolfSSL_UseSNI();
  1345. test_wolfSSL_UseMaxFragment();
  1346. test_wolfSSL_UseTruncatedHMAC();
  1347. test_wolfSSL_UseSupportedCurve();
  1348. test_wolfSSL_UseALPN();
  1349. test_wolfSSL_Cleanup();
  1350. printf(" End API Tests\n");
  1351. }