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/pem.h>         /* For public PVK functions */
24e1051a39Sopenharmony_ci#include <openssl/x509.h>
25e1051a39Sopenharmony_ci#include <openssl/err.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 msblob2key_ctx_st;            /* Forward declaration */
34e1051a39Sopenharmony_citypedef void *b2i_of_void_fn(const unsigned char **in, unsigned int bitlen,
35e1051a39Sopenharmony_ci                             int ispub);
36e1051a39Sopenharmony_citypedef void adjust_key_fn(void *, struct msblob2key_ctx_st *ctx);
37e1051a39Sopenharmony_citypedef void free_key_fn(void *);
38e1051a39Sopenharmony_cistruct keytype_desc_st {
39e1051a39Sopenharmony_ci    int type;                 /* EVP key type */
40e1051a39Sopenharmony_ci    const char *name;         /* Keytype */
41e1051a39Sopenharmony_ci    const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci    b2i_of_void_fn *read_private_key;
44e1051a39Sopenharmony_ci    b2i_of_void_fn *read_public_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 msblob2key_freectx;
50e1051a39Sopenharmony_cistatic OSSL_FUNC_decoder_decode_fn msblob2key_decode;
51e1051a39Sopenharmony_cistatic OSSL_FUNC_decoder_export_object_fn msblob2key_export_object;
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_ci/*
54e1051a39Sopenharmony_ci * Context used for DER to key decoding.
55e1051a39Sopenharmony_ci */
56e1051a39Sopenharmony_cistruct msblob2key_ctx_st {
57e1051a39Sopenharmony_ci    PROV_CTX *provctx;
58e1051a39Sopenharmony_ci    const struct keytype_desc_st *desc;
59e1051a39Sopenharmony_ci    /* The selection that is passed to msblob2key_decode() */
60e1051a39Sopenharmony_ci    int selection;
61e1051a39Sopenharmony_ci};
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_cistatic struct msblob2key_ctx_st *
64e1051a39Sopenharmony_cimsblob2key_newctx(void *provctx, const struct keytype_desc_st *desc)
65e1051a39Sopenharmony_ci{
66e1051a39Sopenharmony_ci    struct msblob2key_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 msblob2key_freectx(void *vctx)
76e1051a39Sopenharmony_ci{
77e1051a39Sopenharmony_ci    struct msblob2key_ctx_st *ctx = vctx;
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci    OPENSSL_free(ctx);
80e1051a39Sopenharmony_ci}
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_cistatic int msblob2key_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 msblob2key_ctx_st *ctx = vctx;
87e1051a39Sopenharmony_ci    BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
88e1051a39Sopenharmony_ci    const unsigned char *p;
89e1051a39Sopenharmony_ci    unsigned char hdr_buf[16], *buf = NULL;
90e1051a39Sopenharmony_ci    unsigned int bitlen, magic, length;
91e1051a39Sopenharmony_ci    int isdss = -1;
92e1051a39Sopenharmony_ci    int ispub = -1;
93e1051a39Sopenharmony_ci    void *key = NULL;
94e1051a39Sopenharmony_ci    int ok = 0;
95e1051a39Sopenharmony_ci
96e1051a39Sopenharmony_ci    if (in == NULL)
97e1051a39Sopenharmony_ci        return 0;
98e1051a39Sopenharmony_ci
99e1051a39Sopenharmony_ci    if (BIO_read(in, hdr_buf, 16) != 16) {
100e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
101e1051a39Sopenharmony_ci        goto next;
102e1051a39Sopenharmony_ci    }
103e1051a39Sopenharmony_ci    ERR_set_mark();
104e1051a39Sopenharmony_ci    p = hdr_buf;
105e1051a39Sopenharmony_ci    ok = ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, &ispub) > 0;
106e1051a39Sopenharmony_ci    ERR_pop_to_mark();
107e1051a39Sopenharmony_ci    if (!ok)
108e1051a39Sopenharmony_ci        goto next;
109e1051a39Sopenharmony_ci
110e1051a39Sopenharmony_ci    ctx->selection = selection;
111e1051a39Sopenharmony_ci    ok = 0;                      /* Assume that we fail */
112e1051a39Sopenharmony_ci
113e1051a39Sopenharmony_ci    if ((isdss && ctx->desc->type != EVP_PKEY_DSA)
114e1051a39Sopenharmony_ci        || (!isdss && ctx->desc->type != EVP_PKEY_RSA))
115e1051a39Sopenharmony_ci        goto next;
116e1051a39Sopenharmony_ci
117e1051a39Sopenharmony_ci    length = ossl_blob_length(bitlen, isdss, ispub);
118e1051a39Sopenharmony_ci    if (length > BLOB_MAX_LENGTH) {
119e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG);
120e1051a39Sopenharmony_ci        goto next;
121e1051a39Sopenharmony_ci    }
122e1051a39Sopenharmony_ci    buf = OPENSSL_malloc(length);
123e1051a39Sopenharmony_ci    if (buf == NULL) {
124e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, ERR_R_MALLOC_FAILURE);
125e1051a39Sopenharmony_ci        goto end;
126e1051a39Sopenharmony_ci    }
127e1051a39Sopenharmony_ci    p = buf;
128e1051a39Sopenharmony_ci    if (BIO_read(in, buf, length) != (int)length) {
129e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
130e1051a39Sopenharmony_ci        goto next;
131e1051a39Sopenharmony_ci    }
132e1051a39Sopenharmony_ci
133e1051a39Sopenharmony_ci    if ((selection == 0
134e1051a39Sopenharmony_ci         || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
135e1051a39Sopenharmony_ci        && !ispub
136e1051a39Sopenharmony_ci        && ctx->desc->read_private_key != NULL) {
137e1051a39Sopenharmony_ci        struct ossl_passphrase_data_st pwdata;
138e1051a39Sopenharmony_ci
139e1051a39Sopenharmony_ci        memset(&pwdata, 0, sizeof(pwdata));
140e1051a39Sopenharmony_ci        if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
141e1051a39Sopenharmony_ci            goto end;
142e1051a39Sopenharmony_ci        p = buf;
143e1051a39Sopenharmony_ci        key = ctx->desc->read_private_key(&p, bitlen, ispub);
144e1051a39Sopenharmony_ci        if (selection != 0 && key == NULL)
145e1051a39Sopenharmony_ci            goto next;
146e1051a39Sopenharmony_ci    }
147e1051a39Sopenharmony_ci    if (key == NULL && (selection == 0
148e1051a39Sopenharmony_ci         || (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
149e1051a39Sopenharmony_ci        && ispub
150e1051a39Sopenharmony_ci        && ctx->desc->read_public_key != NULL) {
151e1051a39Sopenharmony_ci        p = buf;
152e1051a39Sopenharmony_ci        key = ctx->desc->read_public_key(&p, bitlen, ispub);
153e1051a39Sopenharmony_ci        if (selection != 0 && key == NULL)
154e1051a39Sopenharmony_ci            goto next;
155e1051a39Sopenharmony_ci    }
156e1051a39Sopenharmony_ci
157e1051a39Sopenharmony_ci    if (key != NULL && ctx->desc->adjust_key != NULL)
158e1051a39Sopenharmony_ci        ctx->desc->adjust_key(key, ctx);
159e1051a39Sopenharmony_ci
160e1051a39Sopenharmony_ci next:
161e1051a39Sopenharmony_ci    /*
162e1051a39Sopenharmony_ci     * Indicated that we successfully decoded something, or not at all.
163e1051a39Sopenharmony_ci     * Ending up "empty handed" is not an error.
164e1051a39Sopenharmony_ci     */
165e1051a39Sopenharmony_ci    ok = 1;
166e1051a39Sopenharmony_ci
167e1051a39Sopenharmony_ci    /*
168e1051a39Sopenharmony_ci     * We free resources here so it's not held up during the callback, because
169e1051a39Sopenharmony_ci     * we know the process is recursive and the allocated chunks of memory
170e1051a39Sopenharmony_ci     * add up.
171e1051a39Sopenharmony_ci     */
172e1051a39Sopenharmony_ci    OPENSSL_free(buf);
173e1051a39Sopenharmony_ci    BIO_free(in);
174e1051a39Sopenharmony_ci    buf = NULL;
175e1051a39Sopenharmony_ci    in = NULL;
176e1051a39Sopenharmony_ci
177e1051a39Sopenharmony_ci    if (key != NULL) {
178e1051a39Sopenharmony_ci        OSSL_PARAM params[4];
179e1051a39Sopenharmony_ci        int object_type = OSSL_OBJECT_PKEY;
180e1051a39Sopenharmony_ci
181e1051a39Sopenharmony_ci        params[0] =
182e1051a39Sopenharmony_ci            OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
183e1051a39Sopenharmony_ci        params[1] =
184e1051a39Sopenharmony_ci            OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
185e1051a39Sopenharmony_ci                                             (char *)ctx->desc->name, 0);
186e1051a39Sopenharmony_ci        /* The address of the key becomes the octet string */
187e1051a39Sopenharmony_ci        params[2] =
188e1051a39Sopenharmony_ci            OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
189e1051a39Sopenharmony_ci                                              &key, sizeof(key));
190e1051a39Sopenharmony_ci        params[3] = OSSL_PARAM_construct_end();
191e1051a39Sopenharmony_ci
192e1051a39Sopenharmony_ci        ok = data_cb(params, data_cbarg);
193e1051a39Sopenharmony_ci    }
194e1051a39Sopenharmony_ci
195e1051a39Sopenharmony_ci end:
196e1051a39Sopenharmony_ci    BIO_free(in);
197e1051a39Sopenharmony_ci    OPENSSL_free(buf);
198e1051a39Sopenharmony_ci    ctx->desc->free_key(key);
199e1051a39Sopenharmony_ci
200e1051a39Sopenharmony_ci    return ok;
201e1051a39Sopenharmony_ci}
202e1051a39Sopenharmony_ci
203e1051a39Sopenharmony_cistatic int
204e1051a39Sopenharmony_cimsblob2key_export_object(void *vctx,
205e1051a39Sopenharmony_ci                         const void *reference, size_t reference_sz,
206e1051a39Sopenharmony_ci                         OSSL_CALLBACK *export_cb, void *export_cbarg)
207e1051a39Sopenharmony_ci{
208e1051a39Sopenharmony_ci    struct msblob2key_ctx_st *ctx = vctx;
209e1051a39Sopenharmony_ci    OSSL_FUNC_keymgmt_export_fn *export =
210e1051a39Sopenharmony_ci        ossl_prov_get_keymgmt_export(ctx->desc->fns);
211e1051a39Sopenharmony_ci    void *keydata;
212e1051a39Sopenharmony_ci
213e1051a39Sopenharmony_ci    if (reference_sz == sizeof(keydata) && export != NULL) {
214e1051a39Sopenharmony_ci        /* The contents of the reference is the address to our object */
215e1051a39Sopenharmony_ci        keydata = *(void **)reference;
216e1051a39Sopenharmony_ci
217e1051a39Sopenharmony_ci        return export(keydata, ctx->selection, export_cb, export_cbarg);
218e1051a39Sopenharmony_ci    }
219e1051a39Sopenharmony_ci    return 0;
220e1051a39Sopenharmony_ci}
221e1051a39Sopenharmony_ci
222e1051a39Sopenharmony_ci/* ---------------------------------------------------------------------- */
223e1051a39Sopenharmony_ci
224e1051a39Sopenharmony_ci#define dsa_decode_private_key  (b2i_of_void_fn *)ossl_b2i_DSA_after_header
225e1051a39Sopenharmony_ci#define dsa_decode_public_key   (b2i_of_void_fn *)ossl_b2i_DSA_after_header
226e1051a39Sopenharmony_ci#define dsa_adjust              NULL
227e1051a39Sopenharmony_ci#define dsa_free                (void (*)(void *))DSA_free
228e1051a39Sopenharmony_ci
229e1051a39Sopenharmony_ci/* ---------------------------------------------------------------------- */
230e1051a39Sopenharmony_ci
231e1051a39Sopenharmony_ci#define rsa_decode_private_key  (b2i_of_void_fn *)ossl_b2i_RSA_after_header
232e1051a39Sopenharmony_ci#define rsa_decode_public_key   (b2i_of_void_fn *)ossl_b2i_RSA_after_header
233e1051a39Sopenharmony_ci
234e1051a39Sopenharmony_cistatic void rsa_adjust(void *key, struct msblob2key_ctx_st *ctx)
235e1051a39Sopenharmony_ci{
236e1051a39Sopenharmony_ci    ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
237e1051a39Sopenharmony_ci}
238e1051a39Sopenharmony_ci
239e1051a39Sopenharmony_ci#define rsa_free                        (void (*)(void *))RSA_free
240e1051a39Sopenharmony_ci
241e1051a39Sopenharmony_ci/* ---------------------------------------------------------------------- */
242e1051a39Sopenharmony_ci
243e1051a39Sopenharmony_ci#define IMPLEMENT_MSBLOB(KEYTYPE, keytype)                              \
244e1051a39Sopenharmony_ci    static const struct keytype_desc_st mstype##2##keytype##_desc = {   \
245e1051a39Sopenharmony_ci        EVP_PKEY_##KEYTYPE, #KEYTYPE,                                   \
246e1051a39Sopenharmony_ci        ossl_##keytype##_keymgmt_functions,                             \
247e1051a39Sopenharmony_ci        keytype##_decode_private_key,                                   \
248e1051a39Sopenharmony_ci        keytype##_decode_public_key,                                    \
249e1051a39Sopenharmony_ci        keytype##_adjust,                                               \
250e1051a39Sopenharmony_ci        keytype##_free                                                  \
251e1051a39Sopenharmony_ci    };                                                                  \
252e1051a39Sopenharmony_ci    static OSSL_FUNC_decoder_newctx_fn msblob2##keytype##_newctx;       \
253e1051a39Sopenharmony_ci    static void *msblob2##keytype##_newctx(void *provctx)               \
254e1051a39Sopenharmony_ci    {                                                                   \
255e1051a39Sopenharmony_ci        return msblob2key_newctx(provctx, &mstype##2##keytype##_desc);  \
256e1051a39Sopenharmony_ci    }                                                                   \
257e1051a39Sopenharmony_ci    const OSSL_DISPATCH                                                 \
258e1051a39Sopenharmony_ci    ossl_msblob_to_##keytype##_decoder_functions[] = {                  \
259e1051a39Sopenharmony_ci        { OSSL_FUNC_DECODER_NEWCTX,                                     \
260e1051a39Sopenharmony_ci          (void (*)(void))msblob2##keytype##_newctx },                  \
261e1051a39Sopenharmony_ci        { OSSL_FUNC_DECODER_FREECTX,                                    \
262e1051a39Sopenharmony_ci          (void (*)(void))msblob2key_freectx },                         \
263e1051a39Sopenharmony_ci        { OSSL_FUNC_DECODER_DECODE,                                     \
264e1051a39Sopenharmony_ci          (void (*)(void))msblob2key_decode },                          \
265e1051a39Sopenharmony_ci        { OSSL_FUNC_DECODER_EXPORT_OBJECT,                              \
266e1051a39Sopenharmony_ci          (void (*)(void))msblob2key_export_object },                   \
267e1051a39Sopenharmony_ci        { 0, NULL }                                                     \
268e1051a39Sopenharmony_ci    }
269e1051a39Sopenharmony_ci
270e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DSA
271e1051a39Sopenharmony_ciIMPLEMENT_MSBLOB(DSA, dsa);
272e1051a39Sopenharmony_ci#endif
273e1051a39Sopenharmony_ciIMPLEMENT_MSBLOB(RSA, rsa);
274