Coverage Report

Created: 2026-02-14 07:20

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