Coverage Report

Created: 2026-10-16 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/pkcs12/p12_add.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2026 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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/core.h>
13
#include <openssl/core_names.h>
14
#include <openssl/pkcs12.h>
15
#include "p12_local.h"
16
#include "crypto/pkcs7/pk7_local.h"
17
18
/* Pack an object into an OCTET STRING and turn into a safebag */
19
20
PKCS12_SAFEBAG *PKCS12_item_pack_safebag(void *obj, const ASN1_ITEM *it,
21
    int nid1, int nid2)
22
0
{
23
0
    PKCS12_BAGS *bag;
24
0
    PKCS12_SAFEBAG *safebag;
25
26
0
    if ((bag = PKCS12_BAGS_new()) == NULL) {
27
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
28
0
        return NULL;
29
0
    }
30
0
    bag->type = OBJ_nid2obj(nid1);
31
0
    if (!ASN1_item_pack(obj, it, &bag->value.octet)) {
32
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
33
0
        goto err;
34
0
    }
35
0
    if ((safebag = PKCS12_SAFEBAG_new()) == NULL) {
36
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
37
0
        goto err;
38
0
    }
39
0
    safebag->value.bag = bag;
40
0
    safebag->type = OBJ_nid2obj(nid2);
41
0
    return safebag;
42
43
0
err:
44
0
    PKCS12_BAGS_free(bag);
45
0
    return NULL;
46
0
}
47
48
/* Turn a stack of SAFEBAGS into a PKCS#7 data Contentinfo */
49
PKCS7 *PKCS12_pack_p7data(STACK_OF(PKCS12_SAFEBAG) *sk)
50
0
{
51
0
    PKCS7 *p7;
52
53
0
    if ((p7 = PKCS7_new()) == NULL) {
54
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
55
0
        return NULL;
56
0
    }
57
0
    p7->type = OBJ_nid2obj(NID_pkcs7_data);
58
0
    if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL) {
59
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
60
0
        goto err;
61
0
    }
62
63
0
    if (!ASN1_item_pack(sk, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), &p7->d.data)) {
64
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CANT_PACK_STRUCTURE);
65
0
        goto err;
66
0
    }
67
0
    return p7;
68
69
0
err:
70
0
    PKCS7_free(p7);
71
0
    return NULL;
72
0
}
73
74
/* Unpack SAFEBAGS from PKCS#7 data ContentInfo */
75
STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(PKCS7 *p7)
76
0
{
77
0
    if (!PKCS7_type_is_data(p7)) {
78
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
79
0
        return NULL;
80
0
    }
81
82
0
    if (p7->d.data == NULL) {
83
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
84
0
        return NULL;
85
0
    }
86
87
0
    return ASN1_item_unpack_ex(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
88
0
        ossl_pkcs7_ctx_get0_libctx(&p7->ctx),
89
0
        ossl_pkcs7_ctx_get0_propq(&p7->ctx));
90
0
}
91
92
/* Turn a stack of SAFEBAGS into a PKCS#7 encrypted data ContentInfo */
93
94
PKCS7 *PKCS12_pack_p7encdata_ex(int pbe_nid, const char *pass, int passlen,
95
    unsigned char *salt, int saltlen, int iter,
96
    STACK_OF(PKCS12_SAFEBAG) *bags,
97
    OSSL_LIB_CTX *ctx, const char *propq)
98
0
{
99
0
    PKCS7 *p7;
100
0
    X509_ALGOR *pbe;
101
0
    EVP_CIPHER *pbe_ciph = NULL;
102
103
0
    if ((p7 = PKCS7_new_ex(ctx, propq)) == NULL) {
104
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
105
0
        return NULL;
106
0
    }
107
0
    if (!PKCS7_set_type(p7, NID_pkcs7_encrypted)) {
108
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE);
109
0
        goto err;
110
0
    }
111
112
0
    ERR_set_mark();
113
0
    pbe_ciph = EVP_CIPHER_fetch(ctx, OBJ_nid2sn(pbe_nid), propq);
114
0
    ERR_pop_to_mark();
115
116
0
    if (pbe_ciph != NULL) {
117
0
        pbe = PKCS5_pbe2_set_iv_ex(pbe_ciph, iter, salt, saltlen, NULL, -1, ctx);
118
0
    } else {
119
0
        pbe = PKCS5_pbe_set_ex(pbe_nid, iter, salt, saltlen, ctx);
120
0
    }
121
122
0
    if (pbe == NULL) {
123
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
124
0
        goto err;
125
0
    }
126
0
    X509_ALGOR_free(p7->d.encrypted->enc_data->algorithm);
127
0
    p7->d.encrypted->enc_data->algorithm = pbe;
128
0
    ASN1_OCTET_STRING_free(p7->d.encrypted->enc_data->enc_data);
129
0
    if (!(p7->d.encrypted->enc_data->enc_data = PKCS12_item_i2d_encrypt_ex(pbe, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), pass,
130
0
              passlen, bags, 1, ctx, propq))) {
131
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
132
0
        goto err;
133
0
    }
134
135
0
    EVP_CIPHER_free(pbe_ciph);
136
0
    return p7;
137
138
0
err:
139
0
    PKCS7_free(p7);
140
0
    EVP_CIPHER_free(pbe_ciph);
141
0
    return NULL;
142
0
}
143
144
PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, const char *pass, int passlen,
145
    unsigned char *salt, int saltlen, int iter,
146
    STACK_OF(PKCS12_SAFEBAG) *bags)
