~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to misc/cms/tests/cms_dec.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <botan/cms_dec.h>
2
 
using namespace Botan;
3
 
 
4
 
#include <iostream>
5
 
 
6
 
int main(int argc, char* argv[])
7
 
   {
8
 
   if(argc != 2)
9
 
      {
10
 
      printf("Usage: %s <filename>\n", argv[0]);
11
 
      return 1;
12
 
      }
13
 
 
14
 
   LibraryInitializer init;
15
 
 
16
 
   try {
17
 
      X509_Certificate mycert("mycert.pem");
18
 
      PKCS8_PrivateKey* mykey = PKCS8::load_key("mykey.pem", "cut");
19
 
 
20
 
      X509_Certificate yourcert("yourcert.pem");
21
 
      X509_Certificate cacert("cacert.pem");
22
 
      X509_Certificate int_ca("int_ca.pem");
23
 
 
24
 
      X509_Store store;
25
 
      store.add_cert(mycert);
26
 
      store.add_cert(yourcert);
27
 
      store.add_cert(cacert, true);
28
 
      store.add_cert(int_ca);
29
 
 
30
 
      DataSource_Stream message(argv[1]);
31
 
 
32
 
      User_Interface ui;
33
 
 
34
 
      CMS_Decoder decoder(message, store, ui, mykey);
35
 
 
36
 
      while(decoder.layer_type() != CMS_Decoder::DATA)
37
 
         {
38
 
         CMS_Decoder::Status status = decoder.layer_status();
39
 
         CMS_Decoder::Content_Type content = decoder.layer_type();
40
 
 
41
 
         if(status == CMS_Decoder::FAILURE)
42
 
            {
43
 
            std::cout << "Failure reading CMS data" << std::endl;
44
 
            break;
45
 
            }
46
 
 
47
 
         if(content == CMS_Decoder::DIGESTED)
48
 
            {
49
 
            std::cout << "Digested data, hash = " << decoder.layer_info()
50
 
                      << std::endl;
51
 
            std::cout << "Hash is "
52
 
                      << ((status == CMS_Decoder::GOOD) ? "good" : "bad")
53
 
                      << std::endl;
54
 
            }
55
 
 
56
 
         if(content == CMS_Decoder::SIGNED)
57
 
            {
58
 
            // how to handle multiple signers? they can all exist within a
59
 
            // single level...
60
 
 
61
 
            std::cout << "Signed by " << decoder.layer_info() << std::endl;
62
 
            //std::cout << "Sign time: " << decoder.xxx() << std::endl;
63
 
            std::cout << "Signature is ";
64
 
            if(status == CMS_Decoder::GOOD)
65
 
               std::cout << "valid";
66
 
            else if(status == CMS_Decoder::BAD)
67
 
               std::cout << "bad";
68
 
            else if(status == CMS_Decoder::NO_KEY)
69
 
               std::cout << "(cannot check, no known cert)";
70
 
            std::cout << std::endl;
71
 
            }
72
 
         if(content == CMS_Decoder::ENVELOPED ||
73
 
            content == CMS_Decoder::COMPRESSED ||
74
 
            content == CMS_Decoder::AUTHENTICATED)
75
 
            {
76
 
            if(content == CMS_Decoder::ENVELOPED)
77
 
               std::cout << "Enveloped";
78
 
            if(content == CMS_Decoder::COMPRESSED)
79
 
               std::cout << "Compressed";
80
 
            if(content == CMS_Decoder::AUTHENTICATED)
81
 
               std::cout << "MACed";
82
 
 
83
 
            std::cout << ", algo = " << decoder.layer_info() << std::endl;
84
 
 
85
 
            if(content == CMS_Decoder::AUTHENTICATED)
86
 
               {
87
 
               std::cout << "MAC status is ";
88
 
               if(status == CMS_Decoder::GOOD)
89
 
                  std::cout << "valid";
90
 
               else if(status == CMS_Decoder::BAD)
91
 
                  std::cout << "bad";
92
 
               else if(status == CMS_Decoder::NO_KEY)
93
 
                  std::cout << "(cannot check, no key)";
94
 
               std::cout << std::endl;
95
 
               }
96
 
            }
97
 
         decoder.next_layer();
98
 
         }
99
 
 
100
 
      if(decoder.layer_type() == CMS_Decoder::DATA)
101
 
         std::cout << "Message is \"" << decoder.get_data()
102
 
                   << '"' << std::endl;
103
 
      else
104
 
         std::cout << "No data anywhere?" << std::endl;
105
 
   }
106
 
   catch(std::exception& e)
107
 
      {
108
 
      std::cerr << e.what() << std::endl;
109
 
      }
110
 
   return 0;
111
 
   }