]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/encode_decode/encoder_dh.c
Fix up issue on AIX caused by broken compiler handling of macro expansion
[thirdparty/openssl.git] / providers / implementations / encode_decode / encoder_dh.c
CommitLineData
045e51cb 1/*
33388b44 2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
045e51cb
RL
3 *
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
8 */
9
ada66e78
P
10/*
11 * DH low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
045e51cb
RL
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 */
7165593c
SL
20#include "internal/ffc.h"
21#include "crypto/dh.h"
ece9304c 22#include "encoder_local.h"
045e51cb 23
363b1e5d 24OSSL_FUNC_keymgmt_new_fn *ossl_prov_get_keymgmt_dh_new(void)
045e51cb 25{
32b0645c
RL
26 return ossl_prov_get_keymgmt_new(dh_keymgmt_functions);
27}
28
363b1e5d 29OSSL_FUNC_keymgmt_free_fn *ossl_prov_get_keymgmt_dh_free(void)
32b0645c
RL
30{
31 return ossl_prov_get_keymgmt_free(dh_keymgmt_functions);
32}
33
363b1e5d 34OSSL_FUNC_keymgmt_import_fn *ossl_prov_get_keymgmt_dh_import(void)
32b0645c
RL
35{
36 return ossl_prov_get_keymgmt_import(dh_keymgmt_functions);
045e51cb
RL
37}
38
39int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type)
40{
41 const char *type_label = NULL;
42 const BIGNUM *priv_key = NULL, *pub_key = NULL;
7165593c 43 const BIGNUM *p = NULL;
045e51cb
RL
44
45 switch (type) {
46 case dh_print_priv:
47 type_label = "DH Private-Key";
48 break;
49 case dh_print_pub:
50 type_label = "DH Public-Key";
51 break;
52 case dh_print_params:
53 type_label = "DH Parameters";
54 break;
55 }
56
57 if (type == dh_print_priv) {
58 priv_key = DH_get0_priv_key(dh);
59 if (priv_key == NULL)
60 goto null_err;
61 }
62
63 if (type == dh_print_priv || type == dh_print_pub) {
64 pub_key = DH_get0_pub_key(dh);
65 if (pub_key == NULL)
66 goto null_err;
67 }
68
69 p = DH_get0_p(dh);
7165593c 70 if (p == NULL)
045e51cb
RL
71 goto null_err;
72
d40b42ab 73 if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p))
045e51cb
RL
74 <= 0)
75 goto err;
76 if (priv_key != NULL
7165593c 77 && !ossl_prov_print_labeled_bignum(out, "private-key:", priv_key))
045e51cb
RL
78 goto err;
79 if (pub_key != NULL
7165593c 80 && !ossl_prov_print_labeled_bignum(out, "public-key:", pub_key))
045e51cb 81 goto err;
7165593c 82 if (!ffc_params_prov_print(out, dh_get0_params(dh)))
045e51cb
RL
83 goto err;
84
85 return 1;
86 err:
87 return 0;
88 null_err:
89 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
90 goto err;
91}
92
93int ossl_prov_prepare_dh_params(const void *dh, int nid,
f552d900 94 void **pstr, int *pstrtype)
045e51cb
RL
95{
96 ASN1_STRING *params = ASN1_STRING_new();
97
98 if (params == NULL) {
99 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
100 return 0;
101 }
102
103 if (nid == EVP_PKEY_DHX)
104 params->length = i2d_DHxparams(dh, &params->data);
105 else
106 params->length = i2d_DHparams(dh, &params->data);
107
108 if (params->length <= 0) {
109 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
110 ASN1_STRING_free(params);
111 return 0;
112 }
113 params->type = V_ASN1_SEQUENCE;
114
115 *pstr = params;
116 *pstrtype = V_ASN1_SEQUENCE;
117 return 1;
118}
119
120int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder)
121{
6cc1dfca
RL
122 const BIGNUM *bn = NULL;
123 ASN1_INTEGER *pub_key = NULL;
045e51cb
RL
124 int ret;
125
6cc1dfca
RL
126 if ((bn = DH_get0_pub_key(dh)) == NULL) {
127 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
128 return 0;
129 }
130 if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
045e51cb
RL
131 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
132 return 0;
133 }
134
135 ret = i2d_ASN1_INTEGER(pub_key, pder);
136
137 ASN1_STRING_clear_free(pub_key);
138 return ret;
139}
140
141int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder)
142{
6cc1dfca
RL
143 const BIGNUM *bn = NULL;
144 ASN1_INTEGER *priv_key = NULL;
045e51cb
RL
145 int ret;
146
6cc1dfca
RL
147 if ((bn = DH_get0_priv_key(dh)) == NULL) {
148 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
149 return 0;
150 }
151 if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
045e51cb
RL
152 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
153 return 0;
154 }
155
156 ret = i2d_ASN1_INTEGER(priv_key, pder);
157
158 ASN1_STRING_clear_free(priv_key);
159 return ret;
160}
161
31d2daec
SL
162
163int ossl_prov_dh_type_to_evp(const DH *dh)
164{
165 return DH_test_flags(dh, DH_FLAG_TYPE_DHX) ? EVP_PKEY_DHX : EVP_PKEY_DH;
166}