diff options
Diffstat (limited to 'src/lib/libcrypto/pkcs7/pk7_smime.c')
-rw-r--r-- | src/lib/libcrypto/pkcs7/pk7_smime.c | 109 |
1 files changed, 39 insertions, 70 deletions
diff --git a/src/lib/libcrypto/pkcs7/pk7_smime.c b/src/lib/libcrypto/pkcs7/pk7_smime.c index 5c6b0fe24b..99a0d63f38 100644 --- a/src/lib/libcrypto/pkcs7/pk7_smime.c +++ b/src/lib/libcrypto/pkcs7/pk7_smime.c | |||
@@ -1,9 +1,9 @@ | |||
1 | /* pk7_smime.c */ | 1 | /* pk7_smime.c */ |
2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | 2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL |
3 | * project. | 3 | * project 1999. |
4 | */ | 4 | */ |
5 | /* ==================================================================== | 5 | /* ==================================================================== |
6 | * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved. | 6 | * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved. |
7 | * | 7 | * |
8 | * Redistribution and use in source and binary forms, with or without | 8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions | 9 | * modification, are permitted provided that the following conditions |
@@ -66,10 +66,10 @@ | |||
66 | PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, | 66 | PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, |
67 | BIO *data, int flags) | 67 | BIO *data, int flags) |
68 | { | 68 | { |
69 | PKCS7 *p7 = NULL; | 69 | PKCS7 *p7; |
70 | PKCS7_SIGNER_INFO *si; | 70 | PKCS7_SIGNER_INFO *si; |
71 | BIO *p7bio = NULL; | 71 | BIO *p7bio; |
72 | STACK_OF(X509_ALGOR) *smcap = NULL; | 72 | STACK_OF(X509_ALGOR) *smcap; |
73 | int i; | 73 | int i; |
74 | 74 | ||
75 | if(!X509_check_private_key(signcert, pkey)) { | 75 | if(!X509_check_private_key(signcert, pkey)) { |
@@ -82,87 +82,66 @@ PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, | |||
82 | return NULL; | 82 | return NULL; |
83 | } | 83 | } |
84 | 84 | ||
85 | if (!PKCS7_set_type(p7, NID_pkcs7_signed)) | 85 | PKCS7_set_type(p7, NID_pkcs7_signed); |
86 | goto err; | ||
87 | 86 | ||
88 | if (!PKCS7_content_new(p7, NID_pkcs7_data)) | 87 | PKCS7_content_new(p7, NID_pkcs7_data); |
89 | goto err; | ||
90 | 88 | ||
91 | if (!(si = PKCS7_add_signature(p7,signcert,pkey,EVP_sha1()))) { | 89 | if (!(si = PKCS7_add_signature(p7,signcert,pkey,EVP_sha1()))) { |
92 | PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR); | 90 | PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR); |
93 | goto err; | 91 | return NULL; |
94 | } | 92 | } |
95 | 93 | ||
96 | if(!(flags & PKCS7_NOCERTS)) { | 94 | if(!(flags & PKCS7_NOCERTS)) { |
97 | if (!PKCS7_add_certificate(p7, signcert)) | 95 | PKCS7_add_certificate(p7, signcert); |
98 | goto err; | ||
99 | if(certs) for(i = 0; i < sk_X509_num(certs); i++) | 96 | if(certs) for(i = 0; i < sk_X509_num(certs); i++) |
100 | if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i))) | 97 | PKCS7_add_certificate(p7, sk_X509_value(certs, i)); |
101 | goto err; | 98 | } |
99 | |||
100 | if(!(p7bio = PKCS7_dataInit(p7, NULL))) { | ||
101 | PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE); | ||
102 | return NULL; | ||
102 | } | 103 | } |
103 | 104 | ||
105 | |||
106 | SMIME_crlf_copy(data, p7bio, flags); | ||
107 | |||
104 | if(!(flags & PKCS7_NOATTR)) { | 108 | if(!(flags & PKCS7_NOATTR)) { |
105 | if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType, | 109 | PKCS7_add_signed_attribute(si, NID_pkcs9_contentType, |
106 | V_ASN1_OBJECT, OBJ_nid2obj(NID_pkcs7_data))) | 110 | V_ASN1_OBJECT, OBJ_nid2obj(NID_pkcs7_data)); |
107 | goto err; | ||
108 | /* Add SMIMECapabilities */ | 111 | /* Add SMIMECapabilities */ |
109 | if(!(flags & PKCS7_NOSMIMECAP)) | 112 | if(!(flags & PKCS7_NOSMIMECAP)) |
110 | { | 113 | { |
111 | if(!(smcap = sk_X509_ALGOR_new_null())) { | 114 | if(!(smcap = sk_X509_ALGOR_new_null())) { |
112 | PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE); | 115 | PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE); |
113 | goto err; | 116 | return NULL; |
114 | } | 117 | } |
115 | #ifndef OPENSSL_NO_DES | 118 | #ifndef OPENSSL_NO_DES |
116 | if (!PKCS7_simple_smimecap (smcap, NID_des_ede3_cbc, -1)) | 119 | PKCS7_simple_smimecap (smcap, NID_des_ede3_cbc, -1); |
117 | goto err; | ||
118 | #endif | 120 | #endif |
119 | #ifndef OPENSSL_NO_RC2 | 121 | #ifndef OPENSSL_NO_RC2 |
120 | if (!PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 128)) | 122 | PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 128); |
121 | goto err; | 123 | PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 64); |
122 | if (!PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 64)) | ||
123 | goto err; | ||
124 | #endif | 124 | #endif |
125 | #ifndef OPENSSL_NO_DES | 125 | #ifndef OPENSSL_NO_DES |
126 | if (!PKCS7_simple_smimecap (smcap, NID_des_cbc, -1)) | 126 | PKCS7_simple_smimecap (smcap, NID_des_cbc, -1); |
127 | goto err; | ||
128 | #endif | 127 | #endif |
129 | #ifndef OPENSSL_NO_RC2 | 128 | #ifndef OPENSSL_NO_RC2 |
130 | if (!PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 40)) | 129 | PKCS7_simple_smimecap (smcap, NID_rc2_cbc, 40); |
131 | goto err; | ||
132 | #endif | 130 | #endif |
133 | if (!PKCS7_add_attrib_smimecap (si, smcap)) | 131 | PKCS7_add_attrib_smimecap (si, smcap); |
134 | goto err; | ||
135 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); | 132 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); |
136 | smcap = NULL; | ||
137 | } | 133 | } |
138 | } | 134 | } |
139 | 135 | ||
140 | if(flags & PKCS7_DETACHED)PKCS7_set_detached(p7, 1); | 136 | if(flags & PKCS7_DETACHED)PKCS7_set_detached(p7, 1); |
141 | 137 | ||
142 | if (flags & PKCS7_STREAM) | 138 | if (!PKCS7_dataFinal(p7,p7bio)) { |
143 | return p7; | ||
144 | |||
145 | |||
146 | if (!(p7bio = PKCS7_dataInit(p7, NULL))) { | ||
147 | PKCS7err(PKCS7_F_PKCS7_SIGN,ERR_R_MALLOC_FAILURE); | ||
148 | goto err; | ||
149 | } | ||
150 | |||
151 | SMIME_crlf_copy(data, p7bio, flags); | ||
152 | |||
153 | |||
154 | if (!PKCS7_dataFinal(p7,p7bio)) { | ||
155 | PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_DATASIGN); | 139 | PKCS7err(PKCS7_F_PKCS7_SIGN,PKCS7_R_PKCS7_DATASIGN); |
156 | goto err; | 140 | return NULL; |
157 | } | 141 | } |
158 | 142 | ||
159 | BIO_free_all(p7bio); | 143 | BIO_free_all(p7bio); |
160 | return p7; | 144 | return p7; |
161 | err: | ||
162 | sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); | ||
163 | BIO_free_all(p7bio); | ||
164 | PKCS7_free(p7); | ||
165 | return NULL; | ||
166 | } | 145 | } |
167 | 146 | ||
168 | int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, | 147 | int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, |
@@ -236,8 +215,6 @@ int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, | |||
236 | sk_X509_free(signers); | 215 | sk_X509_free(signers); |
237 | return 0; | 216 | return 0; |
238 | } | 217 | } |
239 | if (!(flags & PKCS7_NOCRL)) | ||
240 | X509_STORE_CTX_set0_crls(&cert_ctx, p7->d.sign->crl); | ||
241 | i = X509_verify_cert(&cert_ctx); | 218 | i = X509_verify_cert(&cert_ctx); |
242 | if (i <= 0) j = X509_STORE_CTX_get_error(&cert_ctx); | 219 | if (i <= 0) j = X509_STORE_CTX_get_error(&cert_ctx); |
243 | X509_STORE_CTX_cleanup(&cert_ctx); | 220 | X509_STORE_CTX_cleanup(&cert_ctx); |
@@ -274,8 +251,7 @@ int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, | |||
274 | tmpin = indata; | 251 | tmpin = indata; |
275 | 252 | ||
276 | 253 | ||
277 | if (!(p7bio=PKCS7_dataInit(p7,tmpin))) | 254 | p7bio=PKCS7_dataInit(p7,tmpin); |
278 | goto err; | ||
279 | 255 | ||
280 | if(flags & PKCS7_TEXT) { | 256 | if(flags & PKCS7_TEXT) { |
281 | if(!(tmpout = BIO_new(BIO_s_mem()))) { | 257 | if(!(tmpout = BIO_new(BIO_s_mem()))) { |
@@ -354,7 +330,7 @@ STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags) | |||
354 | 330 | ||
355 | if(sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) { | 331 | if(sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) { |
356 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_NO_SIGNERS); | 332 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_NO_SIGNERS); |
357 | return NULL; | 333 | return 0; |
358 | } | 334 | } |
359 | 335 | ||
360 | if(!(signers = sk_X509_new_null())) { | 336 | if(!(signers = sk_X509_new_null())) { |
@@ -377,13 +353,10 @@ STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags) | |||
377 | if (!signer) { | 353 | if (!signer) { |
378 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND); | 354 | PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND); |
379 | sk_X509_free(signers); | 355 | sk_X509_free(signers); |
380 | return NULL; | 356 | return 0; |
381 | } | 357 | } |
382 | 358 | ||
383 | if (!sk_X509_push(signers, signer)) { | 359 | sk_X509_push(signers, signer); |
384 | sk_X509_free(signers); | ||
385 | return NULL; | ||
386 | } | ||
387 | } | 360 | } |
388 | return signers; | 361 | return signers; |
389 | } | 362 | } |
@@ -403,8 +376,7 @@ PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, | |||
403 | return NULL; | 376 | return NULL; |
404 | } | 377 | } |
405 | 378 | ||
406 | if (!PKCS7_set_type(p7, NID_pkcs7_enveloped)) | 379 | PKCS7_set_type(p7, NID_pkcs7_enveloped); |
407 | goto err; | ||
408 | if(!PKCS7_set_cipher(p7, cipher)) { | 380 | if(!PKCS7_set_cipher(p7, cipher)) { |
409 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_ERROR_SETTING_CIPHER); | 381 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_ERROR_SETTING_CIPHER); |
410 | goto err; | 382 | goto err; |
@@ -426,7 +398,7 @@ PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, | |||
426 | 398 | ||
427 | SMIME_crlf_copy(in, p7bio, flags); | 399 | SMIME_crlf_copy(in, p7bio, flags); |
428 | 400 | ||
429 | (void)BIO_flush(p7bio); | 401 | BIO_flush(p7bio); |
430 | 402 | ||
431 | if (!PKCS7_dataFinal(p7,p7bio)) { | 403 | if (!PKCS7_dataFinal(p7,p7bio)) { |
432 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_PKCS7_DATAFINAL_ERROR); | 404 | PKCS7err(PKCS7_F_PKCS7_ENCRYPT,PKCS7_R_PKCS7_DATAFINAL_ERROR); |
@@ -438,7 +410,7 @@ PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, | |||
438 | 410 | ||
439 | err: | 411 | err: |
440 | 412 | ||
441 | BIO_free_all(p7bio); | 413 | BIO_free(p7bio); |
442 | PKCS7_free(p7); | 414 | PKCS7_free(p7); |
443 | return NULL; | 415 | return NULL; |
444 | 416 | ||
@@ -460,7 +432,7 @@ int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags) | |||
460 | return 0; | 432 | return 0; |
461 | } | 433 | } |
462 | 434 | ||
463 | if(cert && !X509_check_private_key(cert, pkey)) { | 435 | if(!X509_check_private_key(cert, pkey)) { |
464 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, | 436 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, |
465 | PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); | 437 | PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); |
466 | return 0; | 438 | return 0; |
@@ -476,13 +448,10 @@ int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags) | |||
476 | /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */ | 448 | /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */ |
477 | if(!(tmpbuf = BIO_new(BIO_f_buffer()))) { | 449 | if(!(tmpbuf = BIO_new(BIO_f_buffer()))) { |
478 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | 450 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); |
479 | BIO_free_all(tmpmem); | ||
480 | return 0; | 451 | return 0; |
481 | } | 452 | } |
482 | if(!(bread = BIO_push(tmpbuf, tmpmem))) { | 453 | if(!(bread = BIO_push(tmpbuf, tmpmem))) { |
483 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); | 454 | PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE); |
484 | BIO_free_all(tmpbuf); | ||
485 | BIO_free_all(tmpmem); | ||
486 | return 0; | 455 | return 0; |
487 | } | 456 | } |
488 | ret = SMIME_text(bread, data); | 457 | ret = SMIME_text(bread, data); |