Remove unnecessary guards around MSBLOB and PVK readers and writers
[openssl.git] / providers / implementations / encode_decode / decode_ms2key.c
1 /*
2  * Copyright 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  * low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <string.h>
17
18 #include <openssl/core_dispatch.h>
19 #include <openssl/core_names.h>
20 #include <openssl/core_object.h>
21 #include <openssl/crypto.h>
22 #include <openssl/params.h>
23 #include <openssl/pem.h>         /* For public PVK functions */
24 #include <openssl/x509.h>
25 #include "internal/passphrase.h"
26 #include "crypto/pem.h"          /* For internal PVK and "blob" headers */
27 #include "prov/bio.h"
28 #include "prov/implementations.h"
29 #include "endecoder_local.h"
30
31 static EVP_PKEY *read_msblob(PROV_CTX *provctx, OSSL_CORE_BIO *cin, int *ispub)
32 {
33     BIO *in = bio_new_from_core_bio(provctx, cin);
34     EVP_PKEY *pkey = ossl_b2i_bio(in, ispub);
35
36     BIO_free(in);
37     return pkey;
38 }
39
40 static EVP_PKEY *read_pvk(PROV_CTX *provctx, OSSL_CORE_BIO *cin,
41                           OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
42 {
43     BIO *in = NULL;
44     EVP_PKEY *pkey = NULL;
45     struct ossl_passphrase_data_st pwdata;
46
47     memset(&pwdata, 0, sizeof(pwdata));
48     if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
49         return NULL;
50
51     in = bio_new_from_core_bio(provctx, cin);
52     pkey = b2i_PVK_bio(in, ossl_pw_pem_password, &pwdata);
53     BIO_free(in);
54
55     return pkey;
56 }
57
58 static OSSL_FUNC_decoder_freectx_fn ms2key_freectx;
59 static OSSL_FUNC_decoder_gettable_params_fn ms2key_gettable_params;
60 static OSSL_FUNC_decoder_get_params_fn msblob2key_get_params;
61 static OSSL_FUNC_decoder_get_params_fn pvk2key_get_params;
62 static OSSL_FUNC_decoder_decode_fn msblob2key_decode;
63 static OSSL_FUNC_decoder_decode_fn pvk2key_decode;
64 static OSSL_FUNC_decoder_export_object_fn ms2key_export_object;
65
66 typedef void *(extract_key_fn)(EVP_PKEY *);
67 typedef void (free_key_fn)(void *);
68 struct keytype_desc_st {
69     int type;                 /* EVP key type */
70     const char *name;         /* Keytype */
71     const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
72
73     /*
74      * These must be the correct EVP_PKEY_get1_{TYPE}() and {TYPE}_free()
75      * function for the key.
76      */
77     extract_key_fn *extract_key;
78     free_key_fn *free_key;
79 };
80
81 /*
82  * Context used for DER to key decoding.
83  */
84 struct ms2key_ctx_st {
85     PROV_CTX *provctx;
86     const struct keytype_desc_st *desc;
87 };
88
89 static struct ms2key_ctx_st *
90 ms2key_newctx(void *provctx, const struct keytype_desc_st *desc)
91 {
92     struct ms2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
93
94     if (ctx != NULL) {
95         ctx->provctx = provctx;
96         ctx->desc = desc;
97     }
98     return ctx;
99 }
100
101 static void ms2key_freectx(void *vctx)
102 {
103     struct ms2key_ctx_st *ctx = vctx;
104
105     OPENSSL_free(ctx);
106 }
107
108 static const OSSL_PARAM *ms2key_gettable_params(ossl_unused void *provctx)
109 {
110     static const OSSL_PARAM gettables[] = {
111         { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
112         OSSL_PARAM_END,
113     };
114
115     return gettables;
116 }
117
118 static int msblob2key_get_params(OSSL_PARAM params[])
119 {
120     OSSL_PARAM *p;
121
122     p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
123     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "MSBLOB"))
124         return 0;
125
126     return 1;
127 }
128
129 static int pvk2key_get_params(OSSL_PARAM params[])
130 {
131     OSSL_PARAM *p;
132
133     p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
134     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "PVK"))
135         return 0;
136
137     return 1;
138 }
139
140 static int ms2key_post(struct ms2key_ctx_st *ctx, EVP_PKEY *pkey,
141                        OSSL_CALLBACK *data_cb, void *data_cbarg)
142 {
143     void *key = NULL;
144     int ok = 0;
145
146     if (pkey != NULL) {
147         /*
148          * Tear out the low-level key pointer from the pkey,
149          * but only if it matches the expected key type.
150          *
151          * TODO(3.0): The check should be done with EVP_PKEY_is_a(), but
152          * as long as we still have #legacy internal keys, it's safer to
153          * use the type numbers in side the provider.
154          */
155         if (EVP_PKEY_id(pkey) == ctx->desc->type)
156             key = ctx->desc->extract_key(pkey);
157     }
158
159     if (key != NULL) {
160         OSSL_PARAM params[4];
161         int object_type = OSSL_OBJECT_PKEY;
162
163         params[0] =
164             OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
165         params[1] =
166             OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
167                                              (char *)ctx->desc->name, 0);
168         /* The address of the key becomes the octet string */
169         params[2] =
170             OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
171                                               &key, sizeof(key));
172         params[3] = OSSL_PARAM_construct_end();
173
174         ok = data_cb(params, data_cbarg);
175     }
176     ctx->desc->free_key(key);
177
178     return ok;
179 }
180
181 static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
182                              OSSL_CALLBACK *data_cb, void *data_cbarg,
183                              OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
184 {
185     struct ms2key_ctx_st *ctx = vctx;
186     int ispub = -1;
187     EVP_PKEY *pkey = read_msblob(ctx->provctx, cin, &ispub);
188     int ok = 0;
189
190     if (selection == 0
191         || (ispub
192             ? (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
193             : (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0))
194         ok = ms2key_post(ctx, pkey, data_cb, data_cbarg);
195
196     EVP_PKEY_free(pkey);
197     return ok;
198 }
199
200 static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
201                           OSSL_CALLBACK *data_cb, void *data_cbarg,
202                           OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
203 {
204     struct ms2key_ctx_st *ctx = vctx;
205     EVP_PKEY *pkey = read_pvk(ctx->provctx, cin, pw_cb, pw_cbarg);
206     int ok = 0;
207
208     if (selection == 0
209         || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
210         ok = ms2key_post(ctx, pkey, data_cb, data_cbarg);
211
212     EVP_PKEY_free(pkey);
213     return ok;
214 }
215
216 static int ms2key_export_object(void *vctx,
217                                 const void *reference, size_t reference_sz,
218                                 OSSL_CALLBACK *export_cb, void *export_cbarg)
219 {
220     struct ms2key_ctx_st *ctx = vctx;
221     OSSL_FUNC_keymgmt_export_fn *export =
222         ossl_prov_get_keymgmt_export(ctx->desc->fns);
223     void *keydata;
224
225     if (reference_sz == sizeof(keydata) && export != NULL) {
226         /* The contents of the reference is the address to our object */
227         keydata = *(void **)reference;
228
229         return export(keydata, OSSL_KEYMGMT_SELECT_ALL,
230                       export_cb, export_cbarg);
231     }
232     return 0;
233 }
234
235 #define IMPLEMENT_TYPE(KEYTYPEstr, KEYTYPE, keytype, extract, free)     \
236     static const struct keytype_desc_st keytype##_desc;                 \
237     static OSSL_FUNC_decoder_newctx_fn ms2##keytype##_newctx;           \
238     static void *ms2##keytype##_newctx(void *provctx)                   \
239     {                                                                   \
240         return ms2key_newctx(provctx, &keytype##_desc);                 \
241     }                                                                   \
242     static const struct keytype_desc_st keytype##_desc =                \
243         { EVP_PKEY_##KEYTYPE, KEYTYPEstr,                               \
244           ossl_##keytype##_keymgmt_functions,                           \
245           (extract_key_fn *)extract,                                    \
246           (free_key_fn *)free }
247
248 #define IMPLEMENT_MS(mstype, keytype)                                   \
249     const OSSL_DISPATCH                                                 \
250         ossl_##mstype##_to_##keytype##_decoder_functions[] = {          \
251         { OSSL_FUNC_DECODER_NEWCTX,                                     \
252           (void (*)(void))ms2##keytype##_newctx },                      \
253         { OSSL_FUNC_DECODER_FREECTX,                                    \
254           (void (*)(void))ms2key_freectx },                             \
255         { OSSL_FUNC_DECODER_GETTABLE_PARAMS,                            \
256           (void (*)(void))ms2key_gettable_params },                     \
257         { OSSL_FUNC_DECODER_GET_PARAMS,                                 \
258           (void (*)(void))mstype##2key_get_params },                    \
259         { OSSL_FUNC_DECODER_DECODE,                                     \
260           (void (*)(void))mstype##2key_decode },                        \
261         { OSSL_FUNC_DECODER_EXPORT_OBJECT,                              \
262           (void (*)(void))ms2key_export_object },                       \
263         { 0, NULL }                                                     \
264     }
265
266 #ifndef OPENSSL_NO_DSA
267 IMPLEMENT_TYPE("DSA", DSA, dsa, EVP_PKEY_get1_DSA, DSA_free);
268 IMPLEMENT_MS(msblob, dsa);
269 IMPLEMENT_MS(pvk, dsa);
270 #endif
271 IMPLEMENT_TYPE("RSA", RSA, rsa, EVP_PKEY_get1_RSA, RSA_free);
272 IMPLEMENT_MS(msblob, rsa);
273 IMPLEMENT_MS(pvk, rsa);