| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/x509.h> |
| 5 |
|
| 6 |
#include <openssl/asn1.h> |
| 7 |
#include <openssl/digest.h> |
| 8 |
#include <openssl/err.h> |
| 9 |
#include <openssl/evp.h> |
| 10 |
#include <openssl/obj.h> |
| 11 |
|
| 12 |
#include "../fipsmodule/pqdsa/internal.h" |
| 13 |
#include "../fipsmodule/evp/internal.h" |
| 14 |
#include "internal.h" |
| 15 |
|
| 16 |
// Restrict the digests that are allowed in X509 certificates |
| 17 |
int x509_digest_nid_ok(const int digest_nid) { |
| 18 |
switch (digest_nid) { |
| 19 |
case NID_md4: |
| 20 |
case NID_md5: |
| 21 |
return 0; |
| 22 |
} |
| 23 |
return 1; |
| 24 |
} |
| 25 |
|
| 26 |
int x509_digest_sign_algorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor) { |
| 27 |
EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx->pctx); |
| 28 |
if (pkey == NULL) { |
| 29 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_CONTEXT_NOT_INITIALISED); |
| 30 |
return 0; |
| 31 |
} |
| 32 |
|
| 33 |
if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { |
| 34 |
int pad_mode; |
| 35 |
if (!EVP_PKEY_CTX_get_rsa_padding(ctx->pctx, &pad_mode)) { |
| 36 |
return 0; |
| 37 |
} |
| 38 |
// RSA-PSS has special signature algorithm logic. |
| 39 |
if (pad_mode == RSA_PKCS1_PSS_PADDING) { |
| 40 |
return x509_rsa_ctx_to_pss(ctx, algor); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if (EVP_PKEY_id(pkey) == EVP_PKEY_ED25519) { |
| 45 |
return X509_ALGOR_set0(algor, OBJ_nid2obj(NID_ED25519), V_ASN1_UNDEF, NULL); |
| 46 |
} |
| 47 |
|
| 48 |
if (EVP_PKEY_id(pkey) == EVP_PKEY_PQDSA) { |
| 49 |
return X509_ALGOR_set0(algor, OBJ_nid2obj(pkey->pkey.pqdsa_key->pqdsa->nid), V_ASN1_UNDEF, NULL); |
| 50 |
} |
| 51 |
|
| 52 |
// Default behavior: look up the OID for the algorithm/hash pair and encode |
| 53 |
// that. |
| 54 |
const EVP_MD *digest = EVP_MD_CTX_md(ctx); |
| 55 |
if (digest == NULL) { |
| 56 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_CONTEXT_NOT_INITIALISED); |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
const int digest_nid = EVP_MD_type(digest); |
| 61 |
int sign_nid; |
| 62 |
if (!x509_digest_nid_ok(digest_nid) || |
| 63 |
!OBJ_find_sigid_by_algs(&sign_nid, digest_nid, EVP_PKEY_id(pkey))) { |
| 64 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED); |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
// RSA signature algorithms include an explicit NULL parameter. Others omit |
| 69 |
// it. |
| 70 |
int paramtype = |
| 71 |
(EVP_PKEY_id(pkey) == EVP_PKEY_RSA) ? V_ASN1_NULL : V_ASN1_UNDEF; |
| 72 |
return X509_ALGOR_set0(algor, OBJ_nid2obj(sign_nid), paramtype, NULL); |
| 73 |
} |
| 74 |
|
| 75 |
int x509_digest_verify_init(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg, |
| 76 |
EVP_PKEY *pkey) { |
| 77 |
// Convert the signature OID into digest and public key OIDs. |
| 78 |
int sigalg_nid = OBJ_obj2nid(sigalg->algorithm); |
| 79 |
int digest_nid, pkey_nid; |
| 80 |
if (!OBJ_find_sigid_algs(sigalg_nid, &digest_nid, &pkey_nid)) { |
| 81 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM); |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
// Check the public key OID matches the public key type. |
| 86 |
// One special case is |EVP_PKEY_id(pkey)| may be |EVP_PKEY_RSA_PSS|. |
| 87 |
// This case happens when |OBJ_find_sigid_algs| returns |NID_rsaEncryption| |
| 88 |
// when |sigalg_nid| is |NID_rsassaPss|. |
| 89 |
if (pkey_nid != EVP_PKEY_id(pkey) && |
| 90 |
!(sigalg_nid == NID_rsassaPss && pkey_nid == NID_rsaEncryption && |
| 91 |
EVP_PKEY_id(pkey) == EVP_PKEY_RSA_PSS) && |
| 92 |
!(sigalg_nid == NID_MLDSA44 && pkey_nid == NID_MLDSA44 && |
| 93 |
EVP_PKEY_id(pkey) == EVP_PKEY_PQDSA) && |
| 94 |
!(sigalg_nid == NID_MLDSA65 && pkey_nid == NID_MLDSA65 && |
| 95 |
EVP_PKEY_id(pkey) == EVP_PKEY_PQDSA) && |
| 96 |
!(sigalg_nid == NID_MLDSA87 && pkey_nid == NID_MLDSA87 && |
| 97 |
EVP_PKEY_id(pkey) == EVP_PKEY_PQDSA)) { |
| 98 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_PUBLIC_KEY_TYPE); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
// Check for permitted digest algorithms |
| 103 |
if (!x509_digest_nid_ok(digest_nid)) { |
| 104 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_DIGEST_AND_KEY_TYPE_NOT_SUPPORTED); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
// NID_undef signals that there are custom parameters to set. |
| 109 |
if (digest_nid == NID_undef) { |
| 110 |
if (sigalg_nid == NID_rsassaPss) { |
| 111 |
return x509_rsa_pss_to_ctx(ctx, sigalg, pkey); |
| 112 |
} |
| 113 |
|
| 114 |
if (sigalg_nid == NID_ED25519 || sigalg_nid == NID_MLDSA44 || |
| 115 |
sigalg_nid == NID_MLDSA65 || sigalg_nid == NID_MLDSA87) { |
| 116 |
if (sigalg->parameter != NULL) { |
| 117 |
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PARAMETER); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
return EVP_DigestVerifyInit(ctx, NULL, NULL, NULL, pkey); |
| 121 |
} |
| 122 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM); |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
// The parameter should be an explicit NULL for RSA and omitted for ECDSA. For |
| 127 |
// compatibility, we allow either for both algorithms. See b/167375496. |
| 128 |
// |
| 129 |
// TODO(davidben): Chromium's verifier allows both forms for RSA, but enforces |
| 130 |
// ECDSA more strictly. Align with Chromium and add a flag for b/167375496. |
| 131 |
if (sigalg->parameter != NULL && sigalg->parameter->type != V_ASN1_NULL) { |
| 132 |
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_PARAMETER); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
// Otherwise, initialize with the digest from the OID. |
| 137 |
const EVP_MD *digest = EVP_get_digestbynid(digest_nid); |
| 138 |
if (digest == NULL) { |
| 139 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
return EVP_DigestVerifyInit(ctx, NULL, digest, NULL, pkey); |
| 144 |
} |