1 | /* |
2 | * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. |
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 | |
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" |
16 | |
17 | OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_dh_importkey(void) |
18 | { |
19 | return ossl_prov_get_importkey(dh_keymgmt_functions); |
20 | } |
21 | |
22 | int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type) |
23 | { |
24 | const char *type_label = NULL; |
25 | const BIGNUM *priv_key = NULL, *pub_key = NULL; |
26 | const BIGNUM *p = NULL, *g = NULL; |
27 | |
28 | |
29 | switch (type) { |
30 | case dh_print_priv: |
31 | type_label = "DH Private-Key" ; |
32 | break; |
33 | case dh_print_pub: |
34 | type_label = "DH Public-Key" ; |
35 | break; |
36 | case dh_print_params: |
37 | type_label = "DH Parameters" ; |
38 | break; |
39 | } |
40 | |
41 | if (type == dh_print_priv) { |
42 | priv_key = DH_get0_priv_key(dh); |
43 | if (priv_key == NULL) |
44 | goto null_err; |
45 | } |
46 | |
47 | if (type == dh_print_priv || type == dh_print_pub) { |
48 | pub_key = DH_get0_pub_key(dh); |
49 | if (pub_key == NULL) |
50 | goto null_err; |
51 | } |
52 | |
53 | p = DH_get0_p(dh); |
54 | g = DH_get0_p(dh); |
55 | if (p == NULL || g == NULL) |
56 | goto null_err; |
57 | |
58 | /* |
59 | * TODO(3.0): add printing of: |
60 | * |
61 | * - q (label "subgroup order:") |
62 | * - j (label "subgroup factor:") |
63 | * - seed (label "seed:") |
64 | * - counter (label "counter:") |
65 | * |
66 | * This can happen as soon as there are DH_get0_ functions for them. |
67 | */ |
68 | |
69 | if (ossl_prov_bio_printf(out, "%s: (%d bit)\n" , type_label, BN_num_bits(p)) |
70 | <= 0) |
71 | goto err; |
72 | if (priv_key != NULL |
73 | && !ossl_prov_print_labeled_bignum(out, " private-key:" , priv_key)) |
74 | goto err; |
75 | if (pub_key != NULL |
76 | && !ossl_prov_print_labeled_bignum(out, " public-key:" , pub_key)) |
77 | goto err; |
78 | if (p != NULL |
79 | && !ossl_prov_print_labeled_bignum(out, " prime:" , p)) |
80 | goto err; |
81 | if (g != NULL |
82 | && !ossl_prov_print_labeled_bignum(out, " generator:" , g)) |
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 | |
93 | int ossl_prov_prepare_dh_params(const void *dh, int nid, |
94 | ASN1_STRING **pstr, int *pstrtype) |
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, ¶ms->data); |
105 | else |
106 | params->length = i2d_DHparams(dh, ¶ms->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 | |
120 | int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder) |
121 | { |
122 | ASN1_INTEGER *pub_key = BN_to_ASN1_INTEGER(DH_get0_pub_key(dh), NULL); |
123 | int ret; |
124 | |
125 | if (pub_key == NULL) { |
126 | ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR); |
127 | return 0; |
128 | } |
129 | |
130 | ret = i2d_ASN1_INTEGER(pub_key, pder); |
131 | |
132 | ASN1_STRING_clear_free(pub_key); |
133 | return ret; |
134 | } |
135 | |
136 | int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder) |
137 | { |
138 | ASN1_INTEGER *priv_key = BN_to_ASN1_INTEGER(DH_get0_priv_key(dh), NULL); |
139 | int ret; |
140 | |
141 | if (priv_key == NULL) { |
142 | ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR); |
143 | return 0; |
144 | } |
145 | |
146 | ret = i2d_ASN1_INTEGER(priv_key, pder); |
147 | |
148 | ASN1_STRING_clear_free(priv_key); |
149 | return ret; |
150 | } |
151 | |
152 | |