]>
Commit | Line | Data |
---|---|---|
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 | /* | |
11 | * DH low level APIs are deprecated for public use, but still ok for | |
12 | * internal use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
16 | #include <openssl/dh.h> | |
17 | #include <openssl/err.h> | |
18 | #include "prov/bio.h" /* ossl_prov_bio_printf() */ | |
19 | #include "prov/implementations.h" /* rsa_keymgmt_functions */ | |
20 | #include "prov/providercommonerr.h" /* PROV_R_BN_ERROR */ | |
21 | #include "serializer_local.h" | |
22 | ||
23 | OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_dh_new(void) | |
24 | { | |
25 | return ossl_prov_get_keymgmt_new(dh_keymgmt_functions); | |
26 | } | |
27 | ||
28 | OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_dh_free(void) | |
29 | { | |
30 | return ossl_prov_get_keymgmt_free(dh_keymgmt_functions); | |
31 | } | |
32 | ||
33 | OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_dh_import(void) | |
34 | { | |
35 | return ossl_prov_get_keymgmt_import(dh_keymgmt_functions); | |
36 | } | |
37 | ||
38 | int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type) | |
39 | { | |
40 | const char *type_label = NULL; | |
41 | const BIGNUM *priv_key = NULL, *pub_key = NULL; | |
42 | const BIGNUM *p = NULL, *g = NULL; | |
43 | ||
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); | |
70 | g = DH_get0_g(dh); | |
71 | if (p == NULL || g == NULL) | |
72 | goto null_err; | |
73 | ||
74 | /* | |
75 | * TODO(3.0): add printing of: | |
76 | * | |
77 | * - q (label "subgroup order:") | |
78 | * - j (label "subgroup factor:") | |
79 | * - seed (label "seed:") | |
80 | * - counter (label "counter:") | |
81 | * | |
82 | * This can happen as soon as there are DH_get0_ functions for them. | |
83 | */ | |
84 | ||
85 | if (ossl_prov_bio_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p)) | |
86 | <= 0) | |
87 | goto err; | |
88 | if (priv_key != NULL | |
89 | && !ossl_prov_print_labeled_bignum(out, " private-key:", priv_key)) | |
90 | goto err; | |
91 | if (pub_key != NULL | |
92 | && !ossl_prov_print_labeled_bignum(out, " public-key:", pub_key)) | |
93 | goto err; | |
94 | if (p != NULL | |
95 | && !ossl_prov_print_labeled_bignum(out, " prime:", p)) | |
96 | goto err; | |
97 | if (g != NULL | |
98 | && !ossl_prov_print_labeled_bignum(out, " generator:", g)) | |
99 | goto err; | |
100 | ||
101 | return 1; | |
102 | err: | |
103 | return 0; | |
104 | null_err: | |
105 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER); | |
106 | goto err; | |
107 | } | |
108 | ||
109 | int ossl_prov_prepare_dh_params(const void *dh, int nid, | |
110 | ASN1_STRING **pstr, int *pstrtype) | |
111 | { | |
112 | ASN1_STRING *params = ASN1_STRING_new(); | |
113 | ||
114 | if (params == NULL) { | |
115 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
116 | return 0; | |
117 | } | |
118 | ||
119 | if (nid == EVP_PKEY_DHX) | |
120 | params->length = i2d_DHxparams(dh, ¶ms->data); | |
121 | else | |
122 | params->length = i2d_DHparams(dh, ¶ms->data); | |
123 | ||
124 | if (params->length <= 0) { | |
125 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
126 | ASN1_STRING_free(params); | |
127 | return 0; | |
128 | } | |
129 | params->type = V_ASN1_SEQUENCE; | |
130 | ||
131 | *pstr = params; | |
132 | *pstrtype = V_ASN1_SEQUENCE; | |
133 | return 1; | |
134 | } | |
135 | ||
136 | int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder) | |
137 | { | |
138 | ASN1_INTEGER *pub_key = BN_to_ASN1_INTEGER(DH_get0_pub_key(dh), NULL); | |
139 | int ret; | |
140 | ||
141 | if (pub_key == NULL) { | |
142 | ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR); | |
143 | return 0; | |
144 | } | |
145 | ||
146 | ret = i2d_ASN1_INTEGER(pub_key, pder); | |
147 | ||
148 | ASN1_STRING_clear_free(pub_key); | |
149 | return ret; | |
150 | } | |
151 | ||
152 | int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder) | |
153 | { | |
154 | ASN1_INTEGER *priv_key = BN_to_ASN1_INTEGER(DH_get0_priv_key(dh), NULL); | |
155 | int ret; | |
156 | ||
157 | if (priv_key == NULL) { | |
158 | ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR); | |
159 | return 0; | |
160 | } | |
161 | ||
162 | ret = i2d_ASN1_INTEGER(priv_key, pder); | |
163 | ||
164 | ASN1_STRING_clear_free(priv_key); | |
165 | return ret; | |
166 | } | |
167 |