Pass library context and property query into private 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/err.h>
24 #include <openssl/pem.h>         /* For public PVK functions */
25 #include <openssl/x509.h>
26 #include "internal/passphrase.h"
27 #include "crypto/pem.h"          /* For internal PVK and "blob" headers */
28 #include "crypto/rsa.h"
29 #include "prov/bio.h"
30 #include "prov/implementations.h"
31 #include "endecoder_local.h"
32
33 struct pvk2key_ctx_st;            /* Forward declaration */
34 typedef int check_key_fn(void *, struct pvk2key_ctx_st *ctx);
35 typedef void adjust_key_fn(void *, struct pvk2key_ctx_st *ctx);
36 typedef void *b2i_PVK_of_bio_pw_fn(BIO *in, pem_password_cb *cb, void *u,
37                                    OSSL_LIB_CTX *libctx, const char *propq);
38 typedef void free_key_fn(void *);
39 struct keytype_desc_st {
40     int type;                 /* EVP key type */
41     const char *name;         /* Keytype */
42     const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
43
44     b2i_PVK_of_bio_pw_fn *read_private_key;
45     adjust_key_fn *adjust_key;
46     free_key_fn *free_key;
47 };
48
49 static OSSL_FUNC_decoder_freectx_fn pvk2key_freectx;
50 static OSSL_FUNC_decoder_gettable_params_fn pvk2key_gettable_params;
51 static OSSL_FUNC_decoder_get_params_fn pvk2key_get_params;
52 static OSSL_FUNC_decoder_decode_fn pvk2key_decode;
53 static OSSL_FUNC_decoder_export_object_fn pvk2key_export_object;
54
55 /*
56  * Context used for DER to key decoding.
57  */
58 struct pvk2key_ctx_st {
59     PROV_CTX *provctx;
60     const struct keytype_desc_st *desc;
61 };
62
63 static struct pvk2key_ctx_st *
64 pvk2key_newctx(void *provctx, const struct keytype_desc_st *desc)
65 {
66     struct pvk2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
67
68     if (ctx != NULL) {
69         ctx->provctx = provctx;
70         ctx->desc = desc;
71     }
72     return ctx;
73 }
74
75 static void pvk2key_freectx(void *vctx)
76 {
77     struct pvk2key_ctx_st *ctx = vctx;
78
79     OPENSSL_free(ctx);
80 }
81
82 static const OSSL_PARAM *pvk2key_gettable_params(ossl_unused void *provctx)
83 {
84     static const OSSL_PARAM gettables[] = {
85         { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
86         OSSL_PARAM_END,
87     };
88
89     return gettables;
90 }
91
92 static int pvk2key_get_params(OSSL_PARAM params[])
93 {
94     OSSL_PARAM *p;
95
96     p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
97     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "PVK"))
98         return 0;
99
100     return 1;
101 }
102
103 static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
104                          OSSL_CALLBACK *data_cb, void *data_cbarg,
105                          OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
106 {
107     struct pvk2key_ctx_st *ctx = vctx;
108     BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
109     void *key = NULL;
110     int ok = 0;
111
112     if ((selection == 0
113          || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
114         && ctx->desc->read_private_key != NULL) {
115         struct ossl_passphrase_data_st pwdata;
116         int err, lib, reason;
117
118         memset(&pwdata, 0, sizeof(pwdata));
119         if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
120             goto end;
121
122         key = ctx->desc->read_private_key(in, ossl_pw_pem_password, &pwdata,
123                                           PROV_LIBCTX_OF(ctx->provctx), NULL);
124
125         /*
126          * Because the PVK API doesn't have a separate decrypt call, we need
127          * to check the error queue for certain well known errors that are
128          * considered fatal and which we pass through, while the rest gets
129          * thrown away.
130          */
131         err = ERR_peek_last_error();
132         lib = ERR_GET_LIB(err);
133         reason = ERR_GET_REASON(err);
134         if (lib == ERR_LIB_PEM
135             && (reason == PEM_R_BAD_PASSWORD_READ
136                 || reason == PEM_R_BAD_DECRYPT)) {
137             ERR_clear_last_mark();
138             goto end;
139         }
140
141         if (selection != 0 && key == NULL)
142             goto next;
143     }
144
145     if (key != NULL && ctx->desc->adjust_key != NULL)
146         ctx->desc->adjust_key(key, ctx);
147
148  next:
149     /*
150      * Indicated that we successfully decoded something, or not at all.
151      * Ending up "empty handed" is not an error.
152      */
153     ok = 1;
154
155     /*
156      * We free resources here so it's not held up during the callback, because
157      * we know the process is recursive and the allocated chunks of memory
158      * add up.
159      */
160     BIO_free(in);
161     in = NULL;
162
163     if (key != NULL) {
164         OSSL_PARAM params[4];
165         int object_type = OSSL_OBJECT_PKEY;
166
167         params[0] =
168             OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
169         params[1] =
170             OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
171                                              (char *)ctx->desc->name, 0);
172         /* The address of the key becomes the octet string */
173         params[2] =
174             OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
175                                               &key, sizeof(key));
176         params[3] = OSSL_PARAM_construct_end();
177
178         ok = data_cb(params, data_cbarg);
179     }
180
181  end:
182     BIO_free(in);
183     ctx->desc->free_key(key);
184
185     return ok;
186 }
187
188 static int pvk2key_export_object(void *vctx,
189                                 const void *reference, size_t reference_sz,
190                                 OSSL_CALLBACK *export_cb, void *export_cbarg)
191 {
192     struct pvk2key_ctx_st *ctx = vctx;
193     OSSL_FUNC_keymgmt_export_fn *export =
194         ossl_prov_get_keymgmt_export(ctx->desc->fns);
195     void *keydata;
196
197     if (reference_sz == sizeof(keydata) && export != NULL) {
198         /* The contents of the reference is the address to our object */
199         keydata = *(void **)reference;
200
201         return export(keydata, OSSL_KEYMGMT_SELECT_ALL,
202                       export_cb, export_cbarg);
203     }
204     return 0;
205 }
206
207 /* ---------------------------------------------------------------------- */
208
209 #define dsa_private_key_bio     (b2i_PVK_of_bio_pw_fn *)b2i_DSA_PVK_bio_ex
210 #define dsa_adjust              NULL
211 #define dsa_free                (void (*)(void *))DSA_free
212
213 /* ---------------------------------------------------------------------- */
214
215 #define rsa_private_key_bio     (b2i_PVK_of_bio_pw_fn *)b2i_RSA_PVK_bio_ex
216
217 static void rsa_adjust(void *key, struct pvk2key_ctx_st *ctx)
218 {
219     ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
220 }
221
222 #define rsa_free                (void (*)(void *))RSA_free
223
224 /* ---------------------------------------------------------------------- */
225
226 #define IMPLEMENT_MS(KEYTYPE, keytype)                                  \
227     static const struct keytype_desc_st                                 \
228     pvk2##keytype##_desc = {                                            \
229         EVP_PKEY_##KEYTYPE, #KEYTYPE,                                   \
230         ossl_##keytype##_keymgmt_functions,                             \
231         keytype##_private_key_bio,                                      \
232         keytype##_adjust,                                               \
233         keytype##_free                                                  \
234     };                                                                  \
235     static OSSL_FUNC_decoder_newctx_fn pvk2##keytype##_newctx;          \
236     static void *pvk2##keytype##_newctx(void *provctx)                  \
237     {                                                                   \
238         return pvk2key_newctx(provctx, &pvk2##keytype##_desc);          \
239     }                                                                   \
240     const OSSL_DISPATCH                                                 \
241     ossl_##pvk_to_##keytype##_decoder_functions[] = {                   \
242         { OSSL_FUNC_DECODER_NEWCTX,                                     \
243           (void (*)(void))pvk2##keytype##_newctx },                     \
244         { OSSL_FUNC_DECODER_FREECTX,                                    \
245           (void (*)(void))pvk2key_freectx },                            \
246         { OSSL_FUNC_DECODER_GETTABLE_PARAMS,                            \
247           (void (*)(void))pvk2key_gettable_params },                    \
248         { OSSL_FUNC_DECODER_GET_PARAMS,                                 \
249           (void (*)(void))pvk2key_get_params },                         \
250         { OSSL_FUNC_DECODER_DECODE,                                     \
251           (void (*)(void))pvk2key_decode },                             \
252         { OSSL_FUNC_DECODER_EXPORT_OBJECT,                              \
253           (void (*)(void))pvk2key_export_object },                      \
254         { 0, NULL }                                                     \
255     }
256
257 #ifndef OPENSSL_NO_DSA
258 IMPLEMENT_MS(DSA, dsa);
259 #endif
260 IMPLEMENT_MS(RSA, rsa);