Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/pkcs7/pk7_smime.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 | /* Simple PKCS#7 processing functions */ | - | ||||||||||||
11 | - | |||||||||||||
12 | #include <stdio.h> | - | ||||||||||||
13 | #include "internal/cryptlib.h" | - | ||||||||||||
14 | #include <openssl/x509.h> | - | ||||||||||||
15 | #include <openssl/x509v3.h> | - | ||||||||||||
16 | - | |||||||||||||
17 | - | |||||||||||||
18 | #define BUFFERSIZE 4096 | - | ||||||||||||
19 | - | |||||||||||||
20 | static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si); | - | ||||||||||||
21 | - | |||||||||||||
22 | PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, | - | ||||||||||||
23 | BIO *data, int flags) | - | ||||||||||||
24 | { | - | ||||||||||||
25 | PKCS7 *p7; | - | ||||||||||||
26 | int i; | - | ||||||||||||
27 | - | |||||||||||||
28 | if ((p7 = PKCS7_new()) == NULL) {
| 0-11 | ||||||||||||
29 | PKCS7err(PKCS7_F_PKCS7_SIGN, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
30 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
31 | } | - | ||||||||||||
32 | - | |||||||||||||
33 | if (!PKCS7_set_type(p7, NID_pkcs7_signed))
| 0-11 | ||||||||||||
34 | goto err; never executed: goto err; | 0 | ||||||||||||
35 | - | |||||||||||||
36 | if (!PKCS7_content_new(p7, NID_pkcs7_data))
| 0-11 | ||||||||||||
37 | goto err; never executed: goto err; | 0 | ||||||||||||
38 | - | |||||||||||||
39 | if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) {
| 0-11 | ||||||||||||
40 | PKCS7err(PKCS7_F_PKCS7_SIGN, PKCS7_R_PKCS7_ADD_SIGNER_ERROR); | - | ||||||||||||
41 | goto err; never executed: goto err; | 0 | ||||||||||||
42 | } | - | ||||||||||||
43 | - | |||||||||||||
44 | if (!(flags & PKCS7_NOCERTS)) {
| 0-11 | ||||||||||||
45 | for (i = 0; i < sk_X509_num(certs); i++) {
| 2-11 | ||||||||||||
46 | if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i)))
| 0-2 | ||||||||||||
47 | goto err; never executed: goto err; | 0 | ||||||||||||
48 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
49 | } executed 11 times by 1 test: end of block Executed by:
| 11 | ||||||||||||
50 | - | |||||||||||||
51 | if (flags & PKCS7_DETACHED)
| 4-7 | ||||||||||||
52 | PKCS7_set_detached(p7, 1); executed 4 times by 1 test: PKCS7_ctrl(p7,1,1, ((void *)0) ); Executed by:
| 4 | ||||||||||||
53 | - | |||||||||||||
54 | if (flags & (PKCS7_STREAM | PKCS7_PARTIAL))
| 0-11 | ||||||||||||
55 | return p7; executed 11 times by 1 test: return p7; Executed by:
| 11 | ||||||||||||
56 | - | |||||||||||||
57 | if (PKCS7_final(p7, data, flags))
| 0 | ||||||||||||
58 | return p7; never executed: return p7; | 0 | ||||||||||||
59 | - | |||||||||||||
60 | err: code before this statement never executed: err: | 0 | ||||||||||||
61 | PKCS7_free(p7); | - | ||||||||||||
62 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
63 | } | - | ||||||||||||
64 | - | |||||||||||||
65 | int PKCS7_final(PKCS7 *p7, BIO *data, int flags) | - | ||||||||||||
66 | { | - | ||||||||||||
67 | BIO *p7bio; | - | ||||||||||||
68 | int ret = 0; | - | ||||||||||||
69 | - | |||||||||||||
70 | if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
| 0-4 | ||||||||||||
71 | PKCS7err(PKCS7_F_PKCS7_FINAL, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
72 | return 0; never executed: return 0; | 0 | ||||||||||||
73 | } | - | ||||||||||||
74 | - | |||||||||||||
75 | SMIME_crlf_copy(data, p7bio, flags); | - | ||||||||||||
76 | - | |||||||||||||
77 | (void)BIO_flush(p7bio); | - | ||||||||||||
78 | - | |||||||||||||
79 | if (!PKCS7_dataFinal(p7, p7bio)) {
| 0-4 | ||||||||||||
80 | PKCS7err(PKCS7_F_PKCS7_FINAL, PKCS7_R_PKCS7_DATASIGN); | - | ||||||||||||
81 | goto err; never executed: goto err; | 0 | ||||||||||||
82 | } | - | ||||||||||||
83 | - | |||||||||||||
84 | ret = 1; | - | ||||||||||||
85 | - | |||||||||||||
86 | err: code before this statement executed 4 times by 1 test: err: Executed by:
| 4 | ||||||||||||
87 | BIO_free_all(p7bio); | - | ||||||||||||
88 | - | |||||||||||||
89 | return ret; executed 4 times by 1 test: return ret; Executed by:
| 4 | ||||||||||||
90 | - | |||||||||||||
91 | } | - | ||||||||||||
92 | - | |||||||||||||
93 | /* Check to see if a cipher exists and if so add S/MIME capabilities */ | - | ||||||||||||
94 | - | |||||||||||||
95 | static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg) | - | ||||||||||||
96 | { | - | ||||||||||||
97 | if (EVP_get_cipherbynid(nid))
| 20-160 | ||||||||||||
98 | return PKCS7_simple_smimecap(sk, nid, arg); executed 160 times by 1 test: return PKCS7_simple_smimecap(sk, nid, arg); Executed by:
| 160 | ||||||||||||
99 | return 1; executed 20 times by 1 test: return 1; Executed by:
| 20 | ||||||||||||
100 | } | - | ||||||||||||
101 | - | |||||||||||||
102 | static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg) | - | ||||||||||||
103 | { | - | ||||||||||||
104 | if (EVP_get_digestbynid(nid))
| 0-60 | ||||||||||||
105 | return PKCS7_simple_smimecap(sk, nid, arg); never executed: return PKCS7_simple_smimecap(sk, nid, arg); | 0 | ||||||||||||
106 | return 1; executed 60 times by 1 test: return 1; Executed by:
| 60 | ||||||||||||
107 | } | - | ||||||||||||
108 | - | |||||||||||||
109 | PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert, | - | ||||||||||||
110 | EVP_PKEY *pkey, const EVP_MD *md, | - | ||||||||||||
111 | int flags) | - | ||||||||||||
112 | { | - | ||||||||||||
113 | PKCS7_SIGNER_INFO *si = NULL; | - | ||||||||||||
114 | STACK_OF(X509_ALGOR) *smcap = NULL; | - | ||||||||||||
115 | if (!X509_check_private_key(signcert, pkey)) {
| 0-24 | ||||||||||||
116 | PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, | - | ||||||||||||
117 | PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | - | ||||||||||||
118 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
119 | } | - | ||||||||||||
120 | - | |||||||||||||
121 | if ((si = PKCS7_add_signature(p7, signcert, pkey, md)) == NULL) {
| 0-24 | ||||||||||||
122 | PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, | - | ||||||||||||
123 | PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR); | - | ||||||||||||
124 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
125 | } | - | ||||||||||||
126 | - | |||||||||||||
127 | if (!(flags & PKCS7_NOCERTS)) {
| 0-24 | ||||||||||||
128 | if (!PKCS7_add_certificate(p7, signcert))
| 0-24 | ||||||||||||
129 | goto err; never executed: goto err; | 0 | ||||||||||||
130 | } executed 24 times by 1 test: end of block Executed by:
| 24 | ||||||||||||
131 | - | |||||||||||||
132 | if (!(flags & PKCS7_NOATTR)) {
| 4-20 | ||||||||||||
133 | if (!PKCS7_add_attrib_content_type(si, NULL))
| 0-20 | ||||||||||||
134 | goto err; never executed: goto err; | 0 | ||||||||||||
135 | /* Add SMIMECapabilities */ | - | ||||||||||||
136 | if (!(flags & PKCS7_NOSMIMECAP)) {
| 0-20 | ||||||||||||
137 | if ((smcap = sk_X509_ALGOR_new_null()) == NULL) {
| 0-20 | ||||||||||||
138 | PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
139 | goto err; never executed: goto err; | 0 | ||||||||||||
140 | } | - | ||||||||||||
141 | if (!add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
| 0-20 | ||||||||||||
142 | || !add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
| 0-20 | ||||||||||||
143 | || !add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
| 0-20 | ||||||||||||
144 | || !add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
| 0-20 | ||||||||||||
145 | || !add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
| 0-20 | ||||||||||||
146 | || !add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
| 0-20 | ||||||||||||
147 | || !add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
| 0-20 | ||||||||||||
148 | || !add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
| 0-20 | ||||||||||||
149 | || !add_cipher_smcap(smcap, NID_rc2_cbc, 128)
| 0-20 | ||||||||||||
150 | || !add_cipher_smcap(smcap, NID_rc2_cbc, 64)
| 0-20 | ||||||||||||
151 | || !add_cipher_smcap(smcap, NID_des_cbc, -1)
| 0-20 | ||||||||||||
152 | || !add_cipher_smcap(smcap, NID_rc2_cbc, 40)
| 0-20 | ||||||||||||
153 | || !PKCS7_add_attrib_smimecap(si, smcap))
| 0-20 | ||||||||||||
154 | goto err; never executed: goto err; | 0 | ||||||||||||
155 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); | - | ||||||||||||
156 | smcap = NULL; | - | ||||||||||||
157 | } executed 20 times by 1 test: end of block Executed by:
| 20 | ||||||||||||
158 | if (flags & PKCS7_REUSE_DIGEST) {
| 1-19 | ||||||||||||
159 | if (!pkcs7_copy_existing_digest(p7, si))
| 0-1 | ||||||||||||
160 | goto err; never executed: goto err; | 0 | ||||||||||||
161 | if (!(flags & PKCS7_PARTIAL) && !PKCS7_SIGNER_INFO_sign(si))
| 0-1 | ||||||||||||
162 | goto err; never executed: goto err; | 0 | ||||||||||||
163 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
164 | } executed 20 times by 1 test: end of block Executed by:
| 20 | ||||||||||||
165 | return si; executed 24 times by 1 test: return si; Executed by:
| 24 | ||||||||||||
166 | err: | - | ||||||||||||
167 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); | - | ||||||||||||
168 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
169 | } | - | ||||||||||||
170 | - | |||||||||||||
171 | /* | - | ||||||||||||
172 | * Search for a digest matching SignerInfo digest type and if found copy | - | ||||||||||||
173 | * across. | - | ||||||||||||
174 | */ | - | ||||||||||||
175 | - | |||||||||||||
176 | static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si) | - | ||||||||||||
177 | { | - | ||||||||||||
178 | int i; | - | ||||||||||||
179 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | - | ||||||||||||
180 | PKCS7_SIGNER_INFO *sitmp; | - | ||||||||||||
181 | ASN1_OCTET_STRING *osdig = NULL; | - | ||||||||||||
182 | sinfos = PKCS7_get_signer_info(p7); | - | ||||||||||||
183 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
| 0-1 | ||||||||||||
184 | sitmp = sk_PKCS7_SIGNER_INFO_value(sinfos, i); | - | ||||||||||||
185 | if (si == sitmp)
| 0-1 | ||||||||||||
186 | break; never executed: break; | 0 | ||||||||||||
187 | if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0)
| 0-1 | ||||||||||||
188 | continue; never executed: continue; | 0 | ||||||||||||
189 | if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) {
| 0-1 | ||||||||||||
190 | osdig = PKCS7_digest_from_attributes(sitmp->auth_attr); | - | ||||||||||||
191 | break; executed 1 time by 1 test: break; Executed by:
| 1 | ||||||||||||
192 | } | - | ||||||||||||
193 | - | |||||||||||||
194 | } never executed: end of block | 0 | ||||||||||||
195 | - | |||||||||||||
196 | if (osdig)
| 0-1 | ||||||||||||
197 | return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length); executed 1 time by 1 test: return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length); Executed by:
| 1 | ||||||||||||
198 | - | |||||||||||||
199 | PKCS7err(PKCS7_F_PKCS7_COPY_EXISTING_DIGEST, | - | ||||||||||||
200 | PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND); | - | ||||||||||||
201 | return 0; never executed: return 0; | 0 | ||||||||||||
202 | } | - | ||||||||||||
203 | - | |||||||||||||
204 | int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, | - | ||||||||||||
205 | BIO *indata, BIO *out, int flags) | - | ||||||||||||
206 | { | - | ||||||||||||
207 | STACK_OF(X509) *signers; | - | ||||||||||||
208 | X509 *signer; | - | ||||||||||||
209 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | - | ||||||||||||
210 | PKCS7_SIGNER_INFO *si; | - | ||||||||||||
211 | X509_STORE_CTX *cert_ctx = NULL; | - | ||||||||||||
212 | char *buf = NULL; | - | ||||||||||||
213 | int i, j = 0, k, ret = 0; | - | ||||||||||||
214 | BIO *p7bio = NULL; | - | ||||||||||||
215 | BIO *tmpin = NULL, *tmpout = NULL; | - | ||||||||||||
216 | - | |||||||||||||
217 | if (!p7) {
| 0-12 | ||||||||||||
218 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_INVALID_NULL_POINTER); | - | ||||||||||||
219 | return 0; never executed: return 0; | 0 | ||||||||||||
220 | } | - | ||||||||||||
221 | - | |||||||||||||
222 | if (!PKCS7_type_is_signed(p7)) {
| 0-12 | ||||||||||||
223 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_WRONG_CONTENT_TYPE); | - | ||||||||||||
224 | return 0; never executed: return 0; | 0 | ||||||||||||
225 | } | - | ||||||||||||
226 | - | |||||||||||||
227 | /* Check for no data and no content: no data to verify signature */ | - | ||||||||||||
228 | if (PKCS7_get_detached(p7) && !indata) {
| 0-7 | ||||||||||||
229 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_CONTENT); | - | ||||||||||||
230 | return 0; never executed: return 0; | 0 | ||||||||||||
231 | } | - | ||||||||||||
232 | - | |||||||||||||
233 | if (flags & PKCS7_NO_DUAL_CONTENT) {
| 0-12 | ||||||||||||
234 | /* | - | ||||||||||||
235 | * This was originally "#if 0" because we thought that only old broken | - | ||||||||||||
236 | * Netscape did this. It turns out that Authenticode uses this kind | - | ||||||||||||
237 | * of "extended" PKCS7 format, and things like UEFI secure boot and | - | ||||||||||||
238 | * tools like osslsigncode need it. In Authenticode the verification | - | ||||||||||||
239 | * process is different, but the existing PKCs7 verification works. | - | ||||||||||||
240 | */ | - | ||||||||||||
241 | if (!PKCS7_get_detached(p7) && indata) {
| 0 | ||||||||||||
242 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_CONTENT_AND_DATA_PRESENT); | - | ||||||||||||
243 | return 0; never executed: return 0; | 0 | ||||||||||||
244 | } | - | ||||||||||||
245 | } never executed: end of block | 0 | ||||||||||||
246 | - | |||||||||||||
247 | sinfos = PKCS7_get_signer_info(p7); | - | ||||||||||||
248 | - | |||||||||||||
249 | if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
| 0-12 | ||||||||||||
250 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_SIGNATURES_ON_DATA); | - | ||||||||||||
251 | return 0; never executed: return 0; | 0 | ||||||||||||
252 | } | - | ||||||||||||
253 | - | |||||||||||||
254 | signers = PKCS7_get0_signers(p7, certs, flags); | - | ||||||||||||
255 | if (!signers)
| 0-12 | ||||||||||||
256 | return 0; never executed: return 0; | 0 | ||||||||||||
257 | - | |||||||||||||
258 | /* Now verify the certificates */ | - | ||||||||||||
259 | - | |||||||||||||
260 | cert_ctx = X509_STORE_CTX_new(); | - | ||||||||||||
261 | if (cert_ctx == NULL)
| 0-12 | ||||||||||||
262 | goto err; never executed: goto err; | 0 | ||||||||||||
263 | if (!(flags & PKCS7_NOVERIFY))
| 0-12 | ||||||||||||
264 | for (k = 0; k < sk_X509_num(signers); k++) {
| 12-25 | ||||||||||||
265 | signer = sk_X509_value(signers, k); | - | ||||||||||||
266 | if (!(flags & PKCS7_NOCHAIN)) {
| 0-25 | ||||||||||||
267 | if (!X509_STORE_CTX_init(cert_ctx, store, signer,
| 0-25 | ||||||||||||
268 | p7->d.sign->cert)) {
| 0-25 | ||||||||||||
269 | PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB); | - | ||||||||||||
270 | goto err; never executed: goto err; | 0 | ||||||||||||
271 | } | - | ||||||||||||
272 | X509_STORE_CTX_set_default(cert_ctx, "smime_sign"); | - | ||||||||||||
273 | } else if (!X509_STORE_CTX_init(cert_ctx, store, signer, NULL)) { executed 25 times by 1 test: end of block Executed by:
| 0-25 | ||||||||||||
274 | PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB); | - | ||||||||||||
275 | goto err; never executed: goto err; | 0 | ||||||||||||
276 | } | - | ||||||||||||
277 | if (!(flags & PKCS7_NOCRL))
| 0-25 | ||||||||||||
278 | X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl); executed 25 times by 1 test: X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl); Executed by:
| 25 | ||||||||||||
279 | i = X509_verify_cert(cert_ctx); | - | ||||||||||||
280 | if (i <= 0)
| 0-25 | ||||||||||||
281 | j = X509_STORE_CTX_get_error(cert_ctx); never executed: j = X509_STORE_CTX_get_error(cert_ctx); | 0 | ||||||||||||
282 | X509_STORE_CTX_cleanup(cert_ctx); | - | ||||||||||||
283 | if (i <= 0) {
| 0-25 | ||||||||||||
284 | PKCS7err(PKCS7_F_PKCS7_VERIFY, | - | ||||||||||||
285 | PKCS7_R_CERTIFICATE_VERIFY_ERROR); | - | ||||||||||||
286 | ERR_add_error_data(2, "Verify error:", | - | ||||||||||||
287 | X509_verify_cert_error_string(j)); | - | ||||||||||||
288 | goto err; never executed: goto err; | 0 | ||||||||||||
289 | } | - | ||||||||||||
290 | /* Check for revocation status here */ | - | ||||||||||||
291 | } executed 25 times by 1 test: end of block Executed by:
| 25 | ||||||||||||
292 | - | |||||||||||||
293 | /* | - | ||||||||||||
294 | * Performance optimization: if the content is a memory BIO then store | - | ||||||||||||
295 | * its contents in a temporary read only memory BIO. This avoids | - | ||||||||||||
296 | * potentially large numbers of slow copies of data which will occur when | - | ||||||||||||
297 | * reading from a read write memory BIO when signatures are calculated. | - | ||||||||||||
298 | */ | - | ||||||||||||
299 | - | |||||||||||||
300 | if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) {
| 2-7 | ||||||||||||
301 | char *ptr; | - | ||||||||||||
302 | long len; | - | ||||||||||||
303 | len = BIO_get_mem_data(indata, &ptr); | - | ||||||||||||
304 | tmpin = BIO_new_mem_buf(ptr, len); | - | ||||||||||||
305 | if (tmpin == NULL) {
| 0-2 | ||||||||||||
306 | PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
307 | goto err; never executed: goto err; | 0 | ||||||||||||
308 | } | - | ||||||||||||
309 | } else executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
310 | tmpin = indata; executed 10 times by 1 test: tmpin = indata; Executed by:
| 10 | ||||||||||||
311 | - | |||||||||||||
312 | if ((p7bio = PKCS7_dataInit(p7, tmpin)) == NULL)
| 0-12 | ||||||||||||
313 | goto err; never executed: goto err; | 0 | ||||||||||||
314 | - | |||||||||||||
315 | if (flags & PKCS7_TEXT) {
| 0-12 | ||||||||||||
316 | if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
| 0 | ||||||||||||
317 | PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
318 | goto err; never executed: goto err; | 0 | ||||||||||||
319 | } | - | ||||||||||||
320 | BIO_set_mem_eof_return(tmpout, 0); | - | ||||||||||||
321 | } else never executed: end of block | 0 | ||||||||||||
322 | tmpout = out; executed 12 times by 1 test: tmpout = out; Executed by:
| 12 | ||||||||||||
323 | - | |||||||||||||
324 | /* We now have to 'read' from p7bio to calculate digests etc. */ | - | ||||||||||||
325 | if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
| 0-12 | ||||||||||||
326 | PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
327 | goto err; never executed: goto err; | 0 | ||||||||||||
328 | } | - | ||||||||||||
329 | for (;;) { | - | ||||||||||||
330 | i = BIO_read(p7bio, buf, BUFFERSIZE); | - | ||||||||||||
331 | if (i <= 0)
| 12 | ||||||||||||
332 | break; executed 12 times by 1 test: break; Executed by:
| 12 | ||||||||||||
333 | if (tmpout)
| 0-12 | ||||||||||||
334 | BIO_write(tmpout, buf, i); executed 12 times by 1 test: BIO_write(tmpout, buf, i); Executed by:
| 12 | ||||||||||||
335 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||
336 | - | |||||||||||||
337 | if (flags & PKCS7_TEXT) {
| 0-12 | ||||||||||||
338 | if (!SMIME_text(tmpout, out)) {
| 0 | ||||||||||||
339 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SMIME_TEXT_ERROR); | - | ||||||||||||
340 | BIO_free(tmpout); | - | ||||||||||||
341 | goto err; never executed: goto err; | 0 | ||||||||||||
342 | } | - | ||||||||||||
343 | BIO_free(tmpout); | - | ||||||||||||
344 | } never executed: end of block | 0 | ||||||||||||
345 | - | |||||||||||||
346 | /* Now Verify All Signatures */ | - | ||||||||||||
347 | if (!(flags & PKCS7_NOSIGS))
| 0-12 | ||||||||||||
348 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
| 12-25 | ||||||||||||
349 | si = sk_PKCS7_SIGNER_INFO_value(sinfos, i); | - | ||||||||||||
350 | signer = sk_X509_value(signers, i); | - | ||||||||||||
351 | j = PKCS7_signatureVerify(p7bio, p7, si, signer); | - | ||||||||||||
352 | if (j <= 0) {
| 0-25 | ||||||||||||
353 | PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SIGNATURE_FAILURE); | - | ||||||||||||
354 | goto err; never executed: goto err; | 0 | ||||||||||||
355 | } | - | ||||||||||||
356 | } executed 25 times by 1 test: end of block Executed by:
| 25 | ||||||||||||
357 | - | |||||||||||||
358 | ret = 1; | - | ||||||||||||
359 | - | |||||||||||||
360 | err: code before this statement executed 12 times by 1 test: err: Executed by:
| 12 | ||||||||||||
361 | X509_STORE_CTX_free(cert_ctx); | - | ||||||||||||
362 | OPENSSL_free(buf); | - | ||||||||||||
363 | if (tmpin == indata) {
| 2-10 | ||||||||||||
364 | if (indata)
| 3-7 | ||||||||||||
365 | BIO_pop(p7bio); executed 3 times by 1 test: BIO_pop(p7bio); Executed by:
| 3 | ||||||||||||
366 | } executed 10 times by 1 test: end of block Executed by:
| 10 | ||||||||||||
367 | BIO_free_all(p7bio); | - | ||||||||||||
368 | sk_X509_free(signers); | - | ||||||||||||
369 | return ret; executed 12 times by 1 test: return ret; Executed by:
| 12 | ||||||||||||
370 | } | - | ||||||||||||
371 | - | |||||||||||||
372 | STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, | - | ||||||||||||
373 | int flags) | - | ||||||||||||
374 | { | - | ||||||||||||
375 | STACK_OF(X509) *signers; | - | ||||||||||||
376 | STACK_OF(PKCS7_SIGNER_INFO) *sinfos; | - | ||||||||||||
377 | PKCS7_SIGNER_INFO *si; | - | ||||||||||||
378 | PKCS7_ISSUER_AND_SERIAL *ias; | - | ||||||||||||
379 | X509 *signer; | - | ||||||||||||
380 | int i; | - | ||||||||||||
381 | - | |||||||||||||
382 | if (!p7) {
| 0-24 | ||||||||||||
383 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_INVALID_NULL_POINTER); | - | ||||||||||||
384 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
385 | } | - | ||||||||||||
386 | - | |||||||||||||
387 | if (!PKCS7_type_is_signed(p7)) {
| 0-24 | ||||||||||||
388 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_WRONG_CONTENT_TYPE); | - | ||||||||||||
389 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
390 | } | - | ||||||||||||
391 | - | |||||||||||||
392 | /* Collect all the signers together */ | - | ||||||||||||
393 | - | |||||||||||||
394 | sinfos = PKCS7_get_signer_info(p7); | - | ||||||||||||
395 | - | |||||||||||||
396 | if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
| 0-24 | ||||||||||||
397 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_NO_SIGNERS); | - | ||||||||||||
398 | return 0; never executed: return 0; | 0 | ||||||||||||
399 | } | - | ||||||||||||
400 | - | |||||||||||||
401 | if ((signers = sk_X509_new_null()) == NULL) {
| 0-24 | ||||||||||||
402 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
403 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
404 | } | - | ||||||||||||
405 | - | |||||||||||||
406 | for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
| 24-50 | ||||||||||||
407 | si = sk_PKCS7_SIGNER_INFO_value(sinfos, i); | - | ||||||||||||
408 | ias = si->issuer_and_serial; | - | ||||||||||||
409 | signer = NULL; | - | ||||||||||||
410 | /* If any certificates passed they take priority */ | - | ||||||||||||
411 | if (certs)
| 0-50 | ||||||||||||
412 | signer = X509_find_by_issuer_and_serial(certs, never executed: signer = X509_find_by_issuer_and_serial(certs, ias->issuer, ias->serial); | 0 | ||||||||||||
413 | ias->issuer, ias->serial); never executed: signer = X509_find_by_issuer_and_serial(certs, ias->issuer, ias->serial); | 0 | ||||||||||||
414 | if (!signer && !(flags & PKCS7_NOINTERN)
| 0-50 | ||||||||||||
415 | && p7->d.sign->cert)
| 0-50 | ||||||||||||
416 | signer = executed 50 times by 1 test: signer = X509_find_by_issuer_and_serial(p7->d.sign->cert, ias->issuer, ias->serial); Executed by:
| 50 | ||||||||||||
417 | X509_find_by_issuer_and_serial(p7->d.sign->cert, executed 50 times by 1 test: signer = X509_find_by_issuer_and_serial(p7->d.sign->cert, ias->issuer, ias->serial); Executed by:
| 50 | ||||||||||||
418 | ias->issuer, ias->serial); executed 50 times by 1 test: signer = X509_find_by_issuer_and_serial(p7->d.sign->cert, ias->issuer, ias->serial); Executed by:
| 50 | ||||||||||||
419 | if (!signer) {
| 0-50 | ||||||||||||
420 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, | - | ||||||||||||
421 | PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND); | - | ||||||||||||
422 | sk_X509_free(signers); | - | ||||||||||||
423 | return 0; never executed: return 0; | 0 | ||||||||||||
424 | } | - | ||||||||||||
425 | - | |||||||||||||
426 | if (!sk_X509_push(signers, signer)) {
| 0-50 | ||||||||||||
427 | sk_X509_free(signers); | - | ||||||||||||
428 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
429 | } | - | ||||||||||||
430 | } executed 50 times by 1 test: end of block Executed by:
| 50 | ||||||||||||
431 | return signers; executed 24 times by 1 test: return signers; Executed by:
| 24 | ||||||||||||
432 | } | - | ||||||||||||
433 | - | |||||||||||||
434 | /* Build a complete PKCS#7 enveloped data */ | - | ||||||||||||
435 | - | |||||||||||||
436 | PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, | - | ||||||||||||
437 | int flags) | - | ||||||||||||
438 | { | - | ||||||||||||
439 | PKCS7 *p7; | - | ||||||||||||
440 | BIO *p7bio = NULL; | - | ||||||||||||
441 | int i; | - | ||||||||||||
442 | X509 *x509; | - | ||||||||||||
443 | if ((p7 = PKCS7_new()) == NULL) {
| 0-4 | ||||||||||||
444 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
445 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
446 | } | - | ||||||||||||
447 | - | |||||||||||||
448 | if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
| 0-4 | ||||||||||||
449 | goto err; never executed: goto err; | 0 | ||||||||||||
450 | if (!PKCS7_set_cipher(p7, cipher)) {
| 0-4 | ||||||||||||
451 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_SETTING_CIPHER); | - | ||||||||||||
452 | goto err; never executed: goto err; | 0 | ||||||||||||
453 | } | - | ||||||||||||
454 | - | |||||||||||||
455 | for (i = 0; i < sk_X509_num(certs); i++) {
| 4-12 | ||||||||||||
456 | x509 = sk_X509_value(certs, i); | - | ||||||||||||
457 | if (!PKCS7_add_recipient(p7, x509)) {
| 0-12 | ||||||||||||
458 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_ADDING_RECIPIENT); | - | ||||||||||||
459 | goto err; never executed: goto err; | 0 | ||||||||||||
460 | } | - | ||||||||||||
461 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||
462 | - | |||||||||||||
463 | if (flags & PKCS7_STREAM)
| 0-4 | ||||||||||||
464 | return p7; executed 4 times by 1 test: return p7; Executed by:
| 4 | ||||||||||||
465 | - | |||||||||||||
466 | if (PKCS7_final(p7, in, flags))
| 0 | ||||||||||||
467 | return p7; never executed: return p7; | 0 | ||||||||||||
468 | - | |||||||||||||
469 | err: code before this statement never executed: err: | 0 | ||||||||||||
470 | - | |||||||||||||
471 | BIO_free_all(p7bio); | - | ||||||||||||
472 | PKCS7_free(p7); | - | ||||||||||||
473 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
474 | - | |||||||||||||
475 | } | - | ||||||||||||
476 | - | |||||||||||||
477 | int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags) | - | ||||||||||||
478 | { | - | ||||||||||||
479 | BIO *tmpmem; | - | ||||||||||||
480 | int ret = 0, i; | - | ||||||||||||
481 | char *buf = NULL; | - | ||||||||||||
482 | - | |||||||||||||
483 | if (!p7) {
| 0-4 | ||||||||||||
484 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_INVALID_NULL_POINTER); | - | ||||||||||||
485 | return 0; never executed: return 0; | 0 | ||||||||||||
486 | } | - | ||||||||||||
487 | - | |||||||||||||
488 | if (!PKCS7_type_is_enveloped(p7)) {
| 0-4 | ||||||||||||
489 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_WRONG_CONTENT_TYPE); | - | ||||||||||||
490 | return 0; never executed: return 0; | 0 | ||||||||||||
491 | } | - | ||||||||||||
492 | - | |||||||||||||
493 | if (cert && !X509_check_private_key(cert, pkey)) {
| 0-3 | ||||||||||||
494 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, | - | ||||||||||||
495 | PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | - | ||||||||||||
496 | return 0; never executed: return 0; | 0 | ||||||||||||
497 | } | - | ||||||||||||
498 | - | |||||||||||||
499 | if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) {
| 0-4 | ||||||||||||
500 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR); | - | ||||||||||||
501 | return 0; never executed: return 0; | 0 | ||||||||||||
502 | } | - | ||||||||||||
503 | - | |||||||||||||
504 | if (flags & PKCS7_TEXT) {
| 0-4 | ||||||||||||
505 | BIO *tmpbuf, *bread; | - | ||||||||||||
506 | /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */ | - | ||||||||||||
507 | if ((tmpbuf = BIO_new(BIO_f_buffer())) == NULL) {
| 0 | ||||||||||||
508 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
509 | BIO_free_all(tmpmem); | - | ||||||||||||
510 | return 0; never executed: return 0; | 0 | ||||||||||||
511 | } | - | ||||||||||||
512 | if ((bread = BIO_push(tmpbuf, tmpmem)) == NULL) {
| 0 | ||||||||||||
513 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
514 | BIO_free_all(tmpbuf); | - | ||||||||||||
515 | BIO_free_all(tmpmem); | - | ||||||||||||
516 | return 0; never executed: return 0; | 0 | ||||||||||||
517 | } | - | ||||||||||||
518 | ret = SMIME_text(bread, data); | - | ||||||||||||
519 | if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
| 0 | ||||||||||||
520 | if (!BIO_get_cipher_status(tmpmem))
| 0 | ||||||||||||
521 | ret = 0; never executed: ret = 0; | 0 | ||||||||||||
522 | } never executed: end of block | 0 | ||||||||||||
523 | BIO_free_all(bread); | - | ||||||||||||
524 | return ret; never executed: return ret; | 0 | ||||||||||||
525 | } | - | ||||||||||||
526 | if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
| 0-4 | ||||||||||||
527 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
528 | goto err; never executed: goto err; | 0 | ||||||||||||
529 | } | - | ||||||||||||
530 | for (;;) { | - | ||||||||||||
531 | i = BIO_read(tmpmem, buf, BUFFERSIZE); | - | ||||||||||||
532 | if (i <= 0) {
| 4 | ||||||||||||
533 | ret = 1; | - | ||||||||||||
534 | if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
| 0-4 | ||||||||||||
535 | if (!BIO_get_cipher_status(tmpmem))
| 0-4 | ||||||||||||
536 | ret = 0; never executed: ret = 0; | 0 | ||||||||||||
537 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
538 | - | |||||||||||||
539 | break; executed 4 times by 1 test: break; Executed by:
| 4 | ||||||||||||
540 | } | - | ||||||||||||
541 | if (BIO_write(data, buf, i) != i) {
| 0-4 | ||||||||||||
542 | break; never executed: break; | 0 | ||||||||||||
543 | } | - | ||||||||||||
544 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
545 | err: code before this statement executed 4 times by 1 test: err: Executed by:
| 4 | ||||||||||||
546 | OPENSSL_free(buf); | - | ||||||||||||
547 | BIO_free_all(tmpmem); | - | ||||||||||||
548 | return ret; executed 4 times by 1 test: return ret; Executed by:
| 4 | ||||||||||||
549 | } | - | ||||||||||||
Source code | Switch to Preprocessed file |