147
0
{
148
0
    return PKCS12_pack_p7encdata_ex(pbe_nid, pass, passlen, salt, saltlen,
149
0
        iter, bags, NULL, NULL);
150
0
}
151
152
STACK_OF(PKCS12_SAFEBAG) *ossl_pkcs12_unpack_p7encdata_ex(PKCS7 *p7,
153
    const char *pass, int passlen,
154
    OSSL_LIB_CTX *libctx, const char *propq)
155
0
{
156
0
    if (!PKCS7_type_is_encrypted(p7))
157
0
        return NULL;
158
159
0
    if (p7->d.encrypted == NULL) {
160
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
161
0
        return NULL;
162
0
    }
163
164
0
    return PKCS12_item_decrypt_d2i_ex(p7->d.encrypted->enc_data->algorithm,
165
0
        ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
166
0
        pass, passlen,
167
0
        p7->d.encrypted->enc_data->enc_data, 1,
168
0
        libctx, propq);
169
0
}
170
171
STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass,
172
    int passlen)
173
0
{
174
0
    return ossl_pkcs12_unpack_p7encdata_ex(p7, pass, passlen,
175
0
        p7->ctx.libctx, p7->ctx.propq);
176
0
}
177
178
PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey_ex(const PKCS12_SAFEBAG *bag,
179
    const char *pass, int passlen,
180
    OSSL_LIB_CTX *ctx, const char *propq)
181
0
{
182
0
    return PKCS8_decrypt_ex(bag->value.shkeybag, pass, passlen, ctx, propq);
183
0
}
184
185
PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(const PKCS12_SAFEBAG *bag,
186
    const char *pass, int passlen)
187
0
{
188
0
    return PKCS12_decrypt_skey_ex(bag, pass, passlen, NULL, NULL);
189
0
}
190
191
PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_secretbag(const PKCS12_SAFEBAG *bag,
192
    const char *pass, int passlen,
193
    OSSL_LIB_CTX *ctx, const char *propq)
194
0
{
195
0
    const ASN1_TYPE *bag_obj;
196
0
    const unsigned char *p;
197
0
    X509_SIG *p8enc = NULL;
198
0
    PKCS8_PRIV_KEY_INFO *p8inf = NULL;
199
200
0
    if (PKCS12_SAFEBAG_get_nid(bag) != NID_secretBag)
201
0
        return NULL;
202
0
    if (PKCS12_SAFEBAG_get_bag_nid(bag) != NID_pkcs8ShroudedKeyBag)
203
0
        return NULL;
204
205
0
    bag_obj = PKCS12_SAFEBAG_get0_bag_obj(bag);
206
0
    if (bag_obj == NULL || bag_obj->type != V_ASN1_OCTET_STRING)
207
0
        return NULL;
208
209
0
    p = ASN1_STRING_get0_data(bag_obj->value.octet_string);
210
0
    p8enc = d2i_X509_SIG(NULL, &p,
211
0
        (unsigned int)ASN1_STRING_get_length(bag_obj->value.octet_string));
212
0
    if (p8enc == NULL)
213
0
        return NULL;
214
215
0
    p8inf = PKCS8_decrypt_ex(p8enc, pass, passlen, ctx, propq);
216
0
    X509_SIG_free(p8enc);
217
0
    return p8inf;
218
0
}
219
220
int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(PKCS7) *safes)
221
0
{
222
0
    if (ASN1_item_pack(safes, ASN1_ITEM_rptr(PKCS12_AUTHSAFES),
223
0
            &p12->authsafes->d.data))
224
0
        return 1;
225
0
    return 0;
226
0
}
227
228
STACK_OF(PKCS7) *ossl_pkcs12_unpack_authsafes_ex(const PKCS12 *p12,
229
    OSSL_LIB_CTX *libctx, const char *propq)
230
0
{
231
0
    STACK_OF(PKCS7) *p7s;
232
0
    PKCS7 *p7;
233
0
    int i;
234
235
0
    if (!PKCS7_type_is_data(p12->authsafes)) {
236
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
237
0
        return NULL;
238
0
    }
239
240
0
    if (p12->authsafes->d.data == NULL) {
241
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
242
0
        return NULL;
243
0
    }
244
245
0
    p7s = ASN1_item_unpack_ex(p12->authsafes->d.data,
246
0
        ASN1_ITEM_rptr(PKCS12_AUTHSAFES), libctx, propq);
247
0
    if (p7s != NULL) {
248
0
        for (i = 0; i < sk_PKCS7_num(p7s); i++) {
249
0
            p7 = sk_PKCS7_value(p7s, i);
250
0
            ossl_pkcs7_set0_libctx(p7, libctx);
251
0
            if (!ossl_pkcs7_set1_propq(p7, propq))
252
0
                goto err;
253
0
            ossl_pkcs7_resolve_libctx(p7);
254
0
        }
255
0
    }
256
0
    return p7s;
257
0
err:
258
0
    sk_PKCS7_pop_free(p7s, PKCS7_free);
259
0
    return NULL;
260
0
}
261
262
STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12)
263
0
{
264
0
    PKCS7_CTX *p7ctx;
265
0
    p7ctx = &p12->authsafes->ctx;
266
267
0
    return ossl_pkcs12_unpack_authsafes_ex(p12, ossl_pkcs7_ctx_get0_libctx(p7ctx), ossl_pkcs7_ctx_get0_propq(p7ctx));
268
0
}