2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2016-05-18 02:24:46 +08:00
|
|
|
* Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2006-04-11 21:28:52 +08:00
|
|
|
*
|
2018-12-06 20:40:06 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:24:46 +08:00
|
|
|
* 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 21:28:52 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2006-04-17 20:08:22 +08:00
|
|
|
#include <openssl/objects.h>
|
2006-04-11 21:28:52 +08:00
|
|
|
#include <openssl/evp.h>
|
2014-10-29 06:59:34 +08:00
|
|
|
#include "internal/bn_int.h"
|
2017-09-04 22:02:59 +08:00
|
|
|
#include "internal/asn1_int.h"
|
2015-03-24 06:57:47 +08:00
|
|
|
#include "internal/evp_int.h"
|
2006-04-11 21:28:52 +08:00
|
|
|
|
|
|
|
int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
|
2015-01-22 11:40:55 +08: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 21:28:52 +08:00
|
|
|
|
|
|
|
int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
|
2015-01-22 11:40:55 +08: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 22:31:55 +08:00
|
|
|
if (ppkey == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return -1;
|
|
|
|
|
2015-09-01 22:31:55 +08:00
|
|
|
if (*ppkey == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
*ppkey = EVP_PKEY_new();
|
|
|
|
|
2015-09-01 22:31:55 +08:00
|
|
|
if (*ppkey == NULL) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ret = ctx->pmeth->paramgen(ctx, *ppkey);
|
|
|
|
if (ret <= 0) {
|
|
|
|
EVP_PKEY_free(*ppkey);
|
|
|
|
*ppkey = NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2006-04-11 21:28:52 +08:00
|
|
|
|
|
|
|
int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
|
2015-01-22 11:40:55 +08: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 21:28:52 +08:00
|
|
|
|
|
|
|
int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
|
2015-01-22 11:40:55 +08: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 19:12:26 +08:00
|
|
|
if (ppkey == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return -1;
|
|
|
|
|
2015-10-30 19:12:26 +08:00
|
|
|
if (*ppkey == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
*ppkey = EVP_PKEY_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (*ppkey == NULL)
|
|
|
|
return -1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
ret = ctx->pmeth->keygen(ctx, *ppkey);
|
|
|
|
if (ret <= 0) {
|
|
|
|
EVP_PKEY_free(*ppkey);
|
|
|
|
*ppkey = NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2006-04-11 21:28:52 +08:00
|
|
|
|
|
|
|
void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
ctx->pkey_gencb = cb;
|
|
|
|
}
|
2006-04-11 21:28:52 +08:00
|
|
|
|
2006-06-01 01:34:14 +08:00
|
|
|
EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
return ctx->pkey_gencb;
|
|
|
|
}
|
2006-06-01 01:34:14 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
|
|
|
|
* callbacks.
|
2006-04-11 21:28:52 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int trans_cb(int a, int b, BN_GENCB *gcb)
|
2015-01-22 11:40:55 +08: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 21:28:52 +08:00
|
|
|
|
|
|
|
void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BN_GENCB_set(cb, trans_cb, ctx);
|
|
|
|
}
|
2006-04-11 21:28:52 +08:00
|
|
|
|
|
|
|
int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
|
2015-01-22 11:40:55 +08: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-12 01:20:40 +08:00
|
|
|
|
|
|
|
EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
|
2015-01-22 11:40:55 +08: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-11 02:06:56 +08:00
|
|
|
if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto merr;
|
|
|
|
if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
|
|
|
|
goto merr;
|
|
|
|
merr:
|
2015-03-28 22:54:15 +08:00
|
|
|
EVP_PKEY_CTX_free(mac_ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
return mac_key;
|
|
|
|
}
|
2017-09-04 22:02:59 +08: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-11-01 00:45:24 +08: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);
|
|
|
|
}
|