Coverage Report

Created: 2025-06-13 06:58

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