3434#ifdef OPENSSL_EXTRA
3535static int X509StoreGetIssuerEx (WOLFSSL_X509 * * issuer ,
3636 WOLFSSL_STACK * certs , WOLFSSL_X509 * x );
37- static int X509StorePopCert (WOLFSSL_STACK * certs_stack ,
38- WOLFSSL_STACK * dest_stack ,
39- WOLFSSL_X509 * cert );
4037static int X509StoreAddCa (WOLFSSL_X509_STORE * store ,
4138 WOLFSSL_X509 * x509 , int type );
42- static int X509StoreRemoveCa (WOLFSSL_X509_STORE * store ,
43- WOLFSSL_X509 * x509 , int type );
4439#endif
4540
4641/* Based on OpenSSL default max depth */
@@ -409,6 +404,64 @@ static int addAllButSelfSigned(WOLF_STACK_OF(WOLFSSL_X509)*to,
409404 return ret ;
410405}
411406
407+ static int X509StoreRemoveCa (WOLFSSL_X509_STORE * store ,
408+ WOLFSSL_X509 * x509 , int type ) {
409+ int result = WC_NO_ERR_TRACE (WOLFSSL_FATAL_ERROR );
410+ byte hash [KEYID_SIZE ];
411+
412+ if (store != NULL && x509 != NULL && x509 -> derCert != NULL ) {
413+ result = GetHashId (x509 -> subjKeyId , (int )x509 -> subjKeyIdSz ,
414+ hash , HashIdAlg (x509 -> sigOID ));
415+ if (result ) {
416+ result = WOLFSSL_FATAL_ERROR ;
417+ } else {
418+ result = RemoveCA (store -> cm , hash , type );
419+ }
420+ }
421+
422+ return result ;
423+ }
424+
425+ static int X509StoreMoveCert (WOLFSSL_STACK * certs_stack ,
426+ WOLFSSL_STACK * dest_stack ,
427+ WOLFSSL_X509 * cert ) {
428+ int i ;
429+
430+ if (certs_stack == NULL || dest_stack == NULL || cert == NULL )
431+ return WOLFSSL_FATAL_ERROR ;
432+
433+ for (i = 0 ; i < wolfSSL_sk_X509_num (certs_stack ); i ++ ) {
434+ if (wolfSSL_sk_X509_value (certs_stack , i ) == cert ) {
435+ wolfSSL_sk_X509_push (dest_stack ,
436+ (WOLFSSL_X509 * )wolfSSL_sk_pop_node (certs_stack , i ));
437+ return WOLFSSL_SUCCESS ;
438+ }
439+ }
440+
441+ return WOLFSSL_FAILURE ;
442+ }
443+
444+
445+ /* Current certificate failed, but it is possible there is an
446+ * alternative cert with the same subject key which will work.
447+ * Retry until all possible candidate certs are exhausted. */
448+ static int X509VerifyCertSetupRetry (WOLFSSL_X509_STORE_CTX * ctx ,
449+ WOLF_STACK_OF (WOLFSSL_X509 )* certs , WOLF_STACK_OF (WOLFSSL_X509 )* failed ,
450+ int * depth , int origDepth ) {
451+ int ret = WC_NO_ERR_TRACE (WOLFSSL_FAILURE );
452+
453+ WOLFSSL_MSG ("X509_verify_cert current cert failed, "
454+ "retrying with other certs." );
455+ ret = X509StoreRemoveCa (ctx -> store , ctx -> current_cert ,
456+ WOLFSSL_TEMP_CA );
457+ X509StoreMoveCert (certs , failed , ctx -> current_cert );
458+ ctx -> current_cert = wolfSSL_sk_X509_pop (ctx -> chain );
459+ if (* depth < origDepth )
460+ * depth += 1 ;
461+
462+ return ret ;
463+ }
464+
412465/* Verifies certificate chain using WOLFSSL_X509_STORE_CTX
413466 * returns 1 on success or <= 0 on failure.
414467 */
@@ -499,25 +552,28 @@ int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx)
499552 goto exit ;
500553 }
501554 }
502- } else {
555+ } else
503556 #endif
504- ret = X509StoreAddCa (ctx -> store , issuer ,
505- WOLFSSL_TEMP_CA );
506- if (ret != WOLFSSL_SUCCESS ) {
507- goto retry ;
508- }
509- added = 1 ;
510- ret = X509StoreVerifyCert (ctx );
511- if (ret != WOLFSSL_SUCCESS ) {
512- if ((origDepth - depth ) <= 1 )
513- added = 0 ;
514- goto retry ;
515- }
516- /* Add it to the current chain and look at the issuer cert next */
517- wolfSSL_sk_X509_push (ctx -> chain , ctx -> current_cert );
518- #if defined(OPENSSL_ALL ) || defined(WOLFSSL_QT )
557+ {
558+ ret = X509StoreAddCa (ctx -> store , issuer ,
559+ WOLFSSL_TEMP_CA );
560+ if (ret != WOLFSSL_SUCCESS ) {
561+ X509VerifyCertSetupRetry (ctx , certs , failedCerts ,
562+ & depth , origDepth );
563+ continue ;
564+ }
565+ added = 1 ;
566+ ret = X509StoreVerifyCert (ctx );
567+ if (ret != WOLFSSL_SUCCESS ) {
568+ if ((origDepth - depth ) <= 1 )
569+ added = 0 ;
570+ X509VerifyCertSetupRetry (ctx , certs , failedCerts ,
571+ & depth , origDepth );
572+ continue ;
573+ }
574+ /* Add it to the current chain and look at the issuer cert next */
575+ wolfSSL_sk_X509_push (ctx -> chain , ctx -> current_cert );
519576 }
520- #endif
521577 ctx -> current_cert = issuer ;
522578 }
523579 else if (ret == WC_NO_ERR_TRACE (WOLFSSL_FAILURE )) {
@@ -531,7 +587,9 @@ int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx)
531587 wolfSSL_sk_X509_push (ctx -> chain , ctx -> current_cert );
532588 ret = WOLFSSL_SUCCESS ;
533589 } else {
534- goto retry ;
590+ X509VerifyCertSetupRetry (ctx , certs , failedCerts ,
591+ & depth , origDepth );
592+ continue ;
535593 }
536594 }
537595
@@ -564,33 +622,18 @@ int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx)
564622 }
565623
566624 depth -- ;
567- continue ;
568-
569- retry :
570- /* Current certificate failed, but it is possible there is an
571- * alternative cert with the same subject key which will work.
572- * Retry until all possible candidate certs are exhausted. */
573- WOLFSSL_MSG ("X509_verify_cert current cert failed,"
574- "retrying with other certs." );
575- ret = X509StoreRemoveCa (ctx -> store , ctx -> current_cert ,
576- WOLFSSL_TEMP_CA );
577- X509StorePopCert (certs , failedCerts , ctx -> current_cert );
578- ctx -> current_cert = wolfSSL_sk_X509_pop (ctx -> chain );
579- if (depth < origDepth )
580- depth ++ ;
581625 }
582626
583627exit :
584628 /* Copy back failed certs if verification failed. */
585629 if (ret != WOLFSSL_SUCCESS ) {
586- while (wolfSSL_sk_X509_num (failedCerts ) > 0 )
630+ for (int cnt = 0 , total = wolfSSL_sk_X509_num (failedCerts );
631+ cnt < total ; cnt ++ )
587632 {
588633 wolfSSL_sk_X509_push (certs , wolfSSL_sk_X509_pop (failedCerts ));
589634 }
590635 }
591- if (failedCerts ) {
592- wolfSSL_sk_X509_pop_free (failedCerts , NULL );
593- }
636+ wolfSSL_sk_X509_pop_free (failedCerts , NULL );
594637
595638 /* Remove additional intermediates from init from the store */
596639 if (ctx != NULL && numInterAdd > 0 ) {
@@ -1098,25 +1141,6 @@ static int X509StoreGetIssuerEx(WOLFSSL_X509 **issuer,
10981141 return WOLFSSL_FAILURE ;
10991142}
11001143
1101- static int X509StorePopCert (WOLFSSL_STACK * certs_stack ,
1102- WOLFSSL_STACK * dest_stack ,
1103- WOLFSSL_X509 * cert ) {
1104- int i ;
1105-
1106- if (certs_stack == NULL || dest_stack == NULL || cert == NULL )
1107- return WOLFSSL_FATAL_ERROR ;
1108-
1109- for (i = 0 ; i < wolfSSL_sk_X509_num (certs_stack ); i ++ ) {
1110- if (wolfSSL_sk_X509_value (certs_stack , i ) == cert ) {
1111- wolfSSL_sk_X509_push (dest_stack ,
1112- (WOLFSSL_X509 * )wolfSSL_sk_pop_node (certs_stack , i ));
1113- return WOLFSSL_SUCCESS ;
1114- }
1115- }
1116-
1117- return WOLFSSL_FAILURE ;
1118- }
1119-
11201144#endif
11211145
11221146/******************************************************************************
@@ -1450,40 +1474,6 @@ static int X509StoreAddCa(WOLFSSL_X509_STORE* store,
14501474 return result ;
14511475}
14521476
1453- static int X509StoreRemoveCa (WOLFSSL_X509_STORE * store ,
1454- WOLFSSL_X509 * x509 , int type ) {
1455- int result = WC_NO_ERR_TRACE (WOLFSSL_FATAL_ERROR );
1456- DecodedCert * dCert = NULL ;
1457-
1458- if (store != NULL && x509 != NULL && x509 -> derCert != NULL ) {
1459- dCert = (DecodedCert * )XMALLOC (sizeof (DecodedCert ), NULL ,
1460- DYNAMIC_TYPE_DCERT );
1461-
1462- if (dCert == NULL ) {
1463- return result ;
1464- }
1465- XMEMSET (dCert , 0 , sizeof (DecodedCert ));
1466- wc_InitDecodedCert (dCert , x509 -> derCert -> buffer ,
1467- x509 -> derCert -> length , NULL );
1468- result = wc_ParseCert (dCert , CA_TYPE , NO_VERIFY , store -> cm );
1469- if (result ) {
1470- wc_FreeDecodedCert (dCert );
1471- XFREE (dCert , NULL , DYNAMIC_TYPE_DCERT );
1472- return WOLFSSL_FATAL_ERROR ;
1473- }
1474-
1475- result = RemoveCA (store -> cm , dCert -> extSubjKeyId , type );
1476- }
1477-
1478- if (dCert ) {
1479- wc_FreeDecodedCert (dCert );
1480- XFREE (dCert , NULL , DYNAMIC_TYPE_DCERT );
1481- }
1482-
1483- return result ;
1484- }
1485-
1486-
14871477int wolfSSL_X509_STORE_add_cert (WOLFSSL_X509_STORE * store , WOLFSSL_X509 * x509 )
14881478{
14891479 int result = WC_NO_ERR_TRACE (WOLFSSL_FATAL_ERROR );
0 commit comments