Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/evp/pmeth_check.c
Line
Count
Source
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
137k
{
30
137k
    EVP_KEYMGMT *keymgmt;
31
137k
    void *keydata;
32
33
137k
    if (evp_pkey_ctx_is_legacy(ctx))
34
0
        return -1;
35
36
137k
    keymgmt = ctx->keymgmt;
37
137k
    keydata = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
38
137k
        &keymgmt, ctx->propquery);
39
137k
    if (keydata == NULL) {
40
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
41
0
        return 0;
42
0
    }
43
44
137k
    return evp_keymgmt_validate(keymgmt, keydata, selection, checktype);
45
137k
}
46
47
static int evp_pkey_public_check_combined(EVP_PKEY_CTX *ctx, int checktype)
48
12.4k
{
49
12.4k
    EVP_PKEY *pkey = ctx->pkey;
50
12.4k
    int ok;
51
52
12.4k
    if (pkey == NULL) {
53
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
54
0
        return 0;
55
0
    }
56
57
12.4k
    if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
58
12.4k
             checktype))
59
12.4k
        != -1)
60
12.4k
        return ok;
61
62
12.4k
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
63
0
    return -2;
64
12.4k
}
65
66
int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
67
70.0k
{
68
70.0k
    return evp_pkey_public_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK);
69
70.0k
}
70
71
int EVP_PKEY_public_check_quick(EVP_PKEY_CTX *ctx)
72
0
{
73
0
    return evp_pkey_public_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_QUICK_CHECK);
74
0
}
75
76
static int evp_pkey_param_check_combined(EVP_PKEY_CTX *ctx, int checktype)
77
4.81k
{
78
4.81k
    EVP_PKEY *pkey = ctx->pkey;
79
4.81k
    int ok;
80
81
4.81k
    if (pkey == NULL) {
82
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
83
0
        return 0;
84
0
    }
85
86
4.81k
    if ((ok = try_provided_check(ctx,
87
4.81k
             OSSL_KEYMGMT_SELECT_ALL_PARAMETERS,
88
4.81k
             checktype))
89
4.81k
        != -1)
90
4.81k
        return ok;
91
92
4.81k
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
93
0
    return -2;
94
4.81k
}
95
96
int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
97
20.3k
{
98
20.3k
    return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK);
99
20.3k
}
100
101
int EVP_PKEY_param_check_quick(EVP_PKEY_CTX *ctx)
102
10.1k
{
103
10.1k
    return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_QUICK_CHECK);
104
10.1k
}
105
106
int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)
107
18.6k
{
108
18.6k
    EVP_PKEY *pkey = ctx->pkey;
109
18.6k
    int ok;
110
111
18.6k
    if (pkey == NULL) {
112
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
113
0
        return 0;
114
0
    }
115
116
18.6k
    if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
117
18.6k
             OSSL_KEYMGMT_VALIDATE_FULL_CHECK))
118
18.6k
        != -1)
119
18.6k
        return ok;
120
121
    /* not supported for legacy keys */
122
18.6k
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
123
0
    return -2;
124
18.6k
}
125
126
int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
127
0
{
128
0
    return EVP_PKEY_pairwise_check(ctx);
129
0
}
130
131
int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx)
132
3.03k
{
133
3.03k
    EVP_PKEY *pkey = ctx->pkey;
134
3.03k
    int ok;
135
136
3.03k
    if (pkey == NULL) {
137
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
138
0
        return 0;
139
0
    }
140
141
3.03k
    if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_KEYPAIR,
142
3.03k
             OSSL_KEYMGMT_VALIDATE_FULL_CHECK))
143
3.03k
        != -1)
144
3.03k
        return ok;
145
146
3.03k
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
147
0
    return -2;
148
3.03k
}