Coverage Report

Created: 2025-06-13 06:58

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