Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/evp/pmeth_check.c

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