xref: /openssl/crypto/pkcs12/p12_add.c (revision afecd85d)
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/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 
PKCS12_item_pack_safebag(void * obj,const ASN1_ITEM * it,int nid1,int nid2)20 PKCS12_SAFEBAG *PKCS12_item_pack_safebag(void *obj, const ASN1_ITEM *it,
21                                          int nid1, int nid2)
22 {
23     PKCS12_BAGS *bag;
24     PKCS12_SAFEBAG *safebag;
25 
26     if ((bag = PKCS12_BAGS_new()) == NULL) {
27         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
28         return NULL;
29     }
30     bag->type = OBJ_nid2obj(nid1);
31     if (!ASN1_item_pack(obj, it, &bag->value.octet)) {
32         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
33         goto err;
34     }
35     if ((safebag = PKCS12_SAFEBAG_new()) == NULL) {
36         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
37         goto err;
38     }
39     safebag->value.bag = bag;
40     safebag->type = OBJ_nid2obj(nid2);
41     return safebag;
42 
43  err:
44     PKCS12_BAGS_free(bag);
45     return NULL;
46 }
47 
48 /* Turn a stack of SAFEBAGS into a PKCS#7 data Contentinfo */
PKCS12_pack_p7data(STACK_OF (PKCS12_SAFEBAG)* sk)49 PKCS7 *PKCS12_pack_p7data(STACK_OF(PKCS12_SAFEBAG) *sk)
50 {
51     PKCS7 *p7;
52 
53     if ((p7 = PKCS7_new()) == NULL) {
54         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
55         return NULL;
56     }
57     p7->type = OBJ_nid2obj(NID_pkcs7_data);
58     if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL) {
59         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
60         goto err;
61     }
62 
63     if (!ASN1_item_pack(sk, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), &p7->d.data)) {
64         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CANT_PACK_STRUCTURE);
65         goto err;
66     }
67     return p7;
68 
69  err:
70     PKCS7_free(p7);
71     return NULL;
72 }
73 
74 /* Unpack SAFEBAGS from PKCS#7 data ContentInfo */
STACK_OF(PKCS12_SAFEBAG)75 STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(PKCS7 *p7)
76 {
77     if (!PKCS7_type_is_data(p7)) {
78         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
79         return NULL;
80     }
81     return ASN1_item_unpack(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS));
82 }
83 
84 /* Turn a stack of SAFEBAGS into a PKCS#7 encrypted data ContentInfo */
85 
PKCS12_pack_p7encdata_ex(int pbe_nid,const char * pass,int passlen,unsigned char * salt,int saltlen,int iter,STACK_OF (PKCS12_SAFEBAG)* bags,OSSL_LIB_CTX * ctx,const char * propq)86 PKCS7 *PKCS12_pack_p7encdata_ex(int pbe_nid, const char *pass, int passlen,
87                                 unsigned char *salt, int saltlen, int iter,
88                                 STACK_OF(PKCS12_SAFEBAG) *bags,
89                                 OSSL_LIB_CTX *ctx, const char *propq)
90 {
91     PKCS7 *p7;
92     X509_ALGOR *pbe;
93     const EVP_CIPHER *pbe_ciph = NULL;
94     EVP_CIPHER *pbe_ciph_fetch = NULL;
95 
96     if ((p7 = PKCS7_new_ex(ctx, propq)) == NULL) {
97         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
98         return NULL;
99     }
100     if (!PKCS7_set_type(p7, NID_pkcs7_encrypted)) {
101         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE);
102         goto err;
103     }
104 
105     ERR_set_mark();
106     pbe_ciph = pbe_ciph_fetch = EVP_CIPHER_fetch(ctx, OBJ_nid2sn(pbe_nid), propq);
107     if (pbe_ciph == NULL)
108         pbe_ciph = EVP_get_cipherbynid(pbe_nid);
109     ERR_pop_to_mark();
110 
111     if (pbe_ciph != NULL) {
112         pbe = PKCS5_pbe2_set_iv_ex(pbe_ciph, iter, salt, saltlen, NULL, -1, ctx);
113     } else {
114         pbe = PKCS5_pbe_set_ex(pbe_nid, iter, salt, saltlen, ctx);
115     }
116 
117     if (pbe == NULL) {
118         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
119         goto err;
120     }
121     X509_ALGOR_free(p7->d.encrypted->enc_data->algorithm);
122     p7->d.encrypted->enc_data->algorithm = pbe;
123     ASN1_OCTET_STRING_free(p7->d.encrypted->enc_data->enc_data);
124     if (!(p7->d.encrypted->enc_data->enc_data =
125           PKCS12_item_i2d_encrypt_ex(pbe, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), pass,
126                                      passlen, bags, 1, ctx, propq))) {
127         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
128         goto err;
129     }
130 
131     EVP_CIPHER_free(pbe_ciph_fetch);
132     return p7;
133 
134  err:
135     PKCS7_free(p7);
136     EVP_CIPHER_free(pbe_ciph_fetch);
137     return NULL;
138 }
139 
PKCS12_pack_p7encdata(int pbe_nid,const char * pass,int passlen,unsigned char * salt,int saltlen,int iter,STACK_OF (PKCS12_SAFEBAG)* bags)140 PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, const char *pass, int passlen,
141                              unsigned char *salt, int saltlen, int iter,
142                              STACK_OF(PKCS12_SAFEBAG) *bags)
143 {
144     return PKCS12_pack_p7encdata_ex(pbe_nid, pass, passlen, salt, saltlen,
145                                     iter, bags, NULL, NULL);
146 }
147 
STACK_OF(PKCS12_SAFEBAG)148 STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass,
149                                                   int passlen)
150 {
151     if (!PKCS7_type_is_encrypted(p7))
152         return NULL;
153     return PKCS12_item_decrypt_d2i_ex(p7->d.encrypted->enc_data->algorithm,
154                                    ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
155                                    pass, passlen,
156                                    p7->d.encrypted->enc_data->enc_data, 1,
157                                    p7->ctx.libctx, p7->ctx.propq);
158 }
159 
PKCS12_decrypt_skey_ex(const PKCS12_SAFEBAG * bag,const char * pass,int passlen,OSSL_LIB_CTX * ctx,const char * propq)160 PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey_ex(const PKCS12_SAFEBAG *bag,
161                                             const char *pass, int passlen,
162                                             OSSL_LIB_CTX *ctx, const char *propq)
163 {
164     return PKCS8_decrypt_ex(bag->value.shkeybag, pass, passlen, ctx, propq);
165 }
166 
PKCS12_decrypt_skey(const PKCS12_SAFEBAG * bag,const char * pass,int passlen)167 PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(const PKCS12_SAFEBAG *bag,
168                                          const char *pass, int passlen)
169 {
170     return PKCS12_decrypt_skey_ex(bag, pass, passlen, NULL, NULL);
171 }
172 
PKCS12_pack_authsafes(PKCS12 * p12,STACK_OF (PKCS7)* safes)173 int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(PKCS7) *safes)
174 {
175     if (ASN1_item_pack(safes, ASN1_ITEM_rptr(PKCS12_AUTHSAFES),
176                        &p12->authsafes->d.data))
177         return 1;
178     return 0;
179 }
180 
STACK_OF(PKCS7)181 STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12)
182 {
183     STACK_OF(PKCS7) *p7s;
184     PKCS7 *p7;
185     int i;
186 
187     if (!PKCS7_type_is_data(p12->authsafes)) {
188         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
189         return NULL;
190     }
191     p7s = ASN1_item_unpack(p12->authsafes->d.data,
192                            ASN1_ITEM_rptr(PKCS12_AUTHSAFES));
193     if (p7s != NULL) {
194         for (i = 0; i < sk_PKCS7_num(p7s); i++) {
195             p7 = sk_PKCS7_value(p7s, i);
196             if (!ossl_pkcs7_ctx_propagate(p12->authsafes, p7))
197                 goto err;
198         }
199     }
200     return p7s;
201 err:
202     sk_PKCS7_free(p7s);
203     return NULL;
204 }
205