Beginnings of EVP cipher overhaul. This should eventually
[openssl.git] / crypto / pkcs7 / dec.c
index e02a8059c1517d2182faa775a7ea07339c3c3cc6..6752ec568a91ecc2a435685d84e562635271a984 100644 (file)
  * [including the GNU Public Licence.]
  */
 #include <stdio.h>
-#include "asn1.h"
-#include "bio.h"
-#include "x509.h"
-#include "pem.h"
+#include <stdlib.h>
+#include <string.h>
+#include <openssl/bio.h>
+#include <openssl/x509.h>
+#include <openssl/pem.h>
+#include <openssl/err.h>
+#include <openssl/asn1.h>
 
 int verify_callback(int ok, X509_STORE_CTX *ctx);
 
 BIO *bio_err=NULL;
 
-main(argc,argv)
+int main(argc,argv)
 int argc;
 char *argv[];
        {
+       char *keyfile=NULL;
        BIO *in;
-       X509 *x509,*x;
        EVP_PKEY *pkey;
+       X509 *x509;
        PKCS7 *p7;
-       PKCS7_SIGNED *s;
        PKCS7_SIGNER_INFO *si;
-       PKCS7_ISSUER_AND_SERIAL *ias;
        X509_STORE_CTX cert_ctx;
        X509_STORE *cert_store=NULL;
-       X509_LOOKUP *lookup=NULL;
        BIO *data,*detached=NULL,*p7bio=NULL;
        char buf[1024*4];
-       unsigned char *p,*pp;
-       int i,j,printit=0;
-       STACK *sk;
+       unsigned char *pp;
+       int i,printit=0;
+       STACK_OF(PKCS7_SIGNER_INFO) *sk;
 
-       SSLeay_add_all_algorithms();
+       OpenSSL_add_all_algorithms();
        bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
-       EVP_add_digest(EVP_sha1());
-       EVP_add_cipher(EVP_des_ede3_cbc());
-
-        if ((in=BIO_new_file("server.pem","r")) == NULL) goto err;
-        if ((x509=PEM_read_bio_X509(in,NULL,NULL)) == NULL) goto err;
-        BIO_reset(in);
-        if ((pkey=PEM_read_bio_PrivateKey(in,NULL,NULL)) == NULL) goto err;
-        BIO_free(in);
 
        data=BIO_new(BIO_s_file());
-again:
        pp=NULL;
        while (argc > 1)
                {
@@ -107,28 +99,41 @@ again:
                        {
                        printit=1;
                        }
-               else if ((strcmp(argv[0],"-d") == 0) && (argc >= 2))
+               else if ((strcmp(argv[0],"-k") == 0) && (argc >= 2)) {
+                       keyfile = argv[1];
+                       argc-=1;
+                       argv+=1;
+               } else if ((strcmp(argv[0],"-d") == 0) && (argc >= 2))
                        {
                        detached=BIO_new(BIO_s_file());
                        if (!BIO_read_filename(detached,argv[1]))
                                goto err;
-                       argc--;
-                       argv++;
-                       }
-               else
-                       {
-                       pp=argv[0];
-                       if (!BIO_read_filename(data,argv[0]))
-                               goto err;
+                       argc-=1;
+                       argv+=1;
                        }
+               else break;
                }
 
+        if (!BIO_read_filename(data,argv[0])) goto err; 
+
+       if(!keyfile) {
+               fprintf(stderr, "No private key file specified\n");
+               goto err;
+       }
+
+        if ((in=BIO_new_file(keyfile,"r")) == NULL) goto err;
+        if ((x509=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL) goto err;
+        BIO_reset(in);
+        if ((pkey=PEM_read_bio_PrivateKey(in,NULL,NULL,NULL)) == NULL)
+               goto err;
+        BIO_free(in);
+
        if (pp == NULL)
                BIO_set_fp(data,stdin,BIO_NOCLOSE);
 
 
        /* Load the PKCS7 object from a file */
-       if ((p7=PEM_read_bio_PKCS7(data,NULL,NULL)) == NULL) goto err;
+       if ((p7=PEM_read_bio_PKCS7(data,NULL,NULL,NULL)) == NULL) goto err;
 
 
 
@@ -144,8 +149,8 @@ again:
 
        /* We need to process the data */
        /* We cannot support detached encryption */
-       p7bio=PKCS7_dataDecode(p7,pkey,detached,cert_store);
-       
+       p7bio=PKCS7_dataDecode(p7,pkey,detached,x509);
+
        if (p7bio == NULL)
                {
                printf("problems decoding\n");
@@ -158,23 +163,23 @@ again:
                i=BIO_read(p7bio,buf,sizeof(buf));
                /* print it? */
                if (i <= 0) break;
-               write(fileno(stdout),buf,i);
+               fwrite(buf,1, i, stdout);
                }
 
        /* We can now verify signatures */
        sk=PKCS7_get_signer_info(p7);
        if (sk == NULL)
                {
-               printf("there are no signatures on this data\n");
+               fprintf(stderr, "there are no signatures on this data\n");
                }
        else
                {
                /* Ok, first we need to, for each subject entry,
                 * see if we can verify */
                ERR_clear_error();
-               for (i=0; i<sk_num(sk); i++)
+               for (i=0; i<sk_PKCS7_SIGNER_INFO_num(sk); i++)
                        {
-                       si=(PKCS7_SIGNER_INFO *)sk_value(sk,i);
+                       si=sk_PKCS7_SIGNER_INFO_value(sk,i);
                        i=PKCS7_dataVerify(cert_store,&cert_ctx,p7bio,p7,si);
                        if (i <= 0)
                                goto err;