2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/cryptlib.h"
13 #include <openssl/objects.h>
14 #include <openssl/evp.h>
15 #include "crypto/bn.h"
16 #include "crypto/asn1.h"
17 #include "crypto/evp.h"
18 #include "evp_local.h"
20 int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
22 EVP_PKEY *pkey = ctx->pkey;
27 EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
31 keymgmt = pkey->pkeys[0].keymgmt;
32 key = pkey->pkeys[0].keydata;
34 if (key != NULL && keymgmt != NULL)
35 return evp_keymgmt_validate(keymgmt, key,
36 OSSL_KEYMGMT_SELECT_PUBLIC_KEY);
39 /* call customized public key check function first */
40 if (ctx->pmeth->public_check != NULL)
41 return ctx->pmeth->public_check(pkey);
43 /* use default public key check function in ameth */
44 if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
45 EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
46 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
50 return pkey->ameth->pkey_public_check(pkey);
53 int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
55 EVP_PKEY *pkey = ctx->pkey;
60 EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
64 keymgmt = pkey->pkeys[0].keymgmt;
65 key = pkey->pkeys[0].keydata;
67 if (key != NULL && keymgmt != NULL)
68 return evp_keymgmt_validate(keymgmt, key,
69 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
71 /* call customized param check function first */
72 if (ctx->pmeth->param_check != NULL)
73 return ctx->pmeth->param_check(pkey);
76 /* use default param check function in ameth */
77 if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
78 EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
79 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
83 return pkey->ameth->pkey_param_check(pkey);
86 int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx)
88 EVP_PKEY *pkey = ctx->pkey;
93 EVPerr(0, EVP_R_NO_KEY_SET);
97 keymgmt = pkey->pkeys[0].keymgmt;
98 key = pkey->pkeys[0].keydata;
100 if (key != NULL && keymgmt != NULL)
101 return evp_keymgmt_validate(keymgmt, key,
102 OSSL_KEYMGMT_SELECT_PRIVATE_KEY);
103 /* not supported for legacy keys */
107 int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx)
109 EVP_PKEY *pkey = ctx->pkey;
111 EVP_KEYMGMT *keymgmt;
114 EVPerr(0, EVP_R_NO_KEY_SET);
118 keymgmt = pkey->pkeys[0].keymgmt;
119 key = pkey->pkeys[0].keydata;
121 if (key != NULL && keymgmt != NULL)
122 return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_KEYPAIR);
123 /* not supported for legacy keys */
127 int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
129 EVP_PKEY *pkey = ctx->pkey;
131 EVP_KEYMGMT *keymgmt;
134 EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
138 keymgmt = pkey->pkeys[0].keymgmt;
139 key = pkey->pkeys[0].keydata;
141 if (key != NULL && keymgmt != NULL)
142 return evp_keymgmt_validate(keymgmt, key, OSSL_KEYMGMT_SELECT_ALL);
145 /* call customized check function first */
146 if (ctx->pmeth->check != NULL)
147 return ctx->pmeth->check(pkey);
149 /* use default check function in ameth */
150 if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
151 EVPerr(EVP_F_EVP_PKEY_CHECK,
152 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
156 return pkey->ameth->pkey_check(pkey);