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