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#include <openssl/core.h>
11e1051a39Sopenharmony_ci#include <openssl/core_dispatch.h>
12e1051a39Sopenharmony_ci#include <openssl/core_names.h>
13e1051a39Sopenharmony_ci#include <openssl/core_object.h>
14e1051a39Sopenharmony_ci#include <openssl/asn1.h>
15e1051a39Sopenharmony_ci#include <openssl/err.h>
16e1051a39Sopenharmony_ci#include <openssl/objects.h>
17e1051a39Sopenharmony_ci#include <openssl/pkcs12.h>
18e1051a39Sopenharmony_ci#include <openssl/x509.h>
19e1051a39Sopenharmony_ci#include <openssl/proverr.h>
20e1051a39Sopenharmony_ci#include "internal/asn1.h"
21e1051a39Sopenharmony_ci#include "internal/sizes.h"
22e1051a39Sopenharmony_ci#include "prov/bio.h"
23e1051a39Sopenharmony_ci#include "prov/implementations.h"
24e1051a39Sopenharmony_ci#include "endecoder_local.h"
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_cistatic OSSL_FUNC_decoder_newctx_fn epki2pki_newctx;
27e1051a39Sopenharmony_cistatic OSSL_FUNC_decoder_freectx_fn epki2pki_freectx;
28e1051a39Sopenharmony_cistatic OSSL_FUNC_decoder_decode_fn epki2pki_decode;
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ci/*
31e1051a39Sopenharmony_ci * Context used for EncryptedPrivateKeyInfo to PrivateKeyInfo decoding.
32e1051a39Sopenharmony_ci */
33e1051a39Sopenharmony_cistruct epki2pki_ctx_st {
34e1051a39Sopenharmony_ci    PROV_CTX *provctx;
35e1051a39Sopenharmony_ci};
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_cistatic void *epki2pki_newctx(void *provctx)
38e1051a39Sopenharmony_ci{
39e1051a39Sopenharmony_ci    struct epki2pki_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci    if (ctx != NULL)
42e1051a39Sopenharmony_ci        ctx->provctx = provctx;
43e1051a39Sopenharmony_ci    return ctx;
44e1051a39Sopenharmony_ci}
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_cistatic void epki2pki_freectx(void *vctx)
47e1051a39Sopenharmony_ci{
48e1051a39Sopenharmony_ci    struct epki2pki_ctx_st *ctx = vctx;
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_ci    OPENSSL_free(ctx);
51e1051a39Sopenharmony_ci}
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_ci/*
54e1051a39Sopenharmony_ci * The selection parameter in epki2pki_decode() is not used by this function
55e1051a39Sopenharmony_ci * because it's not relevant just to decode EncryptedPrivateKeyInfo to
56e1051a39Sopenharmony_ci * PrivateKeyInfo.
57e1051a39Sopenharmony_ci */
58e1051a39Sopenharmony_cistatic int epki2pki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
59e1051a39Sopenharmony_ci                           OSSL_CALLBACK *data_cb, void *data_cbarg,
60e1051a39Sopenharmony_ci                           OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
61e1051a39Sopenharmony_ci{
62e1051a39Sopenharmony_ci    struct epki2pki_ctx_st *ctx = vctx;
63e1051a39Sopenharmony_ci    BUF_MEM *mem = NULL;
64e1051a39Sopenharmony_ci    unsigned char *der = NULL;
65e1051a39Sopenharmony_ci    const unsigned char *pder = NULL;
66e1051a39Sopenharmony_ci    long der_len = 0;
67e1051a39Sopenharmony_ci    X509_SIG *p8 = NULL;
68e1051a39Sopenharmony_ci    PKCS8_PRIV_KEY_INFO *p8inf = NULL;
69e1051a39Sopenharmony_ci    const X509_ALGOR *alg = NULL;
70e1051a39Sopenharmony_ci    BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin);
71e1051a39Sopenharmony_ci    int ok = 0;
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_ci    if (in == NULL)
74e1051a39Sopenharmony_ci        return 0;
75e1051a39Sopenharmony_ci
76e1051a39Sopenharmony_ci    ok = (asn1_d2i_read_bio(in, &mem) >= 0);
77e1051a39Sopenharmony_ci    BIO_free(in);
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci    /* We return "empty handed".  This is not an error. */
80e1051a39Sopenharmony_ci    if (!ok)
81e1051a39Sopenharmony_ci        return 1;
82e1051a39Sopenharmony_ci
83e1051a39Sopenharmony_ci    pder = der = (unsigned char *)mem->data;
84e1051a39Sopenharmony_ci    der_len = (long)mem->length;
85e1051a39Sopenharmony_ci    OPENSSL_free(mem);
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_ci    ok = 1;                      /* Assume good */
88e1051a39Sopenharmony_ci    ERR_set_mark();
89e1051a39Sopenharmony_ci    if ((p8 = d2i_X509_SIG(NULL, &pder, der_len)) != NULL) {
90e1051a39Sopenharmony_ci        char pbuf[1024];
91e1051a39Sopenharmony_ci        size_t plen = 0;
92e1051a39Sopenharmony_ci
93e1051a39Sopenharmony_ci        ERR_clear_last_mark();
94e1051a39Sopenharmony_ci
95e1051a39Sopenharmony_ci        if (!pw_cb(pbuf, sizeof(pbuf), &plen, NULL, pw_cbarg)) {
96e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PASSPHRASE);
97e1051a39Sopenharmony_ci            ok = 0;
98e1051a39Sopenharmony_ci        } else {
99e1051a39Sopenharmony_ci            const ASN1_OCTET_STRING *oct;
100e1051a39Sopenharmony_ci            unsigned char *new_der = NULL;
101e1051a39Sopenharmony_ci            int new_der_len = 0;
102e1051a39Sopenharmony_ci
103e1051a39Sopenharmony_ci            X509_SIG_get0(p8, &alg, &oct);
104e1051a39Sopenharmony_ci            if (!PKCS12_pbe_crypt_ex(alg, pbuf, plen,
105e1051a39Sopenharmony_ci                                     oct->data, oct->length,
106e1051a39Sopenharmony_ci                                     &new_der, &new_der_len, 0,
107e1051a39Sopenharmony_ci                                     PROV_LIBCTX_OF(ctx->provctx), NULL)) {
108e1051a39Sopenharmony_ci                ok = 0;
109e1051a39Sopenharmony_ci            } else {
110e1051a39Sopenharmony_ci                OPENSSL_free(der);
111e1051a39Sopenharmony_ci                der = new_der;
112e1051a39Sopenharmony_ci                der_len = new_der_len;
113e1051a39Sopenharmony_ci            }
114e1051a39Sopenharmony_ci            alg = NULL;
115e1051a39Sopenharmony_ci        }
116e1051a39Sopenharmony_ci        X509_SIG_free(p8);
117e1051a39Sopenharmony_ci    } else {
118e1051a39Sopenharmony_ci        ERR_pop_to_mark();
119e1051a39Sopenharmony_ci    }
120e1051a39Sopenharmony_ci
121e1051a39Sopenharmony_ci    ERR_set_mark();
122e1051a39Sopenharmony_ci    pder = der;
123e1051a39Sopenharmony_ci    p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &pder, der_len);
124e1051a39Sopenharmony_ci    ERR_pop_to_mark();
125e1051a39Sopenharmony_ci
126e1051a39Sopenharmony_ci    if (p8inf != NULL && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf)) {
127e1051a39Sopenharmony_ci        /*
128e1051a39Sopenharmony_ci         * We have something and recognised it as PrivateKeyInfo, so let's
129e1051a39Sopenharmony_ci         * pass all the applicable data to the callback.
130e1051a39Sopenharmony_ci         */
131e1051a39Sopenharmony_ci        char keytype[OSSL_MAX_NAME_SIZE];
132e1051a39Sopenharmony_ci        OSSL_PARAM params[5], *p = params;
133e1051a39Sopenharmony_ci        int objtype = OSSL_OBJECT_PKEY;
134e1051a39Sopenharmony_ci
135e1051a39Sopenharmony_ci        OBJ_obj2txt(keytype, sizeof(keytype), alg->algorithm, 0);
136e1051a39Sopenharmony_ci
137e1051a39Sopenharmony_ci        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
138e1051a39Sopenharmony_ci                                                keytype, 0);
139e1051a39Sopenharmony_ci        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
140e1051a39Sopenharmony_ci                                                "PrivateKeyInfo", 0);
141e1051a39Sopenharmony_ci        *p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
142e1051a39Sopenharmony_ci                                                 der, der_len);
143e1051a39Sopenharmony_ci        *p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
144e1051a39Sopenharmony_ci        *p = OSSL_PARAM_construct_end();
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ci        ok = data_cb(params, data_cbarg);
147e1051a39Sopenharmony_ci    }
148e1051a39Sopenharmony_ci    PKCS8_PRIV_KEY_INFO_free(p8inf);
149e1051a39Sopenharmony_ci    OPENSSL_free(der);
150e1051a39Sopenharmony_ci    return ok;
151e1051a39Sopenharmony_ci}
152e1051a39Sopenharmony_ci
153e1051a39Sopenharmony_ciconst OSSL_DISPATCH ossl_EncryptedPrivateKeyInfo_der_to_der_decoder_functions[] = {
154e1051a39Sopenharmony_ci    { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))epki2pki_newctx },
155e1051a39Sopenharmony_ci    { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))epki2pki_freectx },
156e1051a39Sopenharmony_ci    { OSSL_FUNC_DECODER_DECODE, (void (*)(void))epki2pki_decode },
157e1051a39Sopenharmony_ci    { 0, NULL }
158e1051a39Sopenharmony_ci};
159