2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <sys/types.h>
14 #include "internal/cryptlib.h"
16 #include <openssl/bn.h>
17 #include <openssl/x509.h>
18 #include <openssl/objects.h>
19 #include <openssl/buffer.h>
20 #include <openssl/evp.h>
21 #include "crypto/asn1.h"
22 #include "crypto/evp.h"
23 #include "crypto/rsa.h"
25 #ifndef OPENSSL_NO_DEPRECATED_3_0
27 int ASN1_verify(i2d_of_void
*i2d
, X509_ALGOR
*a
, ASN1_BIT_STRING
*signature
,
28 char *data
, EVP_PKEY
*pkey
)
30 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
32 unsigned char *p
, *buf_in
= NULL
;
36 ERR_raise(ERR_LIB_ASN1
, ERR_R_EVP_LIB
);
39 i
= OBJ_obj2nid(a
->algorithm
);
40 type
= EVP_get_digestbyname(OBJ_nid2sn(i
));
42 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM
);
46 if (signature
->type
== V_ASN1_BIT_STRING
&& signature
->flags
& 0x7) {
47 ERR_raise(ERR_LIB_ASN1
, ASN1_R_INVALID_BIT_STRING_BITS_LEFT
);
51 inl
= i2d(data
, NULL
);
53 ERR_raise(ERR_LIB_ASN1
, ERR_R_INTERNAL_ERROR
);
56 buf_in
= OPENSSL_malloc((unsigned int)inl
);
62 ret
= EVP_VerifyInit_ex(ctx
, type
, NULL
)
63 && EVP_VerifyUpdate(ctx
, (unsigned char *)buf_in
, inl
);
65 OPENSSL_clear_free(buf_in
, (unsigned int)inl
);
68 ERR_raise(ERR_LIB_ASN1
, ERR_R_EVP_LIB
);
73 if (EVP_VerifyFinal(ctx
, (unsigned char *)signature
->data
,
74 (unsigned int)signature
->length
, pkey
) <= 0) {
75 ERR_raise(ERR_LIB_ASN1
, ERR_R_EVP_LIB
);
87 int ASN1_item_verify(const ASN1_ITEM
*it
, const X509_ALGOR
*alg
,
88 const ASN1_BIT_STRING
*signature
, const void *data
,
91 return ASN1_item_verify_ex(it
, alg
, signature
, data
, NULL
, pkey
, NULL
, NULL
);
94 int ASN1_item_verify_ex(const ASN1_ITEM
*it
, const X509_ALGOR
*alg
,
95 const ASN1_BIT_STRING
*signature
, const void *data
,
96 const ASN1_OCTET_STRING
*id
, EVP_PKEY
*pkey
,
97 OSSL_LIB_CTX
*libctx
, const char *propq
)
102 if ((ctx
= evp_md_ctx_new_ex(pkey
, id
, libctx
, propq
)) != NULL
) {
103 rv
= ASN1_item_verify_ctx(it
, alg
, signature
, data
, ctx
);
104 EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx
));
105 EVP_MD_CTX_free(ctx
);
110 int ASN1_item_verify_ctx(const ASN1_ITEM
*it
, const X509_ALGOR
*alg
,
111 const ASN1_BIT_STRING
*signature
, const void *data
,
115 unsigned char *buf_in
= NULL
;
116 int ret
= -1, inl
= 0;
120 pkey
= EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx
));
123 ERR_raise(ERR_LIB_ASN1
, ERR_R_PASSED_NULL_PARAMETER
);
127 if (signature
->type
== V_ASN1_BIT_STRING
&& signature
->flags
& 0x7) {
128 ERR_raise(ERR_LIB_ASN1
, ASN1_R_INVALID_BIT_STRING_BITS_LEFT
);
132 /* Convert signature OID into digest and public key OIDs */
133 if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg
->algorithm
), &mdnid
, &pknid
)) {
134 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM
);
138 if (mdnid
== NID_undef
&& evp_pkey_is_legacy(pkey
)) {
139 if (pkey
->ameth
== NULL
|| pkey
->ameth
->item_verify
== NULL
) {
140 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM
);
143 ret
= pkey
->ameth
->item_verify(ctx
, it
, data
, alg
, signature
, pkey
);
145 * Return values meaning:
147 * 1: method does everything.
148 * 2: carry on as normal, method has called EVP_DigestVerifyInit()
151 ERR_raise(ERR_LIB_ASN1
, ERR_R_EVP_LIB
);
155 const EVP_MD
*type
= NULL
;
158 * We don't yet have the ability for providers to be able to handle
159 * X509_ALGOR style parameters. Fortunately the only one that needs this
160 * so far is RSA-PSS, so we just special case this for now. In some
161 * future version of OpenSSL we should push this to the provider.
163 if (mdnid
== NID_undef
&& pknid
== EVP_PKEY_RSA_PSS
) {
164 if (!EVP_PKEY_is_a(pkey
, "RSA") && !EVP_PKEY_is_a(pkey
, "RSA-PSS")) {
165 ERR_raise(ERR_LIB_ASN1
, ASN1_R_WRONG_PUBLIC_KEY_TYPE
);
168 /* This function also calls EVP_DigestVerifyInit */
169 if (ossl_rsa_pss_to_ctx(ctx
, NULL
, alg
, pkey
) <= 0) {
170 ERR_raise(ERR_LIB_ASN1
, ERR_R_INTERNAL_ERROR
);
174 /* Check public key OID matches public key type */
175 if (!EVP_PKEY_is_a(pkey
, OBJ_nid2sn(pknid
))) {
176 ERR_raise(ERR_LIB_ASN1
, ASN1_R_WRONG_PUBLIC_KEY_TYPE
);
180 if (mdnid
!= NID_undef
) {
181 type
= EVP_get_digestbynid(mdnid
);
183 ERR_raise_data(ERR_LIB_ASN1
,
184 ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM
,
191 * Note that some algorithms (notably Ed25519 and Ed448) may allow
192 * a NULL digest value.
194 if (!EVP_DigestVerifyInit(ctx
, NULL
, type
, NULL
, pkey
)) {
195 ERR_raise(ERR_LIB_ASN1
, ERR_R_EVP_LIB
);
202 inl
= ASN1_item_i2d(data
, &buf_in
, it
);
204 ERR_raise(ERR_LIB_ASN1
, ERR_R_INTERNAL_ERROR
);
207 if (buf_in
== NULL
) {
208 ERR_raise(ERR_LIB_ASN1
, ERR_R_ASN1_LIB
);
213 ret
= EVP_DigestVerify(ctx
, signature
->data
, (size_t)signature
->length
,
216 ERR_raise(ERR_LIB_ASN1
, ERR_R_EVP_LIB
);
221 OPENSSL_clear_free(buf_in
, inll
);