coverity 1456642: fix null check
[oweals/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_importkey_fn *rsa_importkey =
147         ossl_prov_get_rsa_importkey();
148     int ok = 0;
149
150     if (rsa_importkey != NULL) {
151         RSA *rsa = rsa_importkey(ctx->provctx, params);
152
153         ok = rsa_priv_der(vctx, rsa, out, cb, cbarg);
154         RSA_free(rsa);
155     }
156     return ok;
157 }
158
159 static int rsa_priv_der(void *vctx, void *rsa, BIO *out,
160                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
161 {
162     struct rsa_priv_ctx_st *ctx = vctx;
163     int ret;
164
165     ctx->sc.cb = cb;
166     ctx->sc.cbarg = cbarg;
167
168     ret = ossl_prov_write_priv_der_from_obj(out, rsa, EVP_PKEY_RSA,
169                                             prepare_rsa_params,
170                                             (i2d_of_void *)i2d_RSAPrivateKey,
171                                             &ctx->sc);
172
173     return ret;
174 }
175
176 /* Private key : PEM */
177 static int rsa_pem_priv_data(void *vctx, const OSSL_PARAM params[], BIO *out,
178                              OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
179 {
180     struct rsa_priv_ctx_st *ctx = vctx;
181     OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
182         ossl_prov_get_rsa_importkey();
183     int ok = 0;
184
185     if (rsa_importkey != NULL) {
186         RSA *rsa = rsa_importkey(ctx, params);
187
188         ok = rsa_pem_priv(vctx, rsa, out, cb, cbarg);
189         RSA_free(rsa);
190     }
191     return ok;
192 }
193
194 static int rsa_pem_priv(void *vctx, void *rsa, BIO *out,
195                         OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
196 {
197     struct rsa_priv_ctx_st *ctx = vctx;
198     int ret;
199
200     ctx->sc.cb = cb;
201     ctx->sc.cbarg = cbarg;
202
203     ret = ossl_prov_write_priv_pem_from_obj(out, rsa, EVP_PKEY_RSA,
204                                             prepare_rsa_params,
205                                             (i2d_of_void *)i2d_RSAPrivateKey,
206                                             &ctx->sc);
207
208     return ret;
209 }
210
211 /*
212  * There's no specific print context, so we use the provider context
213  */
214 static void *rsa_print_newctx(void *provctx)
215 {
216     return provctx;
217 }
218
219 static void rsa_print_freectx(void *ctx)
220 {
221 }
222
223 static int rsa_priv_print_data(void *provctx, const OSSL_PARAM params[],
224                                BIO *out,
225                                OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
226 {
227     OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
228         ossl_prov_get_rsa_importkey();
229     int ok = 0;
230
231     if (rsa_importkey != NULL) {
232         RSA *rsa = rsa_importkey(provctx, params); /* ctx == provctx */
233
234         ok = rsa_priv_print(provctx, rsa, out, cb, cbarg);
235         RSA_free(rsa);
236     }
237     return ok;
238 }
239
240 static int rsa_priv_print(void *ctx, void *rsa, BIO *out,
241                           OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
242 {
243     return ossl_prov_print_rsa(out, rsa, 1);
244 }
245
246 const OSSL_DISPATCH rsa_priv_der_serializer_functions[] = {
247     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_priv_newctx },
248     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_priv_freectx },
249     { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
250       (void (*)(void))rsa_priv_set_ctx_params },
251     { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
252       (void (*)(void))rsa_priv_settable_ctx_params },
253     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_priv_der_data },
254     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_priv_der },
255     { 0, NULL }
256 };
257
258 const OSSL_DISPATCH rsa_priv_pem_serializer_functions[] = {
259     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_priv_newctx },
260     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_priv_freectx },
261     { OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS,
262       (void (*)(void))rsa_priv_set_ctx_params },
263     { OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS,
264       (void (*)(void))rsa_priv_settable_ctx_params },
265     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pem_priv_data },
266     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pem_priv },
267     { 0, NULL }
268 };
269
270 const OSSL_DISPATCH rsa_priv_text_serializer_functions[] = {
271     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_print_newctx },
272     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_print_freectx },
273     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_priv_print },
274     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
275       (void (*)(void))rsa_priv_print_data },
276     { 0, NULL }
277 };