2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:24:46 +00:00
|
|
|
* Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2006-04-11 13:28:52 +00:00
|
|
|
*
|
2016-05-17 18:24:46 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2006-04-11 13:28:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2006-04-17 12:08:22 +00:00
|
|
|
#include <openssl/objects.h>
|
2006-04-11 13:28:52 +00:00
|
|
|
#include <openssl/evp.h>
|
2019-09-27 21:57:58 +00:00
|
|
|
#include "crypto/bn.h"
|
|
|
|
#include "crypto/asn1.h"
|
|
|
|
#include "crypto/evp.h"
|
2006-04-11 13:28:52 +00:00
|
|
|
|
|
|
|
int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT,
|
|
|
|
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
ctx->operation = EVP_PKEY_OP_PARAMGEN;
|
|
|
|
if (!ctx->pmeth->paramgen_init)
|
|
|
|
return 1;
|
|
|
|
ret = ctx->pmeth->paramgen_init(ctx);
|
|
|
|
if (ret <= 0)
|
|
|
|
ctx->operation = EVP_PKEY_OP_UNDEFINED;
|
|
|
|
return ret;
|
|
|
|
}
|
2006-04-11 13:28:52 +00:00
|
|
|
|
|
|
|
int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_PARAMGEN,
|
|
|
|
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-01 14:31:55 +00:00
|
|
|
if (ppkey == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return -1;
|
|
|
|
|
2015-09-01 14:31:55 +00:00
|
|
|
if (*ppkey == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
*ppkey = EVP_PKEY_new();
|
|
|
|
|
2015-09-01 14:31:55 +00:00
|
|
|
if (*ppkey == NULL) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = ctx->pmeth->paramgen(ctx, *ppkey);
|
|
|
|
if (ret <= 0) {
|
|
|
|
EVP_PKEY_free(*ppkey);
|
|
|
|
*ppkey = NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2006-04-11 13:28:52 +00:00
|
|
|
|
|
|
|
int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT,
|
|
|
|
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
ctx->operation = EVP_PKEY_OP_KEYGEN;
|
|
|
|
if (!ctx->pmeth->keygen_init)
|
|
|
|
return 1;
|
|
|
|
ret = ctx->pmeth->keygen_init(ctx);
|
|
|
|
if (ret <= 0)
|
|
|
|
ctx->operation = EVP_PKEY_OP_UNDEFINED;
|
|
|
|
return ret;
|
|
|
|
}
|
2006-04-11 13:28:52 +00:00
|
|
|
|
|
|
|
int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_KEYGEN,
|
|
|
|
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-30 11:12:26 +00:00
|
|
|
if (ppkey == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return -1;
|
|
|
|
|
2015-10-30 11:12:26 +00:00
|
|
|
if (*ppkey == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
*ppkey = EVP_PKEY_new();
|
2015-10-30 11:12:26 +00:00
|
|
|
if (*ppkey == NULL)
|
|
|
|
return -1;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
ret = ctx->pmeth->keygen(ctx, *ppkey);
|
|
|
|
if (ret <= 0) {
|
|
|
|
EVP_PKEY_free(*ppkey);
|
|
|
|
*ppkey = NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2006-04-11 13:28:52 +00:00
|
|
|
|
|
|
|
void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
ctx->pkey_gencb = cb;
|
|
|
|
}
|
2006-04-11 13:28:52 +00:00
|
|
|
|
2006-05-31 17:34:14 +00:00
|
|
|
EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return ctx->pkey_gencb;
|
|
|
|
}
|
2006-05-31 17:34:14 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
|
|
|
|
* callbacks.
|
2006-04-11 13:28:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int trans_cb(int a, int b, BN_GENCB *gcb)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
|
|
|
|
ctx->keygen_info[0] = a;
|
|
|
|
ctx->keygen_info[1] = b;
|
|
|
|
return ctx->pkey_gencb(ctx);
|
|
|
|
}
|
2006-04-11 13:28:52 +00:00
|
|
|
|
|
|
|
void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
BN_GENCB_set(cb, trans_cb, ctx);
|
|
|
|
}
|
2006-04-11 13:28:52 +00:00
|
|
|
|
|
|
|
int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (idx == -1)
|
|
|
|
return ctx->keygen_info_count;
|
|
|
|
if (idx < 0 || idx > ctx->keygen_info_count)
|
|
|
|
return 0;
|
|
|
|
return ctx->keygen_info[idx];
|
|
|
|
}
|
2007-04-11 17:20:40 +00:00
|
|
|
|
|
|
|
EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *key, int keylen)
|
|
|
|
{
|
|
|
|
EVP_PKEY_CTX *mac_ctx = NULL;
|
|
|
|
EVP_PKEY *mac_key = NULL;
|
|
|
|
mac_ctx = EVP_PKEY_CTX_new_id(type, e);
|
|
|
|
if (!mac_ctx)
|
|
|
|
return NULL;
|
|
|
|
if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
|
|
|
|
goto merr;
|
2015-02-10 18:06:56 +00:00
|
|
|
if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto merr;
|
|
|
|
if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
|
|
|
|
goto merr;
|
|
|
|
merr:
|
2015-03-28 14:54:15 +00:00
|
|
|
EVP_PKEY_CTX_free(mac_ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
return mac_key;
|
|
|
|
}
|
2017-09-04 14:02:59 +00:00
|
|
|
|
|
|
|
int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey = ctx->pkey;
|
|
|
|
|
|
|
|
if (pkey == NULL) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* call customized check function first */
|
|
|
|
if (ctx->pmeth->check != NULL)
|
|
|
|
return ctx->pmeth->check(pkey);
|
|
|
|
|
|
|
|
/* use default check function in ameth */
|
|
|
|
if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_CHECK,
|
|
|
|
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkey->ameth->pkey_check(pkey);
|
|
|
|
}
|
2017-10-31 16:45:24 +00:00
|
|
|
|
|
|
|
int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey = ctx->pkey;
|
|
|
|
|
|
|
|
if (pkey == NULL) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* call customized public key check function first */
|
|
|
|
if (ctx->pmeth->public_check != NULL)
|
|
|
|
return ctx->pmeth->public_check(pkey);
|
|
|
|
|
|
|
|
/* use default public key check function in ameth */
|
|
|
|
if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
|
|
|
|
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkey->ameth->pkey_public_check(pkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey = ctx->pkey;
|
|
|
|
|
|
|
|
if (pkey == NULL) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* call customized param check function first */
|
|
|
|
if (ctx->pmeth->param_check != NULL)
|
|
|
|
return ctx->pmeth->param_check(pkey);
|
|
|
|
|
|
|
|
/* use default param check function in ameth */
|
|
|
|
if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
|
|
|
|
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkey->ameth->pkey_param_check(pkey);
|
|
|
|
}
|