Adding a slash between the directoryt and the file is a problem with
[openssl.git] / crypto / x509 / x509_vfy.c
index c8fa53dbec93b21ab0978cd322120fb49a1bad5a..2bb21b443ec0674b9738935302ad2173a5066e81 100644 (file)
@@ -80,9 +80,6 @@ static int check_cert(X509_STORE_CTX *ctx);
 static int internal_verify(X509_STORE_CTX *ctx);
 const char *X509_version="X.509" OPENSSL_VERSION_PTEXT;
 
-static STACK_OF(CRYPTO_EX_DATA_FUNCS) *x509_store_ctx_method=NULL;
-static int x509_store_ctx_num=0;
-
 
 static int null_callback(int ok, X509_STORE_CTX *e)
        {
@@ -112,7 +109,6 @@ int X509_verify_cert(X509_STORE_CTX *ctx)
                }
 
        cb=ctx->verify_cb;
-       if (cb == NULL) cb=null_callback;
 
        /* first we make sure the chain we are going to build is
         * present and that the first entry is in place */
@@ -352,8 +348,7 @@ static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
        ctx->error = ret;
        ctx->current_cert = x;
        ctx->current_issuer = issuer;
-       if (ctx->verify_cb)
-               return ctx->verify_cb(0, ctx);
+       return ctx->verify_cb(0, ctx);
        return 0;
 }
 
@@ -385,11 +380,19 @@ static int check_chain_purpose(X509_STORE_CTX *ctx)
        X509 *x;
        int (*cb)();
        cb=ctx->verify_cb;
-       if (cb == NULL) cb=null_callback;
        /* Check all untrusted certificates */
        for (i = 0; i < ctx->last_untrusted; i++)
                {
                x = sk_X509_value(ctx->chain, i);
+               if (!(ctx->flags & X509_V_FLAG_IGNORE_CRITICAL)
+                       && (x->ex_flags & EXFLAG_CRITICAL))
+                       {
+                       ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
+                       ctx->error_depth = i;
+                       ctx->current_cert = x;
+                       ok=cb(0,ctx);
+                       if (!ok) goto end;
+                       }
                if (!X509_check_purpose(x, ctx->purpose, i))
                        {
                        if (i)
@@ -427,7 +430,6 @@ static int check_trust(X509_STORE_CTX *ctx)
        X509 *x;
        int (*cb)();
        cb=ctx->verify_cb;
-       if (cb == NULL) cb=null_callback;
 /* For now just check the last certificate in the chain */
        i = sk_X509_num(ctx->chain) - 1;
        x = sk_X509_value(ctx->chain, i);
@@ -451,9 +453,9 @@ static int check_revocation(X509_STORE_CTX *ctx)
        if (!(ctx->flags & X509_V_FLAG_CRL_CHECK))
                return 1;
        if (ctx->flags & X509_V_FLAG_CRL_CHECK_ALL)
-               last = 0;
-       else
                last = sk_X509_num(ctx->chain) - 1;
+       else
+               last = 0;
        for(i = 0; i <= last; i++)
                {
                ctx->error_depth = i;
@@ -479,8 +481,7 @@ static int check_cert(X509_STORE_CTX *ctx)
        if(!ok)
                {
                ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
-               if (ctx->verify_cb)
-                       ok = ctx->verify_cb(0, ctx);
+               ok = ctx->verify_cb(0, ctx);
                goto err;
                }
        ctx->current_crl = crl;
@@ -529,8 +530,7 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
                if(!ctx->check_issued(ctx, issuer, issuer))
                        {
                        ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
-                       if(ctx->verify_cb)
-                               ok = ctx->verify_cb(0, ctx);
+                       ok = ctx->verify_cb(0, ctx);
                        if(!ok) goto err;
                        }
                }
@@ -544,8 +544,7 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
                if(!ikey)
                        {
                        ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
-                       if(ctx->verify_cb)
-                               ok = ctx->verify_cb(0, ctx);
+                       ok = ctx->verify_cb(0, ctx);
                        if (!ok) goto err;
                        }
                else
@@ -554,8 +553,7 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
                        if(X509_CRL_verify(crl, ikey) <= 0)
                                {
                                ctx->error=X509_V_ERR_CRL_SIGNATURE_FAILURE;
-                               if(ctx->verify_cb)
-                                       ok = ctx->verify_cb(0, ctx);
+                               ok = ctx->verify_cb(0, ctx);
                                if (!ok) goto err;
                                }
                        }
@@ -571,18 +569,14 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
        if (i == 0)
                {
                ctx->error=X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
-               ok= 0;
-               if(ctx->verify_cb)
-                       ok = ctx->verify_cb(0, ctx);
+               ok = ctx->verify_cb(0, ctx);
                if (!ok) goto err;
                }
 
        if (i > 0)
                {
                ctx->error=X509_V_ERR_CRL_NOT_YET_VALID;
-               ok= 0;
-               if(ctx->verify_cb)
-                       ok = ctx->verify_cb(0, ctx);
+               ok = ctx->verify_cb(0, ctx);
                if (!ok) goto err;
                }
 
@@ -593,18 +587,14 @@ static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
                if (i == 0)
                        {
                        ctx->error=X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
-                       ok= 0;
-                       if(ctx->verify_cb)
-                               ok = ctx->verify_cb(0, ctx);
+                       ok = ctx->verify_cb(0, ctx);
                        if (!ok) goto err;
                        }
 
                if (i < 0)
                        {
                        ctx->error=X509_V_ERR_CRL_HAS_EXPIRED;
-                       ok= 0;
-                       if(ctx->verify_cb)
-                               ok = ctx->verify_cb(0, ctx);
+                       ok = ctx->verify_cb(0, ctx);
                        if (!ok) goto err;
                        }
                }
