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