v3_purp.c revision 9dc0df1bac950d6e491f9a7c7e4888f2b301cb15
/* v3_purp.c */
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
* project 2001.
*/
/* ====================================================================
* Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
*
* 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 above 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
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED 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 OpenSSL PROJECT OR
* ITS 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.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <stdio.h>
#include "cryptlib.h"
#include <openssl/x509_vfy.h>
static void x509v3_cache_extensions(X509 *x);
static int check_ssl_ca(const X509 *x);
static int xp_cmp(const X509_PURPOSE * const *a,
const X509_PURPOSE * const *b);
static void xptable_free(X509_PURPOSE *p);
static X509_PURPOSE xstandard[] = {
{X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, check_purpose_ssl_client, "SSL client", "sslclient", NULL},
{X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ssl_server, "SSL server", "sslserver", NULL},
{X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},
{X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, "S/MIME signing", "smimesign", NULL},
{X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},
{X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, "CRL signing", "crlsign", NULL},
};
static int xp_cmp(const X509_PURPOSE * const *a,
const X509_PURPOSE * const *b)
{
}
/* As much as I'd like to make X509_check_purpose use a "const" X509*
* I really can't because it does recalculate hashes and do other non-const
* things. */
{
int idx;
const X509_PURPOSE *pt;
if(!(x->ex_flags & EXFLAG_SET)) {
}
}
int X509_PURPOSE_set(int *p, int purpose)
{
return 0;
}
*p = purpose;
return 1;
}
int X509_PURPOSE_get_count(void)
{
if(!xptable) return X509_PURPOSE_COUNT;
}
{
}
int X509_PURPOSE_get_by_sname(char *sname)
{
int i;
for(i = 0; i < X509_PURPOSE_get_count(); i++) {
xptmp = X509_PURPOSE_get0(i);
}
return -1;
}
int X509_PURPOSE_get_by_id(int purpose)
{
int idx;
return purpose - X509_PURPOSE_MIN;
if(!xptable) return -1;
return idx + X509_PURPOSE_COUNT;
}
{
int idx;
/* This is set according to what we change: application can't set it */
/* This will always be set for application modified trust entries */
/* Get existing entry if any */
/* Need a new entry */
if(idx == -1) {
return 0;
}
/* OPENSSL_free existing name if dynamic */
}
/* dup supplied name */
return 0;
}
/* Keep the dynamic flag of existing entry */
/* Set all other flags */
/* If its a new entry manage the dynamic table */
if(idx == -1) {
return 0;
}
return 0;
}
}
return 1;
}
static void xptable_free(X509_PURPOSE *p)
{
if(!p) return;
if (p->flags & X509_PURPOSE_DYNAMIC)
{
if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
OPENSSL_free(p->name);
OPENSSL_free(p->sname);
}
OPENSSL_free(p);
}
}
void X509_PURPOSE_cleanup(void)
{
unsigned int i;
}
{
}
{
}
{
}
{
}
static int nid_cmp(int *a, int *b)
{
return *a - *b;
}
{
/* This table is a list of the NIDs of supported extensions:
* that is those which are used by the verify process. If
* an extension is critical and doesn't appear in this list
* then the verify process will normally reject the certificate.
* The list must be kept in numerical order because it will be
* searched using bsearch.
*/
static int supported_nids[] = {
NID_netscape_cert_type, /* 71 */
NID_key_usage, /* 83 */
NID_subject_alt_name, /* 85 */
NID_basic_constraints, /* 87 */
NID_ext_key_usage, /* 126 */
NID_proxyCertInfo /* 661 */
};
int ex_nid;
return 0;
sizeof(supported_nids)/sizeof(int), sizeof(int),
(int (*)(const void *, const void *))nid_cmp))
return 1;
return 0;
}
static void x509v3_cache_extensions(X509 *x)
{
int i;
if(x->ex_flags & EXFLAG_SET) return;
#ifndef OPENSSL_NO_SHA
#endif
/* Does subject name match issuer ? */
/* V1 should mean no extensions ... */
/* Handle basic constraints */
x->ex_flags |= EXFLAG_INVALID;
x->ex_pathlen = 0;
} else x->ex_pathlen = -1;
x->ex_flags |= EXFLAG_BCONS;
}
/* Handle proxy certificates */
|| X509_get_ext_by_NID(x, NID_subject_alt_name, 0) >= 0
|| X509_get_ext_by_NID(x, NID_issuer_alt_name, 0) >= 0) {
x->ex_flags |= EXFLAG_INVALID;
}
if (pci->pcPathLengthConstraint) {
x->ex_pcpathlen =
} else x->ex_pcpathlen = -1;
x->ex_flags |= EXFLAG_PROXY;
}
/* Handle key usage */
} else x->ex_kusage = 0;
x->ex_flags |= EXFLAG_KUSAGE;
}
x->ex_xkusage = 0;
x->ex_flags |= EXFLAG_XKUSAGE;
for(i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
case NID_server_auth:
x->ex_xkusage |= XKU_SSL_SERVER;
break;
case NID_client_auth:
x->ex_xkusage |= XKU_SSL_CLIENT;
break;
case NID_email_protect:
x->ex_xkusage |= XKU_SMIME;
break;
case NID_code_sign:
x->ex_xkusage |= XKU_CODE_SIGN;
break;
case NID_ms_sgc:
case NID_ns_sgc:
x->ex_xkusage |= XKU_SGC;
break;
case NID_OCSP_sign:
x->ex_xkusage |= XKU_OCSP_SIGN;
break;
case NID_time_stamp:
x->ex_xkusage |= XKU_TIMESTAMP;
break;
case NID_dvcs:
x->ex_xkusage |= XKU_DVCS;
break;
}
}
}
else x->ex_nscert = 0;
x->ex_flags |= EXFLAG_NSCERT;
}
for (i = 0; i < X509_get_ext_count(x); i++)
{
ex = X509_get_ext(x, i);
if (!X509_EXTENSION_get_critical(ex))
continue;
if (!X509_supported_extension(ex))
{
x->ex_flags |= EXFLAG_CRITICAL;
break;
}
}
x->ex_flags |= EXFLAG_SET;
}
/* CA checks common to all purposes
* return codes:
* 0 not a CA
* 1 is a CA
* 2 basicConstraints absent so "maybe" a CA
* 3 basicConstraints absent but self signed V1.
* 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
*/
#define xku_reject(x, usage) \
{
/* keyUsage if present should allow cert signing */
if(ku_reject(x, KU_KEY_CERT_SIGN)) return 0;
if(x->ex_flags & EXFLAG_BCONS) {
/* If basicConstraints says not a CA then say so */
else return 0;
} else {
/* we support V1 roots for... uh, I don't really know why. */
/* If key usage present it must have certSign so tolerate it */
/* Older certificates could have Netscape-specific CA types */
else if (x->ex_flags & EXFLAG_NSCERT
/* can this still be regarded a CA certificate? I doubt it */
return 0;
}
}
int X509_check_ca(X509 *x)
{
if(!(x->ex_flags & EXFLAG_SET)) {
}
return check_ca(x);
}
/* Check SSL CA: common checks for SSL client and server */
static int check_ssl_ca(const X509 *x)
{
int ca_ret;
if(!ca_ret) return 0;
/* check nsCertType if present */
else return 0;
}
{
if(xku_reject(x,XKU_SSL_CLIENT)) return 0;
if(ca) return check_ssl_ca(x);
/* We need to do digital signatures with it */
if(ku_reject(x,KU_DIGITAL_SIGNATURE)) return 0;
/* nsCertType if present should allow SSL client use */
if(ns_reject(x, NS_SSL_CLIENT)) return 0;
return 1;
}
{
if(ca) return check_ssl_ca(x);
if(ns_reject(x, NS_SSL_SERVER)) return 0;
/* Now as for keyUsage: we'll at least need to sign OR encipher */
return 1;
}
{
int ret;
/* We need to encipher or Netscape complains */
if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
return ret;
}
{
if(xku_reject(x,XKU_SMIME)) return 0;
if(ca) {
int ca_ret;
if(!ca_ret) return 0;
/* check nsCertType if present */
else return 0;
}
if(x->ex_flags & EXFLAG_NSCERT) {
/* Workaround for some buggy certificates */
return 0;
}
return 1;
}
{
int ret;
return ret;
}
{
int ret;
if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
return ret;
}
{
if(ca) {
int ca_ret;
else return 0;
}
if(ku_reject(x, KU_CRL_SIGN)) return 0;
return 1;
}
/* OCSP helper: this is *not* a full OCSP check. It just checks that
* each CA is valid. Additional checks must be made on the chain.
*/
{
/* Must be a valid CA. Should we really support the "I don't know"
value (2)? */
/* leaf certificate is checked in OCSP_verify() */
return 1;
}
{
return 1;
}
/* Various checks to see if one certificate issued the second.
* This can be used to prune a set of possible issuer certificates
* which have been looked up using some simple method such as by
* subject name.
* These are:
* 1. Check issuer_name(subject) == subject_name(issuer)
* 2. If akid(subject) exists check it matches issuer
* 3. If key_usage(issuer) exists check it supports certificate signing
* returns 0 for OK, positive for reason for mismatch, reasons match
* codes for X509_verify_cert()
*/
{
/* Check key ids (if present) */
return X509_V_ERR_AKID_SKID_MISMATCH;
/* Check serial number */
/* Check issuer name */
/* Ugh, for some peculiar reason AKID includes
* SEQUENCE OF GeneralName. So look for a DirName.
* There may be more than one but we only take any
* notice of the first.
*/
int i;
for(i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
break;
}
}
}
}
{
}
return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
return X509_V_OK;
}