Botan 1.10.17

asn1_int.cpp
Go to the documentation of this file.
1 /*
2 * ASN.1 Internals
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/asn1_int.h>
9 #include <botan/der_enc.h>
10 #include <botan/ber_dec.h>
11 #include <botan/data_src.h>
12 #include <botan/parsing.h>
13
14 namespace Botan {
15
16 /*
17 * BER Decoding Exceptions
18 */
19 BER_Decoding_Error::BER_Decoding_Error(const std::string& str) :
20 Decoding_Error("BER: " + str) {}
21
22 BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) :
23 BER_Decoding_Error(str + ": " + to_string(tag)) {}
24
25 BER_Bad_Tag::BER_Bad_Tag(const std::string& str,
26 ASN1_Tag tag1, ASN1_Tag tag2) :
27 BER_Decoding_Error(str + ": " + to_string(tag1) + "/" + to_string(tag2)) {}
28
29 namespace ASN1 {
30
31 /*
32 * Put some arbitrary bytes into a SEQUENCE
33 */
34 SecureVector<byte> put_in_sequence(const MemoryRegion<byte>& contents)
35 {
36 return DER_Encoder()
37 .start_cons(SEQUENCE)
38 .raw_bytes(contents)
39 .end_cons()
40 .get_contents();
41 }
42
43 /*
44 * Convert a BER object into a string object
45 */
46 std::string to_string(const BER_Object& obj)
47 {
48 return std::string(reinterpret_cast<const char*>(&obj.value[0]),
49 obj.value.size());
50 }
51
52 /*
53 * Do heuristic tests for BER data
54 */
55 bool maybe_BER(DataSource& source)
56 {
57 byte first_byte;
58 if(!source.peek_byte(first_byte))
59 throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
60
61 if(first_byte == (SEQUENCE | CONSTRUCTED))
62 return true;
63 return false;
64 }
65
66 }
67
68 }
Botan::Stream_IO_Error
Definition: exceptn.h:165
Botan::BER_Decoding_Error::BER_Decoding_Error
BER_Decoding_Error(const std::string &)
Definition: asn1_int.cpp:19
Botan::BER_Bad_Tag::BER_Bad_Tag
BER_Bad_Tag(const std::string &msg, ASN1_Tag tag)
Definition: asn1_int.cpp:22
Botan::DER_Encoder::get_contents
SecureVector< byte > get_contents()
Definition: der_enc.cpp:122
Botan::DER_Encoder::raw_bytes
DER_Encoder & raw_bytes(const byte val[], size_t len)
Definition: der_enc.cpp:188
Botan::ASN1::maybe_BER
bool maybe_BER(DataSource &source)
Definition: asn1_int.cpp:55
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_int.cpp:46
Botan::DER_Encoder::end_cons
DER_Encoder & end_cons()
Definition: der_enc.cpp:145
Botan::DataSource
Definition: data_src.h:20
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::CONSTRUCTED
Definition: asn1_int.h:25
Botan::ASN1_Tag
ASN1_Tag
Definition: asn1_int.h:19
Botan::Decoding_Error
Definition: exceptn.h:138
Botan::DataSource::peek_byte
size_t peek_byte(byte &out) const
Definition: data_src.cpp:27
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::BER_Object::value
SecureVector< byte > value
Definition: asn1_int.h:83
Botan::ASN1::put_in_sequence
SecureVector< byte > put_in_sequence(const MemoryRegion< byte > &contents)
Definition: asn1_int.cpp:34
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::BER_Decoding_Error
Definition: asn1_int.h:107
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
Botan::to_string
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
Botan::SecureVector< byte >
Botan::BER_Object
Definition: asn1_int.h:77