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
14namespace Botan {
15
16/*
17* BER Decoding Exceptions
18*/
20 Decoding_Error("BER: " + str) {}
21
22BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) :
23 BER_Decoding_Error(str + ": " + to_string(tag)) {}
24
25BER_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
29namespace ASN1 {
30
31/*
32* Put some arbitrary bytes into a SEQUENCE
33*/
35 {
36 return DER_Encoder()
38 .raw_bytes(contents)
39 .end_cons()
40 .get_contents();
41 }
42
43/*
44* Convert a BER object into a string object
45*/
46std::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*/
55bool 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}
SecureVector< byte > value
Definition asn1_int.h:83
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition der_enc.cpp:135
DER_Encoder & raw_bytes(const byte val[], size_t len)
Definition der_enc.cpp:188
SecureVector< byte > get_contents()
Definition der_enc.cpp:122
DER_Encoder & end_cons()
Definition der_enc.cpp:145
size_t peek_byte(byte &out) const
Definition data_src.cpp:27
size_t size() const
Definition secmem.h:29
bool maybe_BER(DataSource &source)
Definition asn1_int.cpp:55
std::string to_string(const BER_Object &obj)
Definition asn1_int.cpp:46
SecureVector< byte > put_in_sequence(const MemoryRegion< byte > &contents)
Definition asn1_int.cpp:34
std::string to_string(u64bit n, size_t min_len)
Definition parsing.cpp:42
ASN1_Tag
Definition asn1_int.h:19
@ CONSTRUCTED
Definition asn1_int.h:25
@ SEQUENCE
Definition asn1_int.h:35
BER_Bad_Tag(const std::string &msg, ASN1_Tag tag)
Definition asn1_int.cpp:22
BER_Decoding_Error(const std::string &)
Definition asn1_int.cpp:19
Decoding_Error(const std::string &name)
Definition exceptn.h:140
Stream_IO_Error(const std::string &err)
Definition exceptn.h:167