This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / crypto / pkcs7 / sign.c
1 #include <stdio.h>
2 #include "bio.h"
3 #include "x509.h"
4 #include "pem.h"
5
6 main(argc,argv)
7 int argc;
8 char *argv[];
9         {
10         X509 *x509;
11         EVP_PKEY *pkey;
12         PKCS7 *p7;
13         PKCS7 *p7_data;
14         PKCS7_SIGNER_INFO *si;
15         BIO *in;
16         BIO *data,*p7bio;
17         char buf[1024*4];
18         int i,j;
19         int nodetach=0;
20
21         EVP_add_digest(EVP_md2());
22         EVP_add_digest(EVP_md5());
23         EVP_add_digest(EVP_sha1());
24         EVP_add_digest(EVP_mdc2());
25
26         data=BIO_new(BIO_s_file());
27 again:
28         if (argc > 1)
29                 {
30                 if (strcmp(argv[1],"-nd") == 0)
31                         {
32                         nodetach=1;
33                         argv++; argc--;
34                         goto again;
35                         }
36                 if (!BIO_read_filename(data,argv[1]))
37                         goto err;
38                 }
39         else
40                 BIO_set_fp(data,stdin,BIO_NOCLOSE);
41
42         if ((in=BIO_new_file("server.pem","r")) == NULL) goto err;
43         if ((x509=PEM_read_bio_X509(in,NULL,NULL)) == NULL) goto err;
44         BIO_reset(in);
45         if ((pkey=PEM_read_bio_PrivateKey(in,NULL,NULL)) == NULL) goto err;
46         BIO_free(in);
47
48         p7=PKCS7_new();
49         PKCS7_set_type(p7,NID_pkcs7_signed);
50          
51         if (PKCS7_add_signature(p7,x509,pkey,EVP_sha1()) == NULL) goto err;
52
53         /* we may want to add more */
54         PKCS7_add_certificate(p7,x509);
55
56         /* Set the content of the signed to 'data' */
57         PKCS7_content_new(p7,NID_pkcs7_data);
58
59         if (!nodetach)
60                 PKCS7_set_detached(p7,1);
61
62         if ((p7bio=PKCS7_dataInit(p7,NULL)) == NULL) goto err;
63
64         for (;;)
65                 {
66                 i=BIO_read(data,buf,sizeof(buf));
67                 if (i <= 0) break;
68                 BIO_write(p7bio,buf,i);
69                 }
70
71         if (!PKCS7_dataSign(p7,p7bio)) goto err;
72         BIO_free(p7bio);
73
74         PEM_write_PKCS7(stdout,p7);
75         PKCS7_free(p7);
76
77         exit(0);
78 err:
79         ERR_load_crypto_strings();
80         ERR_print_errors_fp(stderr);
81         exit(1);
82         }
83