Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/pkcs12/p12_sbag.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2023 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/pkcs12.h>
13
#include "p12_local.h"
14
#include "crypto/x509.h"
15
16
#ifndef OPENSSL_NO_DEPRECATED_1_1_0
17
ASN1_TYPE *PKCS12_get_attr(const PKCS12_SAFEBAG *bag, int attr_nid)
18
0
{
19
0
    return PKCS12_get_attr_gen(bag->attrib, attr_nid);
20
0
}
21
#endif
22
23
const ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(const PKCS12_SAFEBAG *bag,
24
    int attr_nid)
25
0
{
26
0
    return PKCS12_get_attr_gen(bag->attrib, attr_nid);
27
0
}
28
29
ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8, int attr_nid)
30
0
{
31
0
    return PKCS12_get_attr_gen(PKCS8_pkey_get0_attrs(p8), attr_nid);
32
0
}
33
34
const PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(const PKCS12_SAFEBAG *bag)
35
0
{
36
0
    if (PKCS12_SAFEBAG_get_nid(bag) != NID_keyBag)
37
0
        return NULL;
38
0
    return bag->value.keybag;
39
0
}
40
41
const X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(const PKCS12_SAFEBAG *bag)
42
0
{
43
0
    if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag)
44
0
        return NULL;
45
0
    return bag->value.shkeybag;
46
0
}
47
48
const STACK_OF(PKCS12_SAFEBAG) *
49
PKCS12_SAFEBAG_get0_safes(const PKCS12_SAFEBAG *bag)
50
0
{
51
0
    if (OBJ_obj2nid(bag->type) != NID_safeContentsBag)
52
0
        return NULL;
53
0
    return bag->value.safes;
54
0
}
55
56
const ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(const PKCS12_SAFEBAG *bag)
57
0
{
58
0
    return bag->type;
59
0
}
60
61
int PKCS12_SAFEBAG_get_nid(const PKCS12_SAFEBAG *bag)
62
0
{
63
0
    return OBJ_obj2nid(bag->type);
64
0
}
65
66
int PKCS12_SAFEBAG_get_bag_nid(const PKCS12_SAFEBAG *bag)
67
0
{
68
0
    int btype = PKCS12_SAFEBAG_get_nid(bag);
69
70
0
    if (btype != NID_certBag && btype != NID_crlBag && btype != NID_secretBag)
71
0
        return -1;
72
0
    return OBJ_obj2nid(bag->value.bag->type);
73
0
}
74
75
const ASN1_OBJECT *PKCS12_SAFEBAG_get0_bag_type(const PKCS12_SAFEBAG *bag)
76
0
{
77
0
    int btype = PKCS12_SAFEBAG_get_nid(bag);
78
79
0
    if (btype != NID_certBag && btype != NID_crlBag && btype != NID_secretBag)
80
0
        return NULL;
81
0
    return bag->value.bag->type;
82
0
}
83
84
const ASN1_TYPE *PKCS12_SAFEBAG_get0_bag_obj(const PKCS12_SAFEBAG *bag)
85
0
{
86
0
    int vtype = PKCS12_SAFEBAG_get_bag_nid(bag);
87
88
0
    if (vtype == -1 || vtype == NID_x509Certificate || vtype == NID_x509Crl
89
0
        || vtype == NID_sdsiCertificate)
90
0
        return NULL;
91
0
    return bag->value.bag->value.other;
92
0
}
93
94
X509 *PKCS12_SAFEBAG_get1_cert(const PKCS12_SAFEBAG *bag)
95
0
{
96
0
    if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag)
97
0
        return NULL;
98
0
    if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate)
99
0
        return NULL;
100
0
    return ASN1_item_unpack(bag->value.bag->value.octet,
101
0
        ASN1_ITEM_rptr(X509));
102
0
}
103
104
X509_CRL *PKCS12_SAFEBAG_get1_crl(const PKCS12_SAFEBAG *bag)
105
0
{
106
0
    if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag)
107
0
        return NULL;
108
0
    if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl)
109
0
        return NULL;
110
0
    return ASN1_item_unpack(bag->value.bag->value.octet,
111
0
        ASN1_ITEM_rptr(X509_CRL));
112
0
}
113
114
X509 *PKCS12_SAFEBAG_get1_cert_ex(const PKCS12_SAFEBAG *bag,
115
    OSSL_LIB_CTX *libctx, const char *propq)
116
0
{
117
0
    X509 *ret = NULL;
118
119
0
    if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag)
120
0
        return NULL;
121
0
    if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate)
122
0
        return NULL;
123
0
    ret = ASN1_item_unpack_ex(bag->value.bag->value.octet,
124
0
        ASN1_ITEM_rptr(X509), libctx, propq);
125
0
    if (!ossl_x509_set0_libctx(ret, libctx, propq)) {
126
0
        X509_free(ret);
127
0
        return NULL;
128
0
    }
129
0
    return ret;
130
0
}
131
132
X509_CRL *PKCS12_SAFEBAG_get1_crl_ex(const PKCS12_SAFEBAG *bag,
133
    OSSL_LIB_CTX *libctx, const char *propq)
134
0
{
135
0
    X509_CRL *ret = NULL;
136
137
0
    if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag)
138
0
        return NULL;
139
0
    if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl)
140
0
        return NULL;
