x509_vfy.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* crypto/x509/x509_vfy.c */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include "cryptlib.h"
{
return ok;
}
#if 0
{
return X509_subject_name_cmp(*a,*b);
}
#endif
{
int num;
int (*cb)();
{
return -1;
}
/* first we make sure the chain we are going to build is
* present and that the first entry is in place */
{
{
goto end;
}
}
/* We use a temporary STACK so we can chop and hack at it */
{
goto end;
}
for (;;)
{
/* If we have enough, we break */
* note of it and, if appropriate, use the
* X509_V_ERR_CERT_CHAIN_TOO_LONG error
* code later.
*/
/* If we are self signed, we break */
/* If we were passed a cert chain, use it first */
{
{
{
goto end;
}
ctx->last_untrusted++;
x=xtmp;
num++;
/* reparse the full chain for
* the next one */
continue;
}
}
break;
}
/* at this point, chain should contain a list of untrusted
* certificates. We now need to add at least one trusted one,
* if possible, otherwise we complain. */
/* Examine last certificate in chain and see if it
* is self signed.
*/
xn = X509_get_subject_name(x);
{
/* we have a self signed certificate */
{
/* We have a single self signed certificate: see if
* we can find it in the store. We must have an exact
* match to avoid possible impersonation.
*/
{
ctx->current_cert=x;
}
else
{
/* We have a match: replace certificate with store version
* so we get any trust settings.
*/
X509_free(x);
x = xtmp;
ctx->last_untrusted=0;
}
}
else
{
/* extract and save self signed certificate for later use */
ctx->last_untrusted--;
num--;
}
}
/* We now lookup certs from the certificate store */
for (;;)
{
/* If we have enough, we break */
/* If we are self signed, we break */
if (ok == 0) break;
x = xtmp;
{
return 0;
}
num++;
}
/* we now have our chain, lets check it... */
/* Is last certificate looked up self signed? */
{
{
else
ctx->current_cert=x;
}
else
{
num++;
}
}
/* We have the chain complete: now we need to check its purpose */
/* The chain extensions are OK: check trust */
/* We may as well copy down any DSA parameters that are required */
/* Check revocation status: we do this after copying parameters
* because they may be needed for CRL signature verification.
*/
/* At this point, we have a chain and just need to verify it */
else
if (0)
{
end:
}
return ok;
}
/* Given a STACK_OF(X509) find the issuer of cert (if any)
*/
{
int i;
for (i = 0; i < sk_X509_num(sk); i++)
{
return issuer;
}
return NULL;
}
/* Given a possible certificate and issuer check them */
{
int ret;
return 1;
/* If we haven't asked for issuer errors don't set ctx */
return 0;
ctx->current_cert = x;
return 0;
}
/* Alternative lookup method: look from a STACK stored in other_ctx */
{
if (*issuer)
{
return 1;
}
else
return 0;
}
/* Check a certificate chains extensions for consistency
* with the supplied purpose
*/
{
#ifdef OPENSSL_NO_CHAIN_VERIFY
return 1;
#else
int i, ok=0;
X509 *x;
int (*cb)();
/* Check all untrusted certificates */
for (i = 0; i < ctx->last_untrusted; i++)
{
int ret;
&& (x->ex_flags & EXFLAG_CRITICAL))
{
ctx->error_depth = i;
ctx->current_cert = x;
}
if ((ret == 0)
&& (ret != 1)))
{
if (i)
else
ctx->error_depth = i;
ctx->current_cert = x;
}
/* Check pathlen */
&& (i > (x->ex_pathlen + 1)))
{
ctx->error_depth = i;
ctx->current_cert = x;
}
}
ok = 1;
end:
return ok;
#endif
}
{
#ifdef OPENSSL_NO_CHAIN_VERIFY
return 1;
#else
int i, ok;
X509 *x;
int (*cb)();
/* For now just check the last certificate in the chain */
if (ok == X509_TRUST_TRUSTED)
return 1;
ctx->error_depth = i;
ctx->current_cert = x;
if (ok == X509_TRUST_REJECTED)
else
return ok;
#endif
}
{
return 1;
else
last = 0;
for(i = 0; i <= last; i++)
{
ctx->error_depth = i;
}
return 1;
}
{
X509 *x;
ctx->current_cert = x;
/* Try to retrieve relevant CRL */
/* If error looking up CRL, nothing we can do except
* notify callback
*/
if(!ok)
{
goto err;
}
err:
return ok;
}
/* Retrieve CRL corresponding to certificate: currently just a
* subject lookup: maybe use AKID later...
* Also might look up any included CRLs too (e.g PKCS#7 signedData).
*/
{
int ok;
if (!ok) return 0;
return 1;
}
/* Check CRL validity */
{
/* Find CRL issuer: if not last certificate then issuer
* is next certificate in chain.
*/
else
{
/* If not self signed, can't check signature */
{
}
}
if(issuer)
{
/* Check for cRLSign bit if keyUsage present */
{
}
/* Attempt to get issuer certificate public key */
if(!ikey)
{
}
else
{
/* Verify CRL signature */
{
}
}
}
/* OK, CRL signature valid check times */
else
if (i == 0)
{
}
if (i > 0)
{
}
{
if (i == 0)
{
}
if (i < 0)
{
}
}
ok = 1;
err:
return ok;
}
/* Check certificate against CRL */
{
/* Look for serial number of certificate in CRL */
/* If found assume revoked: want something cleverer than
* this to handle entry extensions in V2 CRLs.
*/
if(idx >= 0)
{
if (!ok) return 0;
}
return 1;
/* See if we have any critical CRL extensions: since we
* currently don't handle any CRL extensions the CRL must be
* rejected.
* This code accesses the X509_CRL structure directly: applications
* shouldn't do this.
*/
{
{
if(!ok) return 0;
break;
}
}
return 1;
}
{
int i,ok=0,n;
int (*cb)();
n--;
else
else
{
if (n <= 0)
{
goto end;
}
else
{
n--;
ctx->error_depth=n;
}
}
/* ctx->error=0; not needed */
while (n >= 0)
{
ctx->error_depth=n;
{
{
}
/* 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
* used to detect invalid self-signatures, but
* we don't verify again and again in SSL
* handshakes and the like once the cert has
* been declared trusted. */
{
if (!ok)
{
goto end;
}
}
if (i == 0)
{
}
if (i > 0)
{
}
}
if (i == 0)
{
}
if (i < 0)
{
}
/* The last error (if any) is still in the error value */
n--;
if (n >= 0)
{
}
}
ok=1;
end:
return ok;
}
{
}
{
char *str;
long offset;
int i,j;
p=buff1;
{
if ((i < 11) || (i > 17)) return 0;
p+=10;
str+=10;
}
else
{
if (i < 13) return 0;
p+=12;
str+=12;
}
{ *(p++)='0'; *(p++)='0'; }
else
{
*(p++)= *(str++);
*(p++)= *(str++);
/* Skip any fractional seconds... */
if (*str == '.')
{
str++;
}
}
*(p++)='Z';
*(p++)='\0';
if (*str == 'Z')
offset=0;
else
{
return 0;
if (*str == '-')
}
{
if (i < 50) i+=100; /* cf. RFC 2459 */
if (j < 50) j+=100;
if (i < j) return -1;
if (i > j) return 1;
}
if (i == 0) /* wait a second then return younger :-) */
return -1;
else
return i;
}
{
}
{
time_t t;
int type = -1;
else time(&t);
t+=adj;
return ASN1_TIME_set(s, t);
}
{
int i,j;
for (i=0; i<sk_X509_num(chain); i++)
{
{
return 0;
}
if (!EVP_PKEY_missing_parameters(ktmp))
break;
else
{
}
}
{
return 0;
}
/* first, populate the other certs */
for (j=i-1; j >= 0; j--)
{
}
return 1;
}
{
/* This function is (usually) called only once, by
* SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
}
{
}
{
}
{
}
{
}
{
return ctx->error_depth;
}
{
return ctx->current_cert;
}
{
}
{
int i;
X509 *x;
for (i = 0; i < sk_X509_num(chain); i++)
{
x = sk_X509_value(chain, i);
}
return chain;
}
{
}
{
}
{
}
{
}
/* This function is used to set the X509_STORE_CTX purpose and trust
* values. This is intended to be used when another structure has its
* own trust and purpose values which (if set) will be inherited by
* the ctx. If they aren't set then we will usually have a default
* purpose in mind which should then be used to set the trust value.
* An example of this is SSL use: an SSL structure will have its own
* purpose and trust settings which the application can set: if they
*/
{
int idx;
/* If purpose not set use default */
/* If we have a purpose then check it is valid */
if (purpose)
{
if (idx == -1)
{
return 0;
}
{
if (idx == -1)
{
return 0;
}
}
/* If trust not set then get from purpose default */
}
if (trust)
{
if (idx == -1)
{
return 0;
}
}
return 1;
}
X509_STORE_CTX *X509_STORE_CTX_new(void)
{
if (!ctx)
{
return NULL;
}
return ctx;
}
{
}
{
ctx->current_method=0;
ctx->last_untrusted=0;
ctx->check_time=0;
ctx->error_depth=0;
/* Inherit callbacks and flags from X509_STORE if not set
* use defaults.
*/
if (store)
{
}
else
{
}
else
else
else
else
else
else
else
else
/* 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)); */
{
return 0;
}
return 1;
}
/* Set alternative lookup method: just a STACK of trusted certificates.
* This avoids X509_STORE nastiness where it isn't needed.
*/
{
}
{
{
}
}
{
}
{
ctx->check_time = t;
}
int (*verify_cb)(int, X509_STORE_CTX *))
{
}