2016-05-17 18:52:22 +00:00
|
|
|
/*
|
2019-09-10 12:56:40 +00:00
|
|
|
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/asn1.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/x509.h>
|
1999-11-16 00:56:03 +00:00
|
|
|
#include <openssl/x509v3.h>
|
2019-09-27 21:57:58 +00:00
|
|
|
#include "crypto/x509.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-06-01 02:36:58 +00:00
|
|
|
int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
2015-09-16 17:40:26 +00:00
|
|
|
const X509_CINF *ai, *bi;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2015-09-16 17:40:26 +00:00
|
|
|
ai = &a->cert_info;
|
|
|
|
bi = &b->cert_info;
|
2015-10-11 20:05:49 +00:00
|
|
|
i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (i)
|
2017-10-17 14:04:09 +00:00
|
|
|
return i;
|
|
|
|
return X509_NAME_cmp(ai->issuer, bi->issuer);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2001-02-19 16:06:34 +00:00
|
|
|
#ifndef OPENSSL_NO_MD5
|
1999-04-19 21:31:43 +00:00
|
|
|
unsigned long X509_issuer_and_serial_hash(X509 *a)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
unsigned long ret = 0;
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char md[16];
|
|
|
|
char *f;
|
|
|
|
|
2015-11-27 13:02:12 +00:00
|
|
|
if (ctx == NULL)
|
|
|
|
goto err;
|
2015-09-16 17:40:26 +00:00
|
|
|
f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
OPENSSL_free(f);
|
|
|
|
if (!EVP_DigestUpdate
|
2015-11-27 13:02:12 +00:00
|
|
|
(ctx, (unsigned char *)a->cert_info.serialNumber.data,
|
2015-10-11 20:05:49 +00:00
|
|
|
(unsigned long)a->cert_info.serialNumber.length))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
|
|
|
|
((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
|
|
|
|
) & 0xffffffffL;
|
|
|
|
err:
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX_free(ctx);
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2000-06-01 02:36:58 +00:00
|
|
|
int X509_issuer_name_cmp(const X509 *a, const X509 *b)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-06-01 02:36:58 +00:00
|
|
|
int X509_subject_name_cmp(const X509 *a, const X509 *b)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-06-01 02:36:58 +00:00
|
|
|
int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return X509_NAME_cmp(a->crl.issuer, b->crl.issuer);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2006-07-24 12:39:22 +00:00
|
|
|
int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return memcmp(a->sha1_hash, b->sha1_hash, 20);
|
|
|
|
}
|
2006-07-24 12:39:22 +00:00
|
|
|
|
2016-07-30 10:47:18 +00:00
|
|
|
X509_NAME *X509_get_issuer_name(const X509 *a)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return a->cert_info.issuer;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
unsigned long X509_issuer_name_hash(X509 *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return X509_NAME_hash(x->cert_info.issuer);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2010-01-12 17:29:34 +00:00
|
|
|
#ifndef OPENSSL_NO_MD5
|
|
|
|
unsigned long X509_issuer_name_hash_old(X509 *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return X509_NAME_hash_old(x->cert_info.issuer);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2010-01-12 17:29:34 +00:00
|
|
|
#endif
|
|
|
|
|
2016-07-30 10:47:18 +00:00
|
|
|
X509_NAME *X509_get_subject_name(const X509 *a)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return a->cert_info.subject;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-07-30 17:36:16 +00:00
|
|
|
ASN1_INTEGER *X509_get_serialNumber(X509 *a)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-10-11 20:05:49 +00:00
|
|
|
return &a->cert_info.serialNumber;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-08-18 14:16:31 +00:00
|
|
|
const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a)
|
|
|
|
{
|
|
|
|
return &a->cert_info.serialNumber;
|
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
unsigned long X509_subject_name_hash(X509 *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return X509_NAME_hash(x->cert_info.subject);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-01-21 00:03:51 +00:00
|
|
|
|
2010-01-12 17:29:34 +00:00
|
|
|
#ifndef OPENSSL_NO_MD5
|
|
|
|
unsigned long X509_subject_name_hash_old(X509 *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return X509_NAME_hash_old(x->cert_info.subject);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2010-01-12 17:29:34 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Compare two certificates: they must be identical for this to work. NB:
|
|
|
|
* Although "cmp" operations are generally prototyped to take "const"
|
|
|
|
* arguments (eg. for use in STACKs), the way X509 handling is - these
|
|
|
|
* operations may involve ensuring the hashes are up-to-date and ensuring
|
|
|
|
* certain cert information is cached. So this is the point where the
|
|
|
|
* "depth-first" constification tree has to halt with an evil cast.
|
1999-11-16 00:56:03 +00:00
|
|
|
*/
|
2000-06-01 02:36:58 +00:00
|
|
|
int X509_cmp(const X509 *a, const X509 *b)
|
1999-11-16 00:56:03 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
int rv;
|
|
|
|
/* ensure hash is valid */
|
|
|
|
X509_check_purpose((X509 *)a, -1, 0);
|
|
|
|
X509_check_purpose((X509 *)b, -1, 0);
|
|
|
|
|
|
|
|
rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
/* Check for match against stored encoding too */
|
2015-09-16 17:40:26 +00:00
|
|
|
if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) {
|
2016-04-28 00:02:35 +00:00
|
|
|
if (a->cert_info.enc.len < b->cert_info.enc.len)
|
|
|
|
return -1;
|
|
|
|
if (a->cert_info.enc.len > b->cert_info.enc.len)
|
|
|
|
return 1;
|
2015-09-16 17:40:26 +00:00
|
|
|
return memcmp(a->cert_info.enc.enc, b->cert_info.enc.enc,
|
|
|
|
a->cert_info.enc.len);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
return rv;
|
1999-11-16 00:56:03 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2006-07-18 12:36:19 +00:00
|
|
|
int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2002-11-09 21:52:20 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Ensure canonical encoding is present and up to date */
|
2002-11-09 21:52:20 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!a->canon_enc || a->modified) {
|
|
|
|
ret = i2d_X509_NAME((X509_NAME *)a, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return -2;
|
|
|
|
}
|
2004-12-01 01:45:30 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!b->canon_enc || b->modified) {
|
|
|
|
ret = i2d_X509_NAME((X509_NAME *)b, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return -2;
|
|
|
|
}
|
2004-12-01 01:45:30 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = a->canon_enclen - b->canon_enclen;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2018-05-16 10:59:47 +00:00
|
|
|
if (ret != 0 || a->canon_enclen == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
return ret;
|
2004-12-01 01:45:30 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2009-01-15 13:22:39 +00:00
|
|
|
unsigned long X509_NAME_hash(X509_NAME *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
unsigned long ret = 0;
|
|
|
|
unsigned char md[SHA_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
/* Make sure X509_NAME structure contains valid cached encoding */
|
|
|
|
i2d_X509_NAME(x, NULL);
|
|
|
|
if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
|
|
|
|
NULL))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
|
|
|
|
((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
|
|
|
|
) & 0xffffffffL;
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2006-07-18 12:36:19 +00:00
|
|
|
|
2001-02-19 16:06:34 +00:00
|
|
|
#ifndef OPENSSL_NO_MD5
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* I now DER encode the name and hash it. Since I cache the DER encoding,
|
|
|
|
* this is reasonably efficient.
|
|
|
|
*/
|
2009-01-15 13:22:39 +00:00
|
|
|
|
|
|
|
unsigned long X509_NAME_hash_old(X509_NAME *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned long ret = 0;
|
|
|
|
unsigned char md[16];
|
|
|
|
|
2015-11-27 13:02:12 +00:00
|
|
|
if (md_ctx == NULL)
|
|
|
|
return ret;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Make sure X509_NAME structure contains valid cached encoding */
|
|
|
|
i2d_X509_NAME(x, NULL);
|
2015-11-27 13:02:12 +00:00
|
|
|
EVP_MD_CTX_set_flags(md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
|
|
|
|
if (EVP_DigestInit_ex(md_ctx, EVP_md5(), NULL)
|
|
|
|
&& EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length)
|
|
|
|
&& EVP_DigestFinal_ex(md_ctx, md, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
|
|
|
|
((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
|
|
|
|
) & 0xffffffffL;
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX_free(md_ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Search a stack of X509 for a match */
|
1999-04-19 21:31:43 +00:00
|
|
|
X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_INTEGER *serial)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
X509 x, *x509 = NULL;
|
|
|
|
|
|
|
|
if (!sk)
|
|
|
|
return NULL;
|
|
|
|
|
2015-10-11 20:05:49 +00:00
|
|
|
x.cert_info.serialNumber = *serial;
|
2015-09-16 17:40:26 +00:00
|
|
|
x.cert_info.issuer = name;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
for (i = 0; i < sk_X509_num(sk); i++) {
|
|
|
|
x509 = sk_X509_value(sk, i);
|
|
|
|
if (X509_issuer_and_serial_cmp(x509, &x) == 0)
|
2017-10-17 14:04:09 +00:00
|
|
|
return x509;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2017-10-17 14:04:09 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sk_X509_num(sk); i++) {
|
|
|
|
x509 = sk_X509_value(sk, i);
|
|
|
|
if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
|
2017-10-17 14:04:09 +00:00
|
|
|
return x509;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2017-10-17 14:04:09 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-07-08 22:43:28 +00:00
|
|
|
EVP_PKEY *X509_get0_pubkey(const X509 *x)
|
2015-12-14 13:13:32 +00:00
|
|
|
{
|
|
|
|
if (x == NULL)
|
|
|
|
return NULL;
|
|
|
|
return X509_PUBKEY_get0(x->cert_info.key);
|
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
EVP_PKEY *X509_get_pubkey(X509 *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-09-16 17:40:26 +00:00
|
|
|
if (x == NULL)
|
2015-12-14 13:13:32 +00:00
|
|
|
return NULL;
|
|
|
|
return X509_PUBKEY_get(x->cert_info.key);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 11:00:56 +00:00
|
|
|
|
2016-07-08 22:43:28 +00:00
|
|
|
int X509_check_private_key(const X509 *x, const EVP_PKEY *k)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-07-08 22:43:28 +00:00
|
|
|
const EVP_PKEY *xk;
|
2015-01-22 03:40:55 +00:00
|
|
|
int ret;
|
|
|
|
|
2015-12-14 13:13:32 +00:00
|
|
|
xk = X509_get0_pubkey(x);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (xk)
|
|
|
|
ret = EVP_PKEY_cmp(xk, k);
|
|
|
|
else
|
|
|
|
ret = -2;
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
|
|
|
|
break;
|
|
|
|
case -2:
|
|
|
|
X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
|
|
|
|
}
|
|
|
|
if (ret > 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check a suite B algorithm is permitted: pass in a public key and the NID
|
|
|
|
* of its signature (or 0 if no signature). The pflags is a pointer to a
|
|
|
|
* flags field which must contain the suite B verification flags.
|
2012-08-03 13:51:43 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-17 16:40:08 +00:00
|
|
|
#ifndef OPENSSL_NO_EC
|
|
|
|
|
2012-08-03 13:51:43 +00:00
|
|
|
static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
const EC_GROUP *grp = NULL;
|
|
|
|
int curve_nid;
|
2016-01-19 00:21:12 +00:00
|
|
|
if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
|
|
|
|
grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!grp)
|
|
|
|
return X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
|
|
|
|
curve_nid = EC_GROUP_get_curve_name(grp);
|
|
|
|
/* Check curve is consistent with LOS */
|
|
|
|
if (curve_nid == NID_secp384r1) { /* P-384 */
|
|
|
|
/*
|
|
|
|
* Check signature algorithm is consistent with curve.
|
|
|
|
*/
|
|
|
|
if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384)
|
|
|
|
return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
|
|
|
|
if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS))
|
|
|
|
return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
|
|
|
|
/* If we encounter P-384 we cannot use P-256 later */
|
|
|
|
*pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY;
|
|
|
|
} else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */
|
|
|
|
if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256)
|
|
|
|
return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
|
|
|
|
if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY))
|
|
|
|
return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
|
|
|
|
} else
|
|
|
|
return X509_V_ERR_SUITE_B_INVALID_CURVE;
|
|
|
|
|
|
|
|
return X509_V_OK;
|
|
|
|
}
|
2012-08-03 13:51:43 +00:00
|
|
|
|
2012-08-03 15:58:15 +00:00
|
|
|
int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned long flags)
|
|
|
|
{
|
|
|
|
int rv, i, sign_nid;
|
2016-01-17 21:50:52 +00:00
|
|
|
EVP_PKEY *pk;
|
|
|
|
unsigned long tflags = flags;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
|
|
|
|
return X509_V_OK;
|
2016-01-17 21:50:52 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* If no EE certificate passed in must be first in chain */
|
|
|
|
if (x == NULL) {
|
|
|
|
x = sk_X509_value(chain, 0);
|
|
|
|
i = 1;
|
|
|
|
} else
|
|
|
|
i = 0;
|
|
|
|
|
2016-01-17 21:50:52 +00:00
|
|
|
pk = X509_get0_pubkey(x);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build
|
|
|
|
* a chain all, just report trust success or failure, but must also report
|
|
|
|
* Suite-B errors if applicable. This is indicated via a NULL chain
|
|
|
|
* pointer. All we need to do is check the leaf key algorithm.
|
|
|
|
*/
|
|
|
|
if (chain == NULL)
|
|
|
|
return check_suite_b(pk, -1, &tflags);
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (X509_get_version(x) != 2) {
|
|
|
|
rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
|
|
|
|
/* Correct error depth */
|
|
|
|
i = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check EE key only */
|
|
|
|
rv = check_suite_b(pk, -1, &tflags);
|
|
|
|
if (rv != X509_V_OK) {
|
|
|
|
/* Correct error depth */
|
|
|
|
i = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
for (; i < sk_X509_num(chain); i++) {
|
|
|
|
sign_nid = X509_get_signature_nid(x);
|
|
|
|
x = sk_X509_value(chain, i);
|
|
|
|
if (X509_get_version(x) != 2) {
|
|
|
|
rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
|
|
|
|
goto end;
|
|
|
|
}
|
2015-12-20 00:32:36 +00:00
|
|
|
pk = X509_get0_pubkey(x);
|
2015-01-22 03:40:55 +00:00
|
|
|
rv = check_suite_b(pk, sign_nid, &tflags);
|
|
|
|
if (rv != X509_V_OK)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Final check: root CA signature */
|
|
|
|
rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
|
|
|
|
end:
|
|
|
|
if (rv != X509_V_OK) {
|
|
|
|
/* Invalid signature or LOS errors are for previous cert */
|
|
|
|
if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM
|
|
|
|
|| rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)
|
|
|
|
i--;
|
|
|
|
/*
|
|
|
|
* If we have LOS error and flags changed then we are signing P-384
|
2016-02-05 20:23:54 +00:00
|
|
|
* with P-256. Use more meaningful error.
|
2015-01-22 03:40:55 +00:00
|
|
|
*/
|
|
|
|
if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)
|
|
|
|
rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
|
|
|
|
if (perror_depth)
|
|
|
|
*perror_depth = i;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-03 13:51:43 +00:00
|
|
|
|
2012-08-03 15:58:15 +00:00
|
|
|
int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int sign_nid;
|
|
|
|
if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
|
|
|
|
return X509_V_OK;
|
2015-09-17 13:44:19 +00:00
|
|
|
sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
|
2015-01-22 03:40:55 +00:00
|
|
|
return check_suite_b(pk, sign_nid, &flags);
|
|
|
|
}
|
2013-08-17 16:40:08 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned long flags)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-17 16:40:08 +00:00
|
|
|
|
|
|
|
int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-17 16:40:08 +00:00
|
|
|
|
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Not strictly speaking an "up_ref" as a STACK doesn't have a reference
|
|
|
|
* count but it has the same effect by duping the STACK and upping the ref of
|
|
|
|
* each X509 structure.
|
2012-08-03 15:58:15 +00:00
|
|
|
*/
|
|
|
|
STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
STACK_OF(X509) *ret;
|
|
|
|
int i;
|
|
|
|
ret = sk_X509_dup(chain);
|
2019-08-16 13:18:51 +00:00
|
|
|
if (ret == NULL)
|
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
for (i = 0; i < sk_X509_num(ret); i++) {
|
|
|
|
X509 *x = sk_X509_value(ret, i);
|
2019-08-16 13:18:51 +00:00
|
|
|
if (!X509_up_ref(x))
|
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2019-08-16 13:18:51 +00:00
|
|
|
err:
|
|
|
|
while (i-- > 0)
|
|
|
|
X509_free (sk_X509_value(ret, i));
|
|
|
|
sk_X509_free(ret);
|
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|