Rename <openssl/core_numbers.h> -> <openssl/core_dispatch.h>
[openssl.git] / providers / implementations / serializers / serializer_rsa_priv.c
1 /*
2  * Copyright 2019-2020 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 /*
11  * RSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <openssl/core_dispatch.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>
24 #include "crypto/rsa.h"
25 #include "prov/bio.h"
26 #include "prov/implementations.h"
27 #include "prov/providercommonerr.h"
28 #include "prov/provider_ctx.h"
29 #include "serializer_local.h"
30
31 static OSSL_OP_serializer_newctx_fn rsa_priv_newctx;
32 static OSSL_OP_serializer_freectx_fn rsa_priv_freectx;
33 static OSSL_OP_serializer_set_ctx_params_fn rsa_priv_set_ctx_params;
34 static OSSL_OP_serializer_settable_ctx_params_fn rsa_priv_settable_ctx_params;
35 static OSSL_OP_serializer_serialize_data_fn rsa_priv_der_data;
36 static OSSL_OP_serializer_serialize_object_fn rsa_priv_der;
37 static OSSL_OP_serializer_serialize_data_fn rsa_pem_priv_data;
38 static OSSL_OP_serializer_serialize_object_fn rsa_pem_priv;
39
40 static OSSL_OP_serializer_newctx_fn rsa_print_newctx;
41 static OSSL_OP_serializer_freectx_fn rsa_print_freectx;
42 static OSSL_OP_serializer_serialize_data_fn rsa_priv_print_data;
43 static OSSL_OP_serializer_serialize_object_fn rsa_priv_print;
44
45  /*
46  * Context used for private key serialization.
47  */
48 struct rsa_priv_ctx_st {
49     void *provctx;
50
51     struct pkcs8_encrypt_ctx_st sc;
52 };
53
54 /* Private key : context */
55 static 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;
61         /* -1 is the "whatever" indicator, i.e. the PKCS8 library default PBE */
62         ctx->sc.pbe_nid = -1;
63     }
64     return ctx;
65 }
66
67 static 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
76 static 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
87 static 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 */
123 static int rsa_priv_der_data(void *vctx, const OSSL_PARAM params[],
124                              OSSL_CORE_BIO *out,
125                              OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
126 {
127     struct rsa_priv_ctx_st *ctx = vctx;
128     OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
129     OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
130     OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
131     int ok = 0;
132
133     if (rsa_import != NULL) {
134         RSA *rsa;
135
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);
141     }
142     return ok;
143 }
144
145 static int rsa_priv_der(void *vctx, void *rsa, OSSL_CORE_BIO *cout,
146                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
147 {
148     struct rsa_priv_ctx_st *ctx = vctx;
149     int ret;
150     BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
151
152     if (out == NULL)
153         return 0;
154
155     ctx->sc.cb = cb;
156     ctx->sc.cbarg = cbarg;
157
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,
161                                             (i2d_of_void *)i2d_RSAPrivateKey,
162                                             &ctx->sc);
163     BIO_free(out);
164
165     return ret;
166 }
167
168 /* Private key : PEM */
169 static int rsa_pem_priv_data(void *vctx, const OSSL_PARAM params[],
170                              OSSL_CORE_BIO *out,
171                              OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
172 {
173     struct rsa_priv_ctx_st *ctx = vctx;
174     OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
175     OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
176     OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
177     int ok = 0;
178
179     if (rsa_import != NULL) {
180         RSA *rsa;
181
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);
187     }
188     return ok;
189 }
190
191 static int rsa_pem_priv(void *vctx, void *rsa, OSSL_CORE_BIO *cout,
192                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
193 {
194     struct rsa_priv_ctx_st *ctx = vctx;
195     int ret;
196     BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
197
198     if (out == NULL)
199         return 0;
200
201     ctx->sc.cb = cb;
202     ctx->sc.cbarg = cbarg;
203
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,
207                                             (i2d_of_void *)i2d_RSAPrivateKey,
208                                             &ctx->sc);
209     BIO_free(out);
210
211     return ret;
212 }
213
214 /*
215  * There's no specific print context, so we use the provider context
216  */
217 static void *rsa_print_newctx(void *provctx)
218 {
219     return provctx;
220 }
221
222 static void rsa_print_freectx(void *ctx)
223 {
224 }
225
226 static int rsa_priv_print_data(void *vctx, const OSSL_PARAM params[],
227                                OSSL_CORE_BIO *out,
228                                OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
229 {
230     struct rsa_priv_ctx_st *ctx = vctx;
231     OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
232     OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
233     OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
234     int ok = 0;
235
236     if (rsa_import != NULL) {
237         RSA *rsa;
238
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);
244     }
245     return ok;
246 }
247
248 static int rsa_priv_print(void *ctx, void *rsa, OSSL_CORE_BIO *cout,
249                           OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
250 {
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;
261 }
262
263 const 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
275 const 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
287 const 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 };