x509_str.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. /* x509_str.c
  2. *
  3. * Copyright (C) 2006-2022 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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #if !defined(WOLFSSL_X509_STORE_INCLUDED)
  26. #ifndef WOLFSSL_IGNORE_FILE_WARN
  27. #warning x509_str.c does not need to be compiled separately from ssl.c
  28. #endif
  29. #else
  30. #ifndef WOLFCRYPT_ONLY
  31. #ifndef NO_CERTS
  32. /*******************************************************************************
  33. * START OF X509_STORE_CTX APIs
  34. ******************************************************************************/
  35. #ifdef OPENSSL_EXTRA
  36. WOLFSSL_X509_STORE_CTX* wolfSSL_X509_STORE_CTX_new(void)
  37. {
  38. WOLFSSL_X509_STORE_CTX* ctx;
  39. WOLFSSL_ENTER("X509_STORE_CTX_new");
  40. ctx = (WOLFSSL_X509_STORE_CTX*)XMALLOC(sizeof(WOLFSSL_X509_STORE_CTX), NULL,
  41. DYNAMIC_TYPE_X509_CTX);
  42. if (ctx != NULL) {
  43. ctx->param = NULL;
  44. wolfSSL_X509_STORE_CTX_init(ctx, NULL, NULL, NULL);
  45. }
  46. return ctx;
  47. }
  48. int wolfSSL_X509_STORE_CTX_init(WOLFSSL_X509_STORE_CTX* ctx,
  49. WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509, WOLF_STACK_OF(WOLFSSL_X509)* sk)
  50. {
  51. WOLFSSL_X509* x509_cert;
  52. int ret = 0;
  53. (void)sk;
  54. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_init");
  55. if (ctx != NULL) {
  56. ctx->store = store;
  57. #ifndef WOLFSSL_X509_STORE_CERTS
  58. ctx->current_cert = x509;
  59. #else
  60. if(x509 != NULL){
  61. ctx->current_cert = wolfSSL_X509_d2i(NULL, x509->derCert->buffer,
  62. x509->derCert->length);
  63. if(ctx->current_cert == NULL)
  64. return WOLFSSL_FAILURE;
  65. } else
  66. ctx->current_cert = NULL;
  67. #endif
  68. ctx->chain = sk;
  69. /* Add intermediate certificates from stack to store */
  70. while (sk != NULL) {
  71. x509_cert = sk->data.x509;
  72. if (x509_cert != NULL && x509_cert->isCa) {
  73. ret = wolfSSL_X509_STORE_add_cert(store, x509_cert);
  74. if (ret < 0) {
  75. return WOLFSSL_FAILURE;
  76. }
  77. }
  78. sk = sk->next;
  79. }
  80. ctx->sesChain = NULL;
  81. ctx->domain = NULL;
  82. #ifdef HAVE_EX_DATA
  83. XMEMSET(&ctx->ex_data, 0, sizeof(ctx->ex_data));
  84. #endif
  85. ctx->userCtx = NULL;
  86. ctx->error = 0;
  87. ctx->error_depth = 0;
  88. ctx->discardSessionCerts = 0;
  89. if (ctx->param == NULL) {
  90. ctx->param = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  91. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  92. NULL, DYNAMIC_TYPE_OPENSSL);
  93. if (ctx->param == NULL){
  94. WOLFSSL_MSG("wolfSSL_X509_STORE_CTX_init failed");
  95. return WOLFSSL_FAILURE;
  96. }
  97. }
  98. return WOLFSSL_SUCCESS;
  99. }
  100. return WOLFSSL_FAILURE;
  101. }
  102. /* free's extra data */
  103. void wolfSSL_X509_STORE_CTX_free(WOLFSSL_X509_STORE_CTX* ctx)
  104. {
  105. WOLFSSL_ENTER("X509_STORE_CTX_free");
  106. if (ctx != NULL) {
  107. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  108. wolfSSL_CRYPTO_cleanup_ex_data(&ctx->ex_data);
  109. #endif
  110. if (ctx->param != NULL) {
  111. XFREE(ctx->param, NULL, DYNAMIC_TYPE_OPENSSL);
  112. ctx->param = NULL;
  113. }
  114. XFREE(ctx, NULL, DYNAMIC_TYPE_X509_CTX);
  115. }
  116. }
  117. void wolfSSL_X509_STORE_CTX_cleanup(WOLFSSL_X509_STORE_CTX* ctx)
  118. {
  119. if (ctx != NULL) {
  120. if (ctx->param != NULL) {
  121. XFREE(ctx->param, NULL, DYNAMIC_TYPE_OPENSSL);
  122. ctx->param = NULL;
  123. }
  124. wolfSSL_X509_STORE_CTX_init(ctx, NULL, NULL, NULL);
  125. }
  126. }
  127. void wolfSSL_X509_STORE_CTX_trusted_stack(WOLFSSL_X509_STORE_CTX *ctx, WOLF_STACK_OF(WOLFSSL_X509) *sk)
  128. {
  129. if (ctx != NULL) {
  130. ctx->chain = sk;
  131. }
  132. }
  133. /* Returns corresponding X509 error from internal ASN error <e> */
  134. int GetX509Error(int e)
  135. {
  136. switch (e) {
  137. case ASN_BEFORE_DATE_E:
  138. return X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
  139. case ASN_AFTER_DATE_E:
  140. return X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
  141. case ASN_NO_SIGNER_E: /* get issuer error if no CA found locally */
  142. return X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
  143. case ASN_SELF_SIGNED_E:
  144. return X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
  145. case ASN_PATHLEN_INV_E:
  146. case ASN_PATHLEN_SIZE_E:
  147. return X509_V_ERR_PATH_LENGTH_EXCEEDED;
  148. case ASN_SIG_OID_E:
  149. case ASN_SIG_CONFIRM_E:
  150. case ASN_SIG_HASH_E:
  151. case ASN_SIG_KEY_E:
  152. return X509_V_ERR_CERT_SIGNATURE_FAILURE;
  153. default:
  154. #ifdef HAVE_WOLFSSL_MSG_EX
  155. WOLFSSL_MSG_EX("Error not configured or implemented yet: %d", e);
  156. #else
  157. WOLFSSL_MSG("Error not configured or implemented yet");
  158. #endif
  159. return e;
  160. }
  161. }
  162. /* Verifies certificate chain using WOLFSSL_X509_STORE_CTX
  163. * returns 0 on success or < 0 on failure.
  164. */
  165. int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx)
  166. {
  167. int ret = 0;
  168. int depth = 0;
  169. int error;
  170. #ifndef NO_ASN_TIME
  171. byte *afterDate, *beforeDate;
  172. #endif
  173. WOLFSSL_ENTER("wolfSSL_X509_verify_cert");
  174. if (ctx != NULL && ctx->store != NULL && ctx->store->cm != NULL
  175. && ctx->current_cert != NULL && ctx->current_cert->derCert != NULL) {
  176. ret = wolfSSL_CertManagerVerifyBuffer(ctx->store->cm,
  177. ctx->current_cert->derCert->buffer,
  178. ctx->current_cert->derCert->length,
  179. WOLFSSL_FILETYPE_ASN1);
  180. /* If there was an error, process it and add it to CTX */
  181. if (ret < 0) {
  182. /* Get corresponding X509 error */
  183. error = GetX509Error(ret);
  184. /* Set error depth */
  185. if (ctx->chain)
  186. depth = (int)ctx->chain->num;
  187. wolfSSL_X509_STORE_CTX_set_error(ctx, error);
  188. wolfSSL_X509_STORE_CTX_set_error_depth(ctx, depth);
  189. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  190. if (ctx->store && ctx->store->verify_cb)
  191. ctx->store->verify_cb(0, ctx);
  192. #endif
  193. }
  194. #ifndef NO_ASN_TIME
  195. error = 0;
  196. /* wolfSSL_CertManagerVerifyBuffer only returns ASN_AFTER_DATE_E or
  197. ASN_BEFORE_DATE_E if there are no additional errors found in the
  198. cert. Therefore, check if the cert is expired or not yet valid
  199. in order to return the correct expected error. */
  200. afterDate = ctx->current_cert->notAfter.data;
  201. beforeDate = ctx->current_cert->notBefore.data;
  202. if (XVALIDATE_DATE(afterDate, (byte)ctx->current_cert->notAfter.type,
  203. AFTER) < 1) {
  204. error = X509_V_ERR_CERT_HAS_EXPIRED;
  205. }
  206. else if (XVALIDATE_DATE(beforeDate,
  207. (byte)ctx->current_cert->notBefore.type, BEFORE) < 1) {
  208. error = X509_V_ERR_CERT_NOT_YET_VALID;
  209. }
  210. if (error != 0 ) {
  211. wolfSSL_X509_STORE_CTX_set_error(ctx, error);
  212. wolfSSL_X509_STORE_CTX_set_error_depth(ctx, depth);
  213. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  214. if (ctx->store && ctx->store->verify_cb)
  215. ctx->store->verify_cb(0, ctx);
  216. #endif
  217. }
  218. #endif
  219. /* OpenSSL returns 0 when a chain can't be built */
  220. if (ret == ASN_NO_SIGNER_E)
  221. return WOLFSSL_FAILURE;
  222. else
  223. return ret;
  224. }
  225. return WOLFSSL_FATAL_ERROR;
  226. }
  227. #endif /* OPENSSL_EXTRA */
  228. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  229. WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get_current_cert(
  230. WOLFSSL_X509_STORE_CTX* ctx)
  231. {
  232. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_current_cert");
  233. if (ctx)
  234. return ctx->current_cert;
  235. return NULL;
  236. }
  237. int wolfSSL_X509_STORE_CTX_get_error(WOLFSSL_X509_STORE_CTX* ctx)
  238. {
  239. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error");
  240. if (ctx != NULL)
  241. return ctx->error;
  242. return 0;
  243. }
  244. int wolfSSL_X509_STORE_CTX_get_error_depth(WOLFSSL_X509_STORE_CTX* ctx)
  245. {
  246. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error_depth");
  247. if(ctx)
  248. return ctx->error_depth;
  249. return WOLFSSL_FATAL_ERROR;
  250. }
  251. /* get X509_STORE_CTX ex_data, max idx is MAX_EX_DATA */
  252. void* wolfSSL_X509_STORE_CTX_get_ex_data(WOLFSSL_X509_STORE_CTX* ctx, int idx)
  253. {
  254. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_ex_data");
  255. #ifdef HAVE_EX_DATA
  256. if (ctx != NULL) {
  257. return wolfSSL_CRYPTO_get_ex_data(&ctx->ex_data, idx);
  258. }
  259. #else
  260. (void)ctx;
  261. (void)idx;
  262. #endif
  263. return NULL;
  264. }
  265. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  266. #ifdef OPENSSL_EXTRA
  267. void wolfSSL_X509_STORE_CTX_set_verify_cb(WOLFSSL_X509_STORE_CTX *ctx,
  268. WOLFSSL_X509_STORE_CTX_verify_cb verify_cb)
  269. {
  270. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_verify_cb");
  271. if(ctx == NULL)
  272. return;
  273. ctx->verify_cb = verify_cb;
  274. }
  275. /* Gets pointer to X509_STORE that was used to create context.
  276. *
  277. * Return valid pointer on success, NULL if ctx was NULL or not initialized
  278. */
  279. WOLFSSL_X509_STORE* wolfSSL_X509_STORE_CTX_get0_store(
  280. WOLFSSL_X509_STORE_CTX* ctx)
  281. {
  282. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get0_store");
  283. if (ctx == NULL)
  284. return NULL;
  285. return ctx->store;
  286. }
  287. WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get0_cert(WOLFSSL_X509_STORE_CTX* ctx)
  288. {
  289. if (ctx == NULL)
  290. return NULL;
  291. return ctx->current_cert;
  292. }
  293. void wolfSSL_X509_STORE_CTX_set_time(WOLFSSL_X509_STORE_CTX* ctx,
  294. unsigned long flags,
  295. time_t t)
  296. {
  297. (void)flags;
  298. if (ctx == NULL || ctx->param == NULL)
  299. return;
  300. ctx->param->check_time = t;
  301. ctx->param->flags |= WOLFSSL_USE_CHECK_TIME;
  302. }
  303. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  304. #ifndef NO_WOLFSSL_STUB
  305. int wolfSSL_X509_STORE_CTX_set_purpose(WOLFSSL_X509_STORE_CTX *ctx,
  306. int purpose)
  307. {
  308. (void)ctx;
  309. (void)purpose;
  310. WOLFSSL_STUB("wolfSSL_X509_STORE_CTX_set_purpose (not implemented)");
  311. return 0;
  312. }
  313. void wolfSSL_X509_STORE_CTX_set_flags(WOLFSSL_X509_STORE_CTX *ctx,
  314. unsigned long flags)
  315. {
  316. (void)ctx;
  317. (void)flags;
  318. WOLFSSL_STUB("wolfSSL_X509_STORE_CTX_set_flags (not implemented)");
  319. }
  320. #endif /* !NO_WOLFSSL_STUB */
  321. #endif /* WOLFSSL_QT || OPENSSL_ALL */
  322. #endif /* OPENSSL_EXTRA */
  323. #ifdef OPENSSL_EXTRA
  324. /* set X509_STORE_CTX ex_data, max idx is MAX_EX_DATA. Return WOLFSSL_SUCCESS
  325. * on success, WOLFSSL_FAILURE on error. */
  326. int wolfSSL_X509_STORE_CTX_set_ex_data(WOLFSSL_X509_STORE_CTX* ctx, int idx,
  327. void *data)
  328. {
  329. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_ex_data");
  330. #ifdef HAVE_EX_DATA
  331. if (ctx != NULL)
  332. {
  333. return wolfSSL_CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  334. }
  335. #else
  336. (void)ctx;
  337. (void)idx;
  338. (void)data;
  339. #endif
  340. return WOLFSSL_FAILURE;
  341. }
  342. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  343. /* set X509_STORE_CTX ex_data, max idx is MAX_EX_DATA. Return WOLFSSL_SUCCESS
  344. * on success, WOLFSSL_FAILURE on error. */
  345. int wolfSSL_X509_STORE_CTX_set_ex_data_with_cleanup(
  346. WOLFSSL_X509_STORE_CTX* ctx,
  347. int idx,
  348. void *data,
  349. wolfSSL_ex_data_cleanup_routine_t cleanup_routine)
  350. {
  351. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_ex_data_with_cleanup");
  352. if (ctx != NULL)
  353. {
  354. return wolfSSL_CRYPTO_set_ex_data_with_cleanup(&ctx->ex_data, idx, data,
  355. cleanup_routine);
  356. }
  357. return WOLFSSL_FAILURE;
  358. }
  359. #endif /* HAVE_EX_DATA_CLEANUP_HOOKS */
  360. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL)
  361. void wolfSSL_X509_STORE_CTX_set_depth(WOLFSSL_X509_STORE_CTX* ctx, int depth)
  362. {
  363. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_depth");
  364. if (ctx)
  365. ctx->depth = depth;
  366. }
  367. #endif
  368. WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get0_current_issuer(
  369. WOLFSSL_X509_STORE_CTX* ctx)
  370. {
  371. int ret;
  372. WOLFSSL_X509* issuer;
  373. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get0_current_issuer");
  374. if (ctx == NULL) {
  375. return NULL;
  376. }
  377. ret = wolfSSL_X509_STORE_CTX_get1_issuer(&issuer, ctx, ctx->current_cert);
  378. if (ret == WOLFSSL_SUCCESS) {
  379. return issuer;
  380. }
  381. return NULL;
  382. }
  383. /* Set an error stat in the X509 STORE CTX
  384. *
  385. */
  386. void wolfSSL_X509_STORE_CTX_set_error(WOLFSSL_X509_STORE_CTX* ctx, int er)
  387. {
  388. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_error");
  389. if (ctx != NULL) {
  390. ctx->error = er;
  391. }
  392. }
  393. /* Set the error depth in the X509 STORE CTX */
  394. void wolfSSL_X509_STORE_CTX_set_error_depth(WOLFSSL_X509_STORE_CTX* ctx,
  395. int depth)
  396. {
  397. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_error_depth");
  398. if (ctx != NULL) {
  399. ctx->error_depth = depth;
  400. }
  401. }
  402. WOLFSSL_STACK* wolfSSL_X509_STORE_CTX_get_chain(WOLFSSL_X509_STORE_CTX* ctx)
  403. {
  404. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_chain");
  405. if (ctx == NULL) {
  406. return NULL;
  407. }
  408. #ifdef SESSION_CERTS
  409. /* if chain is null but sesChain is available then populate stack */
  410. if (ctx->chain == NULL && ctx->sesChain != NULL) {
  411. int i;
  412. WOLFSSL_X509_CHAIN* c = ctx->sesChain;
  413. WOLFSSL_STACK* sk = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK),
  414. NULL, DYNAMIC_TYPE_X509);
  415. if (sk == NULL) {
  416. return NULL;
  417. }
  418. XMEMSET(sk, 0, sizeof(WOLFSSL_STACK));
  419. for (i = 0; i < c->count && i < MAX_CHAIN_DEPTH; i++) {
  420. WOLFSSL_X509* x509 = wolfSSL_get_chain_X509(c, i);
  421. if (x509 == NULL) {
  422. WOLFSSL_MSG("Unable to get x509 from chain");
  423. wolfSSL_sk_X509_pop_free(sk, NULL);
  424. return NULL;
  425. }
  426. if (wolfSSL_sk_X509_push(sk, x509) != WOLFSSL_SUCCESS) {
  427. WOLFSSL_MSG("Unable to load x509 into stack");
  428. wolfSSL_sk_X509_pop_free(sk, NULL);
  429. wolfSSL_X509_free(x509);
  430. return NULL;
  431. }
  432. }
  433. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  434. /* add CA used to verify top of chain to the list */
  435. if (c->count > 0) {
  436. WOLFSSL_X509* x509 = wolfSSL_get_chain_X509(c, c->count - 1);
  437. if (x509 != NULL) {
  438. WOLFSSL_X509* issuer = NULL;
  439. if (wolfSSL_X509_STORE_CTX_get1_issuer(&issuer, ctx, x509)
  440. == WOLFSSL_SUCCESS) {
  441. /* check that the certificate being looked up is not self
  442. * signed and that a issuer was found */
  443. if (issuer != NULL && wolfSSL_X509_NAME_cmp(&x509->issuer,
  444. &x509->subject) != 0) {
  445. if (wolfSSL_sk_X509_push(sk, issuer) != WOLFSSL_SUCCESS) {
  446. WOLFSSL_MSG("Unable to load CA x509 into stack");
  447. wolfSSL_sk_X509_pop_free(sk, NULL);
  448. wolfSSL_X509_free(issuer);
  449. return NULL;
  450. }
  451. }
  452. else {
  453. WOLFSSL_MSG("Certificate is self signed");
  454. if (issuer != NULL)
  455. wolfSSL_X509_free(issuer);
  456. }
  457. }
  458. else {
  459. WOLFSSL_MSG("Could not find CA for certificate");
  460. }
  461. }
  462. }
  463. #endif
  464. ctx->chain = sk;
  465. }
  466. #endif /* SESSION_CERTS */
  467. return ctx->chain;
  468. }
  469. /* like X509_STORE_CTX_get_chain(), but return a copy with data reference
  470. counts increased */
  471. WOLFSSL_STACK* wolfSSL_X509_STORE_CTX_get1_chain(WOLFSSL_X509_STORE_CTX* ctx)
  472. {
  473. WOLFSSL_STACK* ref;
  474. if (ctx == NULL) {
  475. return NULL;
  476. }
  477. /* get chain in ctx */
  478. ref = wolfSSL_X509_STORE_CTX_get_chain(ctx);
  479. if (ref == NULL) {
  480. return ref;
  481. }
  482. /* create duplicate of ctx chain */
  483. return wolfSSL_sk_dup(ref);
  484. }
  485. #ifndef NO_WOLFSSL_STUB
  486. WOLFSSL_X509_STORE_CTX *wolfSSL_X509_STORE_CTX_get0_parent_ctx(
  487. WOLFSSL_X509_STORE_CTX *ctx)
  488. {
  489. (void)ctx;
  490. WOLFSSL_STUB("wolfSSL_X509_STORE_CTX_get0_parent_ctx");
  491. return NULL;
  492. }
  493. int wolfSSL_X509_STORE_get_by_subject(WOLFSSL_X509_STORE_CTX* ctx, int idx,
  494. WOLFSSL_X509_NAME* name, WOLFSSL_X509_OBJECT* obj)
  495. {
  496. (void)ctx;
  497. (void)idx;
  498. (void)name;
  499. (void)obj;
  500. WOLFSSL_STUB("X509_STORE_get_by_subject");
  501. return 0;
  502. }
  503. #endif
  504. #endif /* OPENSSL_EXTRA */
  505. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  506. #if defined(WOLFSSL_SIGNER_DER_CERT)
  507. WOLF_STACK_OF(WOLFSSL_X509)* wolfSSL_X509_STORE_get1_certs(
  508. WOLFSSL_X509_STORE_CTX* ctx, WOLFSSL_X509_NAME* name)
  509. {
  510. WOLF_STACK_OF(WOLFSSL_X509)* ret = NULL;
  511. int err = 0;
  512. WOLFSSL_X509_STORE* store = NULL;
  513. WOLFSSL_STACK* sk = NULL;
  514. WOLFSSL_STACK* certToFilter = NULL;
  515. WOLFSSL_X509_NAME* certToFilterName = NULL;
  516. WOLF_STACK_OF(WOLFSSL_X509)* filteredCerts = NULL;
  517. WOLFSSL_X509* filteredCert = NULL;
  518. WOLFSSL_ENTER("wolfSSL_X509_STORE_get1_certs");
  519. if (name == NULL) {
  520. err = 1;
  521. }
  522. if (err == 0) {
  523. store = wolfSSL_X509_STORE_CTX_get0_store(ctx);
  524. if (store == NULL) {
  525. err = 1;
  526. }
  527. }
  528. if (err == 0) {
  529. filteredCerts = wolfSSL_sk_X509_new_null();
  530. if (filteredCerts == NULL) {
  531. err = 1;
  532. }
  533. }
  534. if (err == 0) {
  535. sk = wolfSSL_CertManagerGetCerts(store->cm);
  536. if (sk == NULL) {
  537. err = 1;
  538. }
  539. }
  540. if (err == 0) {
  541. certToFilter = sk;
  542. while (certToFilter != NULL) {
  543. certToFilterName = wolfSSL_X509_get_subject_name(
  544. certToFilter->data.x509);
  545. if (certToFilterName != NULL) {
  546. if (wolfSSL_X509_NAME_cmp(certToFilterName, name) == 0) {
  547. filteredCert = wolfSSL_X509_dup(certToFilter->data.x509);
  548. if (filteredCert == NULL) {
  549. err = 1;
  550. break;
  551. }
  552. else {
  553. wolfSSL_sk_X509_push(filteredCerts, filteredCert);
  554. }
  555. }
  556. }
  557. certToFilter = certToFilter->next;
  558. }
  559. }
  560. if (err == 1) {
  561. if (filteredCerts != NULL) {
  562. wolfSSL_sk_X509_pop_free(filteredCerts, NULL);
  563. }
  564. ret = NULL;
  565. }
  566. else {
  567. ret = filteredCerts;
  568. }
  569. if (sk != NULL) {
  570. wolfSSL_sk_X509_pop_free(sk, NULL);
  571. }
  572. return ret;
  573. }
  574. #endif /* WOLFSSL_SIGNER_DER_CERT */
  575. #endif /* OPENSSL_EXTRA && !NO_FILESYSTEM */
  576. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  577. defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  578. int wolfSSL_X509_STORE_CTX_get1_issuer(WOLFSSL_X509 **issuer,
  579. WOLFSSL_X509_STORE_CTX *ctx, WOLFSSL_X509 *x)
  580. {
  581. WOLFSSL_STACK* node;
  582. if (issuer == NULL || ctx == NULL || x == NULL)
  583. return WOLFSSL_FATAL_ERROR;
  584. if (ctx->chain != NULL) {
  585. for (node = ctx->chain; node != NULL; node = node->next) {
  586. if (wolfSSL_X509_check_issued(node->data.x509, x) == X509_V_OK) {
  587. *issuer = x;
  588. return WOLFSSL_SUCCESS;
  589. }
  590. }
  591. }
  592. /* Result is ignored when passed to wolfSSL_OCSP_cert_to_id(). */
  593. return x509GetIssuerFromCM(issuer, ctx->store->cm, x);
  594. }
  595. #endif /* WOLFSSL_NGINX || WOLFSSL_HAPROXY || OPENSSL_EXTRA || OPENSSL_ALL */
  596. /*******************************************************************************
  597. * END OF X509_STORE_CTX APIs
  598. ******************************************************************************/
  599. /*******************************************************************************
  600. * START OF X509_STORE APIs
  601. ******************************************************************************/
  602. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  603. defined(WOLFSSL_WPAS_SMALL)
  604. WOLFSSL_X509_STORE* wolfSSL_X509_STORE_new(void)
  605. {
  606. WOLFSSL_X509_STORE* store = NULL;
  607. WOLFSSL_ENTER("SSL_X509_STORE_new");
  608. if ((store = (WOLFSSL_X509_STORE*)XMALLOC(sizeof(WOLFSSL_X509_STORE), NULL,
  609. DYNAMIC_TYPE_X509_STORE)) == NULL)
  610. goto err_exit;
  611. XMEMSET(store, 0, sizeof(WOLFSSL_X509_STORE));
  612. store->isDynamic = 1;
  613. store->refCount = 1;
  614. #ifndef SINGLE_THREADED
  615. if (wc_InitMutex(&store->refMutex) != 0)
  616. goto err_exit;
  617. #endif
  618. if ((store->cm = wolfSSL_CertManagerNew()) == NULL)
  619. goto err_exit;
  620. #ifdef HAVE_CRL
  621. store->crl = store->cm->crl;
  622. #endif
  623. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  624. /* Link store's new Certificate Manager to self by default */
  625. store->cm->x509_store_p = store;
  626. if ((store->param = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  627. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  628. NULL, DYNAMIC_TYPE_OPENSSL)) == NULL) {
  629. goto err_exit;
  630. }
  631. XMEMSET(store->param, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  632. if ((store->lookup.dirs = (WOLFSSL_BY_DIR*)XMALLOC(sizeof(WOLFSSL_BY_DIR),
  633. NULL, DYNAMIC_TYPE_OPENSSL)) == NULL) {
  634. WOLFSSL_MSG("store->lookup.dir memory allocation error");
  635. goto err_exit;
  636. }
  637. XMEMSET(store->lookup.dirs, 0, sizeof(WOLFSSL_BY_DIR));
  638. if (wc_InitMutex(&store->lookup.dirs->lock) != 0) {
  639. WOLFSSL_MSG("Bad mutex init");
  640. goto err_exit;
  641. }
  642. #endif
  643. return store;
  644. err_exit:
  645. if (store == NULL)
  646. return NULL;
  647. wolfSSL_X509_STORE_free(store);
  648. return NULL;
  649. }
  650. void wolfSSL_X509_STORE_free(WOLFSSL_X509_STORE* store)
  651. {
  652. int doFree = 0;
  653. if (store != NULL && store->isDynamic) {
  654. #ifndef SINGLE_THREADED
  655. if (wc_LockMutex(&store->refMutex) != 0) {
  656. WOLFSSL_MSG("Couldn't lock store mutex");
  657. }
  658. #endif
  659. store->refCount--;
  660. if (store->refCount == 0)
  661. doFree = 1;
  662. #ifndef SINGLE_THREADED
  663. wc_UnLockMutex(&store->refMutex);
  664. #endif
  665. if (doFree) {
  666. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  667. wolfSSL_CRYPTO_cleanup_ex_data(&store->ex_data);
  668. #endif
  669. if (store->cm != NULL) {
  670. wolfSSL_CertManagerFree(store->cm);
  671. store->cm = NULL;
  672. }
  673. #ifdef OPENSSL_ALL
  674. if (store->objs != NULL) {
  675. wolfSSL_sk_X509_OBJECT_pop_free(store->objs, NULL);
  676. }
  677. #endif
  678. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  679. if (store->param != NULL) {
  680. XFREE(store->param, NULL, DYNAMIC_TYPE_OPENSSL);
  681. store->param = NULL;
  682. }
  683. if (store->lookup.dirs != NULL) {
  684. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  685. if (store->lookup.dirs->dir_entry) {
  686. wolfSSL_sk_BY_DIR_entry_free(store->lookup.dirs->dir_entry);
  687. }
  688. #endif
  689. wc_FreeMutex(&store->lookup.dirs->lock);
  690. XFREE(store->lookup.dirs, NULL, DYNAMIC_TYPE_OPENSSL);
  691. store->lookup.dirs = NULL;
  692. }
  693. #endif
  694. XFREE(store, NULL, DYNAMIC_TYPE_X509_STORE);
  695. }
  696. }
  697. }
  698. /**
  699. * Get ex_data in WOLFSSL_STORE at given index
  700. * @param store a pointer to WOLFSSL_X509_STORE structure
  701. * @param idx Index of ex_data to get data from
  702. * @return void pointer to ex_data on success or NULL on failure
  703. */
  704. void* wolfSSL_X509_STORE_get_ex_data(WOLFSSL_X509_STORE* store, int idx)
  705. {
  706. WOLFSSL_ENTER("wolfSSL_X509_STORE_get_ex_data");
  707. #ifdef HAVE_EX_DATA
  708. if (store != NULL && idx < MAX_EX_DATA && idx >= 0) {
  709. return wolfSSL_CRYPTO_get_ex_data(&store->ex_data, idx);
  710. }
  711. #else
  712. (void)store;
  713. (void)idx;
  714. #endif
  715. return NULL;
  716. }
  717. int wolfSSL_X509_STORE_up_ref(WOLFSSL_X509_STORE* store)
  718. {
  719. if (store) {
  720. #ifndef SINGLE_THREADED
  721. if (wc_LockMutex(&store->refMutex) != 0) {
  722. WOLFSSL_MSG("Failed to lock store mutex");
  723. return WOLFSSL_FAILURE;
  724. }
  725. #endif
  726. store->refCount++;
  727. #ifndef SINGLE_THREADED
  728. wc_UnLockMutex(&store->refMutex);
  729. #endif
  730. return WOLFSSL_SUCCESS;
  731. }
  732. return WOLFSSL_FAILURE;
  733. }
  734. /**
  735. * Set ex_data for WOLFSSL_STORE
  736. * @param store a pointer to WOLFSSL_X509_STORE structure
  737. * @param idx Index of ex data to set
  738. * @param data Data to set in ex data
  739. * @return WOLFSSL_SUCCESS on success or WOLFSSL_FAILURE on failure
  740. */
  741. int wolfSSL_X509_STORE_set_ex_data(WOLFSSL_X509_STORE* store, int idx,
  742. void *data)
  743. {
  744. WOLFSSL_ENTER("wolfSSL_X509_STORE_set_ex_data");
  745. #ifdef HAVE_EX_DATA
  746. if (store != NULL && idx < MAX_EX_DATA) {
  747. return wolfSSL_CRYPTO_set_ex_data(&store->ex_data, idx, data);
  748. }
  749. #else
  750. (void)store;
  751. (void)idx;
  752. (void)data;
  753. #endif
  754. return WOLFSSL_FAILURE;
  755. }
  756. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  757. /**
  758. * Set ex_data for WOLFSSL_STORE
  759. * @param store a pointer to WOLFSSL_X509_STORE structure
  760. * @param idx Index of ex data to set
  761. * @param data Data to set in ex data
  762. * @return WOLFSSL_SUCCESS on success or WOLFSSL_FAILURE on failure
  763. */
  764. int wolfSSL_X509_STORE_set_ex_data_with_cleanup(
  765. WOLFSSL_X509_STORE* store,
  766. int idx,
  767. void *data,
  768. wolfSSL_ex_data_cleanup_routine_t cleanup_routine)
  769. {
  770. WOLFSSL_ENTER("wolfSSL_X509_STORE_set_ex_data_with_cleanup");
  771. if (store != NULL && idx < MAX_EX_DATA) {
  772. return wolfSSL_CRYPTO_set_ex_data_with_cleanup(&store->ex_data, idx,
  773. data, cleanup_routine);
  774. }
  775. return WOLFSSL_FAILURE;
  776. }
  777. #endif /* HAVE_EX_DATA_CLEANUP_HOOKS */
  778. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER || WOLFSSL_WPAS_SMALL */
  779. #ifdef OPENSSL_EXTRA
  780. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  781. void wolfSSL_X509_STORE_set_verify_cb(WOLFSSL_X509_STORE *st,
  782. WOLFSSL_X509_STORE_CTX_verify_cb verify_cb)
  783. {
  784. WOLFSSL_ENTER("WOLFSSL_X509_STORE_set_verify_cb");
  785. if (st != NULL) {
  786. st->verify_cb = verify_cb;
  787. }
  788. }
  789. #endif /* WOLFSSL_QT || OPENSSL_ALL */
  790. WOLFSSL_X509_LOOKUP* wolfSSL_X509_STORE_add_lookup(WOLFSSL_X509_STORE* store,
  791. WOLFSSL_X509_LOOKUP_METHOD* m)
  792. {
  793. WOLFSSL_ENTER("SSL_X509_STORE_add_lookup");
  794. if (store == NULL || m == NULL)
  795. return NULL;
  796. /* Make sure the lookup has a back reference to the store. */
  797. store->lookup.store = store;
  798. /* store a type to know which method wants to be used for */
  799. store->lookup.type = m->type;
  800. return &store->lookup;
  801. }
  802. int wolfSSL_X509_STORE_add_cert(WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509)
  803. {
  804. int result = WOLFSSL_FATAL_ERROR;
  805. WOLFSSL_ENTER("wolfSSL_X509_STORE_add_cert");
  806. if (store != NULL && store->cm != NULL && x509 != NULL
  807. && x509->derCert != NULL) {
  808. DerBuffer* derCert = NULL;
  809. result = AllocDer(&derCert, x509->derCert->length,
  810. x509->derCert->type, NULL);
  811. if (result == 0) {
  812. /* AddCA() frees the buffer. */
  813. XMEMCPY(derCert->buffer,
  814. x509->derCert->buffer, x509->derCert->length);
  815. result = AddCA(store->cm, &derCert, WOLFSSL_USER_CA, VERIFY);
  816. }
  817. }
  818. WOLFSSL_LEAVE("wolfSSL_X509_STORE_add_cert", result);
  819. if (result != WOLFSSL_SUCCESS) {
  820. result = WOLFSSL_FATAL_ERROR;
  821. }
  822. return result;
  823. }
  824. int wolfSSL_X509_STORE_set_flags(WOLFSSL_X509_STORE* store, unsigned long flag)
  825. {
  826. int ret = WOLFSSL_SUCCESS;
  827. WOLFSSL_ENTER("wolfSSL_X509_STORE_set_flags");
  828. if (store == NULL)
  829. return WOLFSSL_FAILURE;
  830. if ((flag & WOLFSSL_CRL_CHECKALL) || (flag & WOLFSSL_CRL_CHECK)) {
  831. ret = wolfSSL_CertManagerEnableCRL(store->cm, (int)flag);
  832. }
  833. return ret;
  834. }
  835. int wolfSSL_X509_STORE_set_default_paths(WOLFSSL_X509_STORE* store)
  836. {
  837. (void)store;
  838. return WOLFSSL_SUCCESS;
  839. }
  840. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  841. /* Loads certificate(s) files in pem format into X509_STORE struct from either
  842. * a file or directory.
  843. * Returns WOLFSSL_SUCCESS on success or WOLFSSL_FAILURE if an error occurs.
  844. */
  845. WOLFSSL_API int wolfSSL_X509_STORE_load_locations(WOLFSSL_X509_STORE *str,
  846. const char *file, const char *dir)
  847. {
  848. WOLFSSL_CTX* ctx;
  849. char *name = NULL;
  850. int ret = WOLFSSL_SUCCESS;
  851. int successes = 0;
  852. #ifdef WOLFSSL_SMALL_STACK
  853. ReadDirCtx* readCtx = NULL;
  854. #else
  855. ReadDirCtx readCtx[1];
  856. #endif
  857. WOLFSSL_ENTER("X509_STORE_load_locations");
  858. if (str == NULL || str->cm == NULL || (file == NULL && dir == NULL))
  859. return WOLFSSL_FAILURE;
  860. /* tmp ctx for setting our cert manager */
  861. ctx = wolfSSL_CTX_new(cm_pick_method());
  862. if (ctx == NULL)
  863. return WOLFSSL_FAILURE;
  864. wolfSSL_CertManagerFree(ctx->cm);
  865. ctx->cm = str->cm;
  866. #ifdef HAVE_CRL
  867. if (str->cm->crl == NULL) {
  868. if (wolfSSL_CertManagerEnableCRL(str->cm, 0) != WOLFSSL_SUCCESS) {
  869. WOLFSSL_MSG("Enable CRL failed");
  870. wolfSSL_CTX_free(ctx);
  871. return WOLFSSL_FAILURE;
  872. }
  873. }
  874. #endif
  875. /* Load individual file */
  876. if (file) {
  877. /* Try to process file with type DETECT_CERT_TYPE to parse the
  878. correct certificate header and footer type */
  879. ret = ProcessFile(ctx, file, WOLFSSL_FILETYPE_PEM, DETECT_CERT_TYPE,
  880. NULL, 0, str->cm->crl, 0);
  881. if (ret != WOLFSSL_SUCCESS) {
  882. WOLFSSL_MSG("Failed to load file");
  883. ret = WOLFSSL_FAILURE;
  884. }
  885. }
  886. /* Load files in dir */
  887. if (dir && ret == WOLFSSL_SUCCESS) {
  888. #ifdef WOLFSSL_SMALL_STACK
  889. readCtx = (ReadDirCtx*)XMALLOC(sizeof(ReadDirCtx), ctx->heap,
  890. DYNAMIC_TYPE_TMP_BUFFER);
  891. if (readCtx == NULL) {
  892. WOLFSSL_MSG("Memory error");
  893. wolfSSL_CTX_free(ctx);
  894. return WOLFSSL_FAILURE;
  895. }
  896. #endif
  897. /* try to load each regular file in dir */
  898. ret = wc_ReadDirFirst(readCtx, dir, &name);
  899. while (ret == 0 && name) {
  900. WOLFSSL_MSG(name);
  901. /* Try to process file with type DETECT_CERT_TYPE to parse the
  902. correct certificate header and footer type */
  903. ret = ProcessFile(ctx, name, WOLFSSL_FILETYPE_PEM, DETECT_CERT_TYPE,
  904. NULL, 0, str->cm->crl, 0);
  905. /* Not failing on load errors */
  906. if (ret != WOLFSSL_SUCCESS)
  907. WOLFSSL_MSG("Failed to load file in path, continuing");
  908. else
  909. successes++;
  910. ret = wc_ReadDirNext(readCtx, dir, &name);
  911. }
  912. wc_ReadDirClose(readCtx);
  913. /* Success if at least one file in dir was loaded */
  914. if (successes > 0)
  915. ret = WOLFSSL_SUCCESS;
  916. else {
  917. WOLFSSL_ERROR(ret);
  918. ret = WOLFSSL_FAILURE;
  919. }
  920. #ifdef WOLFSSL_SMALL_STACK
  921. XFREE(readCtx, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
  922. #endif
  923. }
  924. ctx->cm = NULL;
  925. wolfSSL_CTX_free(ctx);
  926. return ret;
  927. }
  928. #endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
  929. int wolfSSL_X509_CA_num(WOLFSSL_X509_STORE* store)
  930. {
  931. int i = 0;
  932. int cnt_ret = 0;
  933. Signer **table;
  934. WOLFSSL_ENTER("wolfSSL_X509_CA_num");
  935. if (store == NULL || store->cm == NULL){
  936. WOLFSSL_MSG("invalid parameter");
  937. return WOLFSSL_FAILURE;
  938. }
  939. table = store->cm->caTable;
  940. if (table){
  941. if (wc_LockMutex(&store->cm->caLock) == 0){
  942. for (i = 0; i < CA_TABLE_SIZE; i++) {
  943. Signer* signer = table[i];
  944. while (signer) {
  945. Signer* next = signer->next;
  946. cnt_ret++;
  947. signer = next;
  948. }
  949. }
  950. wc_UnLockMutex(&store->cm->caLock);
  951. }
  952. }
  953. return cnt_ret;
  954. }
  955. /******************************************************************************
  956. * wolfSSL_X509_STORE_GetCerts - retrieve stack of X509 in a certificate store ctx
  957. *
  958. * This API can be used in SSL verify callback function to view cert chain
  959. * See examples/client/client.c and myVerify() function in test.h
  960. *
  961. * RETURNS:
  962. * returns stack of X509 certs on success, otherwise returns a NULL.
  963. */
  964. WOLFSSL_STACK* wolfSSL_X509_STORE_GetCerts(WOLFSSL_X509_STORE_CTX* s)
  965. {
  966. int certIdx = 0;
  967. WOLFSSL_BUFFER_INFO* cert = NULL;
  968. DecodedCert* dCert = NULL;
  969. WOLFSSL_X509* x509 = NULL;
  970. WOLFSSL_STACK* sk = NULL;
  971. int found = 0;
  972. if (s == NULL) {
  973. return NULL;
  974. }
  975. sk = wolfSSL_sk_X509_new_null();
  976. if (sk == NULL) {
  977. return NULL;
  978. }
  979. for (certIdx = s->totalCerts - 1; certIdx >= 0; certIdx--) {
  980. /* get certificate buffer */
  981. cert = &s->certs[certIdx];
  982. dCert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, DYNAMIC_TYPE_DCERT);
  983. if (dCert == NULL) {
  984. goto error;
  985. }
  986. XMEMSET(dCert, 0, sizeof(DecodedCert));
  987. InitDecodedCert(dCert, cert->buffer, cert->length, NULL);
  988. /* Parse Certificate */
  989. if (ParseCert(dCert, CERT_TYPE, NO_VERIFY, NULL)){
  990. goto error;
  991. }
  992. x509 = wolfSSL_X509_new();
  993. if (x509 == NULL) {
  994. goto error;
  995. }
  996. InitX509(x509, 1, NULL);
  997. if (CopyDecodedToX509(x509, dCert) == 0) {
  998. if (wolfSSL_sk_X509_push(sk, x509) != WOLFSSL_SUCCESS) {
  999. WOLFSSL_MSG("Unable to load x509 into stack");
  1000. wolfSSL_X509_free(x509);
  1001. goto error;
  1002. }
  1003. }
  1004. else {
  1005. goto error;
  1006. }
  1007. found = 1;
  1008. FreeDecodedCert(dCert);
  1009. XFREE(dCert, NULL, DYNAMIC_TYPE_DCERT);
  1010. dCert = NULL;
  1011. }
  1012. if (!found) {
  1013. wolfSSL_sk_X509_pop_free(sk, NULL);
  1014. sk = NULL;
  1015. }
  1016. return sk;
  1017. error:
  1018. if (dCert) {
  1019. FreeDecodedCert(dCert);
  1020. XFREE(dCert, NULL, DYNAMIC_TYPE_DCERT);
  1021. }
  1022. if (sk)
  1023. wolfSSL_sk_X509_pop_free(sk, NULL);
  1024. return NULL;
  1025. }
  1026. #endif /* OPENSSL_EXTRA */
  1027. #ifdef OPENSSL_ALL
  1028. WOLF_STACK_OF(WOLFSSL_X509_OBJECT)* wolfSSL_X509_STORE_get0_objects(
  1029. WOLFSSL_X509_STORE* store)
  1030. {
  1031. WOLFSSL_STACK* ret = NULL;
  1032. WOLFSSL_STACK* cert_stack = NULL;
  1033. WOLFSSL_X509* x509 = NULL;
  1034. WOLFSSL_ENTER("wolfSSL_X509_STORE_get0_objects");
  1035. if (store == NULL || store->cm == NULL) {
  1036. WOLFSSL_MSG("Missing or empty store");
  1037. return NULL;
  1038. }
  1039. if (store->objs != NULL) {
  1040. #if defined(WOLFSSL_SIGNER_DER_CERT) && !defined(NO_FILESYSTEM)
  1041. /* want to update objs stack by cm stack again before returning it*/
  1042. wolfSSL_sk_X509_OBJECT_pop_free(store->objs, NULL);
  1043. store->objs = NULL;
  1044. #else
  1045. if (wolfSSL_sk_X509_OBJECT_num(store->objs) == 0) {
  1046. /* Let's try generating the stack again */
  1047. wolfSSL_sk_X509_OBJECT_pop_free(store->objs, NULL);
  1048. store->objs = NULL;
  1049. }
  1050. else
  1051. return store->objs;
  1052. #endif
  1053. }
  1054. if ((ret = wolfSSL_sk_X509_OBJECT_new()) == NULL) {
  1055. WOLFSSL_MSG("wolfSSL_sk_X509_OBJECT_new error");
  1056. goto err_cleanup;
  1057. }
  1058. #if defined(WOLFSSL_SIGNER_DER_CERT) && !defined(NO_FILESYSTEM)
  1059. cert_stack = wolfSSL_CertManagerGetCerts(store->cm);
  1060. /* wolfSSL_sk_X509_pop checks for NULL */
  1061. while ((x509 = wolfSSL_sk_X509_pop(cert_stack)) != NULL) {
  1062. WOLFSSL_X509_OBJECT* obj = wolfSSL_X509_OBJECT_new();
  1063. if (obj == NULL) {
  1064. WOLFSSL_MSG("wolfSSL_X509_OBJECT_new error");
  1065. goto err_cleanup;
  1066. }
  1067. if (wolfSSL_sk_X509_OBJECT_push(ret, obj) != WOLFSSL_SUCCESS) {
  1068. WOLFSSL_MSG("wolfSSL_sk_X509_OBJECT_push error");
  1069. wolfSSL_X509_OBJECT_free(obj);
  1070. goto err_cleanup;
  1071. }
  1072. obj->type = WOLFSSL_X509_LU_X509;
  1073. obj->data.x509 = x509;
  1074. }
  1075. #endif
  1076. #ifdef HAVE_CRL
  1077. if (store->cm->crl != NULL) {
  1078. WOLFSSL_X509_OBJECT* obj = wolfSSL_X509_OBJECT_new();
  1079. if (obj == NULL) {
  1080. WOLFSSL_MSG("wolfSSL_X509_OBJECT_new error");
  1081. goto err_cleanup;
  1082. }
  1083. if (wolfSSL_sk_X509_OBJECT_push(ret, obj) != WOLFSSL_SUCCESS) {
  1084. WOLFSSL_MSG("wolfSSL_sk_X509_OBJECT_push error");
  1085. wolfSSL_X509_OBJECT_free(obj);
  1086. goto err_cleanup;
  1087. }
  1088. obj->type = WOLFSSL_X509_LU_CRL;
  1089. obj->data.crl = store->cm->crl;
  1090. }
  1091. #endif
  1092. if (cert_stack)
  1093. wolfSSL_sk_X509_pop_free(cert_stack, NULL);
  1094. store->objs = ret;
  1095. return ret;
  1096. err_cleanup:
  1097. if (ret)
  1098. wolfSSL_sk_X509_OBJECT_free(ret);
  1099. if (cert_stack)
  1100. wolfSSL_sk_X509_pop_free(cert_stack, NULL);
  1101. if (x509)
  1102. wolfSSL_X509_free(x509);
  1103. return NULL;
  1104. }
  1105. #endif /* OPENSSL_ALL */
  1106. /*******************************************************************************
  1107. * END OF X509_STORE APIs
  1108. ******************************************************************************/
  1109. #endif /* NO_CERTS */
  1110. #endif /* !WOLFCRYPT_ONLY */
  1111. #endif /* !WOLFSSL_X509_STORE_INCLUDED */