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