@@ -122,8 +122,11 @@ static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x);
122
122
static int internal_verify (X509_STORE_CTX * ctx );
123
123
124
124
static int null_callback (int ok , X509_STORE_CTX * e ) { return ok ; }
125
- static int null_verify_crit_oids_callback (X509_STORE_CTX * ctx , X509 * x509 ,
126
- STACK_OF (ASN1_OBJECT ) * oids ) {
125
+ static int null_verify_custom_crit_oids_callback (X509_STORE_CTX * ctx ,
126
+ X509 * x509 ,
127
+ STACK_OF (ASN1_OBJECT ) * oids ) {
128
+ // This returns 0 by default, so that the callback must be configured by the
129
+ // user when enabling the custom critical extensions feature.
127
130
return 0 ;
128
131
}
129
132
@@ -565,7 +568,7 @@ static int get_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *x) {
565
568
return X509_STORE_CTX_get1_issuer (issuer , ctx , x );
566
569
}
567
570
568
- static int check_custom_known_critical_extension (X509_STORE_CTX * ctx , X509 * x ) {
571
+ static int check_custom_critical_extensions (X509_STORE_CTX * ctx , X509 * x ) {
569
572
if (ctx -> custom_crit_oids == NULL ) {
570
573
// Fail if custom critical extensions are enabled, but none were set.
571
574
return 0 ;
@@ -575,6 +578,12 @@ static int check_custom_known_critical_extension(X509_STORE_CTX *ctx, X509 *x) {
575
578
return 0 ;
576
579
}
577
580
581
+ // Allocate |found_exts| to pass to the callback.
582
+ STACK_OF (ASN1_OBJECT ) * found_exts = sk_ASN1_OBJECT_new_null ();
583
+ if (found_exts == NULL ) {
584
+ return 0 ;
585
+ }
586
+
578
587
// Iterate through all critical extensions of |x| and validate against the
579
588
// ones that aren't recognized by |X509_supported_extension|.
580
589
int last_pos = X509_get_ext_by_critical (x , 1 , -1 );
@@ -585,10 +594,12 @@ static int check_custom_known_critical_extension(X509_STORE_CTX *ctx, X509 *x) {
585
594
586
595
// Iterate through all set |custom_crit_oids|.
587
596
for (size_t i = 0 ; i < known_oid_count ; i ++ ) {
588
- ASN1_OBJECT * known_ext =
589
- sk_ASN1_OBJECT_value (ctx -> custom_crit_oids , i );
597
+ ASN1_OBJECT * known_ext = sk_ASN1_OBJECT_value (ctx -> custom_crit_oids , i );
590
598
if (OBJ_cmp (ext -> object , known_ext ) == 0 ) {
591
599
found = 1 ;
600
+ if (!sk_ASN1_OBJECT_push (found_exts , known_ext )) {
601
+ return 0 ;
602
+ }
592
603
break ;
593
604
}
594
605
}
@@ -600,7 +611,17 @@ static int check_custom_known_critical_extension(X509_STORE_CTX *ctx, X509 *x) {
600
611
}
601
612
last_pos = X509_get_ext_by_critical (x , 1 , last_pos );
602
613
}
603
- // If we get here, all unknown critical extensions were found
614
+
615
+ // If we get here, all unknown critical extensions in |x| were
616
+ // properly handled and we pass the ones that were found to the caller.
617
+ if (!ctx -> verify_custom_crit_oids (ctx , x , found_exts )) {
618
+ return 0 ;
619
+ }
620
+
621
+ // Remove the |EXFLAG_CRITICAL| flag from |x|, now that all unknown
622
+ // critical extensions have been handled.
623
+ x -> ex_flags &= ~EXFLAG_CRITICAL ;
624
+
604
625
return 1 ;
605
626
}
606
627
@@ -614,11 +635,14 @@ static int check_chain_extensions(X509_STORE_CTX *ctx) {
614
635
// Check all untrusted certificates
615
636
for (int i = 0 ; i < ctx -> last_untrusted ; i ++ ) {
616
637
X509 * x = sk_X509_value (ctx -> chain , i );
617
- if ((!(ctx -> param -> flags & X509_V_FLAG_IGNORE_CRITICAL ) &&
638
+ if ( // OpenSSL's historic check for unknown critical extensions.
639
+ // |EXFLAG_CRITICAL| indicates an unsupported critical extension was
640
+ // found in |x| during the initial parsing of the certificate.
641
+ (!(ctx -> param -> flags & X509_V_FLAG_IGNORE_CRITICAL ) &&
618
642
(x -> ex_flags & EXFLAG_CRITICAL )) &&
619
- // Do check for enabling custom unknown critical extensions.
620
- (! check_custom_known_critical_extension ( ctx , x ) ||
621
- ! ctx -> verify_crit_oids (ctx , x , ctx -> custom_crit_oids ) )) {
643
+ // AWS-LC specific logic for enabling custom unknown critical
644
+ // extensions.
645
+ ! check_custom_critical_extensions (ctx , x )) {
622
646
ctx -> error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION ;
623
647
ctx -> error_depth = i ;
624
648
ctx -> current_cert = x ;
@@ -1728,7 +1752,7 @@ int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
1728
1752
ctx -> check_crl = check_crl ;
1729
1753
}
1730
1754
1731
- ctx -> verify_crit_oids = null_verify_crit_oids_callback ;
1755
+ ctx -> verify_custom_crit_oids = null_verify_custom_crit_oids_callback ;
1732
1756
1733
1757
return 1 ;
1734
1758
@@ -1830,6 +1854,7 @@ int X509_STORE_CTX_add_custom_crit_oid(X509_STORE_CTX *ctx, ASN1_OBJECT *oid) {
1830
1854
}
1831
1855
1832
1856
void X509_STORE_CTX_set_verify_crit_oids (
1833
- X509_STORE_CTX * ctx , X509_STORE_CTX_verify_crit_oids verify_crit_oids ) {
1834
- ctx -> verify_crit_oids = verify_crit_oids ;
1857
+ X509_STORE_CTX * ctx ,
1858
+ X509_STORE_CTX_verify_crit_oids_cb verify_custom_crit_oids ) {
1859
+ ctx -> verify_custom_crit_oids = verify_custom_crit_oids ;
1835
1860
}
0 commit comments