]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs12/p12_add.c
Update copyright year
[thirdparty/openssl.git] / crypto / pkcs12 / p12_add.c
1 /*
2 * Copyright 1999-2020 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 /* Pack an object into an OCTET STRING and turn into a safebag */
16
17 PKCS12_SAFEBAG *PKCS12_item_pack_safebag(void *obj, const ASN1_ITEM *it,
18 int nid1, int nid2)
19 {
20 PKCS12_BAGS *bag;
21 PKCS12_SAFEBAG *safebag;
22
23 if ((bag = PKCS12_BAGS_new()) == NULL) {
24 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
25 return NULL;
26 }
27 bag->type = OBJ_nid2obj(nid1);
28 if (!ASN1_item_pack(obj, it, &bag->value.octet)) {
29 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
30 goto err;
31 }
32 if ((safebag = PKCS12_SAFEBAG_new()) == NULL) {
33 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
34 goto err;
35 }
36 safebag->value.bag = bag;
37 safebag->type = OBJ_nid2obj(nid2);
38 return safebag;
39
40 err:
41 PKCS12_BAGS_free(bag);
42 return NULL;
43 }
44
45 /* Turn a stack of SAFEBAGS into a PKCS#7 data Contentinfo */
46 PKCS7 *PKCS12_pack_p7data(STACK_OF(PKCS12_SAFEBAG) *sk)
47 {
48 PKCS7 *p7;
49
50 if ((p7 = PKCS7_new()) == NULL) {
51 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
52 return NULL;
53 }
54 p7->type = OBJ_nid2obj(NID_pkcs7_data);
55 if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL) {
56 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
57 goto err;
58 }
59
60 if (!ASN1_item_pack(sk, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), &p7->d.data)) {
61 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CANT_PACK_STRUCTURE);
62 goto err;
63 }
64 return p7;
65
66 err:
67 PKCS7_free(p7);
68 return NULL;
69 }
70
71 /* Unpack SAFEBAGS from PKCS#7 data ContentInfo */
72 STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(PKCS7 *p7)
73 {
74 if (!PKCS7_type_is_data(p7)) {
75 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
76 return NULL;
77 }
78 return ASN1_item_unpack(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS));
79 }
80
81 /* Turn a stack of SAFEBAGS into a PKCS#7 encrypted data ContentInfo */
82
83 PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, const char *pass, int passlen,
84 unsigned char *salt, int saltlen, int iter,
85 STACK_OF(PKCS12_SAFEBAG) *bags)
86 {
87 PKCS7 *p7;
88 X509_ALGOR *pbe;
89 const EVP_CIPHER *pbe_ciph;
90
91 if ((p7 = PKCS7_new()) == NULL) {
92 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
93 return NULL;
94 }
95 if (!PKCS7_set_type(p7, NID_pkcs7_encrypted)) {
96 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE);
97 goto err;
98 }
99
100 pbe_ciph = EVP_get_cipherbynid(pbe_nid);
101
102 if (pbe_ciph)
103 pbe = PKCS5_pbe2_set(pbe_ciph, iter, salt, saltlen);
104 else
105 pbe = PKCS5_pbe_set(pbe_nid, iter, salt, saltlen);
106
107 if (pbe == NULL) {
108 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
109 goto err;
110 }
111 X509_ALGOR_free(p7->d.encrypted->enc_data->algorithm);
112 p7->d.encrypted->enc_data->algorithm = pbe;
113 ASN1_OCTET_STRING_free(p7->d.encrypted->enc_data->enc_data);
114 if (!(p7->d.encrypted->enc_data->enc_data =
115 PKCS12_item_i2d_encrypt(pbe, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), pass,
116 passlen, bags, 1))) {
117 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
118 goto err;
119 }
120
121 return p7;
122
123 err:
124 PKCS7_free(p7);
125 return NULL;
126 }
127
128 STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass,
129 int passlen)
130 {
131 if (!PKCS7_type_is_encrypted(p7))
132 return NULL;
133 return PKCS12_item_decrypt_d2i(p7->d.encrypted->enc_data->algorithm,
134 ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
135 pass, passlen,
136 p7->d.encrypted->enc_data->enc_data, 1);
137 }
138
139 PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(const PKCS12_SAFEBAG *bag,
140 const char *pass, int passlen)
141 {
142 return PKCS8_decrypt(bag->value.shkeybag, pass, passlen);
143 }
144
145 int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(PKCS7) *safes)
146 {
147 if (ASN1_item_pack(safes, ASN1_ITEM_rptr(PKCS12_AUTHSAFES),
148 &p12->authsafes->d.data))
149 return 1;
150 return 0;
151 }
152
153 STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12)
154 {
155 if (!PKCS7_type_is_data(p12->authsafes)) {
156 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
157 return NULL;
158 }
159 return ASN1_item_unpack(p12->authsafes->d.data,
160 ASN1_ITEM_rptr(PKCS12_AUTHSAFES));
161 }