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