]>
Commit | Line | Data |
---|---|---|
264b789b RL |
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 | ||
f41ac0ee P |
10 | /* |
11 | * DSA low level APIs are deprecated for public use, but still ok for | |
12 | * internal use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
264b789b RL |
16 | #include <openssl/core_numbers.h> |
17 | #include <openssl/core_names.h> | |
18 | #include <openssl/err.h> | |
19 | #include <openssl/pem.h> | |
20 | #include <openssl/dsa.h> | |
21 | #include <openssl/types.h> | |
22 | #include <openssl/params.h> | |
23 | #include "prov/bio.h" | |
24 | #include "prov/implementations.h" | |
25 | #include "serializer_local.h" | |
26 | ||
27 | static OSSL_OP_serializer_newctx_fn dsa_priv_newctx; | |
28 | static OSSL_OP_serializer_freectx_fn dsa_priv_freectx; | |
29 | static OSSL_OP_serializer_set_ctx_params_fn dsa_priv_set_ctx_params; | |
30 | static OSSL_OP_serializer_settable_ctx_params_fn dsa_priv_settable_ctx_params; | |
31 | static OSSL_OP_serializer_serialize_data_fn dsa_priv_der_data; | |
32 | static OSSL_OP_serializer_serialize_object_fn dsa_priv_der; | |
33 | static OSSL_OP_serializer_serialize_data_fn dsa_pem_priv_data; | |
34 | static OSSL_OP_serializer_serialize_object_fn dsa_pem_priv; | |
35 | ||
36 | static OSSL_OP_serializer_newctx_fn dsa_print_newctx; | |
37 | static OSSL_OP_serializer_freectx_fn dsa_print_freectx; | |
38 | static OSSL_OP_serializer_serialize_data_fn dsa_priv_print_data; | |
39 | static OSSL_OP_serializer_serialize_object_fn dsa_priv_print; | |
40 | ||
41 | /* | |
42 | * Context used for private key serialization. | |
43 | */ | |
44 | struct dsa_priv_ctx_st { | |
45 | void *provctx; | |
46 | ||
47 | struct pkcs8_encrypt_ctx_st sc; | |
48 | }; | |
49 | ||
50 | /* Private key : context */ | |
51 | static void *dsa_priv_newctx(void *provctx) | |
52 | { | |
53 | struct dsa_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx)); | |
54 | ||
55 | if (ctx != NULL) { | |
56 | ctx->provctx = provctx; | |
264b789b | 57 | |
75e571b5 P |
58 | /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */ |
59 | ctx->sc.pbe_nid = -1; | |
60 | } | |
264b789b RL |
61 | return ctx; |
62 | } | |
63 | ||
64 | static void dsa_priv_freectx(void *vctx) | |
65 | { | |
66 | struct dsa_priv_ctx_st *ctx = vctx; | |
67 | ||
68 | EVP_CIPHER_free(ctx->sc.cipher); | |
69 | OPENSSL_free(ctx->sc.cipher_pass); | |
70 | OPENSSL_free(ctx); | |
71 | } | |
72 | ||
73 | static const OSSL_PARAM *dsa_priv_settable_ctx_params(void) | |
74 | { | |
75 | static const OSSL_PARAM settables[] = { | |
76 | OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0), | |
77 | OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0), | |
78 | OSSL_PARAM_END, | |
79 | }; | |
80 | ||
81 | return settables; | |
82 | } | |
83 | ||
84 | static int dsa_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[]) | |
85 | { | |
86 | struct dsa_priv_ctx_st *ctx = vctx; | |
87 | const OSSL_PARAM *p; | |
88 | ||
89 | if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER)) | |
90 | != NULL) { | |
91 | const OSSL_PARAM *propsp = | |
92 | OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES); | |
93 | const char *props = NULL; | |
94 | ||
95 | if (p->data_type != OSSL_PARAM_UTF8_STRING) | |
96 | return 0; | |
97 | if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING) | |
98 | return 0; | |
99 | props = (propsp != NULL ? propsp->data : NULL); | |
100 | ||
101 | EVP_CIPHER_free(ctx->sc.cipher); | |
102 | ctx->sc.cipher_intent = p->data != NULL; | |
103 | if (p->data != NULL | |
104 | && ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props)) | |
105 | == NULL)) | |
106 | return 0; | |
107 | } | |
108 | if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS)) | |
109 | != NULL) { | |
110 | OPENSSL_free(ctx->sc.cipher_pass); | |
111 | ctx->sc.cipher_pass = NULL; | |
112 | if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0, | |
113 | &ctx->sc.cipher_pass_length)) | |
114 | return 0; | |
115 | } | |
116 | return 1; | |
117 | } | |
118 | ||
119 | /* Private key : DER */ | |
120 | static int dsa_priv_der_data(void *vctx, const OSSL_PARAM params[], BIO *out, | |
121 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
122 | { | |
123 | struct dsa_priv_ctx_st *ctx = vctx; | |
32b0645c RL |
124 | OSSL_OP_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new(); |
125 | OSSL_OP_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free(); | |
126 | OSSL_OP_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import(); | |
264b789b RL |
127 | int ok = 0; |
128 | ||
32b0645c RL |
129 | if (dsa_import != NULL) { |
130 | DSA *dsa; | |
264b789b | 131 | |
32b0645c RL |
132 | if ((dsa = dsa_new(ctx->provctx)) != NULL |
133 | && dsa_import(dsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params) | |
134 | && dsa_priv_der(ctx, dsa, out, cb, cbarg)) | |
135 | ok = 1; | |
136 | dsa_free(dsa); | |
264b789b RL |
137 | } |
138 | return ok; | |
139 | } | |
140 | ||
141 | static int dsa_priv_der(void *vctx, void *dsa, BIO *out, | |
142 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
143 | { | |
144 | struct dsa_priv_ctx_st *ctx = vctx; | |
145 | ||
146 | ctx->sc.cb = cb; | |
147 | ctx->sc.cbarg = cbarg; | |
148 | ||
149 | return ossl_prov_write_priv_der_from_obj(out, dsa, EVP_PKEY_DSA, | |
150 | ossl_prov_prepare_dsa_params, | |
151 | ossl_prov_dsa_priv_to_der, | |
152 | &ctx->sc); | |
153 | } | |
154 | ||
155 | /* Private key : PEM */ | |
156 | static int dsa_pem_priv_data(void *vctx, const OSSL_PARAM params[], BIO *out, | |
157 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
158 | { | |
159 | struct dsa_priv_ctx_st *ctx = vctx; | |
32b0645c RL |
160 | OSSL_OP_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new(); |
161 | OSSL_OP_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free(); | |
162 | OSSL_OP_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import(); | |
264b789b RL |
163 | int ok = 0; |
164 | ||
32b0645c RL |
165 | if (dsa_import != NULL) { |
166 | DSA *dsa; | |
264b789b | 167 | |
32b0645c RL |
168 | if ((dsa = dsa_new(ctx->provctx)) != NULL |
169 | && dsa_import(dsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params) | |
170 | && dsa_pem_priv(ctx, dsa, out, cb, cbarg)) | |
171 | ok = 1; | |
172 | dsa_free(dsa); | |
264b789b RL |
173 | } |
174 | return ok; | |
175 | } | |
176 | ||
177 | static int dsa_pem_priv(void *vctx, void *dsa, BIO *out, | |
178 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
179 | { | |
180 | struct dsa_priv_ctx_st *ctx = vctx; | |
181 | ||
182 | ctx->sc.cb = cb; | |
183 | ctx->sc.cbarg = cbarg; | |
184 | ||
185 | return ossl_prov_write_priv_pem_from_obj(out, dsa, EVP_PKEY_DSA, | |
186 | ossl_prov_prepare_dsa_params, | |
187 | ossl_prov_dsa_priv_to_der, | |
188 | &ctx->sc); | |
189 | } | |
190 | ||
191 | /* | |
192 | * There's no specific print context, so we use the provider context | |
193 | */ | |
194 | static void *dsa_print_newctx(void *provctx) | |
195 | { | |
196 | return provctx; | |
197 | } | |
198 | ||
199 | static void dsa_print_freectx(void *ctx) | |
200 | { | |
201 | } | |
202 | ||
32b0645c | 203 | static int dsa_priv_print_data(void *vctx, const OSSL_PARAM params[], |
264b789b RL |
204 | BIO *out, |
205 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
206 | { | |
32b0645c RL |
207 | struct dsa_priv_ctx_st *ctx = vctx; |
208 | OSSL_OP_keymgmt_new_fn *dsa_new = ossl_prov_get_keymgmt_dsa_new(); | |
209 | OSSL_OP_keymgmt_free_fn *dsa_free = ossl_prov_get_keymgmt_dsa_free(); | |
210 | OSSL_OP_keymgmt_import_fn *dsa_import = ossl_prov_get_keymgmt_dsa_import(); | |
264b789b RL |
211 | int ok = 0; |
212 | ||
32b0645c RL |
213 | if (dsa_import != NULL) { |
214 | DSA *dsa; | |
264b789b | 215 | |
32b0645c RL |
216 | if ((dsa = dsa_new(ctx->provctx)) != NULL |
217 | && dsa_import(dsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params) | |
218 | && dsa_priv_print(ctx, dsa, out, cb, cbarg)) | |
219 | ok = 1; | |
220 | dsa_free(dsa); | |
264b789b RL |
221 | } |
222 | return ok; | |
223 | } | |
224 | ||
225 | static int dsa_priv_print(void *ctx, void *dsa, BIO *out, | |
226 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
227 | { | |
228 | return ossl_prov_print_dsa(out, dsa, dsa_print_priv); | |
229 | } | |
230 | ||
231 | const OSSL_DISPATCH dsa_priv_der_serializer_functions[] = { | |
232 | { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_priv_newctx }, | |
233 | { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_priv_freectx }, | |
234 | { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS, | |
235 | (void (*)(void))dsa_priv_set_ctx_params }, | |
236 | { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS, | |
237 | (void (*)(void))dsa_priv_settable_ctx_params }, | |
238 | { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_priv_der_data }, | |
239 | { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_priv_der }, | |
240 | { 0, NULL } | |
241 | }; | |
242 | ||
243 | const OSSL_DISPATCH dsa_priv_pem_serializer_functions[] = { | |
244 | { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_priv_newctx }, | |
245 | { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_priv_freectx }, | |
246 | { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS, | |
247 | (void (*)(void))dsa_priv_set_ctx_params }, | |
248 | { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS, | |
249 | (void (*)(void))dsa_priv_settable_ctx_params }, | |
250 | { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))dsa_pem_priv_data }, | |
251 | { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_pem_priv }, | |
252 | { 0, NULL } | |
253 | }; | |
254 | ||
255 | const OSSL_DISPATCH dsa_priv_text_serializer_functions[] = { | |
256 | { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))dsa_print_newctx }, | |
257 | { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))dsa_print_freectx }, | |
258 | { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))dsa_priv_print }, | |
259 | { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, | |
260 | (void (*)(void))dsa_priv_print_data }, | |
261 | { 0, NULL } | |
262 | }; |