Botan 1.10.17
Botan::CMS_Encoder Class Reference

#include <cms_enc.h>

Public Member Functions

void authenticate (const std::string &, const std::string &="")
void authenticate (const SymmetricKey &, const std::string &="")
void authenticate (const X509_Certificate &, const std::string &="")
 CMS_Encoder (const byte buf[], size_t length)
 CMS_Encoder (const std::string &str)
void compress (const std::string &)
void digest (const std::string &="")
void encrypt (RandomNumberGenerator &, const X509_Certificate &, const std::string="")
void encrypt (RandomNumberGenerator &rng, const std::string &, const std::string &="")
void encrypt (RandomNumberGenerator &rng, const SymmetricKey &, const std::string &="")
SecureVector< byteget_contents ()
std::string PEM_contents ()
void set_data (const byte[], size_t)
void set_data (const std::string &)
void sign (const X509_Certificate &cert, const Private_Key &key, RandomNumberGenerator &rng, const std::vector< X509_Certificate > &cert_chain, const std::string &hash, const std::string &padding)

Static Public Member Functions

static bool can_compress_with (const std::string &)

Detailed Description

CMS Encoding Operation

Definition at line 21 of file cms_enc.h.

Constructor & Destructor Documentation

◆ CMS_Encoder() [1/2]

Botan::CMS_Encoder::CMS_Encoder ( const std::string & str)
inline

Definition at line 56 of file cms_enc.h.

56{ set_data(str); }
void set_data(const std::string &)
Definition cms_enc.cpp:31

References set_data().

◆ CMS_Encoder() [2/2]

Botan::CMS_Encoder::CMS_Encoder ( const byte buf[],
size_t length )
inline

Definition at line 57 of file cms_enc.h.

57{ set_data(buf, length); }

References set_data().

Member Function Documentation

◆ authenticate() [1/3]

void Botan::CMS_Encoder::authenticate ( const std::string & ,
const std::string & mac_algo = "" )

Definition at line 387 of file cms_ealg.cpp.

389 {
390 const std::string mac = choose_algo(mac_algo, "HMAC(SHA-1)");
391 throw Internal_Error("FIXME: unimplemented");
392 }
Internal_Error(const std::string &err)
Definition exceptn.h:47

References Botan::Internal_Error::Internal_Error().

◆ authenticate() [2/3]

void Botan::CMS_Encoder::authenticate ( const SymmetricKey & ,
const std::string & mac_algo = "" )

Definition at line 377 of file cms_ealg.cpp.

379 {
380 const std::string mac = choose_algo(mac_algo, "HMAC(SHA-1)");
381 throw Internal_Error("FIXME: unimplemented");
382 }

References Botan::Internal_Error::Internal_Error().

◆ authenticate() [3/3]

void Botan::CMS_Encoder::authenticate ( const X509_Certificate & ,
const std::string & mac_algo = "" )

Definition at line 367 of file cms_ealg.cpp.

369 {
370 const std::string mac = choose_algo(mac_algo, "HMAC(SHA-1)");
371 throw Internal_Error("FIXME: unimplemented");
372 }

References Botan::Internal_Error::Internal_Error().

◆ can_compress_with()

bool Botan::CMS_Encoder::can_compress_with ( const std::string & algo)
static

Definition at line 56 of file cms_comp.cpp.

57 {
58 if(algo == "")
59 throw Invalid_Algorithm_Name("Empty string to can_compress_with");
60
61#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
62 if(algo == "Zlib")
63 return true;
64#endif
65
66 return false;
67 }
Invalid_Algorithm_Name(const std::string &name)
Definition exceptn.h:121

References Botan::Invalid_Algorithm_Name::Invalid_Algorithm_Name().

Referenced by compress().

◆ compress()

void Botan::CMS_Encoder::compress ( const std::string & algo)

Definition at line 24 of file cms_comp.cpp.