@@ -630,8 +620,7 @@ static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
         * this to handle entry extensions in V2 CRLs.
         */
        ctx->error = X509_V_ERR_CERT_REVOKED;
-       if (ctx->verify_cb)
-               ok = ctx->verify_cb(0, ctx);
+       ok = ctx->verify_cb(0, ctx);
        return ok;
        }
 
@@ -644,7 +633,6 @@ static int internal_verify(X509_STORE_CTX *ctx)
        int (*cb)();
 
        cb=ctx->verify_cb;
-       if (cb == NULL) cb=null_callback;
 
        n=sk_X509_num(ctx->chain);
        ctx->error_depth=n-1;
@@ -686,7 +674,7 @@ static int internal_verify(X509_STORE_CTX *ctx)
                                ok=(*cb)(0,ctx);
                                if (!ok) goto end;
                                }
-                       if (X509_verify(xs,pkey) <= 0)
+                       else if (X509_verify(xs,pkey) <= 0)
                                /* XXX  For the final trusted self-signed cert,
                                 * this is a waste of time.  That check should
                                 * optional so that e.g. 'openssl x509' can be
@@ -742,8 +730,6 @@ static int internal_verify(X509_STORE_CTX *ctx)
                        if (!ok) goto end;
                        }
 
-               /* CRL CHECK */
-
                /* The last error (if any) is still in the error value */
                ctx->current_cert=xs;
                ok=(*cb)(1,ctx);
@@ -770,7 +756,7 @@ int X509_cmp_time(ASN1_TIME *ctm, time_t *cmp_time)
        {
        char *str;
        ASN1_TIME atm;
-       time_t offset;
+       long offset;
        char buff1[24],buff2[24],*p;
        int i,j;
 
@@ -909,14 +895,9 @@ int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_fu
             CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
        {
        /* This function is (usually) called only once, by
-        * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
-        * That function uses locking, so we don't (usually)
-        * have to worry about locking here. For the whole cruel
-        * truth, see crypto/ex_data.c */
-       x509_store_ctx_num++;
-       return CRYPTO_get_ex_new_index(x509_store_ctx_num-1,
-               &x509_store_ctx_method,
-               argl,argp,new_func,dup_func,free_func);
+        * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
+       return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
+                       new_func, dup_func, free_func);
        }
 
 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
@@ -1041,8 +1022,8 @@ int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
                        }
                }
 
-       if (purpose) ctx->purpose = purpose;
-       if (trust) ctx->trust = trust;
+       if (purpose && !ctx->purpose) ctx->purpose = purpose;
+       if (trust && !ctx->trust) ctx->trust = trust;
        return 1;
 }
 
@@ -1050,7 +1031,12 @@ X509_STORE_CTX *X509_STORE_CTX_new(void)
 {
        X509_STORE_CTX *ctx;
        ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
-       if (ctx) memset(ctx, 0, sizeof(X509_STORE_CTX));
+       if (!ctx)
+               {
+               X509err(X509_F_X509_STORE_CTX_NEW,ERR_R_MALLOC_FAILURE);
+               return NULL;
+               }
+       memset(ctx, 0, sizeof(X509_STORE_CTX));
        return ctx;
 }
 
