]>
Commit | Line | Data |
---|---|---|
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 | ||
23c48d94 | 16 | #include <openssl/core_dispatch.h> |
045e51cb RL |
17 | #include <openssl/err.h> |
18 | #include <openssl/pem.h> | |
19 | #include <openssl/dh.h> | |
20 | #include <openssl/types.h> | |
21 | #include <openssl/params.h> | |
22 | #include "prov/bio.h" | |
23 | #include "prov/implementations.h" | |
d40b42ab | 24 | #include "prov/provider_ctx.h" |
ece9304c | 25 | #include "encoder_local.h" |
045e51cb | 26 | |
ece9304c RL |
27 | static OSSL_FUNC_encoder_newctx_fn dh_pub_newctx; |
28 | static OSSL_FUNC_encoder_freectx_fn dh_pub_freectx; | |
29 | static OSSL_FUNC_encoder_encode_data_fn dh_pub_der_data; | |
30 | static OSSL_FUNC_encoder_encode_object_fn dh_pub_der; | |
31 | static OSSL_FUNC_encoder_encode_data_fn dh_pub_pem_data; | |
32 | static OSSL_FUNC_encoder_encode_object_fn dh_pub_pem; | |
045e51cb | 33 | |
ece9304c RL |
34 | static OSSL_FUNC_encoder_encode_data_fn dh_pub_print_data; |
35 | static OSSL_FUNC_encoder_encode_object_fn dh_pub_print; | |
045e51cb RL |
36 | |
37 | /* Public key : context */ | |
38 | ||
39 | /* | |
40 | * There's no specific implementation context, so we use the provider context | |
41 | */ | |
42 | static void *dh_pub_newctx(void *provctx) | |
43 | { | |
44 | return provctx; | |
45 | } | |
46 | ||
47 | static void dh_pub_freectx(void *ctx) | |
48 | { | |
49 | } | |
50 | ||
51 | /* Public key : DER */ | |
d40b42ab | 52 | static int dh_pub_der_data(void *ctx, const OSSL_PARAM params[], |
ece9304c RL |
53 | OSSL_CORE_BIO *out, |
54 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
045e51cb | 55 | { |
363b1e5d DMSP |
56 | OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new(); |
57 | OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free(); | |
58 | OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import(); | |
045e51cb RL |
59 | int ok = 0; |
60 | ||
32b0645c RL |
61 | if (dh_import != NULL) { |
62 | DH *dh; | |
045e51cb | 63 | |
32b0645c RL |
64 | /* ctx == provctx */ |
65 | if ((dh = dh_new(ctx)) != NULL | |
66 | && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params) | |
67 | && dh_pub_der(ctx, dh, out, cb, cbarg)) | |
68 | ok = 1; | |
69 | dh_free(dh); | |
045e51cb RL |
70 | } |
71 | return ok; | |
72 | } | |
73 | ||
d40b42ab | 74 | static int dh_pub_der(void *ctx, void *dh, OSSL_CORE_BIO *cout, |
ece9304c | 75 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
045e51cb | 76 | { |
d40b42ab MC |
77 | BIO *out = bio_new_from_core_bio(ctx, cout); |
78 | int ret; | |
79 | ||
80 | if (out == NULL) | |
81 | return 0; | |
82 | ||
31d2daec SL |
83 | ret = ossl_prov_write_pub_der_from_obj(out, dh, |
84 | ossl_prov_dh_type_to_evp(dh), | |
d40b42ab MC |
85 | ossl_prov_prepare_dh_params, |
86 | ossl_prov_dh_pub_to_der); | |
87 | BIO_free(out); | |
88 | ||
89 | return ret; | |
045e51cb RL |
90 | } |
91 | ||
92 | /* Public key : PEM */ | |
d40b42ab MC |
93 | static int dh_pub_pem_data(void *ctx, const OSSL_PARAM params[], |
94 | OSSL_CORE_BIO *out, | |
95 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
045e51cb | 96 | { |
363b1e5d DMSP |
97 | OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new(); |
98 | OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free(); | |
99 | OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import(); | |
045e51cb RL |
100 | int ok = 0; |
101 | ||
32b0645c RL |
102 | if (dh_import != NULL) { |
103 | DH *dh; | |
045e51cb | 104 | |
32b0645c RL |
105 | /* ctx == provctx */ |
106 | if ((dh = dh_new(ctx)) != NULL | |
107 | && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params) | |
108 | && dh_pub_pem(ctx, dh, out, cb, cbarg)) | |
109 | ok = 1; | |
110 | dh_free(dh); | |
045e51cb RL |
111 | } |
112 | return ok; | |
113 | } | |
114 | ||
d40b42ab | 115 | static int dh_pub_pem(void *ctx, void *dh, OSSL_CORE_BIO *cout, |
ece9304c | 116 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
045e51cb | 117 | { |
d40b42ab MC |
118 | BIO *out = bio_new_from_core_bio(ctx, cout); |
119 | int ret; | |
120 | ||
121 | if (out == NULL) | |
122 | return 0; | |
045e51cb | 123 | |
31d2daec SL |
124 | ret = ossl_prov_write_pub_pem_from_obj(out, dh, |
125 | ossl_prov_dh_type_to_evp(dh), | |
d40b42ab MC |
126 | ossl_prov_prepare_dh_params, |
127 | ossl_prov_dh_pub_to_der); | |
128 | BIO_free(out); | |
129 | ||
130 | return ret; | |
045e51cb RL |
131 | } |
132 | ||
d40b42ab MC |
133 | static int dh_pub_print_data(void *ctx, const OSSL_PARAM params[], |
134 | OSSL_CORE_BIO *out, | |
135 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
045e51cb | 136 | { |
363b1e5d DMSP |
137 | OSSL_FUNC_keymgmt_new_fn *dh_new = ossl_prov_get_keymgmt_dh_new(); |
138 | OSSL_FUNC_keymgmt_free_fn *dh_free = ossl_prov_get_keymgmt_dh_free(); | |
139 | OSSL_FUNC_keymgmt_import_fn *dh_import = ossl_prov_get_keymgmt_dh_import(); | |
045e51cb RL |
140 | int ok = 0; |
141 | ||
32b0645c RL |
142 | if (dh_import != NULL) { |
143 | DH *dh; | |
045e51cb | 144 | |
32b0645c RL |
145 | /* ctx == provctx */ |
146 | if ((dh = dh_new(ctx)) != NULL | |
147 | && dh_import(dh, OSSL_KEYMGMT_SELECT_KEYPAIR, params) | |
148 | && dh_pub_print(ctx, dh, out, cb, cbarg)) | |
149 | ok = 1; | |
150 | dh_free(dh); | |
045e51cb RL |
151 | } |
152 | return ok; | |
153 | } | |
154 | ||
d40b42ab | 155 | static int dh_pub_print(void *ctx, void *dh, OSSL_CORE_BIO *cout, |
ece9304c | 156 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) |
045e51cb | 157 | { |
d40b42ab MC |
158 | BIO *out = bio_new_from_core_bio(ctx, cout); |
159 | int ret; | |
160 | ||
161 | if (out == NULL) | |
162 | return 0; | |
163 | ||
164 | ret = ossl_prov_print_dh(out, dh, dh_print_pub); | |
165 | BIO_free(out); | |
166 | ||
167 | return ret; | |
045e51cb RL |
168 | } |
169 | ||
ece9304c RL |
170 | const OSSL_DISPATCH dh_pub_der_encoder_functions[] = { |
171 | { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dh_pub_newctx }, | |
172 | { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dh_pub_freectx }, | |
173 | { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))dh_pub_der_data }, | |
174 | { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dh_pub_der }, | |
045e51cb RL |
175 | { 0, NULL } |
176 | }; | |
177 | ||
ece9304c RL |
178 | const OSSL_DISPATCH dh_pub_pem_encoder_functions[] = { |
179 | { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dh_pub_newctx }, | |
180 | { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dh_pub_freectx }, | |
181 | { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))dh_pub_pem_data }, | |
182 | { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dh_pub_pem }, | |
045e51cb RL |
183 | { 0, NULL } |
184 | }; | |
185 | ||
ece9304c RL |
186 | const OSSL_DISPATCH dh_pub_text_encoder_functions[] = { |
187 | { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))dh_pub_newctx }, | |
188 | { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))dh_pub_freectx }, | |
189 | { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))dh_pub_print }, | |
190 | { OSSL_FUNC_ENCODER_ENCODE_DATA, | |
045e51cb RL |
191 | (void (*)(void))dh_pub_print_data }, |
192 | { 0, NULL } | |
193 | }; |