25 {
27 throw Invalid_Argument("CMS_Encoder: Cannot compress with " + algo);
28
29 Filter* compressor = 0;
30
31#if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
32 if(algo == "Zlib") compressor = new Zlib_Compression;
33#endif
34
35 if(compressor == 0)
36 throw Internal_Error("CMS: Couldn't get ahold of a compressor");
37
38 Pipe pipe(compressor);
39 pipe.process_msg(data);
40 SecureVector<byte> compressed = pipe.read_all();
41
42 DER_Encoder encoder;
43 encoder.start_cons(SEQUENCE).
44 encode(static_cast<size_t>(0)).
45 encode(AlgorithmIdentifier("Compression." + algo,
46 MemoryVector<byte>())).
47 raw_bytes(make_econtent(compressed, type)).
48 end_cons();
49
50 add_layer("CMS.CompressedData", encoder);
51 }
static bool can_compress_with(const std::string &)
Definition cms_comp.cpp:56
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition pem.cpp:19
std::invalid_argument Invalid_Argument
Definition exceptn.h:20
@ SEQUENCE
Definition asn1_int.h:35

References Botan::AlgorithmIdentifier::AlgorithmIdentifier(), can_compress_with(), Botan::Internal_Error::Internal_Error(), Botan::Pipe::process_msg(), Botan::Pipe::read_all(), Botan::SEQUENCE, and Botan::DER_Encoder::start_cons().

◆ digest()

void Botan::CMS_Encoder::digest ( const std::string & user_hash = "")

Definition at line 344 of file cms_ealg.cpp.