@@ -1060,7 +1046,7 @@ void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
        OPENSSL_free(ctx);
 }
 
-void X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
+int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
             STACK_OF(X509) *chain)
        {
        ctx->ctx=store;
@@ -1068,10 +1054,7 @@ void X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
        ctx->cert=x509;
        ctx->untrusted=chain;
        ctx->last_untrusted=0;
-       ctx->purpose=0;
-       ctx->trust=0;
        ctx->check_time=0;
-       ctx->flags=0;
        ctx->other_ctx=NULL;
        ctx->valid=0;
        ctx->chain=NULL;
@@ -1080,16 +1063,80 @@ void X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
        ctx->error_depth=0;
        ctx->current_cert=NULL;
        ctx->current_issuer=NULL;
-       ctx->check_issued = check_issued;
-       ctx->get_issuer = X509_STORE_CTX_get1_issuer;
-       ctx->verify_cb = store->verify_cb;
-       ctx->verify = store->verify;
-       ctx->check_revocation = check_revocation;
-       ctx->get_crl = get_crl;
-       ctx->check_crl = check_crl;
-       ctx->cert_crl = cert_crl;
-       ctx->cleanup = 0;
-       memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA));
+
+       /* Inherit callbacks and flags from X509_STORE if not set
+        * use defaults.
+        */
+
+
+       if (store)
+               {
+               ctx->purpose=store->purpose;
+               ctx->trust=store->trust;
+               ctx->flags = store->flags;
+               ctx->cleanup = store->cleanup;
+               }
+       else
+               {
+               ctx->purpose = 0;
+               ctx->trust = 0;
+               ctx->flags = 0;
+               ctx->cleanup = 0;
+               }
+
+       if (store && store->check_issued)
+               ctx->check_issued = store->check_issued;
+       else
+               ctx->check_issued = check_issued;
+
+       if (store && store->get_issuer)
+               ctx->get_issuer = store->get_issuer;
+       else
+               ctx->get_issuer = X509_STORE_CTX_get1_issuer;
+
+       if (store && store->verify_cb)
+               ctx->verify_cb = store->verify_cb;
+       else
+               ctx->verify_cb = null_callback;
+
+       if (store && store->verify)
+               ctx->verify = store->verify;
+       else
+               ctx->verify = internal_verify;
+
+       if (store && store->check_revocation)
+               ctx->check_revocation = store->check_revocation;
+       else
+               ctx->check_revocation = check_revocation;
+
+       if (store && store->get_crl)
+               ctx->get_crl = store->get_crl;
+       else
+               ctx->get_crl = get_crl;
+
+       if (store && store->check_crl)
+               ctx->check_crl = store->check_crl;
+       else
+               ctx->check_crl = check_crl;
+
+       if (store && store->cert_crl)
+               ctx->cert_crl = store->cert_crl;
+       else
+               ctx->cert_crl = cert_crl;
+
+
+       /* This memset() can't make any sense anyway, so it's removed. As
+        * X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we put a
+        * corresponding "new" here and remove this bogus initialisation. */
+       /* memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA)); */
+       if(!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
+                               &(ctx->ex_data)))
+               {
+               OPENSSL_free(ctx);
+               X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
+               return 0;
+               }
+       return 1;
        }
 
 /* Set alternative lookup method: just a STACK of trusted certificates.
@@ -1110,7 +1157,7 @@ void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
                sk_X509_pop_free(ctx->chain,X509_free);
                ctx->chain=NULL;
                }
-       CRYPTO_free_ex_data(x509_store_ctx_method,ctx,&(ctx->ex_data));
+       CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
        memset(&ctx->ex_data,0,sizeof(CRYPTO_EX_DATA));
        }
 
@@ -1125,6 +1172,12 @@ void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, long flags, time_t t)
        ctx->flags |= X509_V_FLAG_USE_CHECK_TIME;
        }
 
+void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
+                                 int (*verify_cb)(int, X509_STORE_CTX *))
+       {
+       ctx->verify_cb=verify_cb;
+       }
+
 IMPLEMENT_STACK_OF(X509)
 IMPLEMENT_ASN1_SET_OF(X509)