Botan: Botan::CMS_Encoder Class Reference

Botan 1.10.16

Public Member Functions | Static Public Member Functions | List of all members
Botan::CMS_Encoder Class Reference

`#include <cms_enc.h>`

## Public Member Functions
void | authenticate (const X509_Certificate &, const std::string &="")
void | authenticate (const std::string &, const std::string &="")
void | authenticate (const SymmetricKey &, const std::string &="")
| CMS_Encoder (const std::string &str)
| CMS_Encoder (const byte buf[], size_t length)
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< byte > | get_contents ()
std::string | PEM_contents ()
void | set_data (const std::string &)
void | set_data (const byte[], size_t)
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); }
Botan::CMS_Encoder::set_data
void set_data(const std::string &)
Definition: cms_enc.cpp:31

## ◆ 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); }
Botan::CMS_Encoder::set_data
void set_data(const std::string &)
Definition: cms_enc.cpp:31

## Member Function Documentation

## ◆ authenticate() [1/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 }

## ◆ authenticate() [2/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 }

## ◆ authenticate() [3/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 }

## ◆ can_compress_with()

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

Definition at line 56 of file cms_comp.cpp.

References Botan::OID::as_string(), Botan::BER_Decoder::decode(), Botan::BER_Decoder::end_cons(), Botan::OIDS::lookup(), Botan::AlgorithmIdentifier::oid, Botan::Pipe::process_msg(), Botan::Pipe::read_all(), Botan::SEQUENCE, and Botan::BER_Decoder::start_cons().

Referenced by compress().

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 }

## ◆ compress()

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

Definition at line 24 of file cms_comp.cpp.

References can_compress_with(), Botan::PEM_Code::encode(), Botan::Pipe::process_msg(), Botan::Pipe::read_all(), Botan::SEQUENCE, and Botan::DER_Encoder::start_cons().

25 {
26 if(!CMS_Encoder::can_compress_with(algo))
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 }
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::SEQUENCE
Definition: asn1_int.h:35
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::CMS_Encoder::can_compress_with
static bool can_compress_with(const std::string &)
Definition: cms_comp.cpp:56

## ◆ digest()

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

Definition at line 344 of file cms_ealg.cpp.

References Botan::DER_Encoder::encode(), 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.

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),
356 AlgorithmIdentifier::USE_NULL_PARAM))
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 }
Botan::OCTET_STRING
Definition: asn1_int.h:31
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:31
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::AlgorithmIdentifier::USE_NULL_PARAM
Definition: alg_id.h:23
Botan::OIDS::have_oid
bool have_oid(const std::string &name)
Definition: oids.cpp:61

## ◆ 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 }
Botan::KEY_ENCIPHERMENT
Definition: pubkey_enums.h:22
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::KEY_AGREEMENT
Definition: pubkey_enums.h:24
Botan::NO_CONSTRAINTS
Definition: pubkey_enums.h:19
Botan::Key_Constraints
Key_Constraints
Definition: pubkey_enums.h:18

## ◆ 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 }

## ◆ 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.

References Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), 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.

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),
213 AlgorithmIdentifier::USE_NULL_PARAM))
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 }
Botan::SymmetricKey
OctetString SymmetricKey
Definition: symkey.h:147
Botan::OCTET_STRING
Definition: asn1_int.h:31
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:31
Botan::ASN1_Tag
ASN1_Tag
Definition: asn1_int.h:19
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::AlgorithmIdentifier::USE_NULL_PARAM
Definition: alg_id.h:23

## ◆ get_contents()

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

Definition at line 39 of file cms_enc.cpp.

References Botan::MemoryRegion< T >::clear(), Botan::PEM_Code::encode(), Botan::DER_Encoder::get_contents(), Botan::OIDS::lookup(), oid, Botan::SEQUENCE, and Botan::DER_Encoder::start_cons().

Referenced by PEM_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 }
Botan::MemoryRegion::clear
void clear()
Definition: secmem.h:152
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:31
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::PEM_Code::encode
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:19

## ◆ PEM_contents()

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

Definition at line 67 of file cms_enc.cpp.

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

68 {
69 return PEM_Code::encode(get_contents(), "PKCS7");
70 }
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::CMS_Encoder::get_contents
SecureVector< byte > get_contents()
Definition: cms_enc.cpp:39

## ◆ set_data() [1/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 }
Botan::CMS_Encoder::set_data
void set_data(const std::string &)
Definition: cms_enc.cpp:31

## ◆ set_data() [2/2]

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

Definition at line 18 of file cms_enc.cpp.

References Botan::copy_mem(), Botan::MemoryRegion< T >::empty(), and Botan::MemoryRegion< T >::resize().

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 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:211
Botan::MemoryRegion::empty
bool empty() const
Definition: secmem.h:35
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22

## ◆ 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.

References Botan::Public_Key::algo_name(), 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.

290 {
291 std::string padding = pad_algo + "(" + hash + ")";
292
293 Signature_Format format = IEEE_1363;
294
295 PK_Signer signer(key, padding, format);
296
297 AlgorithmIdentifier sig_algo(OIDS::lookup(key.algo_name() + "/" + padding),
298 AlgorithmIdentifier::USE_NULL_PARAM);
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),
314 AlgorithmIdentifier::USE_NULL_PARAM))
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(
328 AlgorithmIdentifier(OIDS::lookup(hash),
329 AlgorithmIdentifier::USE_NULL_PARAM)
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 }
Botan::CONTEXT_SPECIFIC
Definition: asn1_int.h:22
Botan::Signature_Format
Signature_Format
Definition: pubkey.h:24
Botan::OCTET_STRING
Definition: asn1_int.h:31
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:31
Botan::ASN1_Tag
ASN1_Tag
Definition: asn1_int.h:19
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::IEEE_1363
Definition: pubkey.h:24
Botan::SET
Definition: asn1_int.h:36
Botan::AlgorithmIdentifier::USE_NULL_PARAM
Definition: alg_id.h:23

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

- src/cms/cms_enc.h
- src/cms/cms_algo.cpp
- src/cms/cms_comp.cpp
- src/cms/cms_ealg.cpp
- src/cms/cms_enc.cpp