345 {
346 const std::string hash = choose_algo(user_hash, "SHA-1");
347 if(!OIDS::have_oid(hash))
348 throw Encoding_Error("CMS: No OID assigned for " + hash);
349
350 const size_t VERSION = (type != "CMS.DataContent") ? 2 : 0;
351
352 DER_Encoder encoder;
353 encoder.start_cons(SEQUENCE)
354 .encode(VERSION)
355 .encode(AlgorithmIdentifier(OIDS::lookup(hash),
357 .raw_bytes(make_econtent(data, type))
358 .encode(hash_of(data, hash), OCTET_STRING)
359 .end_cons();
360
361 add_layer("CMS.DigestedData", encoder);
362 }
std::string lookup(const OID &oid)
Definition oids.cpp:31
bool have_oid(const std::string &name)
Definition oids.cpp:61
@ OCTET_STRING
Definition asn1_int.h:31
Encoding_Error(const std::string &name)
Definition exceptn.h:131

References Botan::AlgorithmIdentifier::AlgorithmIdentifier(), Botan::DER_Encoder::encode(), Botan::Encoding_Error::Encoding_Error(), Botan::DER_Encoder::end_cons(), Botan::OIDS::have_oid(), Botan::OIDS::lookup(), Botan::OCTET_STRING, Botan::DER_Encoder::raw_bytes(), Botan::SEQUENCE, Botan::DER_Encoder::start_cons(), and Botan::AlgorithmIdentifier::USE_NULL_PARAM.

◆ encrypt() [1/3]

void Botan::CMS_Encoder::encrypt ( RandomNumberGenerator & rng,
const X509_Certificate & to,
const std::string user_cipher = "" )

Definition at line 93 of file cms_ealg.cpp.

96 {
97 const std::string cipher = choose_algo(user_cipher, "TripleDES");
98
99 std::auto_ptr<Public_Key> key(to.subject_public_key());
100 const std::string algo = key->algo_name();
101
102 Key_Constraints constraints = to.constraints();
103
104 if(algo == "RSA")
105 {
106 if(constraints != NO_CONSTRAINTS && !(constraints & KEY_ENCIPHERMENT))
107 throw Invalid_Argument("CMS: Constraints not set for encryption");
108
109 encrypt_ktri(rng, to, key.get(), cipher);
110 }
111 else if(algo == "DH")
112 {
113 if(constraints != NO_CONSTRAINTS && !(constraints & KEY_AGREEMENT))
114 throw Invalid_Argument("CMS: Constraints not set for key agreement");
115
116 encrypt_kari(rng, to, key.get(), cipher);
117 }
118 else
119 throw Invalid_Argument("Unknown CMS PK encryption algorithm " + algo);
120 }
Key_Constraints
@ NO_CONSTRAINTS
@ KEY_AGREEMENT
@ KEY_ENCIPHERMENT

References Botan::X509_Certificate::constraints(), Botan::KEY_AGREEMENT, Botan::KEY_ENCIPHERMENT, Botan::NO_CONSTRAINTS, and Botan::X509_Certificate::subject_public_key().

◆ encrypt() [2/3]

void Botan::CMS_Encoder::encrypt ( RandomNumberGenerator & rng,
const std::string & ,
const std::string & user_cipher = "" )

Definition at line 225 of file cms_ealg.cpp.

228 {
229 const std::string cipher = choose_algo(user_cipher, "TripleDES");
230 throw Internal_Error("FIXME: unimplemented");
231 /*
232 SymmetricKey cek = setup_key(key);
233
234 DER_Encoder encoder;
235 encoder.start_cons(SEQUENCE);
236 encoder.encode(0);
237 encoder.raw_bytes(do_encrypt(rng, cek, cipher));
238 encoder.end_cons();
239
240 add_layer("CMS.EnvelopedData", encoder);
241 */
242 }

References Botan::Internal_Error::Internal_Error().

◆ encrypt() [3/3]

void Botan::CMS_Encoder::encrypt ( RandomNumberGenerator & rng,
const SymmetricKey & kek,
const std::string & user_cipher = "" )

Definition at line 192 of file cms_ealg.cpp.

195 {
196 throw Internal_Error("FIXME: untested");
197
198 const std::string cipher = choose_algo(user_cipher, "TripleDES");
199 SymmetricKey cek = setup_key(rng, cipher);
200
201 SecureVector<byte> kek_id; // FIXME: ?
202
203 DER_Encoder encoder;
204
205 encoder.start_cons(SEQUENCE)
206 .encode(static_cast<size_t>(2))
207 .start_explicit(ASN1_Tag(2))
208 .encode(static_cast<size_t>(4))
209 .start_cons(SEQUENCE)
210 .encode(kek_id, OCTET_STRING)
211 .end_cons()
212 .encode(AlgorithmIdentifier(OIDS::lookup("KeyWrap." + cipher),
214 .encode(wrap_key(rng, cipher, cek, kek), OCTET_STRING)
215 .end_cons()
216 .raw_bytes(do_encrypt(rng, cek, cipher))
217 .end_cons();
218
219 add_layer("CMS.EnvelopedData", encoder);
220 }
OctetString SymmetricKey
Definition symkey.h:147
ASN1_Tag
Definition asn1_int.h:19

References Botan::AlgorithmIdentifier::AlgorithmIdentifier(), Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::Internal_Error::Internal_Error(), Botan::OIDS::lookup(), Botan::OCTET_STRING, Botan::DER_Encoder::raw_bytes(), Botan::SEQUENCE, Botan::DER_Encoder::start_cons(), Botan::DER_Encoder::start_explicit(), and Botan::AlgorithmIdentifier::USE_NULL_PARAM.

◆ get_contents()

SecureVector< byte > Botan::CMS_Encoder::get_contents ( )

Definition at line 39 of file cms_enc.cpp.

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 }

References Botan::DER_Encoder::get_contents(), Botan::OIDS::lookup(), Botan::SEQUENCE, and Botan::DER_Encoder::start_cons().

Referenced by PEM_contents().

◆ PEM_contents()

std::string Botan::CMS_Encoder::PEM_contents ( )

Definition at line 67 of file cms_enc.cpp.

68 {
69 return PEM_Code::encode(get_contents(), "PKCS7");
70 }
SecureVector< byte > get_contents()
Definition cms_enc.cpp:39

References Botan::PEM_Code::encode(), and get_contents().

◆ set_data() [1/2]

void Botan::CMS_Encoder::set_data ( const byte buf[],
size_t length )

Definition at line 18 of file cms_enc.cpp.

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 }
void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:22
Invalid_State(const std::string &err)
Definition exceptn.h:27

References Botan::copy_mem(), and Botan::Invalid_State::Invalid_State().

◆ set_data() [2/2]

void Botan::CMS_Encoder::set_data ( const std::string & str)

Definition at line 31 of file cms_enc.cpp.

32 {
33 set_data(reinterpret_cast<const byte*>(str.c_str()), str.length());
34 }

References set_data().

Referenced by CMS_Encoder(), CMS_Encoder(), and set_data().

◆ sign()

void Botan::CMS_Encoder::sign ( const X509_Certificate & cert,
const Private_Key & key,
RandomNumberGenerator & rng,
const std::vector< X509_Certificate > & cert_chain,
const std::string & hash,
const std::string & padding )

Definition at line 284 of file cms_ealg.cpp.

290 {
291 std::string padding = pad_algo + "(" + hash + ")";
292
294
295 PK_Signer signer(key, padding, format);
296
297 AlgorithmIdentifier sig_algo(OIDS::lookup(key.algo_name() + "/" + padding),
299
300 SecureVector<byte> signed_attr = encode_attr(data, type, hash);
301 signer.update(signed_attr);
302 SecureVector<byte> signature = signer.signature(rng);
303 signed_attr[0] = 0xA0;
304
305 const size_t SI_VERSION = cert.subject_key_id().size() ? 3 : 1;
306 const size_t CMS_VERSION = (type != "CMS.DataContent") ? 3 : SI_VERSION;
307
308 DER_Encoder encoder;
309
310 encoder.start_cons(SEQUENCE)
311 .encode(CMS_VERSION)
312 .start_cons(SET)
313 .encode(AlgorithmIdentifier(OIDS::lookup(hash),
315 .end_cons()
316 .raw_bytes(make_econtent(data, type));
317
318 encoder.start_cons(ASN1_Tag(0), CONTEXT_SPECIFIC);
319 for(size_t j = 0; j != chain.size(); j++)
320 encoder.raw_bytes(chain[j].BER_encode());
321 encoder.raw_bytes(cert.BER_encode()).end_cons();
322
323 encoder.start_cons(SET)
324 .start_cons(SEQUENCE)
325 .encode(SI_VERSION);
326 encode_si(encoder, cert, ((SI_VERSION == 3) ? true : false))
327 .encode(
330 )
331 .raw_bytes(signed_attr)
332 .encode(sig_algo)
333 .encode(signature, OCTET_STRING)
334 .end_cons()
335 .end_cons()
336 .end_cons();
337
338 add_layer("CMS.SignedData", encoder);
339 }
@ CONTEXT_SPECIFIC
Definition asn1_int.h:22
@ SET
Definition asn1_int.h:36
Signature_Format
Definition pubkey.h:24
@ IEEE_1363
Definition pubkey.h:24

References Botan::Public_Key::algo_name(), Botan::AlgorithmIdentifier::AlgorithmIdentifier(), Botan::X509_Object::BER_encode(), Botan::CONTEXT_SPECIFIC, Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::IEEE_1363, Botan::OIDS::lookup(), Botan::OCTET_STRING, Botan::DER_Encoder::raw_bytes(), Botan::SEQUENCE, Botan::SET, Botan::PK_Signer::signature(), Botan::MemoryRegion< T >::size(), Botan::DER_Encoder::start_cons(), Botan::X509_Certificate::subject_key_id(), Botan::PK_Signer::update(), and Botan::AlgorithmIdentifier::USE_NULL_PARAM.


The documentation for this class was generated from the following files: