Coverage Report

Created: 2026-01-09 07:00

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