2 * Copyright 2006-2016 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 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
22 if (ctx == NULL || ctx->algorithm == NULL)
25 evp_pkey_ctx_free_old_ops(ctx);
26 ctx->operation = operation;
27 if (ctx->keymgmt == NULL)
28 ctx->keymgmt = EVP_KEYMGMT_fetch(NULL, ctx->algorithm, ctx->propquery);
29 if (ctx->keymgmt == NULL)
35 ctx->operation = EVP_PKEY_OP_UNDEFINED;
36 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
40 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
42 return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
45 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
47 return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
50 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
52 void *provdata = NULL;
54 if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
55 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
63 *ppkey = EVP_PKEY_new();
66 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
71 evp_keymgmt_fromdata(*ppkey, ctx->keymgmt, params,
72 ctx->operation == EVP_PKEY_OP_PARAMFROMDATA);
76 /* provdata is cached in *ppkey, so we need not bother with it further */
81 * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
84 * EVP_PKEY_param_settable()
85 * EVP_PKEY_param_gettable()
87 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
89 /* We call fromdata_init to get ctx->keymgmt populated */
90 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
91 return evp_keymgmt_importdomparam_types(ctx->keymgmt);
95 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
97 /* We call fromdata_init to get ctx->keymgmt populated */
98 if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
99 return evp_keymgmt_importdomparam_types(ctx->keymgmt);
103 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
106 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
107 EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
108 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
111 ctx->operation = EVP_PKEY_OP_PARAMGEN;
112 if (!ctx->pmeth->paramgen_init)
114 ret = ctx->pmeth->paramgen_init(ctx);
116 ctx->operation = EVP_PKEY_OP_UNDEFINED;
120 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
123 if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
124 EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
125 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
129 if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
130 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
138 *ppkey = EVP_PKEY_new();
140 if (*ppkey == NULL) {
141 EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
145 ret = ctx->pmeth->paramgen(ctx, *ppkey);
147 EVP_PKEY_free(*ppkey);
153 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
156 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
157 EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
158 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
161 ctx->operation = EVP_PKEY_OP_KEYGEN;
162 if (!ctx->pmeth->keygen_init)
164 ret = ctx->pmeth->keygen_init(ctx);
166 ctx->operation = EVP_PKEY_OP_UNDEFINED;
170 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
174 if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
175 EVPerr(EVP_F_EVP_PKEY_KEYGEN,
176 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
179 if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
180 EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
188 *ppkey = EVP_PKEY_new();
192 ret = ctx->pmeth->keygen(ctx, *ppkey);
194 EVP_PKEY_free(*ppkey);
200 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
202 ctx->pkey_gencb = cb;
205 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
207 return ctx->pkey_gencb;
211 * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
215 static int trans_cb(int a, int b, BN_GENCB *gcb)
217 EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
218 ctx->keygen_info[0] = a;
219 ctx->keygen_info[1] = b;
220 return ctx->pkey_gencb(ctx);
223 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
225 BN_GENCB_set(cb, trans_cb, ctx);
228 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
231 return ctx->keygen_info_count;
232 if (idx < 0 || idx > ctx->keygen_info_count)
234 return ctx->keygen_info[idx];
237 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
238 const unsigned char *key, int keylen)
240 EVP_PKEY_CTX *mac_ctx = NULL;
241 EVP_PKEY *mac_key = NULL;
242 mac_ctx = EVP_PKEY_CTX_new_id(type, e);
245 if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
247 if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
249 if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
252 EVP_PKEY_CTX_free(mac_ctx);
256 int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
258 EVP_PKEY *pkey = ctx->pkey;
261 EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
265 /* call customized check function first */
266 if (ctx->pmeth->check != NULL)
267 return ctx->pmeth->check(pkey);
269 /* use default check function in ameth */
270 if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
271 EVPerr(EVP_F_EVP_PKEY_CHECK,
272 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
276 return pkey->ameth->pkey_check(pkey);
279 int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
281 EVP_PKEY *pkey = ctx->pkey;
284 EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
288 /* call customized public key check function first */
289 if (ctx->pmeth->public_check != NULL)
290 return ctx->pmeth->public_check(pkey);
292 /* use default public key check function in ameth */
293 if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
294 EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
295 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
299 return pkey->ameth->pkey_public_check(pkey);
302 int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
304 EVP_PKEY *pkey = ctx->pkey;
307 EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
311 /* call customized param check function first */
312 if (ctx->pmeth->param_check != NULL)
313 return ctx->pmeth->param_check(pkey);
315 /* use default param check function in ameth */
316 if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
317 EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
318 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
322 return pkey->ameth->pkey_param_check(pkey);