mark all block comments that need format preserving so that
[openssl.git] / crypto / x509v3 / v3_ncons.c
index 689df46..06520fe 100644 (file)
@@ -78,6 +78,7 @@ static int nc_dn(X509_NAME *sub, X509_NAME *nm);
 static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
 static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
+static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base);
 
 const X509V3_EXT_METHOD v3_name_constraints = {
        NID_name_constraints, 0,
@@ -189,7 +190,6 @@ static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
                        print_nc_ipadd(bp, tree->base->d.ip);
                else
                        GENERAL_NAME_print(bp, tree->base);
-               tree = sk_GENERAL_SUBTREE_value(trees, i);
                BIO_puts(bp, "\n");
                }
        return 1;
@@ -225,7 +225,8 @@ static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
        return 1;
        }
 
-/* Check a certificate conforms to a specified set of constraints.
+/*-
+ * Check a certificate conforms to a specified set of constraints.
  * Return values:
  *  X509_V_OK: All constraints obeyed.
  *  X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
@@ -234,7 +235,6 @@ static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
  *  X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE:  Unsupported constraint type.
  *  X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
  *  X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
-
  */
 
 int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
@@ -363,6 +363,9 @@ static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
                return nc_uri(gen->d.uniformResourceIdentifier,
                                        base->d.uniformResourceIdentifier);
 
+               case GEN_IPADD:
+               return nc_ip(gen->d.iPAddress, base->d.iPAddress);
+
                default:
                return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
                }
@@ -504,3 +507,34 @@ static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
        return X509_V_OK;
 
        }
+
+static int nc_ip(ASN1_OCTET_STRING *ip, ASN1_OCTET_STRING *base)
+       {
+       int hostlen, baselen, i;
+       unsigned char *hostptr, *baseptr, *maskptr;
+       hostptr = ip->data;
+       hostlen = ip->length;
+       baseptr = base->data;
+       baselen = base->length;
+
+       /* Invalid if not IPv4 or IPv6 */
+       if (! ((hostlen == 4) || (hostlen==16)) )
+               return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
+       if (! ((baselen == 8) || (baselen==32)) )
+               return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
+
+       /* Do not match IPv4 with IPv6 */
+       if (hostlen*2 != baselen)
+               return X509_V_ERR_PERMITTED_VIOLATION;
+
+       maskptr = base->data + hostlen;
+
+       /* Considering possible not aligned base ipAddress */
+       /* Not checking for wrong mask definition: i.e.: 255.0.255.0*/
+       for (i = 0; i < hostlen; i++)
+               if ((hostptr[i] & maskptr[i]) != (baseptr[i] & maskptr[i]))
+                       return X509_V_ERR_PERMITTED_VIOLATION;
+
+       return X509_V_OK;
+
+       }