/src/openssl/crypto/asn1/a_verify.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include <time.h> |
12 | | #include <sys/types.h> |
13 | | |
14 | | #include "internal/cryptlib.h" |
15 | | |
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" |
24 | | |
25 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
26 | | |
27 | | int ASN1_verify(i2d_of_void *i2d, X509_ALGOR *a, ASN1_BIT_STRING *signature, |
28 | | char *data, EVP_PKEY *pkey) |
29 | 0 | { |
30 | 0 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
31 | 0 | EVP_MD *type = NULL; |
32 | 0 | unsigned char *p, *buf_in = NULL; |
33 | 0 | int ret = -1, i, inl; |
34 | |
|
35 | 0 | if (ctx == NULL) { |
36 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
37 | 0 | goto err; |
38 | 0 | } |
39 | 0 | i = OBJ_obj2nid(a->algorithm); |
40 | 0 | type = EVP_MD_fetch(NULL, OBJ_nid2sn(i), NULL); |
41 | 0 | if (type == NULL) { |
42 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM); |
43 | 0 | goto err; |
44 | 0 | } |
45 | | |
46 | 0 | if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) { |
47 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT); |
48 | 0 | goto err; |
49 | 0 | } |
50 | | |
51 | 0 | inl = i2d(data, NULL); |
52 | 0 | if (inl <= 0) { |
53 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR); |
54 | 0 | goto err; |
55 | 0 | } |
56 | 0 | buf_in = OPENSSL_malloc((unsigned int)inl); |
57 | 0 | if (buf_in == NULL) |
58 | 0 | goto err; |
59 | 0 | p = buf_in; |
60 | |
|
61 | 0 | i2d(data, &p); |
62 | 0 | ret = EVP_VerifyInit_ex(ctx, type, NULL) |
63 | 0 | && EVP_VerifyUpdate(ctx, (unsigned char *)buf_in, inl); |
64 | |
|
65 | 0 | OPENSSL_clear_free(buf_in, (unsigned int)inl); |
66 | |
|
67 | 0 | if (!ret) { |
68 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
69 | 0 | goto err; |
70 | 0 | } |
71 | 0 | ret = -1; |
72 | |
|
73 | 0 | if (EVP_VerifyFinal(ctx, (unsigned char *)signature->data, |
74 | 0 | (unsigned int)signature->length, pkey) |
75 | 0 | <= 0) { |
76 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
77 | 0 | ret = 0; |
78 | 0 | goto err; |
79 | 0 | } |
80 | 0 | ret = 1; |
81 | 0 | err: |
82 | 0 | EVP_MD_free(type); |
83 | 0 | EVP_MD_CTX_free(ctx); |
84 | 0 | return ret; |
85 | 0 | } |
86 | | |
87 | | #endif |
88 | | |
89 | | static int item_verify(const ASN1_ITEM *it, const X509_ALGOR *alg, |
90 | | const ASN1_BIT_STRING *signature, const void *data, |
91 | | EVP_MD_CTX *ctx, OSSL_LIB_CTX *libctx, const char *propq) |
92 | 0 | { |
93 | 0 | EVP_PKEY_CTX *pctx; |
94 | 0 | EVP_PKEY *pkey; |
95 | 0 | EVP_MD *type = NULL; |
96 | 0 | unsigned char *buf_in = NULL; |
97 | 0 | int ret = -1, inl = 0; |
98 | 0 | int mdnid, pknid; |
99 | 0 | size_t inll = 0; |
100 | |
|
101 | 0 | pctx = EVP_MD_CTX_get_pkey_ctx(ctx); |
102 | |
|
103 | 0 | if (pctx == NULL) { |
104 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_CONTEXT_NOT_INITIALISED); |
105 | 0 | return -1; |
106 | 0 | } |
107 | | |
108 | 0 | pkey = EVP_PKEY_CTX_get0_pkey(pctx); |
109 | |
|
110 | 0 | if (pkey == NULL) { |
111 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER); |
112 | 0 | return -1; |
113 | 0 | } |
114 | | |
115 | 0 | if (signature->type == V_ASN1_BIT_STRING && signature->flags & 0x7) { |
116 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BIT_STRING_BITS_LEFT); |
117 | 0 | return -1; |
118 | 0 | } |
119 | | |
120 | | /* Convert signature OID into digest and public key OIDs */ |
121 | 0 | if (!OBJ_find_sigid_algs(OBJ_obj2nid(alg->algorithm), &mdnid, &pknid)) { |
122 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM); |
123 | 0 | goto err; |
124 | 0 | } |
125 | | |
126 | 0 | if (mdnid == NID_undef && evp_pkey_is_legacy(pkey)) { |
127 | 0 | if (pkey->ameth == NULL || pkey->ameth->item_verify == NULL) { |
128 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_SIGNATURE_ALGORITHM); |
129 | 0 | goto err; |
130 | 0 | } |
131 | 0 | ret = pkey->ameth->item_verify(ctx, it, data, alg, signature, pkey); |
132 | | /* |
133 | | * Return values meaning: |
134 | | * <=0: error. |
135 | | * 1: method does everything. |
136 | | * 2: carry on as normal, method has called EVP_DigestVerifyInit() |
137 | | */ |
138 | 0 | if (ret <= 0) |
139 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
140 | 0 | if (ret <= 1) |
141 | 0 | goto err; |
142 | 0 | } else { |
143 | | /* |
144 | | * We don't yet have the ability for providers to be able to handle |
145 | | * X509_ALGOR style parameters. Fortunately the only one that needs this |
146 | | * so far is RSA-PSS, so we just special case this for now. In some |
147 | | * future version of OpenSSL we should push this to the provider. |
148 | | */ |
149 | 0 | if (mdnid == NID_undef && pknid == EVP_PKEY_RSA_PSS) { |
150 | 0 | if (!EVP_PKEY_is_a(pkey, "RSA") && !EVP_PKEY_is_a(pkey, "RSA-PSS")) { |
151 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_PUBLIC_KEY_TYPE); |
152 | 0 | goto err; |
153 | 0 | } |
154 | | /* This function also calls EVP_DigestVerifyInit */ |
155 | 0 | if (ossl_rsa_pss_to_ctx(ctx, NULL, alg, pkey) <= 0) { |
156 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR); |
157 | 0 | goto err; |
158 | 0 | } |
159 | 0 | } else { |
160 | | /* Check public key OID matches public key type */ |
161 | 0 | if (!EVP_PKEY_is_a(pkey, OBJ_nid2sn(pknid))) { |
162 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_PUBLIC_KEY_TYPE); |
163 | 0 | goto err; |
164 | 0 | } |
165 | | |
166 | 0 | if (mdnid != NID_undef) { |
167 | 0 | type = EVP_MD_fetch(libctx, OBJ_nid2sn(mdnid), propq); |
168 | 0 | if (type == NULL) { |
169 | 0 | ERR_raise_data(ERR_LIB_ASN1, |
170 | 0 | ASN1_R_UNKNOWN_MESSAGE_DIGEST_ALGORITHM, |
171 | 0 | "nid=0x%x", mdnid); |
172 | 0 | goto err; |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | | /* |
177 | | * Note that some algorithms (notably Ed25519 and Ed448) may allow |
178 | | * a NULL digest value. |
179 | | */ |
180 | 0 | if (!EVP_DigestVerifyInit(ctx, NULL, type, NULL, pkey)) { |
181 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
182 | 0 | ret = 0; |
183 | 0 | goto err; |
184 | 0 | } |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | 0 | inl = ASN1_item_i2d(data, &buf_in, it); |
189 | 0 | if (inl <= 0) { |
190 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR); |
191 | 0 | ret = -1; |
192 | 0 | goto err; |
193 | 0 | } |
194 | 0 | if (buf_in == NULL) { |
195 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
196 | 0 | ret = -1; |
197 | 0 | goto err; |
198 | 0 | } |
199 | 0 | inll = inl; |
200 | |
|
201 | 0 | ret = EVP_DigestVerify(ctx, signature->data, (size_t)signature->length, |
202 | 0 | buf_in, inl); |
203 | 0 | if (ret <= 0) { |
204 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB); |
205 | 0 | goto err; |
206 | 0 | } |
207 | 0 | ret = 1; |
208 | 0 | err: |
209 | 0 | EVP_MD_free(type); |
210 | 0 | OPENSSL_clear_free(buf_in, inll); |
211 | 0 | return ret; |
212 | 0 | } |
213 | | |
214 | | int ASN1_item_verify(const ASN1_ITEM *it, const X509_ALGOR *alg, |
215 | | const ASN1_BIT_STRING *signature, const void *data, |
216 | | EVP_PKEY *pkey) |
217 | 0 | { |
218 | 0 | return ASN1_item_verify_ex(it, alg, signature, data, NULL, pkey, NULL, NULL); |
219 | 0 | } |
220 | | |
221 | | int ASN1_item_verify_ex(const ASN1_ITEM *it, const X509_ALGOR *alg, |
222 | | const ASN1_BIT_STRING *signature, const void *data, |
223 | | const ASN1_OCTET_STRING *id, EVP_PKEY *pkey, |
224 | | OSSL_LIB_CTX *libctx, const char *propq) |
225 | 0 | { |
226 | 0 | EVP_MD_CTX *ctx; |
227 | 0 | int rv = -1; |
228 | |
|
229 | 0 | if ((ctx = evp_md_ctx_new_ex(pkey, id, libctx, propq)) != NULL) { |
230 | 0 | rv = item_verify(it, alg, signature, data, ctx, libctx, propq); |
231 | 0 | EVP_PKEY_CTX_free(EVP_MD_CTX_get_pkey_ctx(ctx)); |
232 | 0 | EVP_MD_CTX_free(ctx); |
233 | 0 | } |
234 | 0 | return rv; |
235 | 0 | } |
236 | | |
237 | | int ASN1_item_verify_ctx(const ASN1_ITEM *it, const X509_ALGOR *alg, |
238 | | const ASN1_BIT_STRING *signature, const void *data, |
239 | | EVP_MD_CTX *ctx) |
240 | 0 | { |
241 | 0 | return item_verify(it, alg, signature, data, ctx, NULL, NULL); |
242 | 0 | } |