blob: 58b3eadf646da826ae65e5801342b04f9f611cbe (
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 Source File *
* (C) 1999-2003 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_sequence();
DER::encode(encoder, OIDS::lookup(type));
encoder.start_explicit(ASN1_Tag(0));
encoder.add_raw_octets(data);
encoder.end_explicit(ASN1_Tag(0));
encoder.end_sequence();
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)
{
DER_Encoder encoder;
encoder.start_sequence();
DER::encode(encoder, OIDS::lookup(type));
encoder.start_explicit(ASN1_Tag(0));
DER::encode(encoder, data, OCTET_STRING);
encoder.end_explicit(ASN1_Tag(0));
encoder.end_sequence();
return encoder.get_contents();
}
}
|