Botan: src/cms/cms_enc.cpp Source File

Botan 1.10.17

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 */
39 SecureVector<byte> CMS_Encoder::get_contents()
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 */
67 std::string CMS_Encoder::PEM_contents()
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 {
78 return DER_Encoder().start_cons(SEQUENCE).
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 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::DER_Encoder::get_contents
SecureVector< byte > get_contents()
Definition: der_enc.cpp:122
Botan::MemoryRegion::clear
void clear()
Definition: secmem.h:158
Botan::Invalid_State
Definition: exceptn.h:25
Botan::CMS_Encoder::PEM_contents
std::string PEM_contents()
Definition: cms_enc.cpp:67
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::MemoryRegion::empty
bool empty() const
Definition: secmem.h:35
Botan::OCTET_STRING
Definition: asn1_int.h:31
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:31
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
Botan
Definition: algo_base.h:14
Botan::PEM_Code::encode
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:19
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
Botan::SecureVector< byte >
Botan::CMS_Encoder::set_data
void set_data(const std::string &)
Definition: cms_enc.cpp:31
Botan::CMS_Encoder::get_contents
SecureVector< byte > get_contents()
Definition: cms_enc.cpp:39
oid
OID oid
Definition: x509_ext.cpp:446