Revert incompatible OCSP_basic_verify changes.
[openssl.git] / crypto / x509 / x509_vfy.c
index ba10811f80bf46b842fc4710a8f9be0e85f696ac..1b3c62ef7de4deb44bf16f482f6aff1a227f368c 100644 (file)
@@ -113,6 +113,7 @@ static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
 static int check_chain_extensions(X509_STORE_CTX *ctx);
 static int check_name_constraints(X509_STORE_CTX *ctx);
+static int check_id(X509_STORE_CTX *ctx);
 static int check_trust(X509_STORE_CTX *ctx);
 static int check_revocation(X509_STORE_CTX *ctx);
 static int check_cert(X509_STORE_CTX *ctx);
@@ -150,6 +151,32 @@ static int x509_subject_cmp(X509 **a, X509 **b)
        }
 #endif
 
+/* Given a certificate try and find an exact match in the store */
+
+static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
+       {
+       STACK_OF(X509) *certs;
+       X509 *xtmp;
+       int i;
+       /* Lookup all certs with matching subject name */
+       certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
+       if (certs == NULL)
+               return NULL;
+       /* Look for exact match */
+       for (i = 0; i < sk_X509_num(certs); i++)
+               {
+               xtmp = sk_X509_value(certs, i);
+               if (!X509_cmp(xtmp, x))
+                       break;
+               }
+       if (i < sk_X509_num(certs))
+               CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509);
+       else
+               xtmp = NULL;
+       sk_X509_pop_free(certs, X509_free);
+       return xtmp;
+       }
+
 int X509_verify_cert(X509_STORE_CTX *ctx)
        {
        X509 *x,*xtmp,*chain_ss=NULL;
@@ -323,8 +350,13 @@ int X509_verify_cert(X509_STORE_CTX *ctx)
 
        /* we now have our chain, lets check it... */
 
-       /* Is last certificate looked up self signed? */
-       if (!ctx->check_issued(ctx,x,x))
+       i = check_trust(ctx);
+
+       /* If explicitly rejected error */
+       if (i == X509_TRUST_REJECTED)
+               goto end;
+       /* If not explicitly trusted then indicate error */
+       if (i != X509_TRUST_TRUSTED)
                {
                if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss))
                        {
@@ -362,9 +394,7 @@ int X509_verify_cert(X509_STORE_CTX *ctx)
        
        if (!ok) goto end;
 
-       /* The chain extensions are OK: check trust */
-
-       if (param->trust > 0) ok = check_trust(ctx);
+       ok = check_id(ctx);
 
        if (!ok) goto end;
 
@@ -656,30 +686,89 @@ static int check_name_constraints(X509_STORE_CTX *ctx)
        return 1;
        }
 
+static int check_id_error(X509_STORE_CTX *ctx, int errcode)
+       {
+       ctx->error = errcode;
+       ctx->current_cert = ctx->cert;
+       ctx->error_depth = 0;
+       return ctx->verify_cb(0, ctx);
+       }
+
+static int check_id(X509_STORE_CTX *ctx)
+       {
+       X509_VERIFY_PARAM *vpm = ctx->param;
+       X509 *x = ctx->cert;
+       if (vpm->host && !X509_check_host(x, vpm->host, vpm->hostlen, 0))
+               {
+               if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
+                       return 0;
+               }
+       if (vpm->email && !X509_check_email(x, vpm->email, vpm->emaillen, 0))
+               {
+               if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
+                       return 0;
+               }
+       if (vpm->ip && !X509_check_ip(x, vpm->ip, vpm->iplen, 0))
+               {
+               if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
+                       return 0;
+               }
+       return 1;
+       }
+
 static int check_trust(X509_STORE_CTX *ctx)
 {
-#ifdef OPENSSL_NO_CHAIN_VERIFY
-       return 1;
-#else
        int i, ok;
-       X509 *x;
+       X509 *x = NULL;
        int (*cb)(int xok,X509_STORE_CTX *xctx);
        cb=ctx->verify_cb;
-/* For now just check the last certificate in the chain */
-       i = sk_X509_num(ctx->chain) - 1;
-       x = sk_X509_value(ctx->chain, i);
-       ok = X509_check_trust(x, ctx->param->trust, 0);
-       if (ok == X509_TRUST_TRUSTED)
-               return 1;
-       ctx->error_depth = i;
-       ctx->current_cert = x;
-       if (ok == X509_TRUST_REJECTED)
-               ctx->error = X509_V_ERR_CERT_REJECTED;
-       else
-               ctx->error = X509_V_ERR_CERT_UNTRUSTED;
-       ok = cb(0, ctx);
-       return ok;
-#endif
+       /* Check all trusted certificates in chain */
+       for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++)
+               {
+               x = sk_X509_value(ctx->chain, i);
+               ok = X509_check_trust(x, ctx->param->trust, 0);
+               /* If explicitly trusted return trusted */
+               if (ok == X509_TRUST_TRUSTED)
+                       return X509_TRUST_TRUSTED;
+               /* If explicitly rejected notify callback and reject if
+                * not overridden.
+                */
+               if (ok == X509_TRUST_REJECTED)
+                       {
+                       ctx->error_depth = i;
+                       ctx->current_cert = x;
+                       ctx->error = X509_V_ERR_CERT_REJECTED;
+                       ok = cb(0, ctx);
+                       if (!ok)
+                               return X509_TRUST_REJECTED;
+                       }
+               }
+       /* If we accept partial chains and have at least one trusted
+        * certificate return success.
+        */
+       if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN)
+               {
+               if (ctx->last_untrusted < sk_X509_num(ctx->chain))
+                       return X509_TRUST_TRUSTED;
+               if (sk_X509_num(ctx->chain) == 1)
+                       {
+                       X509 *mx;
+                       x = sk_X509_value(ctx->chain, 0);
+                       mx = lookup_cert_match(ctx, x);
+                       if (mx)
+                               {
+                               (void)sk_X509_set(ctx->chain, 0, mx);
+                               X509_free(x);
+                               ctx->last_untrusted = 0;
+                               return X509_TRUST_TRUSTED;
+                               }
+                       }
+               }
+
+       /* If no trusted certs in chain at all return untrusted and
+        * allow standard (no issuer cert) etc errors to be indicated.
+        */
+       return X509_TRUST_UNTRUSTED;
 }
 
 static int check_revocation(X509_STORE_CTX *ctx)
@@ -888,7 +977,7 @@ static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
        {
        ASN1_OCTET_STRING *exta, *extb;
        int i;
-       i = X509_CRL_get_ext_by_NID(a, nid, 0);
+       i = X509_CRL_get_ext_by_NID(a, nid, -1);
        if (i >= 0)
                {
                /* Can't have multiple occurrences */
@@ -899,7 +988,7 @@ static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
        else
                exta = NULL;
 
-       i = X509_CRL_get_ext_by_NID(b, nid, 0);
+       i = X509_CRL_get_ext_by_NID(b, nid, -1);
 
        if (i >= 0)
                {
@@ -1607,6 +1696,8 @@ static int internal_verify(X509_STORE_CTX *ctx)
                xs=xi;
        else
                {
+               if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN && n == 0)
+                       return check_cert_time(ctx, xi);
                if (n <= 0)
                        {
                        ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;