PROV: Add type specific MSBLOB and PVK decoding for the MS->key decoders
[openssl.git] / providers / implementations / encode_decode / decode_pvk2key.c
1 /*
2  * Copyright 2020-2021 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 "crypto/rsa.h"
28 #include "prov/bio.h"
29 #include "prov/implementations.h"
30 #include "endecoder_local.h"
31
32 struct pvk2key_ctx_st;            /* Forward declaration */
33 typedef int check_key_fn(void *, struct pvk2key_ctx_st *ctx);
34 typedef void adjust_key_fn(void *, struct pvk2key_ctx_st *ctx);
35 typedef void *b2i_PVK_of_bio_pw_fn(BIO *in, pem_password_cb *cb, void *u);
36 typedef void free_key_fn(void *);
37 struct keytype_desc_st {
38     int type;                 /* EVP key type */
39     const char *name;         /* Keytype */
40     const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
41
42     b2i_PVK_of_bio_pw_fn *read_private_key;
43     adjust_key_fn *adjust_key;
44     free_key_fn *free_key;
45 };
46
47 static OSSL_FUNC_decoder_freectx_fn pvk2key_freectx;
48 static OSSL_FUNC_decoder_gettable_params_fn pvk2key_gettable_params;
49 static OSSL_FUNC_decoder_get_params_fn pvk2key_get_params;
50 static OSSL_FUNC_decoder_decode_fn pvk2key_decode;
51 static OSSL_FUNC_decoder_export_object_fn pvk2key_export_object;
52
53 /*
54  * Context used for DER to key decoding.
55  */
56 struct pvk2key_ctx_st {
57     PROV_CTX *provctx;
58     const struct keytype_desc_st *desc;
59 };
60
61 static struct pvk2key_ctx_st *
62 pvk2key_newctx(void *provctx, const struct keytype_desc_st *desc)
63 {
64     struct pvk2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
65
66     if (ctx != NULL) {
67         ctx->provctx = provctx;
68         ctx->desc = desc;
69     }
70     return ctx;
71 }
72
73 static void pvk2key_freectx(void *vctx)
74 {
75     struct pvk2key_ctx_st *ctx = vctx;
76
77     OPENSSL_free(ctx);
78 }
79
80 static const OSSL_PARAM *pvk2key_gettable_params(ossl_unused void *provctx)
81 {
82     static const OSSL_PARAM gettables[] = {
83         { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
84         OSSL_PARAM_END,
85     };
86
87     return gettables;
88 }
89
90 static int pvk2key_get_params(OSSL_PARAM params[])
91 {
92     OSSL_PARAM *p;
93
94     p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
95     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "PVK"))
96         return 0;
97
98     return 1;
99 }
100
101 static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
102                          OSSL_CALLBACK *data_cb, void *data_cbarg,
103                          OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
104 {
105     struct pvk2key_ctx_st *ctx = vctx;
106     BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
107     void *key = NULL;
108     int ok = 0;
109
110     if ((selection == 0
111          || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
112         && ctx->desc->read_private_key != NULL) {
113         struct ossl_passphrase_data_st pwdata;
114
115         memset(&pwdata, 0, sizeof(pwdata));
116         if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
117             goto end;
118         key = ctx->desc->read_private_key(in, ossl_pw_pem_password, &pwdata);
119         if (selection != 0 && key == NULL)
120             goto next;
121     }
122
123     if (key != NULL && ctx->desc->adjust_key != NULL)
124         ctx->desc->adjust_key(key, ctx);
125
126  next:
127     /*
128      * We free resources here so it's not held up during the callback, because
129      * we know the process is recursive and the allocated chunks of memory
130      * add up.
131      */
132     BIO_free(in);
133     in = NULL;
134
135     if (key != NULL) {
136         OSSL_PARAM params[4];
137         int object_type = OSSL_OBJECT_PKEY;
138
139         params[0] =
140             OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
141         params[1] =
142             OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
143                                              (char *)ctx->desc->name, 0);
144         /* The address of the key becomes the octet string */
145         params[2] =
146             OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
147                                               &key, sizeof(key));
148         params[3] = OSSL_PARAM_construct_end();
149
150         ok = data_cb(params, data_cbarg);
151     }
152
153  end:
154     BIO_free(in);
155     ctx->desc->free_key(key);
156
157     return ok;
158 }
159
160 static int pvk2key_export_object(void *vctx,
161                                 const void *reference, size_t reference_sz,
162                                 OSSL_CALLBACK *export_cb, void *export_cbarg)
163 {
164     struct pvk2key_ctx_st *ctx = vctx;
165     OSSL_FUNC_keymgmt_export_fn *export =
166         ossl_prov_get_keymgmt_export(ctx->desc->fns);
167     void *keydata;
168
169     if (reference_sz == sizeof(keydata) && export != NULL) {
170         /* The contents of the reference is the address to our object */
171         keydata = *(void **)reference;
172
173         return export(keydata, OSSL_KEYMGMT_SELECT_ALL,
174                       export_cb, export_cbarg);
175     }
176     return 0;
177 }
178
179 /* ---------------------------------------------------------------------- */
180
181 #define dsa_private_key_bio     (b2i_PVK_of_bio_pw_fn *)b2i_DSA_PVK_bio
182 #define dsa_adjust              NULL
183 #define dsa_free                (void (*)(void *))DSA_free
184
185 /* ---------------------------------------------------------------------- */
186
187 #define rsa_private_key_bio     (b2i_PVK_of_bio_pw_fn *)b2i_RSA_PVK_bio
188
189 static void rsa_adjust(void *key, struct pvk2key_ctx_st *ctx)
190 {
191     ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
192 }
193
194 #define rsa_free                (void (*)(void *))RSA_free
195
196 /* ---------------------------------------------------------------------- */
197
198 #define IMPLEMENT_MS(KEYTYPE, keytype)                                  \
199     static const struct keytype_desc_st                                 \
200     pvk2##keytype##_desc = {                                            \
201         EVP_PKEY_##KEYTYPE, #KEYTYPE,                                   \
202         ossl_##keytype##_keymgmt_functions,                             \
203         keytype##_private_key_bio,                                      \
204         keytype##_adjust,                                               \
205         keytype##_free                                                  \
206     };                                                                  \
207     static OSSL_FUNC_decoder_newctx_fn pvk2##keytype##_newctx;          \
208     static void *pvk2##keytype##_newctx(void *provctx)                  \
209     {                                                                   \
210         return pvk2key_newctx(provctx, &pvk2##keytype##_desc);          \
211     }                                                                   \
212     const OSSL_DISPATCH                                                 \
213     ossl_##pvk_to_##keytype##_decoder_functions[] = {                   \
214         { OSSL_FUNC_DECODER_NEWCTX,                                     \
215           (void (*)(void))pvk2##keytype##_newctx },                     \
216         { OSSL_FUNC_DECODER_FREECTX,                                    \
217           (void (*)(void))pvk2key_freectx },                            \
218         { OSSL_FUNC_DECODER_GETTABLE_PARAMS,                            \
219           (void (*)(void))pvk2key_gettable_params },                    \
220         { OSSL_FUNC_DECODER_GET_PARAMS,                                 \
221           (void (*)(void))pvk2key_get_params },                         \
222         { OSSL_FUNC_DECODER_DECODE,                                     \
223           (void (*)(void))pvk2key_decode },                             \
224         { OSSL_FUNC_DECODER_EXPORT_OBJECT,                              \
225           (void (*)(void))pvk2key_export_object },                      \
226         { 0, NULL }                                                     \
227     }
228
229 #ifndef OPENSSL_NO_DSA
230 IMPLEMENT_MS(DSA, dsa);
231 #endif
232 IMPLEMENT_MS(RSA, rsa);