Skip to content

Commit c927a34

Browse files
Viktor Dukhovnit8m
Viktor Dukhovni
authored andcommitted
Fix type confusion in nc_match_single()
This function assumes that if the "gen" is an OtherName, then the "base" is a rfc822Name constraint. This assumption is not true in all cases. If the end-entity certificate contains an OtherName SAN of any type besides SmtpUtf8Mailbox and the CA certificate contains a name constraint of OtherName (of any type), then "nc_email_eai" will be invoked, with the OTHERNAME "base" being incorrectly interpreted as a ASN1_IA5STRING. Reported by Corey Bonnell from Digicert. CVE-2022-4203 Reviewed-by: Paul Dale <pauli@openssl.org> Reviewed-by: Hugo Landau <hlandau@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org>
1 parent 36d85b0 commit c927a34

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

crypto/x509/v3_ncons.c

+31-14
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
3131
static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
3232

3333
static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
34-
static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
34+
static int nc_match_single(int effective_type, GENERAL_NAME *sub,
35+
GENERAL_NAME *gen);
3536
static int nc_dn(const X509_NAME *sub, const X509_NAME *nm);
3637
static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
3738
static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
@@ -472,14 +473,17 @@ static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
472473
{
473474
GENERAL_SUBTREE *sub;
474475
int i, r, match = 0;
476+
int effective_type = gen->type;
477+
475478
/*
476479
* We need to compare not gen->type field but an "effective" type because
477480
* the otherName field may contain EAI email address treated specially
478481
* according to RFC 8398, section 6
479482
*/
480-
int effective_type = ((gen->type == GEN_OTHERNAME) &&
481-
(OBJ_obj2nid(gen->d.otherName->type_id) ==
482-
NID_id_on_SmtpUTF8Mailbox)) ? GEN_EMAIL : gen->type;
483+
if (effective_type == GEN_OTHERNAME &&
484+
(OBJ_obj2nid(gen->d.otherName->type_id) == NID_id_on_SmtpUTF8Mailbox)) {
485+
effective_type = GEN_EMAIL;
486+
}
483487

484488
/*
485489
* Permitted subtrees: if any subtrees exist of matching the type at
@@ -488,7 +492,10 @@ static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
488492

489493
for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
490494
sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
491-
if (effective_type != sub->base->type)
495+
if (effective_type != sub->base->type
496+
|| (effective_type == GEN_OTHERNAME &&
497+
OBJ_cmp(gen->d.otherName->type_id,
498+
sub->base->d.otherName->type_id) != 0))
492499
continue;
493500
if (!nc_minmax_valid(sub))
494501
return X509_V_ERR_SUBTREE_MINMAX;
@@ -497,7 +504,7 @@ static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
497504
continue;
498505
if (match == 0)
499506
match = 1;
500-
r = nc_match_single(gen, sub->base);
507+
r = nc_match_single(effective_type, gen, sub->base);
501508
if (r == X509_V_OK)
502509
match = 2;
503510
else if (r != X509_V_ERR_PERMITTED_VIOLATION)
@@ -511,12 +518,15 @@ static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
511518

512519
for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
513520
sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
514-
if (effective_type != sub->base->type)
521+
if (effective_type != sub->base->type
522+
|| (effective_type == GEN_OTHERNAME &&
523+
OBJ_cmp(gen->d.otherName->type_id,
524+
sub->base->d.otherName->type_id) != 0))
515525
continue;
516526
if (!nc_minmax_valid(sub))
517527
return X509_V_ERR_SUBTREE_MINMAX;
518528

519-
r = nc_match_single(gen, sub->base);
529+
r = nc_match_single(effective_type, gen, sub->base);
520530
if (r == X509_V_OK)
521531
return X509_V_ERR_EXCLUDED_VIOLATION;
522532
else if (r != X509_V_ERR_PERMITTED_VIOLATION)
@@ -528,15 +538,22 @@ static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
528538

529539
}
530540

531-
static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
541+
static int nc_match_single(int effective_type, GENERAL_NAME *gen,
542+
GENERAL_NAME *base)
532543
{
533544
switch (gen->type) {
534545
case GEN_OTHERNAME:
535-
/*
536-
* We are here only when we have SmtpUTF8 name,
537-
* so we match the value of othername with base->d.rfc822Name
538-
*/
539-
return nc_email_eai(gen->d.otherName->value, base->d.rfc822Name);
546+
switch (effective_type) {
547+
case GEN_EMAIL:
548+
/*
549+
* We are here only when we have SmtpUTF8 name,
550+
* so we match the value of othername with base->d.rfc822Name
551+
*/
552+
return nc_email_eai(gen->d.otherName->value, base->d.rfc822Name);
553+
554+
default:
555+
return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
556+
}
540557

541558
case GEN_DIRNAME:
542559
return nc_dn(gen->d.directoryName, base->d.directoryName);

0 commit comments

Comments
 (0)