summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/pkcs7/enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/pkcs7/enc.c')
-rw-r--r--src/lib/libcrypto/pkcs7/enc.c79
1 files changed, 50 insertions, 29 deletions
diff --git a/src/lib/libcrypto/pkcs7/enc.c b/src/lib/libcrypto/pkcs7/enc.c
index 625a7c2285..43bfd10a23 100644
--- a/src/lib/libcrypto/pkcs7/enc.c
+++ b/src/lib/libcrypto/pkcs7/enc.c
@@ -56,61 +56,82 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58#include <stdio.h> 58#include <stdio.h>
59#include "bio.h" 59#include <openssl/bio.h>
60#include "x509.h" 60#include <openssl/x509.h>
61#include "pem.h" 61#include <openssl/pem.h>
62#include <openssl/err.h>
62 63
63main(argc,argv) 64int main(argc,argv)
64int argc; 65int argc;
65char *argv[]; 66char *argv[];
66 { 67 {
67 X509 *x509; 68 X509 *x509;
68 EVP_PKEY *pkey;
69 PKCS7 *p7; 69 PKCS7 *p7;
70 PKCS7 *p7_data;
71 PKCS7_SIGNER_INFO *si;
72 BIO *in; 70 BIO *in;
73 BIO *data,*p7bio; 71 BIO *data,*p7bio;
74 char buf[1024*4]; 72 char buf[1024*4];
75 int i,j; 73 int i;
76 int nodetach=0; 74 int nodetach=1;
75 char *keyfile = NULL;
76 const EVP_CIPHER *cipher=NULL;
77 STACK_OF(X509) *recips=NULL;
77 78
78 EVP_add_digest(EVP_sha1()); 79 SSLeay_add_all_algorithms();
79 EVP_add_cipher(EVP_des_cbc());
80 80
81 data=BIO_new(BIO_s_file()); 81 data=BIO_new(BIO_s_file());
82again: 82 while(argc > 1)
83 if (argc > 1)
84 { 83 {
85 if (strcmp(argv[1],"-nd") == 0) 84 if (strcmp(argv[1],"-nd") == 0)
86 { 85 {
87 nodetach=1; 86 nodetach=1;
88 argv++; argc--; 87 argv++; argc--;
89 goto again;
90 } 88 }
91 if (!BIO_read_filename(data,argv[1])) 89 else if ((strcmp(argv[1],"-c") == 0) && (argc >= 2)) {
92 goto err; 90 if(!(cipher = EVP_get_cipherbyname(argv[2]))) {
93 } 91 fprintf(stderr, "Unknown cipher %s\n", argv[2]);
94 else 92 goto err;
95 BIO_set_fp(data,stdin,BIO_NOCLOSE); 93 }
94 argc-=2;
95 argv+=2;
96 } else if ((strcmp(argv[1],"-k") == 0) && (argc >= 2)) {
97 keyfile = argv[2];
98 argc-=2;
99 argv+=2;
100 if (!(in=BIO_new_file(keyfile,"r"))) goto err;
101 if (!(x509=PEM_read_bio_X509(in,NULL,NULL))) goto err;
102 if(!recips) recips = sk_X509_new_null();
103 sk_X509_push(recips, x509);
104 BIO_free(in);
105 } else break;
106 }
96 107
97 if ((in=BIO_new_file("server.pem","r")) == NULL) goto err; 108 if(!recips) {
98 if ((x509=PEM_read_bio_X509(in,NULL,NULL)) == NULL) goto err; 109 fprintf(stderr, "No recipients\n");
110 goto err;
111 }
112
113 if (!BIO_read_filename(data,argv[1])) goto err;
114
115 p7=PKCS7_new();
116#if 0
99 BIO_reset(in); 117 BIO_reset(in);
100 if ((pkey=PEM_read_bio_PrivateKey(in,NULL,NULL)) == NULL) goto err; 118 if ((pkey=PEM_read_bio_PrivateKey(in,NULL,NULL)) == NULL) goto err;
101 BIO_free(in); 119 BIO_free(in);
102
103 p7=PKCS7_new();
104 PKCS7_set_type(p7,NID_pkcs7_signedAndEnveloped); 120 PKCS7_set_type(p7,NID_pkcs7_signedAndEnveloped);
105 121
106 if (PKCS7_add_signature(p7,x509,pkey,EVP_sha1()) == NULL) goto err; 122 if (PKCS7_add_signature(p7,x509,pkey,EVP_sha1()) == NULL) goto err;
107
108 if (!PKCS7_set_cipher(p7,EVP_des_cbc())) goto err;
109 if (PKCS7_add_recipient(p7,x509) == NULL) goto err;
110
111 /* we may want to add more */ 123 /* we may want to add more */
112 PKCS7_add_certificate(p7,x509); 124 PKCS7_add_certificate(p7,x509);
113 125#else
126 PKCS7_set_type(p7,NID_pkcs7_enveloped);
127#endif
128 if(!cipher) cipher = EVP_des_ede3_cbc();
129
130 if (!PKCS7_set_cipher(p7,cipher)) goto err;
131 for(i = 0; i < sk_X509_num(recips); i++) {
132 if (!PKCS7_add_recipient(p7,sk_X509_value(recips, i))) goto err;
133 }
134 sk_X509_pop_free(recips, X509_free);
114 135
115 /* Set the content of the signed to 'data' */ 136 /* Set the content of the signed to 'data' */
116 /* PKCS7_content_new(p7,NID_pkcs7_data); not used in envelope */ 137 /* PKCS7_content_new(p7,NID_pkcs7_data); not used in envelope */
@@ -129,7 +150,7 @@ again:
129 } 150 }
130 BIO_flush(p7bio); 151 BIO_flush(p7bio);
131 152
132 if (!PKCS7_dataSign(p7,p7bio)) goto err; 153 if (!PKCS7_dataFinal(p7,p7bio)) goto err;
133 BIO_free(p7bio); 154 BIO_free(p7bio);
134 155
135 PEM_write_PKCS7(stdout,p7); 156 PEM_write_PKCS7(stdout,p7);