Latest update.
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
# We make separate GOAL variables for each algorithm, to make it easy to
|
||||
# switch each to the Legacy provider when needed.
|
||||
|
||||
$SERIALIZER_GOAL=../../libimplementations.a
|
||||
$RSA_GOAL=../../libimplementations.a
|
||||
$DH_GOAL=../../libimplementations.a
|
||||
$DSA_GOAL=../../libimplementations.a
|
||||
|
||||
SOURCE[$SERIALIZER_GOAL]=serializer_common.c
|
||||
SOURCE[$RSA_GOAL]=serializer_rsa.c serializer_rsa_priv.c serializer_rsa_pub.c
|
||||
IF[{- !$disabled{dh} -}]
|
||||
SOURCE[$DH_GOAL]=serializer_dh.c serializer_dh_priv.c serializer_dh_pub.c serializer_dh_param.c
|
||||
ENDIF
|
||||
IF[{- !$disabled{dsa} -}]
|
||||
SOURCE[$DSA_GOAL]=serializer_dsa.c serializer_dsa_priv.c serializer_dsa_pub.c serializer_dsa_param.c
|
||||
ENDIF
|
||||
@@ -0,0 +1,339 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/opensslconf.h> /* SIXTY_FOUR_BIT_LONG, ... */
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/pem.h> /* PEM_BUFSIZE */
|
||||
#include <openssl/pkcs12.h> /* PKCS8_encrypt() */
|
||||
#include <openssl/types.h>
|
||||
#include <openssl/x509.h> /* i2d_X509_PUBKEY_bio() */
|
||||
#include "crypto/bn.h" /* bn_get_words() */
|
||||
#include "prov/bio.h" /* ossl_prov_bio_printf() */
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommonerr.h" /* PROV_R_READ_KEY */
|
||||
#include "serializer_local.h"
|
||||
|
||||
static PKCS8_PRIV_KEY_INFO *
|
||||
ossl_prov_p8info_from_obj(const void *obj, int obj_nid,
|
||||
ASN1_STRING *params,
|
||||
int params_type,
|
||||
int (*k2d)(const void *obj,
|
||||
unsigned char **pder))
|
||||
{
|
||||
/* der, derlen store the key DER output and its length */
|
||||
unsigned char *der = NULL;
|
||||
int derlen;
|
||||
/* The final PKCS#8 info */
|
||||
PKCS8_PRIV_KEY_INFO *p8info = NULL;
|
||||
|
||||
|
||||
if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL
|
||||
|| (derlen = k2d(obj, &der)) <= 0
|
||||
|| !PKCS8_pkey_set0(p8info, OBJ_nid2obj(obj_nid), 0,
|
||||
params_type, params, der, derlen)) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
PKCS8_PRIV_KEY_INFO_free(p8info);
|
||||
OPENSSL_free(der);
|
||||
p8info = NULL;
|
||||
}
|
||||
|
||||
return p8info;
|
||||
}
|
||||
|
||||
static X509_SIG *ossl_prov_encp8_from_p8info(PKCS8_PRIV_KEY_INFO *p8info,
|
||||
struct pkcs8_encrypt_ctx_st *ctx)
|
||||
{
|
||||
X509_SIG *p8 = NULL;
|
||||
char buf[PEM_BUFSIZE];
|
||||
const void *kstr = ctx->cipher_pass;
|
||||
size_t klen = ctx->cipher_pass_length;
|
||||
|
||||
if (ctx->cipher == NULL)
|
||||
return NULL;
|
||||
|
||||
if (kstr == NULL) {
|
||||
if (!ctx->cb(buf, sizeof(buf), &klen, NULL, ctx->cbarg)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_READ_KEY);
|
||||
return NULL;
|
||||
}
|
||||
kstr = buf;
|
||||
}
|
||||
/* NID == -1 means "standard" */
|
||||
p8 = PKCS8_encrypt(-1, ctx->cipher, kstr, klen, NULL, 0, 0, p8info);
|
||||
if (kstr == buf)
|
||||
OPENSSL_cleanse(buf, klen);
|
||||
return p8;
|
||||
}
|
||||
|
||||
static X509_SIG *ossl_prov_encp8_from_obj(const void *obj, int obj_nid,
|
||||
ASN1_STRING *params,
|
||||
int params_type,
|
||||
int (*k2d)(const void *obj,
|
||||
unsigned char **pder),
|
||||
struct pkcs8_encrypt_ctx_st *ctx)
|
||||
{
|
||||
PKCS8_PRIV_KEY_INFO *p8info =
|
||||
ossl_prov_p8info_from_obj(obj, obj_nid, params, params_type, k2d);
|
||||
X509_SIG *p8 = ossl_prov_encp8_from_p8info(p8info, ctx);
|
||||
|
||||
PKCS8_PRIV_KEY_INFO_free(p8info);
|
||||
return p8;
|
||||
}
|
||||
|
||||
static X509_PUBKEY *ossl_prov_pubkey_from_obj(const void *obj, int obj_nid,
|
||||
ASN1_STRING *params,
|
||||
int params_type,
|
||||
int (*k2d)(const void *obj,
|
||||
unsigned char **pder))
|
||||
{
|
||||
/* der, derlen store the key DER output and its length */
|
||||
unsigned char *der = NULL;
|
||||
int derlen;
|
||||
/* The final X509_PUBKEY */
|
||||
X509_PUBKEY *xpk = NULL;
|
||||
|
||||
|
||||
if ((xpk = X509_PUBKEY_new()) == NULL
|
||||
|| (derlen = k2d(obj, &der)) <= 0
|
||||
|| !X509_PUBKEY_set0_param(xpk, OBJ_nid2obj(obj_nid),
|
||||
params_type, params, der, derlen)) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
X509_PUBKEY_free(xpk);
|
||||
OPENSSL_free(der);
|
||||
xpk = NULL;
|
||||
}
|
||||
|
||||
return xpk;
|
||||
}
|
||||
|
||||
OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_importkey(const OSSL_DISPATCH *fns)
|
||||
{
|
||||
/* Pilfer the keymgmt dispatch table */
|
||||
for (; fns->function_id != 0; fns++)
|
||||
if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORTKEY)
|
||||
return OSSL_get_OP_keymgmt_importkey(fns);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
# ifdef SIXTY_FOUR_BIT_LONG
|
||||
# define BN_FMTu "%lu"
|
||||
# define BN_FMTx "%lx"
|
||||
# endif
|
||||
|
||||
# ifdef SIXTY_FOUR_BIT
|
||||
# define BN_FMTu "%llu"
|
||||
# define BN_FMTx "%llx"
|
||||
# endif
|
||||
|
||||
# ifdef THIRTY_TWO_BIT
|
||||
# define BN_FMTu "%u"
|
||||
# define BN_FMTx "%x"
|
||||
# endif
|
||||
|
||||
int ossl_prov_print_labeled_bignum(BIO *out, const char *label,
|
||||
const BIGNUM *n)
|
||||
{
|
||||
const char *neg;
|
||||
const char *post_label_spc = " ";
|
||||
int bytes;
|
||||
BN_ULONG *words;
|
||||
int i, off;
|
||||
|
||||
if (n == NULL)
|
||||
return 0;
|
||||
if (label == NULL) {
|
||||
label = "";
|
||||
post_label_spc = "";
|
||||
}
|
||||
|
||||
bytes = BN_num_bytes(n);
|
||||
words = bn_get_words(n);
|
||||
neg = BN_is_negative(n) ? "-" : "";
|
||||
|
||||
if (BN_is_zero(n))
|
||||
return ossl_prov_bio_printf(out, "%s%s0\n", label, post_label_spc);
|
||||
|
||||
if (BN_num_bytes(n) <= BN_BYTES)
|
||||
return ossl_prov_bio_printf(out,
|
||||
"%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n",
|
||||
label, post_label_spc, neg, words[0],
|
||||
neg, words[0]);
|
||||
|
||||
if (neg[0] == '-')
|
||||
neg = " (Negative)";
|
||||
|
||||
if (ossl_prov_bio_printf(out, "%s%s\n", label, neg) <= 0)
|
||||
return 0;
|
||||
|
||||
/* Skip past the zero bytes in the first word */
|
||||
for (off = 0; off < BN_BYTES; off++) {
|
||||
BN_ULONG l = words[0];
|
||||
int o = 8 * (BN_BYTES - off - 1);
|
||||
int b = ((l & (0xffLU << o)) >> o) & 0xff;
|
||||
|
||||
if (b != 0)
|
||||
break;
|
||||
}
|
||||
/* print 16 hex digits per line, indented with 4 spaces */
|
||||
for (i = 0; i < bytes; i += 16) {
|
||||
int j, step;
|
||||
|
||||
if (ossl_prov_bio_printf(out, " ") <= 0)
|
||||
return 0;
|
||||
|
||||
for (j = 0; i + j < bytes && j < 16; j += BN_BYTES - step) {
|
||||
int k;
|
||||
BN_ULONG l = words[(i + j + off) / BN_BYTES];
|
||||
|
||||
step = (i + j + off) % BN_BYTES;
|
||||
|
||||
for (k = step; k < BN_BYTES; k++) {
|
||||
int o = 8 * (BN_BYTES - k - 1);
|
||||
int b = ((l & (0xffLU << o)) >> o) & 0xff;
|
||||
|
||||
/* We may have reached the number of bytes prematurely */
|
||||
if (i + j + k - off >= bytes)
|
||||
break;
|
||||
|
||||
if (ossl_prov_bio_printf(out, "%02x:", b) <= 0)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (ossl_prov_bio_printf(out, "\n") <= 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* p2s = param to asn1_string, k2d = key to der */
|
||||
int ossl_prov_write_priv_der_from_obj(BIO *out, const void *obj, int obj_nid,
|
||||
int (*p2s)(const void *obj, int nid,
|
||||
ASN1_STRING **str,
|
||||
int *strtype),
|
||||
int (*k2d)(const void *obj,
|
||||
unsigned char **pder),
|
||||
struct pkcs8_encrypt_ctx_st *ctx)
|
||||
{
|
||||
int ret = 0;
|
||||
ASN1_STRING *str = NULL;
|
||||
int strtype = 0;
|
||||
|
||||
if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
|
||||
return 0;
|
||||
|
||||
if (ctx->cipher_intent) {
|
||||
X509_SIG *p8 =
|
||||
ossl_prov_encp8_from_obj(obj, obj_nid, str, strtype, k2d, ctx);
|
||||
|
||||
if (p8 != NULL)
|
||||
ret = i2d_PKCS8_bio(out, p8);
|
||||
|
||||
X509_SIG_free(p8);
|
||||
} else {
|
||||
PKCS8_PRIV_KEY_INFO *p8info =
|
||||
ossl_prov_p8info_from_obj(obj, obj_nid, str, strtype, k2d);
|
||||
|
||||
if (p8info != NULL)
|
||||
ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info);
|
||||
|
||||
PKCS8_PRIV_KEY_INFO_free(p8info);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ossl_prov_write_priv_pem_from_obj(BIO *out, const void *obj, int obj_nid,
|
||||
int (*p2s)(const void *obj, int nid,
|
||||
ASN1_STRING **str,
|
||||
int *strtype),
|
||||
int (*k2d)(const void *obj,
|
||||
unsigned char **pder),
|
||||
struct pkcs8_encrypt_ctx_st *ctx)
|
||||
{
|
||||
int ret = 0;
|
||||
ASN1_STRING *str = NULL;
|
||||
int strtype = 0;
|
||||
|
||||
if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
|
||||
return 0;
|
||||
|
||||
if (ctx->cipher_intent) {
|
||||
X509_SIG *p8 = ossl_prov_encp8_from_obj(obj, obj_nid, str, strtype,
|
||||
k2d, ctx);
|
||||
|
||||
if (p8 != NULL)
|
||||
ret = PEM_write_bio_PKCS8(out, p8);
|
||||
|
||||
X509_SIG_free(p8);
|
||||
} else {
|
||||
PKCS8_PRIV_KEY_INFO *p8info =
|
||||
ossl_prov_p8info_from_obj(obj, obj_nid, str, strtype, k2d);
|
||||
|
||||
if (p8info != NULL)
|
||||
ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info);
|
||||
|
||||
PKCS8_PRIV_KEY_INFO_free(p8info);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ossl_prov_write_pub_der_from_obj(BIO *out, const void *obj, int obj_nid,
|
||||
int (*p2s)(const void *obj, int nid,
|
||||
ASN1_STRING **str,
|
||||
int *strtype),
|
||||
int (*k2d)(const void *obj,
|
||||
unsigned char **pder))
|
||||
{
|
||||
int ret = 0;
|
||||
ASN1_STRING *str = NULL;
|
||||
int strtype = 0;
|
||||
X509_PUBKEY *xpk = NULL;
|
||||
|
||||
if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
|
||||
return 0;
|
||||
|
||||
xpk = ossl_prov_pubkey_from_obj(obj, obj_nid, str, strtype, k2d);
|
||||
|
||||
if (xpk != NULL)
|
||||
ret = i2d_X509_PUBKEY_bio(out, xpk);
|
||||
|
||||
/* Also frees |str| */
|
||||
X509_PUBKEY_free(xpk);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ossl_prov_write_pub_pem_from_obj(BIO *out, const void *obj, int obj_nid,
|
||||
int (*p2s)(const void *obj, int nid,
|
||||
ASN1_STRING **str,
|
||||
int *strtype),
|
||||
int (*k2d)(const void *obj,
|
||||
unsigned char **pder))
|
||||
{
|
||||
int ret = 0;
|
||||
ASN1_STRING *str = NULL;
|
||||
int strtype = 0;
|
||||
X509_PUBKEY *xpk = NULL;
|
||||
|
||||
if (p2s != NULL && !p2s(obj, obj_nid, &str, &strtype))
|
||||
return 0;
|
||||
|
||||
xpk = ossl_prov_pubkey_from_obj(obj, obj_nid, str, strtype, k2d);
|
||||
|
||||
if (xpk != NULL)
|
||||
ret = PEM_write_bio_X509_PUBKEY(out, xpk);
|
||||
|
||||
/* Also frees |str| */
|
||||
X509_PUBKEY_free(xpk);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/dh.h>
|
||||
#include <openssl/err.h>
|
||||
#include "prov/bio.h" /* ossl_prov_bio_printf() */
|
||||
#include "prov/implementations.h" /* rsa_keymgmt_functions */
|
||||
#include "prov/providercommonerr.h" /* PROV_R_BN_ERROR */
|
||||
#include "serializer_local.h"
|
||||
|
||||
OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_dh_importkey(void)
|
||||
{
|
||||
return ossl_prov_get_importkey(dh_keymgmt_functions);
|
||||
}
|
||||
|
||||
int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type)
|
||||
{
|
||||
const char *type_label = NULL;
|
||||
const BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
const BIGNUM *p = NULL, *g = NULL;
|
||||
|
||||
|
||||
switch (type) {
|
||||
case dh_print_priv:
|
||||
type_label = "DH Private-Key";
|
||||
break;
|
||||
case dh_print_pub:
|
||||
type_label = "DH Public-Key";
|
||||
break;
|
||||
case dh_print_params:
|
||||
type_label = "DH Parameters";
|
||||
break;
|
||||
}
|
||||
|
||||
if (type == dh_print_priv) {
|
||||
priv_key = DH_get0_priv_key(dh);
|
||||
if (priv_key == NULL)
|
||||
goto null_err;
|
||||
}
|
||||
|
||||
if (type == dh_print_priv || type == dh_print_pub) {
|
||||
pub_key = DH_get0_pub_key(dh);
|
||||
if (pub_key == NULL)
|
||||
goto null_err;
|
||||
}
|
||||
|
||||
p = DH_get0_p(dh);
|
||||
g = DH_get0_p(dh);
|
||||
if (p == NULL || g == NULL)
|
||||
goto null_err;
|
||||
|
||||
/*
|
||||
* TODO(3.0): add printing of:
|
||||
*
|
||||
* - q (label "subgroup order:")
|
||||
* - j (label "subgroup factor:")
|
||||
* - seed (label "seed:")
|
||||
* - counter (label "counter:")
|
||||
*
|
||||
* This can happen as soon as there are DH_get0_ functions for them.
|
||||
*/
|
||||
|
||||
if (ossl_prov_bio_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p))
|
||||
<= 0)
|
||||
goto err;
|
||||
if (priv_key != NULL
|
||||
&& !ossl_prov_print_labeled_bignum(out, " private-key:", priv_key))
|
||||
goto err;
|
||||
if (pub_key != NULL
|
||||
&& !ossl_prov_print_labeled_bignum(out, " public-key:", pub_key))
|
||||
goto err;
|
||||
if (p != NULL
|
||||
&& !ossl_prov_print_labeled_bignum(out, " prime:", p))
|
||||
goto err;
|
||||
if (g != NULL
|
||||
&& !ossl_prov_print_labeled_bignum(out, " generator:", g))
|
||||
goto err;
|
||||
|
||||
return 1;
|
||||
err:
|
||||
return 0;
|
||||
null_err:
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
|
||||
goto err;
|
||||
}
|
||||
|
||||
int ossl_prov_prepare_dh_params(const void *dh, int nid,
|
||||
ASN1_STRING **pstr, int *pstrtype)
|
||||
{
|
||||
ASN1_STRING *params = ASN1_STRING_new();
|
||||
|
||||
if (params == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (nid == EVP_PKEY_DHX)
|
||||
params->length = i2d_DHxparams(dh, ¶ms->data);
|
||||
else
|
||||
params->length = i2d_DHparams(dh, ¶ms->data);
|
||||
|
||||
if (params->length <= 0) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
ASN1_STRING_free(params);
|
||||
return 0;
|
||||
}
|
||||
params->type = V_ASN1_SEQUENCE;
|
||||
|
||||
*pstr = params;
|
||||
*pstrtype = V_ASN1_SEQUENCE;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder)
|
||||
{
|
||||
ASN1_INTEGER *pub_key = BN_to_ASN1_INTEGER(DH_get0_pub_key(dh), NULL);
|
||||
int ret;
|
||||
|
||||
if (pub_key == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = i2d_ASN1_INTEGER(pub_key, pder);
|
||||
|
||||
ASN1_STRING_clear_free(pub_key);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder)
|
||||
{
|
||||
ASN1_INTEGER *priv_key = BN_to_ASN1_INTEGER(DH_get0_priv_key(dh), NULL);
|
||||
int ret;
|
||||
|
||||
if (priv_key == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = i2d_ASN1_INTEGER(priv_key, pder);
|
||||
|
||||
ASN1_STRING_clear_free(priv_key);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/core_numbers.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"
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "serializer_local.h"
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn dh_param_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn dh_param_freectx;
|
||||
static OSSL_OP_serializer_serialize_data_fn dh_param_der_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dh_param_der;
|
||||
static OSSL_OP_serializer_serialize_data_fn dh_param_pem_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dh_param_pem;
|
||||
|
||||
static OSSL_OP_serializer_serialize_data_fn dh_param_print_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dh_param_print;
|
||||
|
||||
/* Parameters : context */
|
||||
|
||||
/*
|
||||
* There's no specific implementation context, so we use the provider context
|
||||
*/
|
||||
static void *dh_param_newctx(void *provctx)
|
||||
{
|
||||
return provctx;
|
||||
}
|
||||
|
||||
static void dh_param_freectx(void *ctx)
|
||||
{
|
||||
}
|
||||
|
||||
/* Public key : DER */
|
||||
static int dh_param_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
|
||||
ossl_prov_get_dh_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dh_importkey != NULL) {
|
||||
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dh_param_der(ctx, dh, out, cb, cbarg);
|
||||
DH_free(dh);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dh_param_der(void *ctx, void *dh, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return i2d_DHparams_bio(out, dh);
|
||||
}
|
||||
|
||||
/* Public key : PEM */
|
||||
static int dh_param_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
|
||||
ossl_prov_get_dh_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dh_importkey != NULL) {
|
||||
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dh_param_pem(ctx, dh, out, cb, cbarg);
|
||||
DH_free(dh);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dh_param_pem(void *ctx, void *dh, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return PEM_write_bio_DHparams(out, dh);
|
||||
}
|
||||
|
||||
static int dh_param_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
|
||||
ossl_prov_get_dh_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dh_importkey != NULL) {
|
||||
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dh_param_print(ctx, dh, out, cb, cbarg);
|
||||
DH_free(dh);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dh_param_print(void *ctx, void *dh, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return ossl_prov_print_dh(out, dh, dh_print_params);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH dh_param_der_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_param_der_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_der },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH dh_param_pem_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_param_pem_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_pem },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH dh_param_text_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_param_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_param_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_param_print },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
|
||||
(void (*)(void))dh_param_print_data },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#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"
|
||||
#include "serializer_local.h"
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn dh_priv_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn dh_priv_freectx;
|
||||
static OSSL_OP_serializer_set_ctx_params_fn dh_priv_set_ctx_params;
|
||||
static OSSL_OP_serializer_settable_ctx_params_fn dh_priv_settable_ctx_params;
|
||||
static OSSL_OP_serializer_serialize_data_fn dh_priv_der_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dh_priv_der;
|
||||
static OSSL_OP_serializer_serialize_data_fn dh_pem_priv_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dh_pem_priv;
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn dh_print_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn dh_print_freectx;
|
||||
static OSSL_OP_serializer_serialize_data_fn dh_priv_print_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dh_priv_print;
|
||||
|
||||
/*
|
||||
* Context used for private key serialization.
|
||||
*/
|
||||
struct dh_priv_ctx_st {
|
||||
void *provctx;
|
||||
|
||||
struct pkcs8_encrypt_ctx_st sc;
|
||||
};
|
||||
|
||||
/* Private key : context */
|
||||
static void *dh_priv_newctx(void *provctx)
|
||||
{
|
||||
struct dh_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
|
||||
|
||||
if (ctx != NULL) {
|
||||
ctx->provctx = provctx;
|
||||
|
||||
/* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
|
||||
ctx->sc.pbe_nid = -1;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void dh_priv_freectx(void *vctx)
|
||||
{
|
||||
struct dh_priv_ctx_st *ctx = vctx;
|
||||
|
||||
EVP_CIPHER_free(ctx->sc.cipher);
|
||||
OPENSSL_free(ctx->sc.cipher_pass);
|
||||
OPENSSL_free(ctx);
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *dh_priv_settable_ctx_params(void)
|
||||
{
|
||||
static const OSSL_PARAM settables[] = {
|
||||
OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0),
|
||||
OSSL_PARAM_END,
|
||||
};
|
||||
|
||||
return settables;
|
||||
}
|
||||
|
||||
static int dh_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
||||
{
|
||||
struct dh_priv_ctx_st *ctx = vctx;
|
||||
const OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER))
|
||||
!= NULL) {
|
||||
const OSSL_PARAM *propsp =
|
||||
OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
|
||||
const char *props = NULL;
|
||||
|
||||
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
||||
return 0;
|
||||
if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING)
|
||||
return 0;
|
||||
props = (propsp != NULL ? propsp->data : NULL);
|
||||
|
||||
EVP_CIPHER_free(ctx->sc.cipher);
|
||||
ctx->sc.cipher_intent = p->data != NULL;
|
||||
if (p->data != NULL
|
||||
&& ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props))
|
||||
== NULL))
|
||||
return 0;
|
||||
}
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS))
|
||||
!= NULL) {
|
||||
OPENSSL_free(ctx->sc.cipher_pass);
|
||||
ctx->sc.cipher_pass = NULL;
|
||||
if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0,
|
||||
&ctx->sc.cipher_pass_length))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Private key : DER */
|
||||
static int dh_priv_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct dh_priv_ctx_st *ctx = vctx;
|
||||
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
|
||||
ossl_prov_get_dh_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dh_importkey != NULL) {
|
||||
DH *dh = dh_importkey(ctx->provctx, params);
|
||||
|
||||
ok = dh_priv_der(ctx, dh, out, cb, cbarg);
|
||||
DH_free(dh);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dh_priv_der(void *vctx, void *dh, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct dh_priv_ctx_st *ctx = vctx;
|
||||
int ret;
|
||||
|
||||
ctx->sc.cb = cb;
|
||||
ctx->sc.cbarg = cbarg;
|
||||
|
||||
ret = ossl_prov_write_priv_der_from_obj(out, dh, EVP_PKEY_DH,
|
||||
ossl_prov_prepare_dh_params,
|
||||
ossl_prov_dh_priv_to_der,
|
||||
&ctx->sc);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Private key : PEM */
|
||||
static int dh_pem_priv_data(void *vctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct dh_priv_ctx_st *ctx = vctx;
|
||||
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
|
||||
ossl_prov_get_dh_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dh_importkey != NULL) {
|
||||
DH *dh = dh_importkey(ctx, params);
|
||||
|
||||
ok = dh_pem_priv(ctx->provctx, dh, out, cb, cbarg);
|
||||
DH_free(dh);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dh_pem_priv(void *vctx, void *dh, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct dh_priv_ctx_st *ctx = vctx;
|
||||
int ret;
|
||||
|
||||
ctx->sc.cb = cb;
|
||||
ctx->sc.cbarg = cbarg;
|
||||
|
||||
ret = ossl_prov_write_priv_pem_from_obj(out, dh, EVP_PKEY_DH,
|
||||
ossl_prov_prepare_dh_params,
|
||||
ossl_prov_dh_priv_to_der,
|
||||
&ctx->sc);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* There's no specific print context, so we use the provider context
|
||||
*/
|
||||
static void *dh_print_newctx(void *provctx)
|
||||
{
|
||||
return provctx;
|
||||
}
|
||||
|
||||
static void dh_print_freectx(void *ctx)
|
||||
{
|
||||
}
|
||||
|
||||
static int dh_priv_print_data(void *provctx, const OSSL_PARAM params[],
|
||||
BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
|
||||
ossl_prov_get_dh_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dh_importkey != NULL) {
|
||||
DH *dh = dh_importkey(provctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dh_priv_print(provctx, dh, out, cb, cbarg);
|
||||
DH_free(dh);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dh_priv_print(void *ctx, void *dh, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return ossl_prov_print_dh(out, dh, dh_print_priv);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH dh_priv_der_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_priv_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_priv_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
|
||||
(void (*)(void))dh_priv_set_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))dh_priv_settable_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_priv_der_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_priv_der },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH dh_priv_pem_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_priv_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_priv_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
|
||||
(void (*)(void))dh_priv_set_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))dh_priv_settable_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dh_pem_priv_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_pem_priv },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH dh_priv_text_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dh_print_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dh_print_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dh_priv_print },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
|
||||
(void (*)(void))dh_priv_print_data },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/core_numbers.h>
|
||||
#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"
|
||||
#include "serializer_local.h"
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn dh_pub_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn dh_pub_freectx;
|
||||
static OSSL_OP_serializer_serialize_data_fn dh_pub_der_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dh_pub_der;
|
||||
static OSSL_OP_serializer_serialize_data_fn dh_pub_pem_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dh_pub_pem;
|
||||
|
||||
static OSSL_OP_serializer_serialize_data_fn dh_pub_print_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dh_pub_print;
|
||||
|
||||
/* 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 */
|
||||
static int dh_pub_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
|
||||
ossl_prov_get_dh_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dh_importkey != NULL) {
|
||||
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dh_pub_der(ctx, dh, out, cb, cbarg);
|
||||
DH_free(dh);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dh_pub_der(void *ctx, void *dh, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return ossl_prov_write_pub_der_from_obj(out, dh, EVP_PKEY_DH,
|
||||
ossl_prov_prepare_dh_params,
|
||||
ossl_prov_dh_pub_to_der);
|
||||
}
|
||||
|
||||
/* Public key : PEM */
|
||||
static int dh_pub_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
|
||||
ossl_prov_get_dh_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dh_importkey != NULL) {
|
||||
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dh_pub_pem(ctx, dh, out, cb, cbarg);
|
||||
DH_free(dh);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dh_pub_pem(void *ctx, void *dh, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return ossl_prov_write_pub_pem_from_obj(out, dh, EVP_PKEY_DH,
|
||||
ossl_prov_prepare_dh_params,
|
||||
ossl_prov_dh_pub_to_der);
|
||||
|
||||
}
|
||||
|
||||
static int dh_pub_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dh_importkey =
|
||||
ossl_prov_get_dh_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dh_importkey != NULL) {
|
||||
DH *dh = dh_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dh_pub_print(ctx, dh, out, cb, cbarg);
|
||||
DH_free(dh);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dh_pub_print(void *ctx, void *dh, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return ossl_prov_print_dh(out, dh, 0);
|
||||
}
|
||||
|
||||
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 }
|
||||
};
|
||||
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/dsa.h>
|
||||
#include <openssl/err.h>
|
||||
#include "prov/bio.h" /* ossl_prov_bio_printf() */
|
||||
#include "prov/implementations.h" /* rsa_keymgmt_functions */
|
||||
#include "prov/providercommonerr.h" /* PROV_R_BN_ERROR */
|
||||
#include "serializer_local.h"
|
||||
|
||||
OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_dsa_importkey(void)
|
||||
{
|
||||
return ossl_prov_get_importkey(dsa_keymgmt_functions);
|
||||
}
|
||||
|
||||
int ossl_prov_print_dsa(BIO *out, DSA *dsa, enum dsa_print_type type)
|
||||
{
|
||||
const char *type_label = NULL;
|
||||
const BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
const BIGNUM *p = NULL, *q = NULL, *g = NULL;
|
||||
|
||||
|
||||
switch (type) {
|
||||
case dsa_print_priv:
|
||||
type_label = "Private-Key";
|
||||
break;
|
||||
case dsa_print_pub:
|
||||
type_label = "Public-Key";
|
||||
break;
|
||||
case dsa_print_params:
|
||||
type_label = "DSA-Parameters";
|
||||
break;
|
||||
}
|
||||
|
||||
if (type == dsa_print_priv) {
|
||||
priv_key = DSA_get0_priv_key(dsa);
|
||||
if (priv_key == NULL)
|
||||
goto null_err;
|
||||
}
|
||||
|
||||
if (type == dsa_print_priv || type == dsa_print_pub) {
|
||||
pub_key = DSA_get0_pub_key(dsa);
|
||||
if (pub_key == NULL)
|
||||
goto null_err;
|
||||
}
|
||||
|
||||
p = DSA_get0_p(dsa);
|
||||
q = DSA_get0_q(dsa);
|
||||
g = DSA_get0_p(dsa);
|
||||
|
||||
if (p == NULL || q == NULL || g == NULL)
|
||||
goto null_err;
|
||||
|
||||
if (ossl_prov_bio_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p))
|
||||
<= 0)
|
||||
goto err;
|
||||
if (priv_key != NULL
|
||||
&& !ossl_prov_print_labeled_bignum(out, "priv:", priv_key))
|
||||
goto err;
|
||||
if (pub_key != NULL
|
||||
&& !ossl_prov_print_labeled_bignum(out, "pub: ", pub_key))
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, "P: ", p))
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, "Q: ", q))
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, "G: ", g))
|
||||
goto err;
|
||||
|
||||
return 1;
|
||||
err:
|
||||
return 0;
|
||||
null_err:
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
|
||||
goto err;
|
||||
}
|
||||
|
||||
int ossl_prov_prepare_dsa_params(const void *dsa, int nid,
|
||||
ASN1_STRING **pstr, int *pstrtype)
|
||||
{
|
||||
ASN1_STRING *params = ASN1_STRING_new();
|
||||
|
||||
if (params == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
params->length = i2d_DSAparams(dsa, ¶ms->data);
|
||||
|
||||
if (params->length <= 0) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
ASN1_STRING_free(params);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*pstrtype = V_ASN1_SEQUENCE;
|
||||
*pstr = params;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ossl_prov_prepare_all_dsa_params(const void *dsa, int nid,
|
||||
ASN1_STRING **pstr, int *pstrtype)
|
||||
{
|
||||
const BIGNUM *p = DSA_get0_p(dsa);
|
||||
const BIGNUM *q = DSA_get0_q(dsa);
|
||||
const BIGNUM *g = DSA_get0_g(dsa);
|
||||
|
||||
if (p != NULL && q != NULL && g != NULL)
|
||||
return ossl_prov_prepare_dsa_params(dsa, nid, pstr, pstrtype);
|
||||
|
||||
*pstr = NULL;
|
||||
*pstrtype = V_ASN1_UNDEF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ossl_prov_dsa_pub_to_der(const void *dsa, unsigned char **pder)
|
||||
{
|
||||
ASN1_INTEGER *pub_key = BN_to_ASN1_INTEGER(DSA_get0_pub_key(dsa), NULL);
|
||||
int ret;
|
||||
|
||||
if (pub_key == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = i2d_ASN1_INTEGER(pub_key, pder);
|
||||
|
||||
ASN1_STRING_clear_free(pub_key);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ossl_prov_dsa_priv_to_der(const void *dsa, unsigned char **pder)
|
||||
{
|
||||
ASN1_INTEGER *priv_key = BN_to_ASN1_INTEGER(DSA_get0_priv_key(dsa), NULL);
|
||||
int ret;
|
||||
|
||||
if (priv_key == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = i2d_ASN1_INTEGER(priv_key, pder);
|
||||
|
||||
ASN1_STRING_clear_free(priv_key);
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/dsa.h>
|
||||
#include <openssl/types.h>
|
||||
#include <openssl/params.h>
|
||||
#include "prov/bio.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "serializer_local.h"
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn dsa_param_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn dsa_param_freectx;
|
||||
static OSSL_OP_serializer_serialize_data_fn dsa_param_der_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dsa_param_der;
|
||||
static OSSL_OP_serializer_serialize_data_fn dsa_param_pem_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dsa_param_pem;
|
||||
|
||||
static OSSL_OP_serializer_serialize_data_fn dsa_param_print_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dsa_param_print;
|
||||
|
||||
/* Parameters : context */
|
||||
|
||||
/*
|
||||
* There's no specific implementation context, so we use the provider context
|
||||
*/
|
||||
static void *dsa_param_newctx(void *provctx)
|
||||
{
|
||||
return provctx;
|
||||
}
|
||||
|
||||
static void dsa_param_freectx(void *ctx)
|
||||
{
|
||||
}
|
||||
|
||||
/* Public key : DER */
|
||||
static int dsa_param_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dsa_importkey =
|
||||
ossl_prov_get_dsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dsa_importkey != NULL) {
|
||||
DSA *dsa = dsa_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dsa_param_der(ctx, dsa, out, cb, cbarg);
|
||||
DSA_free(dsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dsa_param_der(void *ctx, void *dsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return i2d_DSAparams_bio(out, dsa);
|
||||
}
|
||||
|
||||
/* Public key : PEM */
|
||||
static int dsa_param_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dsa_importkey =
|
||||
ossl_prov_get_dsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dsa_importkey != NULL) {
|
||||
DSA *dsa = dsa_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dsa_param_pem(ctx, dsa, out, cb, cbarg);
|
||||
DSA_free(dsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dsa_param_pem(void *ctx, void *dsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return PEM_write_bio_DSAparams(out, dsa);
|
||||
}
|
||||
|
||||
static int dsa_param_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dsa_importkey =
|
||||
ossl_prov_get_dsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dsa_importkey != NULL) {
|
||||
DSA *dsa = dsa_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dsa_param_print(ctx, dsa, out, cb, cbarg);
|
||||
DSA_free(dsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dsa_param_print(void *ctx, void *dsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return ossl_prov_print_dsa(out, dsa, dsa_print_params);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH dsa_param_der_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_param_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_param_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_param_der_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_param_der },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH dsa_param_pem_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_param_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_param_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_param_pem_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_param_pem },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH dsa_param_text_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_param_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_param_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_param_print },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
|
||||
(void (*)(void))dsa_param_print_data },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -0,0 +1,243 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/dsa.h>
|
||||
#include <openssl/types.h>
|
||||
#include <openssl/params.h>
|
||||
#include "prov/bio.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "serializer_local.h"
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn dsa_priv_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn dsa_priv_freectx;
|
||||
static OSSL_OP_serializer_set_ctx_params_fn dsa_priv_set_ctx_params;
|
||||
static OSSL_OP_serializer_settable_ctx_params_fn dsa_priv_settable_ctx_params;
|
||||
static OSSL_OP_serializer_serialize_data_fn dsa_priv_der_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dsa_priv_der;
|
||||
static OSSL_OP_serializer_serialize_data_fn dsa_pem_priv_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dsa_pem_priv;
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn dsa_print_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn dsa_print_freectx;
|
||||
static OSSL_OP_serializer_serialize_data_fn dsa_priv_print_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dsa_priv_print;
|
||||
|
||||
/*
|
||||
* Context used for private key serialization.
|
||||
*/
|
||||
struct dsa_priv_ctx_st {
|
||||
void *provctx;
|
||||
|
||||
struct pkcs8_encrypt_ctx_st sc;
|
||||
};
|
||||
|
||||
/* Private key : context */
|
||||
static void *dsa_priv_newctx(void *provctx)
|
||||
{
|
||||
struct dsa_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
|
||||
|
||||
if (ctx != NULL) {
|
||||
ctx->provctx = provctx;
|
||||
|
||||
/* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
|
||||
ctx->sc.pbe_nid = -1;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void dsa_priv_freectx(void *vctx)
|
||||
{
|
||||
struct dsa_priv_ctx_st *ctx = vctx;
|
||||
|
||||
EVP_CIPHER_free(ctx->sc.cipher);
|
||||
OPENSSL_free(ctx->sc.cipher_pass);
|
||||
OPENSSL_free(ctx);
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *dsa_priv_settable_ctx_params(void)
|
||||
{
|
||||
static const OSSL_PARAM settables[] = {
|
||||
OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0),
|
||||
OSSL_PARAM_END,
|
||||
};
|
||||
|
||||
return settables;
|
||||
}
|
||||
|
||||
static int dsa_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
||||
{
|
||||
struct dsa_priv_ctx_st *ctx = vctx;
|
||||
const OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER))
|
||||
!= NULL) {
|
||||
const OSSL_PARAM *propsp =
|
||||
OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
|
||||
const char *props = NULL;
|
||||
|
||||
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
||||
return 0;
|
||||
if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING)
|
||||
return 0;
|
||||
props = (propsp != NULL ? propsp->data : NULL);
|
||||
|
||||
EVP_CIPHER_free(ctx->sc.cipher);
|
||||
ctx->sc.cipher_intent = p->data != NULL;
|
||||
if (p->data != NULL
|
||||
&& ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props))
|
||||
== NULL))
|
||||
return 0;
|
||||
}
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS))
|
||||
!= NULL) {
|
||||
OPENSSL_free(ctx->sc.cipher_pass);
|
||||
ctx->sc.cipher_pass = NULL;
|
||||
if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0,
|
||||
&ctx->sc.cipher_pass_length))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Private key : DER */
|
||||
static int dsa_priv_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct dsa_priv_ctx_st *ctx = vctx;
|
||||
OSSL_OP_keymgmt_importkey_fn *dsa_importkey =
|
||||
ossl_prov_get_dsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dsa_importkey != NULL) {
|
||||
DSA *dsa = dsa_importkey(ctx->provctx, params);
|
||||
|
||||
ok = dsa_priv_der(ctx, dsa, out, cb, cbarg);
|
||||
DSA_free(dsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dsa_priv_der(void *vctx, void *dsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct dsa_priv_ctx_st *ctx = vctx;
|
||||
|
||||
ctx->sc.cb = cb;
|
||||
ctx->sc.cbarg = cbarg;
|
||||
|
||||
return ossl_prov_write_priv_der_from_obj(out, dsa, EVP_PKEY_DSA,
|
||||
ossl_prov_prepare_dsa_params,
|
||||
ossl_prov_dsa_priv_to_der,
|
||||
&ctx->sc);
|
||||
}
|
||||
|
||||
/* Private key : PEM */
|
||||
static int dsa_pem_priv_data(void *vctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct dsa_priv_ctx_st *ctx = vctx;
|
||||
OSSL_OP_keymgmt_importkey_fn *dsa_importkey =
|
||||
ossl_prov_get_dsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dsa_importkey != NULL) {
|
||||
DSA *dsa = dsa_importkey(ctx, params);
|
||||
|
||||
ok = dsa_pem_priv(ctx->provctx, dsa, out, cb, cbarg);
|
||||
DSA_free(dsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dsa_pem_priv(void *vctx, void *dsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct dsa_priv_ctx_st *ctx = vctx;
|
||||
|
||||
ctx->sc.cb = cb;
|
||||
ctx->sc.cbarg = cbarg;
|
||||
|
||||
return ossl_prov_write_priv_pem_from_obj(out, dsa, EVP_PKEY_DSA,
|
||||
ossl_prov_prepare_dsa_params,
|
||||
ossl_prov_dsa_priv_to_der,
|
||||
&ctx->sc);
|
||||
}
|
||||
|
||||
/*
|
||||
* There's no specific print context, so we use the provider context
|
||||
*/
|
||||
static void *dsa_print_newctx(void *provctx)
|
||||
{
|
||||
return provctx;
|
||||
}
|
||||
|
||||
static void dsa_print_freectx(void *ctx)
|
||||
{
|
||||
}
|
||||
|
||||
static int dsa_priv_print_data(void *provctx, const OSSL_PARAM params[],
|
||||
BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dsa_importkey =
|
||||
ossl_prov_get_dsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dsa_importkey != NULL) {
|
||||
DSA *dsa = dsa_importkey(provctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dsa_priv_print(provctx, dsa, out, cb, cbarg);
|
||||
DSA_free(dsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dsa_priv_print(void *ctx, void *dsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return ossl_prov_print_dsa(out, dsa, dsa_print_priv);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH dsa_priv_der_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_priv_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_priv_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
|
||||
(void (*)(void))dsa_priv_set_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))dsa_priv_settable_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_priv_der_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_priv_der },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH dsa_priv_pem_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_priv_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_priv_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
|
||||
(void (*)(void))dsa_priv_set_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))dsa_priv_settable_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_pem_priv_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_pem_priv },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH dsa_priv_text_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_print_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_print_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_priv_print },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
|
||||
(void (*)(void))dsa_priv_print_data },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/dsa.h>
|
||||
#include <openssl/types.h>
|
||||
#include <openssl/params.h>
|
||||
#include "prov/bio.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "serializer_local.h"
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn dsa_pub_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn dsa_pub_freectx;
|
||||
static OSSL_OP_serializer_serialize_data_fn dsa_pub_der_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dsa_pub_der;
|
||||
static OSSL_OP_serializer_serialize_data_fn dsa_pub_pem_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dsa_pub_pem;
|
||||
|
||||
static OSSL_OP_serializer_serialize_data_fn dsa_pub_print_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn dsa_pub_print;
|
||||
|
||||
/* Public key : context */
|
||||
|
||||
/*
|
||||
* There's no specific implementation context, so we use the provider context
|
||||
*/
|
||||
static void *dsa_pub_newctx(void *provctx)
|
||||
{
|
||||
return provctx;
|
||||
}
|
||||
|
||||
static void dsa_pub_freectx(void *ctx)
|
||||
{
|
||||
}
|
||||
|
||||
/* Public key : DER */
|
||||
static int dsa_pub_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dsa_importkey =
|
||||
ossl_prov_get_dsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dsa_importkey != NULL) {
|
||||
DSA *dsa = dsa_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dsa_pub_der(ctx, dsa, out, cb, cbarg);
|
||||
DSA_free(dsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dsa_pub_der(void *ctx, void *dsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
/*
|
||||
* TODO(v3.0) implement setting save_parameters, see dsa_pub_encode()
|
||||
* in crypto/dsa/dsa_ameth.c
|
||||
*/
|
||||
int save_parameters = 1;
|
||||
|
||||
return
|
||||
save_parameters
|
||||
? ossl_prov_write_pub_der_from_obj(out, dsa, EVP_PKEY_DSA,
|
||||
ossl_prov_prepare_all_dsa_params,
|
||||
ossl_prov_dsa_pub_to_der)
|
||||
: ossl_prov_write_pub_der_from_obj(out, dsa, EVP_PKEY_DSA,
|
||||
ossl_prov_prepare_dsa_params,
|
||||
ossl_prov_dsa_pub_to_der);
|
||||
|
||||
}
|
||||
|
||||
/* Public key : PEM */
|
||||
static int dsa_pub_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dsa_importkey =
|
||||
ossl_prov_get_dsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dsa_importkey != NULL) {
|
||||
DSA *dsa = dsa_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dsa_pub_pem(ctx, dsa, out, cb, cbarg);
|
||||
DSA_free(dsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dsa_pub_pem(void *ctx, void *dsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return ossl_prov_write_pub_pem_from_obj(out, dsa, EVP_PKEY_DSA,
|
||||
ossl_prov_prepare_dsa_params,
|
||||
ossl_prov_dsa_pub_to_der);
|
||||
}
|
||||
|
||||
static int dsa_pub_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *dsa_importkey =
|
||||
ossl_prov_get_dsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (dsa_importkey != NULL) {
|
||||
DSA *dsa = dsa_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = dsa_pub_print(ctx, dsa, out, cb, cbarg);
|
||||
DSA_free(dsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int dsa_pub_print(void *ctx, void *dsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return ossl_prov_print_dsa(out, dsa, 0);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH dsa_pub_der_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_pub_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_pub_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_pub_der_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_pub_der },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH dsa_pub_pem_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_pub_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_pub_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_pub_pem_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_pub_pem },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH dsa_pub_text_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_pub_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_pub_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_pub_print },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
|
||||
(void (*)(void))dsa_pub_print_data },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/core.h>
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/asn1.h> /* i2d_of_void */
|
||||
#include <openssl/x509.h> /* X509_SIG */
|
||||
#include <openssl/types.h>
|
||||
|
||||
struct pkcs8_encrypt_ctx_st {
|
||||
/* Set to 1 if intending to encrypt/decrypt, otherwise 0 */
|
||||
int cipher_intent;
|
||||
|
||||
EVP_CIPHER *cipher;
|
||||
int pbe_nid; /* For future variation */
|
||||
|
||||
/* Passphrase that was passed by the caller */
|
||||
void *cipher_pass;
|
||||
size_t cipher_pass_length;
|
||||
|
||||
/* This callback is only used of |cipher_pass| is NULL */
|
||||
OSSL_PASSPHRASE_CALLBACK *cb;
|
||||
void *cbarg;
|
||||
};
|
||||
|
||||
OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_importkey(const OSSL_DISPATCH *fns);
|
||||
|
||||
OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_rsa_importkey(void);
|
||||
OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_dh_importkey(void);
|
||||
OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_dsa_importkey(void);
|
||||
|
||||
int ossl_prov_prepare_dh_params(const void *dh, int nid,
|
||||
ASN1_STRING **pstr, int *pstrtype);
|
||||
int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder);
|
||||
int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder);
|
||||
|
||||
int ossl_prov_prepare_dsa_params(const void *dsa, int nid,
|
||||
ASN1_STRING **pstr, int *pstrtype);
|
||||
/*
|
||||
* Special variant of ossl_prov_prepare_dsa_params() that requires all
|
||||
* three parameters (P, Q and G) to be set. This is used when serializing
|
||||
* the public key.
|
||||
*/
|
||||
int ossl_prov_prepare_all_dsa_params(const void *dsa, int nid,
|
||||
ASN1_STRING **pstr, int *pstrtype);
|
||||
int ossl_prov_dsa_pub_to_der(const void *dsa, unsigned char **pder);
|
||||
int ossl_prov_dsa_priv_to_der(const void *dsa, unsigned char **pder);
|
||||
|
||||
int ossl_prov_print_labeled_bignum(BIO *out, const char *label,
|
||||
const BIGNUM *n);
|
||||
int ossl_prov_print_rsa(BIO *out, RSA *rsa, int priv);
|
||||
|
||||
enum dh_print_type {
|
||||
dh_print_priv,
|
||||
dh_print_pub,
|
||||
dh_print_params
|
||||
};
|
||||
|
||||
int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type);
|
||||
|
||||
enum dsa_print_type {
|
||||
dsa_print_priv,
|
||||
dsa_print_pub,
|
||||
dsa_print_params
|
||||
};
|
||||
|
||||
int ossl_prov_print_dsa(BIO *out, DSA *dsa, enum dsa_print_type type);
|
||||
|
||||
int ossl_prov_write_priv_der_from_obj(BIO *out, const void *obj, int obj_nid,
|
||||
int (*p2s)(const void *obj, int nid,
|
||||
ASN1_STRING **str,
|
||||
int *strtype),
|
||||
int (*k2d)(const void *obj,
|
||||
unsigned char **pder),
|
||||
struct pkcs8_encrypt_ctx_st *ctx);
|
||||
int ossl_prov_write_priv_pem_from_obj(BIO *out, const void *obj, int obj_nid,
|
||||
int (*p2s)(const void *obj, int nid,
|
||||
ASN1_STRING **str,
|
||||
int *strtype),
|
||||
int (*k2d)(const void *obj,
|
||||
unsigned char **pder),
|
||||
struct pkcs8_encrypt_ctx_st *ctx);
|
||||
int ossl_prov_write_pub_der_from_obj(BIO *out, const void *obj, int obj_nid,
|
||||
int (*p2s)(const void *obj, int nid,
|
||||
ASN1_STRING **str,
|
||||
int *strtype),
|
||||
int (*k2d)(const void *obj,
|
||||
unsigned char **pder));
|
||||
int ossl_prov_write_pub_pem_from_obj(BIO *out, const void *obj, int obj_nid,
|
||||
int (*p2s)(const void *obj, int nid,
|
||||
ASN1_STRING **str,
|
||||
int *strtype),
|
||||
int (*k2d)(const void *obj,
|
||||
unsigned char **pder));
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include "crypto/rsa.h" /* rsa_get0_all_params() */
|
||||
#include "prov/bio.h" /* ossl_prov_bio_printf() */
|
||||
#include "prov/implementations.h" /* rsa_keymgmt_functions */
|
||||
#include "serializer_local.h"
|
||||
|
||||
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
|
||||
|
||||
OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_rsa_importkey(void)
|
||||
{
|
||||
return ossl_prov_get_importkey(rsa_keymgmt_functions);
|
||||
}
|
||||
|
||||
int ossl_prov_print_rsa(BIO *out, RSA *rsa, int priv)
|
||||
{
|
||||
const char *modulus_label;
|
||||
const char *exponent_label;
|
||||
const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
|
||||
STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
|
||||
STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
|
||||
STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
|
||||
int ret = 0;
|
||||
|
||||
if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
|
||||
goto err;
|
||||
|
||||
RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
|
||||
rsa_get0_all_params(rsa, factors, exps, coeffs);
|
||||
|
||||
if (priv && rsa_d != NULL) {
|
||||
if (ossl_prov_bio_printf(out, "Private-Key: (%d bit, %d primes)\n",
|
||||
BN_num_bits(rsa_n),
|
||||
sk_BIGNUM_const_num(factors)) <= 0)
|
||||
goto err;
|
||||
modulus_label = "modulus:";
|
||||
exponent_label = "publicExponent:";
|
||||
} else {
|
||||
if (ossl_prov_bio_printf(out, "Public-Key: (%d bit)\n",
|
||||
BN_num_bits(rsa_n)) <= 0)
|
||||
goto err;
|
||||
modulus_label = "Modulus:";
|
||||
exponent_label = "Exponent:";
|
||||
}
|
||||
if (!ossl_prov_print_labeled_bignum(out, modulus_label, rsa_n))
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, exponent_label, rsa_e))
|
||||
goto err;
|
||||
if (priv) {
|
||||
int i;
|
||||
|
||||
if (!ossl_prov_print_labeled_bignum(out, "privateExponent:", rsa_d))
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, "prime1:",
|
||||
sk_BIGNUM_const_value(factors, 0)))
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, "prime2:",
|
||||
sk_BIGNUM_const_value(factors, 1)))
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, "exponent1:",
|
||||
sk_BIGNUM_const_value(exps, 0)))
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, "exponent2:",
|
||||
sk_BIGNUM_const_value(exps, 1)))
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, "coefficient:",
|
||||
sk_BIGNUM_const_value(coeffs, 0)))
|
||||
goto err;
|
||||
for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
|
||||
if (ossl_prov_bio_printf(out, "prime%d:", i + 1) <= 0)
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, NULL,
|
||||
sk_BIGNUM_const_value(factors,
|
||||
i)))
|
||||
goto err;
|
||||
if (ossl_prov_bio_printf(out, "exponent%d:", i + 1) <= 0)
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, NULL,
|
||||
sk_BIGNUM_const_value(exps, i)))
|
||||
goto err;
|
||||
if (ossl_prov_bio_printf(out, "coefficient%d:", i + 1) <= 0)
|
||||
goto err;
|
||||
if (!ossl_prov_print_labeled_bignum(out, NULL,
|
||||
sk_BIGNUM_const_value(coeffs,
|
||||
i - 1)))
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
ret = 1;
|
||||
err:
|
||||
sk_BIGNUM_const_free(factors);
|
||||
sk_BIGNUM_const_free(exps);
|
||||
sk_BIGNUM_const_free(coeffs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,277 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/types.h>
|
||||
#include <openssl/params.h>
|
||||
#include <openssl/safestack.h>
|
||||
#include "prov/bio.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "serializer_local.h"
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn rsa_priv_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn rsa_priv_freectx;
|
||||
static OSSL_OP_serializer_set_ctx_params_fn rsa_priv_set_ctx_params;
|
||||
static OSSL_OP_serializer_settable_ctx_params_fn rsa_priv_settable_ctx_params;
|
||||
static OSSL_OP_serializer_serialize_data_fn rsa_priv_der_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn rsa_priv_der;
|
||||
static OSSL_OP_serializer_serialize_data_fn rsa_pem_priv_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn rsa_pem_priv;
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn rsa_print_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn rsa_print_freectx;
|
||||
static OSSL_OP_serializer_serialize_data_fn rsa_priv_print_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn rsa_priv_print;
|
||||
|
||||
/*
|
||||
* Context used for private key serialization.
|
||||
*/
|
||||
struct rsa_priv_ctx_st {
|
||||
void *provctx;
|
||||
|
||||
struct pkcs8_encrypt_ctx_st sc;
|
||||
};
|
||||
|
||||
/* Helper functions to prepare RSA-PSS params for serialization */
|
||||
|
||||
static int prepare_rsa_params(const void *rsa, int nid,
|
||||
ASN1_STRING **pstr, int *pstrtype)
|
||||
{
|
||||
const RSA_PSS_PARAMS *pss = RSA_get0_pss_params(rsa);
|
||||
*pstr = NULL;
|
||||
|
||||
/* If RSA it's just NULL type */
|
||||
if (nid != EVP_PKEY_RSA_PSS) {
|
||||
*pstrtype = V_ASN1_NULL;
|
||||
return 1;
|
||||
}
|
||||
/* If no PSS parameters we omit parameters entirely */
|
||||
if (pss == NULL) {
|
||||
*pstrtype = V_ASN1_UNDEF;
|
||||
return 1;
|
||||
}
|
||||
/* Encode PSS parameters */
|
||||
if (ASN1_item_pack((void *)pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), pstr)
|
||||
== NULL)
|
||||
return 0;
|
||||
|
||||
*pstrtype = V_ASN1_SEQUENCE;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Private key : context */
|
||||
static void *rsa_priv_newctx(void *provctx)
|
||||
{
|
||||
struct rsa_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
|
||||
|
||||
if (ctx != NULL) {
|
||||
ctx->provctx = provctx;
|
||||
/* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
|
||||
ctx->sc.pbe_nid = -1;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void rsa_priv_freectx(void *vctx)
|
||||
{
|
||||
struct rsa_priv_ctx_st *ctx = vctx;
|
||||
|
||||
EVP_CIPHER_free(ctx->sc.cipher);
|
||||
OPENSSL_free(ctx->sc.cipher_pass);
|
||||
OPENSSL_free(ctx);
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *rsa_priv_settable_ctx_params(void)
|
||||
{
|
||||
static const OSSL_PARAM settables[] = {
|
||||
OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0),
|
||||
OSSL_PARAM_END,
|
||||
};
|
||||
|
||||
return settables;
|
||||
}
|
||||
|
||||
static int rsa_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
||||
{
|
||||
struct rsa_priv_ctx_st *ctx = vctx;
|
||||
const OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER))
|
||||
!= NULL) {
|
||||
const OSSL_PARAM *propsp =
|
||||
OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
|
||||
const char *props = NULL;
|
||||
|
||||
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
||||
return 0;
|
||||
if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING)
|
||||
return 0;
|
||||
props = (propsp != NULL ? propsp->data : NULL);
|
||||
|
||||
EVP_CIPHER_free(ctx->sc.cipher);
|
||||
ctx->sc.cipher_intent = p->data != NULL;
|
||||
if (p->data != NULL
|
||||
&& ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props))
|
||||
== NULL))
|
||||
return 0;
|
||||
}
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS))
|
||||
!= NULL) {
|
||||
OPENSSL_free(ctx->sc.cipher_pass);
|
||||
ctx->sc.cipher_pass = NULL;
|
||||
if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0,
|
||||
&ctx->sc.cipher_pass_length))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Private key : DER */
|
||||
static int rsa_priv_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct rsa_priv_ctx_st *ctx = vctx;
|
||||
OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
|
||||
ossl_prov_get_rsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (rsa_importkey != NULL) {
|
||||
RSA *rsa = rsa_importkey(ctx->provctx, params);
|
||||
|
||||
ok = rsa_priv_der(vctx, rsa, out, cb, cbarg);
|
||||
RSA_free(rsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int rsa_priv_der(void *vctx, void *rsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct rsa_priv_ctx_st *ctx = vctx;
|
||||
int ret;
|
||||
|
||||
ctx->sc.cb = cb;
|
||||
ctx->sc.cbarg = cbarg;
|
||||
|
||||
ret = ossl_prov_write_priv_der_from_obj(out, rsa, EVP_PKEY_RSA,
|
||||
prepare_rsa_params,
|
||||
(i2d_of_void *)i2d_RSAPrivateKey,
|
||||
&ctx->sc);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Private key : PEM */
|
||||
static int rsa_pem_priv_data(void *vctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct rsa_priv_ctx_st *ctx = vctx;
|
||||
OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
|
||||
ossl_prov_get_rsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (rsa_importkey != NULL) {
|
||||
RSA *rsa = rsa_importkey(ctx, params);
|
||||
|
||||
ok = rsa_pem_priv(vctx, rsa, out, cb, cbarg);
|
||||
RSA_free(rsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int rsa_pem_priv(void *vctx, void *rsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
struct rsa_priv_ctx_st *ctx = vctx;
|
||||
int ret;
|
||||
|
||||
ctx->sc.cb = cb;
|
||||
ctx->sc.cbarg = cbarg;
|
||||
|
||||
ret = ossl_prov_write_priv_pem_from_obj(out, rsa, EVP_PKEY_RSA,
|
||||
prepare_rsa_params,
|
||||
(i2d_of_void *)i2d_RSAPrivateKey,
|
||||
&ctx->sc);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* There's no specific print context, so we use the provider context
|
||||
*/
|
||||
static void *rsa_print_newctx(void *provctx)
|
||||
{
|
||||
return provctx;
|
||||
}
|
||||
|
||||
static void rsa_print_freectx(void *ctx)
|
||||
{
|
||||
}
|
||||
|
||||
static int rsa_priv_print_data(void *provctx, const OSSL_PARAM params[],
|
||||
BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
|
||||
ossl_prov_get_rsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (rsa_importkey != NULL) {
|
||||
RSA *rsa = rsa_importkey(provctx, params); /* ctx == provctx */
|
||||
|
||||
ok = rsa_priv_print(provctx, rsa, out, cb, cbarg);
|
||||
RSA_free(rsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int rsa_priv_print(void *ctx, void *rsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return ossl_prov_print_rsa(out, rsa, 1);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH rsa_priv_der_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_priv_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_priv_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
|
||||
(void (*)(void))rsa_priv_set_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))rsa_priv_settable_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_priv_der_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_priv_der },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH rsa_priv_pem_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_priv_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_priv_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
|
||||
(void (*)(void))rsa_priv_set_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))rsa_priv_settable_ctx_params },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pem_priv_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pem_priv },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH rsa_priv_text_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_print_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_print_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_priv_print },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
|
||||
(void (*)(void))rsa_priv_print_data },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/types.h>
|
||||
#include <openssl/params.h>
|
||||
#include "prov/bio.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "serializer_local.h"
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn rsa_pub_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn rsa_pub_freectx;
|
||||
static OSSL_OP_serializer_serialize_data_fn rsa_pub_der_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn rsa_pub_der;
|
||||
static OSSL_OP_serializer_serialize_data_fn rsa_pub_pem_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn rsa_pub_pem;
|
||||
|
||||
static OSSL_OP_serializer_serialize_data_fn rsa_pub_print_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn rsa_pub_print;
|
||||
|
||||
/* Public key : context */
|
||||
|
||||
/*
|
||||
* There's no specific implementation context, so we use the provider context
|
||||
*/
|
||||
static void *rsa_pub_newctx(void *provctx)
|
||||
{
|
||||
return provctx;
|
||||
}
|
||||
|
||||
static void rsa_pub_freectx(void *ctx)
|
||||
{
|
||||
}
|
||||
|
||||
/* Public key : DER */
|
||||
static int rsa_pub_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
|
||||
ossl_prov_get_rsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (rsa_importkey != NULL) {
|
||||
RSA *rsa = rsa_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = rsa_pub_der(ctx, rsa, out, cb, cbarg);
|
||||
RSA_free(rsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int rsa_pub_der(void *ctx, void *rsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return i2d_RSA_PUBKEY_bio(out, rsa);
|
||||
}
|
||||
|
||||
/* Public key : PEM */
|
||||
static int rsa_pub_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
|
||||
ossl_prov_get_rsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (rsa_importkey != NULL) {
|
||||
RSA *rsa = rsa_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = rsa_pub_pem(ctx, rsa, out, cb, cbarg);
|
||||
RSA_free(rsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int rsa_pub_pem(void *ctx, void *rsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return PEM_write_bio_RSA_PUBKEY(out, rsa);
|
||||
}
|
||||
|
||||
static int rsa_pub_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
|
||||
ossl_prov_get_rsa_importkey();
|
||||
int ok = 0;
|
||||
|
||||
if (rsa_importkey != NULL) {
|
||||
RSA *rsa = rsa_importkey(ctx, params); /* ctx == provctx */
|
||||
|
||||
ok = rsa_pub_print(ctx, rsa, out, cb, cbarg);
|
||||
RSA_free(rsa);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int rsa_pub_print(void *ctx, void *rsa, BIO *out,
|
||||
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
|
||||
{
|
||||
return ossl_prov_print_rsa(out, rsa, 0);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH rsa_pub_der_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_der_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_der },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH rsa_pub_pem_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_pem_data },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_pem },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH rsa_pub_text_serializer_functions[] = {
|
||||
{ OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
|
||||
{ OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_print },
|
||||
{ OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
|
||||
(void (*)(void))rsa_pub_print_data },
|
||||
{ 0, NULL }
|
||||
};
|
||||
Reference in New Issue
Block a user