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