Coverage Report

Created: 2025-06-13 06:58

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