Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/pkcs12/p12_add.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /* | - | ||||||
2 | * Copyright 1999-2016 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 | /* 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) {
| 0 | ||||||
24 | PKCS12err(PKCS12_F_PKCS12_ITEM_PACK_SAFEBAG, ERR_R_MALLOC_FAILURE); | - | ||||||
25 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
26 | } | - | ||||||
27 | bag->type = OBJ_nid2obj(nid1); | - | ||||||
28 | if (!ASN1_item_pack(obj, it, &bag->value.octet)) {
| 0 | ||||||
29 | PKCS12err(PKCS12_F_PKCS12_ITEM_PACK_SAFEBAG, ERR_R_MALLOC_FAILURE); | - | ||||||
30 | goto err; never executed: goto err; | 0 | ||||||
31 | } | - | ||||||
32 | if ((safebag = PKCS12_SAFEBAG_new()) == NULL) {
| 0 | ||||||
33 | PKCS12err(PKCS12_F_PKCS12_ITEM_PACK_SAFEBAG, ERR_R_MALLOC_FAILURE); | - | ||||||
34 | goto err; never executed: goto err; | 0 | ||||||
35 | } | - | ||||||
36 | safebag->value.bag = bag; | - | ||||||
37 | safebag->type = OBJ_nid2obj(nid2); | - | ||||||
38 | return safebag; never executed: return safebag; | 0 | ||||||
39 | - | |||||||
40 | err: | - | ||||||
41 | PKCS12_BAGS_free(bag); | - | ||||||
42 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
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) {
| 0 | ||||||
51 | PKCS12err(PKCS12_F_PKCS12_PACK_P7DATA, ERR_R_MALLOC_FAILURE); | - | ||||||
52 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
53 | } | - | ||||||
54 | p7->type = OBJ_nid2obj(NID_pkcs7_data); | - | ||||||
55 | if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL) {
| 0 | ||||||
56 | PKCS12err(PKCS12_F_PKCS12_PACK_P7DATA, ERR_R_MALLOC_FAILURE); | - | ||||||
57 | goto err; never executed: goto err; | 0 | ||||||
58 | } | - | ||||||
59 | - | |||||||
60 | if (!ASN1_item_pack(sk, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), &p7->d.data)) {
| 0 | ||||||
61 | PKCS12err(PKCS12_F_PKCS12_PACK_P7DATA, PKCS12_R_CANT_PACK_STRUCTURE); | - | ||||||
62 | goto err; never executed: goto err; | 0 | ||||||
63 | } | - | ||||||
64 | return p7; never executed: return p7; | 0 | ||||||
65 | - | |||||||
66 | err: | - | ||||||
67 | PKCS7_free(p7); | - | ||||||
68 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
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)) {
| 0-2 | ||||||
75 | PKCS12err(PKCS12_F_PKCS12_UNPACK_P7DATA, | - | ||||||
76 | PKCS12_R_CONTENT_TYPE_NOT_DATA); | - | ||||||
77 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
78 | } | - | ||||||
79 | return ASN1_item_unpack(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS)); executed 2 times by 1 test: return ASN1_item_unpack(p7->d.data, (&(PKCS12_SAFEBAGS_it))); Executed by:
| 2 | ||||||
80 | } | - | ||||||
81 | - | |||||||
82 | /* Turn a stack of SAFEBAGS into a PKCS#7 encrypted data ContentInfo */ | - | ||||||
83 | - | |||||||
84 | PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, const char *pass, int passlen, | - | ||||||
85 | unsigned char *salt, int saltlen, int iter, | - | ||||||
86 | STACK_OF(PKCS12_SAFEBAG) *bags) | - | ||||||
87 | { | - | ||||||
88 | PKCS7 *p7; | - | ||||||
89 | X509_ALGOR *pbe; | - | ||||||
90 | const EVP_CIPHER *pbe_ciph; | - | ||||||
91 | - | |||||||
92 | if ((p7 = PKCS7_new()) == NULL) {
| 0 | ||||||
93 | PKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA, ERR_R_MALLOC_FAILURE); | - | ||||||
94 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
95 | } | - | ||||||
96 | if (!PKCS7_set_type(p7, NID_pkcs7_encrypted)) {
| 0 | ||||||
97 | PKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA, | - | ||||||
98 | PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE); | - | ||||||
99 | goto err; never executed: goto err; | 0 | ||||||
100 | } | - | ||||||
101 | - | |||||||
102 | pbe_ciph = EVP_get_cipherbynid(pbe_nid); | - | ||||||
103 | - | |||||||
104 | if (pbe_ciph)
| 0 | ||||||
105 | pbe = PKCS5_pbe2_set(pbe_ciph, iter, salt, saltlen); never executed: pbe = PKCS5_pbe2_set(pbe_ciph, iter, salt, saltlen); | 0 | ||||||
106 | else | - | ||||||
107 | pbe = PKCS5_pbe_set(pbe_nid, iter, salt, saltlen); never executed: pbe = PKCS5_pbe_set(pbe_nid, iter, salt, saltlen); | 0 | ||||||
108 | - | |||||||
109 | if (!pbe) {
| 0 | ||||||
110 | PKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA, ERR_R_MALLOC_FAILURE); | - | ||||||
111 | goto err; never executed: goto err; | 0 | ||||||
112 | } | - | ||||||
113 | X509_ALGOR_free(p7->d.encrypted->enc_data->algorithm); | - | ||||||
114 | p7->d.encrypted->enc_data->algorithm = pbe; | - | ||||||
115 | ASN1_OCTET_STRING_free(p7->d.encrypted->enc_data->enc_data); | - | ||||||
116 | if (!(p7->d.encrypted->enc_data->enc_data =
| 0 | ||||||
117 | PKCS12_item_i2d_encrypt(pbe, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), pass,
| 0 | ||||||
118 | passlen, bags, 1))) {
| 0 | ||||||
119 | PKCS12err(PKCS12_F_PKCS12_PACK_P7ENCDATA, PKCS12_R_ENCRYPT_ERROR); | - | ||||||
120 | goto err; never executed: goto err; | 0 | ||||||
121 | } | - | ||||||
122 | - | |||||||
123 | return p7; never executed: return p7; | 0 | ||||||
124 | - | |||||||
125 | err: | - | ||||||
126 | PKCS7_free(p7); | - | ||||||
127 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
128 | } | - | ||||||
129 | - | |||||||
130 | STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass, | - | ||||||
131 | int passlen) | - | ||||||
132 | { | - | ||||||
133 | if (!PKCS7_type_is_encrypted(p7))
| 0 | ||||||
134 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
135 | return PKCS12_item_decrypt_d2i(p7->d.encrypted->enc_data->algorithm, never executed: return PKCS12_item_decrypt_d2i(p7->d.encrypted->enc_data->algorithm, (&(PKCS12_SAFEBAGS_it)), pass, passlen, p7->d.encrypted->enc_data->enc_data, 1); | 0 | ||||||
136 | ASN1_ITEM_rptr(PKCS12_SAFEBAGS), never executed: return PKCS12_item_decrypt_d2i(p7->d.encrypted->enc_data->algorithm, (&(PKCS12_SAFEBAGS_it)), pass, passlen, p7->d.encrypted->enc_data->enc_data, 1); | 0 | ||||||
137 | pass, passlen, never executed: return PKCS12_item_decrypt_d2i(p7->d.encrypted->enc_data->algorithm, (&(PKCS12_SAFEBAGS_it)), pass, passlen, p7->d.encrypted->enc_data->enc_data, 1); | 0 | ||||||
138 | p7->d.encrypted->enc_data->enc_data, 1); never executed: return PKCS12_item_decrypt_d2i(p7->d.encrypted->enc_data->algorithm, (&(PKCS12_SAFEBAGS_it)), pass, passlen, p7->d.encrypted->enc_data->enc_data, 1); | 0 | ||||||
139 | } | - | ||||||
140 | - | |||||||
141 | PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(const PKCS12_SAFEBAG *bag, | - | ||||||
142 | const char *pass, int passlen) | - | ||||||
143 | { | - | ||||||
144 | return PKCS8_decrypt(bag->value.shkeybag, pass, passlen); never executed: return PKCS8_decrypt(bag->value.shkeybag, pass, passlen); | 0 | ||||||
145 | } | - | ||||||
146 | - | |||||||
147 | int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(PKCS7) *safes) | - | ||||||
148 | { | - | ||||||
149 | if (ASN1_item_pack(safes, ASN1_ITEM_rptr(PKCS12_AUTHSAFES),
| 0 | ||||||
150 | &p12->authsafes->d.data))
| 0 | ||||||
151 | return 1; never executed: return 1; | 0 | ||||||
152 | return 0; never executed: return 0; | 0 | ||||||
153 | } | - | ||||||
154 | - | |||||||
155 | STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12) | - | ||||||
156 | { | - | ||||||
157 | if (!PKCS7_type_is_data(p12->authsafes)) {
| 0-1 | ||||||
158 | PKCS12err(PKCS12_F_PKCS12_UNPACK_AUTHSAFES, | - | ||||||
159 | PKCS12_R_CONTENT_TYPE_NOT_DATA); | - | ||||||
160 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
161 | } | - | ||||||
162 | return ASN1_item_unpack(p12->authsafes->d.data, executed 1 time by 1 test: return ASN1_item_unpack(p12->authsafes->d.data, (&(PKCS12_AUTHSAFES_it))); Executed by:
| 1 | ||||||
163 | ASN1_ITEM_rptr(PKCS12_AUTHSAFES)); executed 1 time by 1 test: return ASN1_item_unpack(p12->authsafes->d.data, (&(PKCS12_AUTHSAFES_it))); Executed by:
| 1 | ||||||
164 | } | - | ||||||
Source code | Switch to Preprocessed file |