2 * Copyright 2019 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
10 #include <openssl/dh.h>
11 #include <openssl/err.h>
12 #include "prov/bio.h" /* ossl_prov_bio_printf() */
13 #include "prov/implementations.h" /* rsa_keymgmt_functions */
14 #include "prov/providercommonerr.h" /* PROV_R_BN_ERROR */
15 #include "serializer_local.h"
17 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_dh_new(void)
19 return ossl_prov_get_keymgmt_new(dh_keymgmt_functions);
22 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_dh_free(void)
24 return ossl_prov_get_keymgmt_free(dh_keymgmt_functions);
27 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_dh_import(void)
29 return ossl_prov_get_keymgmt_import(dh_keymgmt_functions);
32 int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type)
34 const char *type_label = NULL;
35 const BIGNUM *priv_key = NULL, *pub_key = NULL;
36 const BIGNUM *p = NULL, *g = NULL;
41 type_label = "DH Private-Key";
44 type_label = "DH Public-Key";
47 type_label = "DH Parameters";
51 if (type == dh_print_priv) {
52 priv_key = DH_get0_priv_key(dh);
57 if (type == dh_print_priv || type == dh_print_pub) {
58 pub_key = DH_get0_pub_key(dh);
65 if (p == NULL || g == NULL)
69 * TODO(3.0): add printing of:
71 * - q (label "subgroup order:")
72 * - j (label "subgroup factor:")
73 * - seed (label "seed:")
74 * - counter (label "counter:")
76 * This can happen as soon as there are DH_get0_ functions for them.
79 if (ossl_prov_bio_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p))
83 && !ossl_prov_print_labeled_bignum(out, " private-key:", priv_key))
86 && !ossl_prov_print_labeled_bignum(out, " public-key:", pub_key))
89 && !ossl_prov_print_labeled_bignum(out, " prime:", p))
92 && !ossl_prov_print_labeled_bignum(out, " generator:", g))
99 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
103 int ossl_prov_prepare_dh_params(const void *dh, int nid,
104 ASN1_STRING **pstr, int *pstrtype)
106 ASN1_STRING *params = ASN1_STRING_new();
108 if (params == NULL) {
109 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
113 if (nid == EVP_PKEY_DHX)
114 params->length = i2d_DHxparams(dh, ¶ms->data);
116 params->length = i2d_DHparams(dh, ¶ms->data);
118 if (params->length <= 0) {
119 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
120 ASN1_STRING_free(params);
123 params->type = V_ASN1_SEQUENCE;
126 *pstrtype = V_ASN1_SEQUENCE;
130 int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder)
132 ASN1_INTEGER *pub_key = BN_to_ASN1_INTEGER(DH_get0_pub_key(dh), NULL);
135 if (pub_key == NULL) {
136 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
140 ret = i2d_ASN1_INTEGER(pub_key, pder);
142 ASN1_STRING_clear_free(pub_key);
146 int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder)
148 ASN1_INTEGER *priv_key = BN_to_ASN1_INTEGER(DH_get0_priv_key(dh), NULL);
151 if (priv_key == NULL) {
152 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
156 ret = i2d_ASN1_INTEGER(priv_key, pder);
158 ASN1_STRING_clear_free(priv_key);