Botan  1.10.9
cms_enc.cpp
Go to the documentation of this file.
1 /*
2 * CMS Encoding Base
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/cms_enc.h>
9 #include <botan/der_enc.h>
10 #include <botan/oids.h>
11 #include <botan/pem.h>
12 
13 namespace Botan {
14 
15 /*
16 * Setup the intitial layer of CMS data
17 */
18 void CMS_Encoder::set_data(const byte buf[], size_t length)
19  {
20  if(!data.empty())
21  throw Invalid_State("Cannot call CMS_Encoder::set_data here");
22 
23  data.resize(length);
24  copy_mem(&data[0], buf, length);
25  type = "CMS.DataContent";
26  }
27 
28 /*
29 * Setup the intitial layer of CMS data
30 */
31 void CMS_Encoder::set_data(const std::string& str)
32  {
33  set_data(reinterpret_cast<const byte*>(str.c_str()), str.length());
34  }
35 
36 /*
37 * Finalize and return the CMS encoded data
38 */
40  {
41  DER_Encoder encoder;
42 
43  encoder.start_cons(SEQUENCE).
44  encode(OIDS::lookup(type)).
45  start_explicit(0).
46  raw_bytes(data).
47  end_explicit().
48  end_cons();
49 
50  data.clear();
51 
52  return encoder.get_contents();
53  }
54 
55 /*
56 * Add a new layer of encapsulation
57 */
58 void CMS_Encoder::add_layer(const std::string& oid, DER_Encoder& new_layer)
59  {
60  data = new_layer.get_contents();
61  type = oid;
62  }
63 
64 /*
65 * Return the PEM-encoded data
66 */
68  {
69  return PEM_Code::encode(get_contents(), "PKCS7");
70  }
71 
72 /*
73 * Make an EncapsulatedContentInfo
74 */
75 SecureVector<byte> CMS_Encoder::make_econtent(const SecureVector<byte>& data,
76  const std::string& type)
77  {
79  encode(OIDS::lookup(type)).
80  start_explicit(0).
81  encode(data, OCTET_STRING).
82  end_explicit().
83  end_cons().
84  get_contents();
85  }
86 
87 }
void resize(size_t n)
Definition: secmem.h:211
SecureVector< byte > get_contents()
Definition: der_enc.cpp:122
std::string PEM_contents()
Definition: cms_enc.cpp:67
unsigned char byte
Definition: types.h:22
std::string lookup(const OID &oid)
Definition: oids.cpp:31
bool empty() const
Definition: secmem.h:35
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:19
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
void set_data(const std::string &)
Definition: cms_enc.cpp:31
SecureVector< byte > get_contents()
Definition: cms_enc.cpp:39
OID oid
Definition: x509_ext.cpp:446