Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/pkcs12/p12_sbag.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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_lcl.h"
14
15
#if OPENSSL_API_COMPAT < 0x10100000L
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
0
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
X509 *PKCS12_SAFEBAG_get1_cert(const PKCS12_SAFEBAG *bag)
75
0
{
76
0
    if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag)
77
0
        return NULL;
78
0
    if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate)
79
0
        return NULL;
80
0
    return ASN1_item_unpack(bag->value.bag->value.octet,
81
0
                            ASN1_ITEM_rptr(X509));
82
0
}
83
84
X509_CRL *PKCS12_SAFEBAG_get1_crl(const PKCS12_SAFEBAG *bag)
85
0
{
86
0
    if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag)
87
0
        return NULL;
88
0
    if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl)
89
0
        return NULL;
90
0
    return ASN1_item_unpack(bag->value.bag->value.octet,
91
0
                            ASN1_ITEM_rptr(X509_CRL));
92
0
}
93
94
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509)
95
0
{
96
0
    return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509),
97
0
                                    NID_x509Certificate, NID_certBag);
98
0
}
99
100
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl)
101
0
{
102
0
    return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL),
103
0
                                    NID_x509Crl, NID_crlBag);
104
0
}
105
106
/* Turn PKCS8 object into a keybag */
107
108
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8)
109
0
{
110
0
    PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
111
0
112
0
    if (bag == NULL) {
113
0
        PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_P8INF, ERR_R_MALLOC_FAILURE);
114
0
        return NULL;
115
0
    }
116
0
    bag->type = OBJ_nid2obj(NID_keyBag);
117
0
    bag->value.keybag = p8;
118
0
    return bag;
119
0
}
120
121
/* Turn PKCS8 object into a shrouded keybag */
122
123
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8)
124
0
{
125
0
    PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
126
0
127
0
    /* Set up the safe bag */
128
0
    if (bag == NULL) {
129
0
        PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_PKCS8, ERR_R_MALLOC_FAILURE);
130
0
        return NULL;
131
0
    }
132
0
    bag->type = OBJ_nid2obj(NID_pkcs8ShroudedKeyBag);
133
0
    bag->value.shkeybag = p8;
134
0
    return bag;
135
0
}
136
137
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid,
138
                                                    const char *pass,
139
                                                    int passlen,
140
                                                    unsigned char *salt,
141
                                                    int saltlen, int iter,
142
                                                    PKCS8_PRIV_KEY_INFO *p8inf)
143
0
{
144
0
    PKCS12_SAFEBAG *bag;
145
0
    const EVP_CIPHER *pbe_ciph;
146
0
    X509_SIG *p8;
147
0
148
0
    pbe_ciph = EVP_get_cipherbynid(pbe_nid);
149
0
    if (pbe_ciph)
150
0
        pbe_nid = -1;
151
0
152
0
    p8 = PKCS8_encrypt(pbe_nid, pbe_ciph, pass, passlen, salt, saltlen, iter,
153
0
                       p8inf);
154
0
    if (p8 == NULL)
155
0
        return NULL;
156
0
157
0
    bag = PKCS12_SAFEBAG_create0_pkcs8(p8);
158
0
    if (bag == NULL)
159
0
        X509_SIG_free(p8);
160
0
161
0
    return bag;
162
0
}