2019-11-18 08:57:56 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2019-11-18 08:57:56 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (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
|
|
|
|
*/
|
|
|
|
|
2020-02-03 17:05:31 +08:00
|
|
|
/*
|
|
|
|
* DH low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
2020-06-21 07:21:19 +08:00
|
|
|
#include <openssl/core_dispatch.h>
|
2019-11-18 08:57:56 +08:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/dh.h>
|
|
|
|
#include <openssl/types.h>
|
|
|
|
#include <openssl/params.h>
|
|
|
|
#include "prov/bio.h"
|
|
|
|
#include "prov/implementations.h"
|
2020-05-06 19:29:57 +08:00
|
|
|
#include "prov/provider_ctx.h"
|
2019-11-18 08:57:56 +08:00
|
|
|
#include "serializer_local.h"
|
|
|
|
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_serializer_newctx_fn dh_pub_newctx;
|
|
|
|
static OSSL_FUNC_serializer_freectx_fn dh_pub_freectx;
|
|
|
|
static OSSL_FUNC_serializer_serialize_data_fn dh_pub_der_data;
|
|
|
|
static OSSL_FUNC_serializer_serialize_object_fn dh_pub_der;
|
|
|
|
static OSSL_FUNC_serializer_serialize_data_fn dh_pub_pem_data;
|
|
|
|
static OSSL_FUNC_serializer_serialize_object_fn dh_pub_pem;
|
2019-11-18 08:57:56 +08:00
|
|
|
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_serializer_serialize_data_fn dh_pub_print_data;
|
|
|
|
static OSSL_FUNC_serializer_serialize_object_fn dh_pub_print;
|
2019-11-18 08:57:56 +08:00
|
|
|
|
|
|
|
/* Public key : context */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There's no specific implementation context, so we use the provider context
|
|
|
|
*/
|
|
|
|
static void *dh_pub_newctx(void *provctx)
|
|
|
|
{
|
|
|
|
return provctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dh_pub_freectx(void *ctx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Public key : DER */
|
2020-05-06 19:29:57 +08:00
|
|
|
static int dh_pub_der_data(void *ctx, const OSSL_PARAM params[],
|
|
|
|
OSSL_CORE_BIO *out,
|
2019-11-18 08:57:56 +08:00
|
|
|
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
|
|
|
{
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
|
|
|
|
OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
|
|
|
|
OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
|
2019-11-18 08:57:56 +08:00
|
|
|
int ok = 0;
|
|
|
|
|
2020-02-03 23:36:24 +08:00
|
|
|
if (dh_import != NULL) {
|
|
|
|
DH *dh;
|
2019-11-18 08:57:56 +08:00
|
|
|
|
2020-02-03 23:36:24 +08:00
|
|
|
/* ctx == provctx */
|
|
|
|
if ((dh = dh_new(ctx)) != NULL
|
|
|
|
&& dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
|
|
|
|
&& dh_pub_der(ctx, dh, out, cb, cbarg))
|
|
|
|
ok = 1;
|
|
|
|
dh_free(dh);
|
2019-11-18 08:57:56 +08:00
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-05-06 19:29:57 +08:00
|
|
|
static int dh_pub_der(void *ctx, void *dh, OSSL_CORE_BIO *cout,
|
2019-11-18 08:57:56 +08:00
|
|
|
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
|
|
|
{
|
2020-05-06 19:29:57 +08:00
|
|
|
BIO *out = bio_new_from_core_bio(ctx, cout);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (out == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = ossl_prov_write_pub_der_from_obj(out, dh, EVP_PKEY_DH,
|
|
|
|
ossl_prov_prepare_dh_params,
|
|
|
|
ossl_prov_dh_pub_to_der);
|
|
|
|
BIO_free(out);
|
|
|
|
|
|
|
|
return ret;
|
2019-11-18 08:57:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Public key : PEM */
|
2020-05-06 19:29:57 +08:00
|
|
|
static int dh_pub_pem_data(void *ctx, const OSSL_PARAM params[],
|
|
|
|
OSSL_CORE_BIO *out,
|
|
|
|
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
2019-11-18 08:57:56 +08:00
|
|
|
{
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
|
|
|
|
OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
|
|
|
|
OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
|
2019-11-18 08:57:56 +08:00
|
|
|
int ok = 0;
|
|
|
|
|
2020-02-03 23:36:24 +08:00
|
|
|
if (dh_import != NULL) {
|
|
|
|
DH *dh;
|
2019-11-18 08:57:56 +08:00
|
|
|
|
2020-02-03 23:36:24 +08:00
|
|
|
/* ctx == provctx */
|
|
|
|
if ((dh = dh_new(ctx)) != NULL
|
|
|
|
&& dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
|
|
|
|
&& dh_pub_pem(ctx, dh, out, cb, cbarg))
|
|
|
|
ok = 1;
|
|
|
|
dh_free(dh);
|
2019-11-18 08:57:56 +08:00
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-05-06 19:29:57 +08:00
|
|
|
static int dh_pub_pem(void *ctx, void *dh, OSSL_CORE_BIO *cout,
|
2019-11-18 08:57:56 +08:00
|
|
|
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
|
|
|
{
|
2020-05-06 19:29:57 +08:00
|
|
|
BIO *out = bio_new_from_core_bio(ctx, cout);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (out == NULL)
|
|
|
|
return 0;
|
2019-11-18 08:57:56 +08:00
|
|
|
|
2020-05-06 19:29:57 +08:00
|
|
|
ret = ossl_prov_write_pub_pem_from_obj(out, dh, EVP_PKEY_DH,
|
|
|
|
ossl_prov_prepare_dh_params,
|
|
|
|
ossl_prov_dh_pub_to_der);
|
|
|
|
BIO_free(out);
|
|
|
|
|
|
|
|
return ret;
|
2019-11-18 08:57:56 +08:00
|
|
|
}
|
|
|
|
|
2020-05-06 19:29:57 +08:00
|
|
|
static int dh_pub_print_data(void *ctx, const OSSL_PARAM params[],
|
|
|
|
OSSL_CORE_BIO *out,
|
|
|
|
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
2019-11-18 08:57:56 +08:00
|
|
|
{
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new();
|
|
|
|
OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free();
|
|
|
|
OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import();
|
2019-11-18 08:57:56 +08:00
|
|
|
int ok = 0;
|
|
|
|
|
2020-02-03 23:36:24 +08:00
|
|
|
if (dh_import != NULL) {
|
|
|
|
DH *dh;
|
2019-11-18 08:57:56 +08:00
|
|
|
|
2020-02-03 23:36:24 +08:00
|
|
|
/* ctx == provctx */
|
|
|
|
if ((dh = dh_new(ctx)) != NULL
|
|
|
|
&& dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
|
|
|
|
&& dh_pub_print(ctx, dh, out, cb, cbarg))
|
|
|
|
ok = 1;
|
|
|
|
dh_free(dh);
|
2019-11-18 08:57:56 +08:00
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-05-06 19:29:57 +08:00
|
|
|
static int dh_pub_print(void *ctx, void *dh, OSSL_CORE_BIO *cout,
|
2019-11-18 08:57:56 +08:00
|
|
|
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
|
|
|
{
|
2020-05-06 19:29:57 +08:00
|
|
|
BIO *out = bio_new_from_core_bio(ctx, cout);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (out == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = ossl_prov_print_dh(out, dh, dh_print_pub);
|
|
|
|
BIO_free(out);
|
|
|
|
|
|
|
|
return ret;
|
2019-11-18 08:57:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const OSSL_DISPATCH dh_pub_der_serializer_functions[] = {
|
|
|
|
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx },
|
|
|
|
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx },
|
|
|
|
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_pub_der_data },
|
|
|
|
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_der },
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
const OSSL_DISPATCH dh_pub_pem_serializer_functions[] = {
|
|
|
|
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx },
|
|
|
|
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx },
|
|
|
|
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_pub_pem_data },
|
|
|
|
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_pem },
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
const OSSL_DISPATCH dh_pub_text_serializer_functions[] = {
|
|
|
|
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_pub_newctx },
|
|
|
|
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_pub_freectx },
|
|
|
|
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pub_print },
|
|
|
|
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
|
|
|
|
(void (*)(void))dh_pub_print_data },
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|