Support for multiple CRLs with same issuer name in X509_STORE. Modify
[openssl.git] / crypto / x509 / x509_vfy.c
index 5e2cc82c515b20d2cd005d01c887efd545766e37..e2109a4c3506d64ed0effb3e98ebff9945afe495 100644 (file)
@@ -102,7 +102,7 @@ int X509_verify_cert(X509_STORE_CTX *ctx)
        X509_VERIFY_PARAM *param = ctx->param;
        int depth,i,ok=0;
        int num;
-       int (*cb)();
+       int (*cb)(int xok,X509_STORE_CTX *xctx);
        STACK_OF(X509) *sktmp=NULL;
        if (ctx->cert == NULL)
                {
@@ -388,7 +388,10 @@ static int check_chain_extensions(X509_STORE_CTX *ctx)
 #else
        int i, ok=0, must_be_ca;
        X509 *x;
-       int (*cb)();
+       int (*cb)(int xok,X509_STORE_CTX *xctx);
+       int proxy_path_length = 0;
+       int allow_proxy_certs =
+               !!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
        cb=ctx->verify_cb;
 
        /* must_be_ca can have 1 of 3 values:
@@ -400,6 +403,12 @@ static int check_chain_extensions(X509_STORE_CTX *ctx)
               all certificates in the chain except the leaf certificate.
        */
        must_be_ca = -1;
+
+       /* A hack to keep people who don't want to modify their software
+          happy */
+       if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
+               allow_proxy_certs = 1;
+
        /* Check all untrusted certificates */
        for (i = 0; i < ctx->last_untrusted; i++)
                {
@@ -414,6 +423,14 @@ static int check_chain_extensions(X509_STORE_CTX *ctx)
                        ok=cb(0,ctx);
                        if (!ok) goto end;
                        }
+               if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY))
+                       {
+                       ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
+                       ctx->error_depth = i;
+                       ctx->current_cert = x;
+                       ok=cb(0,ctx);
+                       if (!ok) goto end;
+                       }
                ret = X509_check_ca(x);
                switch(must_be_ca)
                        {
@@ -472,7 +489,7 @@ static int check_chain_extensions(X509_STORE_CTX *ctx)
                        }
                /* Check pathlen */
                if ((i > 1) && (x->ex_pathlen != -1)
-                          && (i > (x->ex_pathlen + 1)))
+                          && (i > (x->ex_pathlen + proxy_path_length + 1)))
                        {
                        ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
                        ctx->error_depth = i;
@@ -480,8 +497,26 @@ static int check_chain_extensions(X509_STORE_CTX *ctx)
                        ok=cb(0,ctx);
                        if (!ok) goto end;
                        }
-               /* The next certificate must be a CA */
-               must_be_ca = 1;
+               /* If this certificate is a proxy certificate, the next
+                  certificate must be another proxy certificate or a EE
+                  certificate.  If not, the next certificate must be a
+                  CA certificate.  */
+               if (x->ex_flags & EXFLAG_PROXY)
+                       {
+                       if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen)
+                               {
+                               ctx->error =
+                                       X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
+                               ctx->error_depth = i;
+                               ctx->current_cert = x;
+                               ok=cb(0,ctx);
+                               if (!ok) goto end;
+                               }
+                       proxy_path_length++;
+                       must_be_ca = 0;
+                       }
+               else
+                       must_be_ca = 1;
                }
        ok = 1;
  end:
@@ -496,7 +531,7 @@ static int check_trust(X509_STORE_CTX *ctx)
 #else
        int i, ok;
        X509 *x;
-       int (*cb)();
+       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;
@@ -678,7 +713,38 @@ static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x)
                return 0;
                }
 
-       *pcrl = xobj.data.crl;
+       /* If CRL times not valid look through store */
+       if (!check_crl_time(ctx, xobj.data.crl, 0))
+               {
+               int idx, i;
+               X509_OBJECT *pobj;
+               X509_OBJECT_free_contents(&xobj);
+               idx = X509_OBJECT_idx_by_subject(ctx->ctx->objs,
+                                                       X509_LU_CRL, nm);
+               if (idx == -1)
+                       return 0;
+               *pcrl = NULL;
+               for (i = idx; i < sk_X509_OBJECT_num(ctx->ctx->objs); i++)
+                       {
+                       pobj = sk_X509_OBJECT_value(ctx->ctx->objs, i);
+                       /* Check to see if it is a CRL and issuer matches */
+                       if (pobj->type != X509_LU_CRL)
+                               break;
+                       if (X509_NAME_cmp(nm,
+                                       X509_CRL_get_issuer(pobj->data.crl)))
+                               break;
+                       /* Set *pcrl because the CRL will either be valid or
+                        * a "best fit" CRL.
+                        */
+                       *pcrl = pobj->data.crl;
+                       if (check_crl_time(ctx, *pcrl, 0))
+                               break;
+                       }
+               if (*pcrl)
+                       CRYPTO_add(&(*pcrl)->references, 1, CRYPTO_LOCK_X509);
+               }
+       else 
+               *pcrl = xobj.data.crl;
        if (crl)
                X509_CRL_free(crl);
        return 1;
@@ -741,7 +807,8 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
                        }
                }
 
-       if (!check_crl_time(ctx, crl, 1))
+       ok = check_crl_time(ctx, crl, 1);
+       if (!ok)
                goto err;
 
        ok = 1;
@@ -814,7 +881,7 @@ static int check_policy(X509_STORE_CTX *ctx)
                                ctx->param->policies, ctx->param->flags);
        if (ret == 0)
                {
-               X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
+               X509err(X509_F_CHECK_POLICY,ERR_R_MALLOC_FAILURE);
                return 0;
                }
        /* Invalid or inconsistent extensions */
@@ -906,7 +973,7 @@ static int internal_verify(X509_STORE_CTX *ctx)
        int ok=0,n;
        X509 *xs,*xi;
        EVP_PKEY *pkey=NULL;
-       int (*cb)();
+       int (*cb)(int xok,X509_STORE_CTX *xctx);
 
        cb=ctx->verify_cb;
 
@@ -971,10 +1038,12 @@ static int internal_verify(X509_STORE_CTX *ctx)
 
                xs->valid = 1;
 
-               if (!check_cert_time(ctx, xs))
+               ok = check_cert_time(ctx, xs);
+               if (!ok)
                        goto end;
 
                /* The last error (if any) is still in the error value */
+               ctx->current_issuer=xi;
                ctx->current_cert=xs;
                ok=(*cb)(1,ctx);
                if (!ok) goto end;
@@ -1043,7 +1112,7 @@ int X509_cmp_time(ASN1_TIME *ctm, time_t *cmp_time)
                offset=0;
        else
                {
-               if ((*str != '+') && (str[5] != '-'))
+               if ((*str != '+') && (*str != '-'))
                        return 0;
                offset=((str[1]-'0')*10+(str[2]-'0'))*60;
                offset+=(str[3]-'0')*10+(str[4]-'0');
@@ -1054,7 +1123,8 @@ int X509_cmp_time(ASN1_TIME *ctm, time_t *cmp_time)
        atm.length=sizeof(buff2);
        atm.data=(unsigned char *)buff2;
 
-       X509_time_adj(&atm,-offset*60, cmp_time);
+       if (X509_time_adj(&atm,-offset*60, cmp_time) == NULL)
+               return 0;
 
        if (ctm->type == V_ASN1_UTCTIME)
                {