Coverage Report

Created: 2026-09-12 06:55

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
106k
{
30
106k
    EVP_KEYMGMT *keymgmt;
31
106k
    void *keydata;
32
33
106k
    if (evp_pkey_ctx_is_legacy(ctx))
34
0
        return -1;
35
36
106k
    keymgmt = ctx->keymgmt;
37
106k
    keydata = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
38
106k
        &keymgmt, ctx->propquery);
39
106k
    if (keydata == NULL) {
40
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
41
0
        return 0;
42
0
    }
43
44
106k
    return evp_keymgmt_validate(keymgmt, keydata, selection, checktype);
45
106k
}
46
47
static int evp_pkey_public_check_combined(EVP_PKEY_CTX *ctx, int checktype)
48
19.4k
{
49
19.4k
    EVP_PKEY *pkey = ctx->pkey;
50
19.4k
    int ok;
51
52
19.4k
    if (pkey == NULL) {
53
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
54
0
        return 0;
55
0
    }
56
57
19.4k
    if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
58
19.4k
             checktype))
59
19.4k
        != -1)
60
19.4k
        return ok;
61
62
19.4k
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
63
0
    return -2;
64
19.4k
}
65
66
int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
67
55.8k
{
68
55.8k
    return evp_pkey_public_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK);
69
55.8k
}
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
5.98k
{
78
5.98k
    EVP_PKEY *pkey = ctx->pkey;
79
5.98k
    int ok;
80
81
5.98k
    if (pkey == NULL) {
82
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
83
0
        return 0;
84
0
    }
85
86
5.98k
    if ((ok = try_provided_check(ctx,
87
5.98k
             OSSL_KEYMGMT_SELECT_ALL_PARAMETERS,
88
5.98k
             checktype))
89
5.98k
        != -1)
90
5.98k
        return ok;
91
92
5.98k
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
93
0
    return -2;
94
5.98k
}
95
96
int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
97
14.9k
{
98
14.9k
    return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK);
99
14.9k
}
100
101
int EVP_PKEY_param_check_quick(EVP_PKEY_CTX *ctx)
102
7.85k
{
103
7.85k
    return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_QUICK_CHECK);
104
7.85k
}
105
106
int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)
107
13.7k
{
108
13.7k
    EVP_PKEY *pkey = ctx->pkey;
109
13.7k
    int ok;
110
111
13.7k
    if (pkey == NULL) {
112
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
113
0
        return 0;
114
0
    }
115
116
13.7k
    if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
117
13.7k
             OSSL_KEYMGMT_VALIDATE_FULL_CHECK))
118
13.7k
        != -1)
119
13.7k
        return ok;
120
121
    /* not supported for legacy keys */
122
13.7k
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
123
0
    return -2;
124
13.7k
}
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
2.93k
{
133
2.93k
    EVP_PKEY *pkey = ctx->pkey;
134
2.93k
    int ok;
135
136
2.93k
    if (pkey == NULL) {
137
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
138
0
        return 0;
139
0
    }
140
141
2.93k
    if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_KEYPAIR,
142
2.93k
             OSSL_KEYMGMT_VALIDATE_FULL_CHECK))
143
2.93k
        != -1)
144
2.93k
        return ok;
145
146
2.93k
    ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
147
0
    return -2;
148
2.93k
}