Botan  2.1.0
Crypto and TLS for C++11
asn1_obj.cpp
Go to the documentation of this file.
1 /*
2 * ASN.1 Internals
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/asn1_obj.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 #include <botan/internal/stl_util.h>
14 
15 namespace Botan {
16 
17 /*
18 * BER Decoding Exceptions
19 */
20 BER_Decoding_Error::BER_Decoding_Error(const std::string& str) :
21  Decoding_Error("BER: " + str) {}
22 
23 BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) :
24  BER_Decoding_Error(str + ": " + std::to_string(tag)) {}
25 
26 BER_Bad_Tag::BER_Bad_Tag(const std::string& str,
27  ASN1_Tag tag1, ASN1_Tag tag2) :
28  BER_Decoding_Error(str + ": " + std::to_string(tag1) + "/" + std::to_string(tag2)) {}
29 
30 namespace ASN1 {
31 
32 /*
33 * Put some arbitrary bytes into a SEQUENCE
34 */
35 std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents)
36  {
37  return DER_Encoder()
39  .raw_bytes(contents)
40  .end_cons()
42  }
43 
44 /*
45 * Convert a BER object into a string object
46 */
47 std::string to_string(const BER_Object& obj)
48  {
49  return to_string(obj.value);
50  }
51 
52 /*
53 * Do heuristic tests for BER data
54 */
55 bool maybe_BER(DataSource& source)
56  {
57  uint8_t first_u8;
58  if(!source.peek_byte(first_u8))
59  {
60  BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
61  throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
62  }
63 
64  if(first_u8 == (SEQUENCE | CONSTRUCTED))
65  return true;
66  return false;
67  }
68 
69 }
70 
71 }
BER_Decoding_Error(const std::string &)
Definition: asn1_obj.cpp:20
BER_Bad_Tag(const std::string &msg, ASN1_Tag tag)
Definition: asn1_obj.cpp:23
std::vector< uint8_t > get_contents_unlocked()
Definition: der_enc.h:27
Definition: bigint.h:619
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:55
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
DER_Encoder & end_cons()
Definition: der_enc.cpp:147
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:195
secure_vector< uint8_t > value
Definition: asn1_obj.h:94
ASN1_Tag
Definition: asn1_obj.h:22
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:53
Definition: alg_id.cpp:13
size_t read_byte(uint8_t &out)
Definition: data_src.cpp:22
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition: asn1_obj.cpp:35
std::string to_string(const secure_vector< uint8_t > &bytes)
Definition: stl_util.h:25
size_t peek_byte(uint8_t &out) const
Definition: data_src.cpp:30
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:137