aboutsummaryrefslogtreecommitdiffstats
path: root/src/cms/cms_enc.cpp
blob: cd739ef08755aed3246e756840c13a787bc60a14 (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
85
86
87
/*
* CMS Encoding Base
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#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[], size_t length)
   {
   if(!data.empty())
      throw Invalid_State("Cannot call CMS_Encoder::set_data here");

   data.resize(length);
   copy_mem(&data[0], 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();
   }

}