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