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