Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/evp/pmeth_check.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2006-2021 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 <stdlib.h>
12
#include "internal/cryptlib.h"
13
#include <openssl/objects.h>
14
#include <openssl/evp.h>
15
#include "crypto/bn.h"
16
#ifndef FIPS_MODULE
17
# include "crypto/asn1.h"
18
#endif
19
#include "crypto/evp.h"
20
#include "evp_local.h"
21
22
/*
23
 * Returns:
24
 *  1   True
25
 *  0   False
26
 * -1   Unsupported (use legacy path)
27
 */
28
static int try_provided_check(EVP_PKEY_CTX *ctx, int selection, int checktype)
29
63.4k
{
30
63.4k
    EVP_KEYMGMT *keymgmt;
31
63.4k
    void *keydata;
32
33
63.4k
    if (evp_pkey_ctx_is_legacy(ctx))
34
0
        return -1;
35
36
63.4k
    keymgmt = ctx->keymgmt;
37
63.4k
    keydata = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
38
63.4k
                                          &keymgmt, ctx->propquery);
39
63.4k
    if (keydata == NULL) {
40
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
41
0
        return 0;
42
0
    }
43
44
63.4k
    return evp_keymgmt_validate(keymgmt, keydata, selection, checktype);
45
63.4k
}
46
47
static int evp_pkey_public_check_combined(EVP_PKEY_CTX *ctx, int checktype)
48
34.6k
{
49
34.6k
    EVP_PKEY *pkey = ctx->pkey;
50
34.6k
    int ok;
51
52
34.6k
    if (pkey == NULL) {
53
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
54
0
        return 0;
55
0
    }
56
57
34.6k
    if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
58
34.6k
                                 checktype)) != -1)
59
34.6k
        return ok;
60
61
0
    if (pkey->type == EVP_PKEY_NONE)
62
0
        goto not_supported;
63
64
0
#ifndef FIPS_MODULE
65
    /* legacy */
66
    /* call customized public key check function first */
67
0
    if (ctx->pmeth->public_check != NULL)
68
0
        return ctx->pmeth->public_check(pkey);
69
70
    /* use default public key check function in ameth */
71
0
    if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL)
72
0
        goto not_supported;
73
74
0
    return pkey->ameth->pkey_public_check(pkey);
75
0
#endif
76
0
 not_supported:
77
0
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
78
0
    return -2;
79
0
}
80
81
int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
82
34.6k
{
83
34.6k
    return evp_pkey_public_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK);
84
34.6k
}
85
86
int EVP_PKEY_public_check_quick(EVP_PKEY_CTX *ctx)
87
0
{
88
0
    return evp_pkey_public_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_QUICK_CHECK);
89
0
}
90
91
static int evp_pkey_param_check_combined(EVP_PKEY_CTX *ctx, int checktype)
92
13.6k
{
93
13.6k
    EVP_PKEY *pkey = ctx->pkey;
94
13.6k
    int ok;
95
96
13.6k
    if (pkey == NULL) {
97
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
98
0
        return 0;
99
0
    }
100
101
13.6k
    if ((ok = try_provided_check(ctx,
102
13.6k
                                 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS,
103
13.6k
                                 checktype)) != -1)
104
13.6k
        return ok;
105
106
0
    if (pkey->type == EVP_PKEY_NONE)
107
0
        goto not_supported;
108
109
0
#ifndef FIPS_MODULE
110
    /* legacy */
111
    /* call customized param check function first */
112
0
    if (ctx->pmeth->param_check != NULL)
113
0
        return ctx->pmeth->param_check(pkey);
114
115
    /* use default param check function in ameth */
116
0
    if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL)
117
0
        goto not_supported;
118
119
0
    return pkey->ameth->pkey_param_check(pkey);
120
0
#endif
121
0
 not_supported:
122
0
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
123
0
    return -2;
124
0
}
125
126
int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
127
8.22k
{
128
8.22k
    return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK);
129
8.22k
}
130
131
int EVP_PKEY_param_check_quick(EVP_PKEY_CTX *ctx)
132
5.47k
{
133
5.47k
    return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_QUICK_CHECK);
134
5.47k
}
135
136
int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)
137
7.57k
{
138
7.57k
    EVP_PKEY *pkey = ctx->pkey;
139
7.57k
    int ok;
140
141
7.57k
    if (pkey == NULL) {
142
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
143
0
        return 0;
144
0
    }
145
146
7.57k
    if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
147
7.57k
                                 OSSL_KEYMGMT_VALIDATE_FULL_CHECK)) != -1)
148
7.57k
        return ok;
149
150
    /* not supported for legacy keys */
151
7.57k
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
152
0
    return -2;
153
7.57k
}
154
155
int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
156
0
{
157
0
    return EVP_PKEY_pairwise_check(ctx);
158
0
}
159
160
int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx)
161
7.57k
{
162
7.57k
    EVP_PKEY *pkey = ctx->pkey;
163
7.57k
    int ok;
164
165
7.57k
    if (pkey == NULL) {
166
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
167
0
        return 0;
168
0
    }
169
170
7.57k
    if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_KEYPAIR,
171
7.57k
                                 OSSL_KEYMGMT_VALIDATE_FULL_CHECK)) != -1)
172
7.57k
        return ok;
173
174
0
    if (pkey->type == EVP_PKEY_NONE)
175
0
        goto not_supported;
176
177
0
#ifndef FIPS_MODULE
178
    /* legacy */
179
    /* call customized check function first */
180
0
    if (ctx->pmeth->check != NULL)
181
0
        return ctx->pmeth->check(pkey);
182
183
    /* use default check function in ameth */
184
0
    if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL)
185
0
        goto not_supported;
186
187
0
    return pkey->ameth->pkey_check(pkey);
188
0
#endif
189
0
 not_supported:
190
0
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
191
0
    return -2;
192
0
}
193