2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * DH low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
16 #include <openssl/err.h>
17 #include "prov/bio.h" /* ossl_prov_bio_printf() */
18 #include "prov/implementations.h" /* rsa_keymgmt_functions */
19 #include "prov/providercommonerr.h" /* PROV_R_BN_ERROR */
20 #include "internal/ffc.h"
21 #include "crypto/dh.h"
22 #include "serializer_local.h"
24 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_dh_new(void)
26 return ossl_prov_get_keymgmt_new(dh_keymgmt_functions);
29 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_dh_free(void)
31 return ossl_prov_get_keymgmt_free(dh_keymgmt_functions);
34 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_dh_import(void)
36 return ossl_prov_get_keymgmt_import(dh_keymgmt_functions);
39 int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type)
41 const char *type_label = NULL;
42 const BIGNUM *priv_key = NULL, *pub_key = NULL;
43 const BIGNUM *p = NULL;
47 type_label = "DH Private-Key";
50 type_label = "DH Public-Key";
53 type_label = "DH Parameters";
57 if (type == dh_print_priv) {
58 priv_key = DH_get0_priv_key(dh);
63 if (type == dh_print_priv || type == dh_print_pub) {
64 pub_key = DH_get0_pub_key(dh);
73 if (ossl_prov_bio_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p))
77 && !ossl_prov_print_labeled_bignum(out, "private-key:", priv_key))
80 && !ossl_prov_print_labeled_bignum(out, "public-key:", pub_key))
82 if (!ffc_params_prov_print(out, dh_get0_params(dh)))
89 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
93 int ossl_prov_prepare_dh_params(const void *dh, int nid,
94 void **pstr, int *pstrtype)
96 ASN1_STRING *params = ASN1_STRING_new();
99 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
103 if (nid == EVP_PKEY_DHX)
104 params->length = i2d_DHxparams(dh, ¶ms->data);
106 params->length = i2d_DHparams(dh, ¶ms->data);
108 if (params->length <= 0) {
109 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
110 ASN1_STRING_free(params);
113 params->type = V_ASN1_SEQUENCE;
116 *pstrtype = V_ASN1_SEQUENCE;
120 int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder)
122 ASN1_INTEGER *pub_key = BN_to_ASN1_INTEGER(DH_get0_pub_key(dh), NULL);
125 if (pub_key == NULL) {
126 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
130 ret = i2d_ASN1_INTEGER(pub_key, pder);
132 ASN1_STRING_clear_free(pub_key);
136 int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder)
138 ASN1_INTEGER *priv_key = BN_to_ASN1_INTEGER(DH_get0_priv_key(dh), NULL);
141 if (priv_key == NULL) {
142 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
146 ret = i2d_ASN1_INTEGER(priv_key, pder);
148 ASN1_STRING_clear_free(priv_key);