aboutsummaryrefslogtreecommitdiffstats
path: root/misc/cms/cms_enc.cpp
blob: d29dae54cfd682f018d31cdd11b4d8efa91c59a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*************************************************
* CMS Encoding Base Source File                  *
* (C) 1999-2007 The Botan Project                *
*************************************************/

#include <botan/cms_enc.h>
#include <botan/der_enc.h>
#include <botan/oids.h>
#include <botan/pem.h>

namespace Botan {

/*************************************************
* Setup the intitial layer of CMS data           *
*************************************************/
void CMS_Encoder::set_data(const byte buf[], u32bit length)
   {
   if(data.has_items())
      throw Invalid_State("Cannot call CMS_Encoder::set_data here");

   data.set(buf, length);
   type = "CMS.DataContent";
   }

/*************************************************
* Setup the intitial layer of CMS data           *
*************************************************/
void CMS_Encoder::set_data(const std::string& str)
   {
   set_data((const byte*)str.c_str(), str.length());
   }

/*************************************************
* Finalize and return the CMS encoded data       *
*************************************************/
SecureVector<byte> CMS_Encoder::get_contents()
   {
   DER_Encoder encoder;

   encoder.start_cons(SEQUENCE).
      encode(OIDS::lookup(type)).
      start_explicit(0).
         raw_bytes(data).
      end_explicit().
   end_cons();

   data.clear();

   return encoder.get_contents();
   }

/*************************************************
* Add a new layer of encapsulation               *
*************************************************/
void CMS_Encoder::add_layer(const std::string& oid, DER_Encoder& new_layer)
   {
   data = new_layer.get_contents();
   type = oid;
   }

/*************************************************
* Return the PEM-encoded data                    *
*************************************************/
std::string CMS_Encoder::PEM_contents()
   {
   return PEM_Code::encode(get_contents(), "PKCS7");
   }

/*************************************************
* Make an EncapsulatedContentInfo                *
*************************************************/
SecureVector<byte> CMS_Encoder::make_econtent(const SecureVector<byte>& data,
                                              const std::string& type)
   {
   return DER_Encoder().start_cons(SEQUENCE).
      encode(OIDS::lookup(type)).
      start_explicit(0).
         encode(data, OCTET_STRING).
      end_explicit().
   end_cons().
   get_contents();
   }

}