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