]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/serializers/serializer_rsa_priv.c
Make the naming scheme for dispatched functions more consistent
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_rsa_priv.c
CommitLineData
677add38 1/*
33388b44 2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
677add38
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
c5f87134
P
10/*
11 * RSA 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>
677add38
RL
17#include <openssl/core_names.h>
18#include <openssl/err.h>
19#include <openssl/pem.h>
20#include <openssl/rsa.h>
21#include <openssl/types.h>
22#include <openssl/params.h>
23#include <openssl/safestack.h>
ea297dca 24#include "crypto/rsa.h"
677add38
RL
25#include "prov/bio.h"
26#include "prov/implementations.h"
27#include "prov/providercommonerr.h"
d40b42ab 28#include "prov/provider_ctx.h"
677add38
RL
29#include "serializer_local.h"
30
363b1e5d
DMSP
31static OSSL_FUNC_serializer_newctx_fn rsa_priv_newctx;
32static OSSL_FUNC_serializer_freectx_fn rsa_priv_freectx;
33static OSSL_FUNC_serializer_set_ctx_params_fn rsa_priv_set_ctx_params;
34static OSSL_FUNC_serializer_settable_ctx_params_fn rsa_priv_settable_ctx_params;
35static OSSL_FUNC_serializer_serialize_data_fn rsa_priv_der_data;
36static OSSL_FUNC_serializer_serialize_object_fn rsa_priv_der;
37static OSSL_FUNC_serializer_serialize_data_fn rsa_pem_priv_data;
38static OSSL_FUNC_serializer_serialize_object_fn rsa_pem_priv;
39
40static OSSL_FUNC_serializer_newctx_fn rsa_print_newctx;
41static OSSL_FUNC_serializer_freectx_fn rsa_print_freectx;
42static OSSL_FUNC_serializer_serialize_data_fn rsa_priv_print_data;
43static OSSL_FUNC_serializer_serialize_object_fn rsa_priv_print;
677add38
RL
44
45 /*
46 * Context used for private key serialization.
47 */
48struct rsa_priv_ctx_st {
49 void *provctx;
50
51 struct pkcs8_encrypt_ctx_st sc;
52};
53
677add38
RL
54/* Private key : context */
55static void *rsa_priv_newctx(void *provctx)
56{
57 struct rsa_priv_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
58
59 if (ctx != NULL) {
60 ctx->provctx = provctx;
ff19035e
P
61 /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
62 ctx->sc.pbe_nid = -1;
677add38 63 }
677add38
RL
64 return ctx;
65}
66
67static void rsa_priv_freectx(void *vctx)
68{
69 struct rsa_priv_ctx_st *ctx = vctx;
70
71 EVP_CIPHER_free(ctx->sc.cipher);
72 OPENSSL_free(ctx->sc.cipher_pass);
73 OPENSSL_free(ctx);
74}
75
76static const OSSL_PARAM *rsa_priv_settable_ctx_params(void)
77{
78 static const OSSL_PARAM settables[] = {
79 OSSL_PARAM_utf8_string(OSSL_SERIALIZER_PARAM_CIPHER, NULL, 0),
80 OSSL_PARAM_octet_string(OSSL_SERIALIZER_PARAM_PASS, NULL, 0),
81 OSSL_PARAM_END,
82 };
83
84 return settables;
85}
86
87static int rsa_priv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
88{
89 struct rsa_priv_ctx_st *ctx = vctx;
90 const OSSL_PARAM *p;
91
92 if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_CIPHER))
93 != NULL) {
94 const OSSL_PARAM *propsp =
95 OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PROPERTIES);
96 const char *props = NULL;
97
98 if (p->data_type != OSSL_PARAM_UTF8_STRING)
99 return 0;
100 if (propsp != NULL && propsp->data_type != OSSL_PARAM_UTF8_STRING)
101 return 0;
102 props = (propsp != NULL ? propsp->data : NULL);
103
104 EVP_CIPHER_free(ctx->sc.cipher);
105 ctx->sc.cipher_intent = p->data != NULL;
106 if (p->data != NULL
107 && ((ctx->sc.cipher = EVP_CIPHER_fetch(NULL, p->data, props))
108 == NULL))
109 return 0;
110 }
111 if ((p = OSSL_PARAM_locate_const(params, OSSL_SERIALIZER_PARAM_PASS))
112 != NULL) {
113 OPENSSL_free(ctx->sc.cipher_pass);
114 ctx->sc.cipher_pass = NULL;
115 if (!OSSL_PARAM_get_octet_string(p, &ctx->sc.cipher_pass, 0,
116 &ctx->sc.cipher_pass_length))
117 return 0;
118 }
119 return 1;
120}
121
122/* Private key : DER */
d40b42ab
MC
123static int rsa_priv_der_data(void *vctx, const OSSL_PARAM params[],
124 OSSL_CORE_BIO *out,
677add38
RL
125 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
126{
127 struct rsa_priv_ctx_st *ctx = vctx;
363b1e5d
DMSP
128 OSSL_FUNC_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
129 OSSL_FUNC_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
130 OSSL_FUNC_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
677add38
RL
131 int ok = 0;
132
32b0645c
RL
133 if (rsa_import != NULL) {
134 RSA *rsa;
677add38 135
32b0645c
RL
136 if ((rsa = rsa_new(ctx->provctx)) != NULL
137 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
138 && rsa_priv_der(ctx, rsa, out, cb, cbarg))
139 ok = 1;
140 rsa_free(rsa);
677add38
RL
141 }
142 return ok;
143}
144
d40b42ab 145static int rsa_priv_der(void *vctx, void *rsa, OSSL_CORE_BIO *cout,
677add38
RL
146 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
147{
148 struct rsa_priv_ctx_st *ctx = vctx;
149 int ret;
d40b42ab
MC
150 BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
151
152 if (out == NULL)
153 return 0;
677add38
RL
154
155 ctx->sc.cb = cb;
156 ctx->sc.cbarg = cbarg;
157
ea297dca
RL
158 ret = ossl_prov_write_priv_der_from_obj(out, rsa,
159 ossl_prov_rsa_type_to_evp(rsa),
160 ossl_prov_prepare_rsa_params,
677add38
RL
161 (i2d_of_void *)i2d_RSAPrivateKey,
162 &ctx->sc);
d40b42ab 163 BIO_free(out);
677add38
RL
164
165 return ret;
166}
167
168/* Private key : PEM */
d40b42ab
MC
169static int rsa_pem_priv_data(void *vctx, const OSSL_PARAM params[],
170 OSSL_CORE_BIO *out,
677add38
RL
171 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
172{
173 struct rsa_priv_ctx_st *ctx = vctx;
363b1e5d
DMSP
174 OSSL_FUNC_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
175 OSSL_FUNC_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
176 OSSL_FUNC_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
677add38
RL
177 int ok = 0;
178
32b0645c
RL
179 if (rsa_import != NULL) {
180 RSA *rsa;
677add38 181
32b0645c
RL
182 if ((rsa = rsa_new(ctx->provctx)) != NULL
183 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
184 && rsa_pem_priv(ctx, rsa, out, cb, cbarg))
185 ok = 1;
186 rsa_free(rsa);
677add38
RL
187 }
188 return ok;
189}
190
d40b42ab 191static int rsa_pem_priv(void *vctx, void *rsa, OSSL_CORE_BIO *cout,
677add38
RL
192 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
193{
194 struct rsa_priv_ctx_st *ctx = vctx;
195 int ret;
d40b42ab
MC
196 BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
197
198 if (out == NULL)
199 return 0;
677add38
RL
200
201 ctx->sc.cb = cb;
202 ctx->sc.cbarg = cbarg;
203
ea297dca
RL
204 ret = ossl_prov_write_priv_pem_from_obj(out, rsa,
205 ossl_prov_rsa_type_to_evp(rsa),
206 ossl_prov_prepare_rsa_params,
677add38
RL
207 (i2d_of_void *)i2d_RSAPrivateKey,
208 &ctx->sc);
d40b42ab 209 BIO_free(out);
677add38
RL
210
211 return ret;
212}
213
214/*
215 * There's no specific print context, so we use the provider context
216 */
217static void *rsa_print_newctx(void *provctx)
218{
219 return provctx;
220}
221
222static void rsa_print_freectx(void *ctx)
223{
224}
225
32b0645c 226static int rsa_priv_print_data(void *vctx, const OSSL_PARAM params[],
d40b42ab 227 OSSL_CORE_BIO *out,
677add38
RL
228 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
229{
32b0645c 230 struct rsa_priv_ctx_st *ctx = vctx;
363b1e5d
DMSP
231 OSSL_FUNC_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
232 OSSL_FUNC_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
233 OSSL_FUNC_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
677add38
RL
234 int ok = 0;
235
32b0645c
RL
236 if (rsa_import != NULL) {
237 RSA *rsa;
677add38 238
32b0645c
RL
239 if ((rsa = rsa_new(ctx->provctx)) != NULL
240 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
241 && rsa_priv_print(ctx, rsa, out, cb, cbarg))
242 ok = 1;
243 rsa_free(rsa);
677add38
RL
244 }
245 return ok;
246}
247
d40b42ab 248static int rsa_priv_print(void *ctx, void *rsa, OSSL_CORE_BIO *cout,
677add38
RL
249 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
250{
d40b42ab
MC
251 BIO *out = bio_new_from_core_bio(ctx, cout);
252 int ret;
253
254 if (out == NULL)
255 return 0;
256
257 ret = ossl_prov_print_rsa(out, rsa, 1);
258 BIO_free(out);
259
260 return ret;
677add38
RL
261}
262
263const OSSL_DISPATCH rsa_priv_der_serializer_functions[] = {
264 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_priv_newctx },
265 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_priv_freectx },
266 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
267 (void (*)(void))rsa_priv_set_ctx_params },
268 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
269 (void (*)(void))rsa_priv_settable_ctx_params },
270 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_priv_der_data },
271 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_priv_der },
272 { 0, NULL }
273};
274
275const OSSL_DISPATCH rsa_priv_pem_serializer_functions[] = {
276 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_priv_newctx },
277 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_priv_freectx },
278 { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
279 (void (*)(void))rsa_priv_set_ctx_params },
280 { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
281 (void (*)(void))rsa_priv_settable_ctx_params },
282 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pem_priv_data },
283 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pem_priv },
284 { 0, NULL }
285};
286
287const OSSL_DISPATCH rsa_priv_text_serializer_functions[] = {
288 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_print_newctx },
289 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_print_freectx },
290 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_priv_print },
291 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
292 (void (*)(void))rsa_priv_print_data },
293 { 0, NULL }
294};