141
0
    ret = ASN1_item_unpack_ex(bag->value.bag->value.octet,
142
0
        ASN1_ITEM_rptr(X509_CRL), libctx, propq);
143
0
    if (!ossl_x509_crl_set0_libctx(ret, libctx, propq)) {
144
0
        X509_CRL_free(ret);
145
0
        return NULL;
146
0
    }
147
0
    return ret;
148
0
}
149
150
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509)
151
0
{
152
0
    return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509),
153
0
        NID_x509Certificate, NID_certBag);
154
0
}
155
156
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl)
157
0
{
158
0
    return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL),
159
0
        NID_x509Crl, NID_crlBag);
160
0
}
161
162
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_secret(int type, int vtype, const unsigned char *value, int len)
163
0
{
164
0
    PKCS12_BAGS *bag;
165
0
    PKCS12_SAFEBAG *safebag;
166
167
0
    if ((bag = PKCS12_BAGS_new()) == NULL) {
168
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
169
0
        return NULL;
170
0
    }
171
0
    bag->type = OBJ_nid2obj(type);
172
173
0
    switch (vtype) {
174
0
    case V_ASN1_OCTET_STRING: {
175
0
        ASN1_OCTET_STRING *strtmp = ASN1_OCTET_STRING_new();
176
177
0
        if (strtmp == NULL) {
178
0
            ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
179
0
            goto err;
180
0
        }
181
        /* Pack data into an octet string */
182
0
        if (!ASN1_OCTET_STRING_set(strtmp, value, len)) {
183
0
            ASN1_OCTET_STRING_free(strtmp);
184
0
            ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR);
185
0
            goto err;
186
0
        }
187
0
        bag->value.other = ASN1_TYPE_new();
188
0
        if (bag->value.other == NULL) {
189
0
            ASN1_OCTET_STRING_free(strtmp);
190
0
            ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
191
0
            goto err;
192
0
        }
193
0
        ASN1_TYPE_set(bag->value.other, vtype, strtmp);
194
0
    } break;
195
196
0
    default:
197
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_TYPE);
198
0
        goto err;
199
0
    }
200
201
0
    if ((safebag = PKCS12_SAFEBAG_new()) == NULL) {
202
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
203
0
        goto err;
204
0
    }
205
0
    safebag->value.bag = bag;
206
0
    safebag->type = OBJ_nid2obj(NID_secretBag);
207
0
    return safebag;
208
209
0
err:
210
0
    PKCS12_BAGS_free(bag);
211
0
    return NULL;
212
0
}
213
214
/* Turn PKCS8 object into a keybag */
215
216
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8)
217
0
{
218
0
    PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
219
220
0
    if (bag == NULL) {
221
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
222
0
        return NULL;
223
0
    }
224
0
    bag->type = OBJ_nid2obj(NID_keyBag);
225
0
    bag->value.keybag = p8;
226
0
    return bag;
227
0
}
228
229
/* Turn PKCS8 object into a shrouded keybag */
230
231
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8)
232
0
{
233
0
    PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
234
235
    /* Set up the safe bag */
236
0
    if (bag == NULL) {
237
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
238
0
        return NULL;
239
0
    }
240
0
    bag->type = OBJ_nid2obj(NID_pkcs8ShroudedKeyBag);
241
0
    bag->value.shkeybag = p8;
242
0
    return bag;
243
0
}
244
245
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(int pbe_nid,
246
    const char *pass,
247
    int passlen,
248
    unsigned char *salt,
249
    int saltlen, int iter,
250
    PKCS8_PRIV_KEY_INFO *p8inf,
251
    OSSL_LIB_CTX *ctx,
252
    const char *propq)
253
0
{
254
0
    PKCS12_SAFEBAG *bag = NULL;
255
0
    const EVP_CIPHER *pbe_ciph = NULL;
256
0
    EVP_CIPHER *pbe_ciph_fetch = NULL;
257
0
    X509_SIG *p8;
258
259
0
    ERR_set_mark();
260
0
    pbe_ciph = pbe_ciph_fetch = EVP_CIPHER_fetch(ctx, OBJ_nid2sn(pbe_nid), propq);
261
0
    if (pbe_ciph == NULL)
262
0
        pbe_ciph = EVP_get_cipherbynid(pbe_nid);
263
0
    ERR_pop_to_mark();
264
265
0
    if (pbe_ciph != NULL)
266
0
        pbe_nid = -1;
267
268
0
    p8 = PKCS8_encrypt_ex(pbe_nid, pbe_ciph, pass, passlen, salt, saltlen, iter,
269
0
        p8inf, ctx, propq);
270
0
    if (p8 == NULL)
271
0
        goto err;
272
273
0
    bag = PKCS12_SAFEBAG_create0_pkcs8(p8);
274
0
    if (bag == NULL)
275
0
        X509_SIG_free(p8);
276
277
0
err:
278
0
    EVP_CIPHER_free(pbe_ciph_fetch);
279
0
    return bag;
280
0
}
281
282
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid,
283
    const char *pass,
284
    int passlen,
285
    unsigned char *salt,
286
    int saltlen, int iter,
287
    PKCS8_PRIV_KEY_INFO *p8inf)
288
0
{
289
0
    return PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(pbe_nid, pass, passlen,
290
0
        salt, saltlen, iter, p8inf,
291
0
        NULL, NULL);
292
0
}