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