Botan
1.11.15
|
00001 /* 00002 * ASN.1 Internals 00003 * (C) 1999-2007 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #include <botan/asn1_obj.h> 00009 #include <botan/der_enc.h> 00010 #include <botan/ber_dec.h> 00011 #include <botan/data_src.h> 00012 #include <botan/parsing.h> 00013 00014 namespace Botan { 00015 00016 /* 00017 * BER Decoding Exceptions 00018 */ 00019 BER_Decoding_Error::BER_Decoding_Error(const std::string& str) : 00020 Decoding_Error("BER: " + str) {} 00021 00022 BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) : 00023 BER_Decoding_Error(str + ": " + std::to_string(tag)) {} 00024 00025 BER_Bad_Tag::BER_Bad_Tag(const std::string& str, 00026 ASN1_Tag tag1, ASN1_Tag tag2) : 00027 BER_Decoding_Error(str + ": " + std::to_string(tag1) + "/" + std::to_string(tag2)) {} 00028 00029 namespace ASN1 { 00030 00031 /* 00032 * Put some arbitrary bytes into a SEQUENCE 00033 */ 00034 std::vector<byte> put_in_sequence(const std::vector<byte>& contents) 00035 { 00036 return DER_Encoder() 00037 .start_cons(SEQUENCE) 00038 .raw_bytes(contents) 00039 .end_cons() 00040 .get_contents_unlocked(); 00041 } 00042 00043 /* 00044 * Convert a BER object into a string object 00045 */ 00046 std::string to_string(const BER_Object& obj) 00047 { 00048 return std::string(reinterpret_cast<const char*>(&obj.value[0]), 00049 obj.value.size()); 00050 } 00051 00052 /* 00053 * Do heuristic tests for BER data 00054 */ 00055 bool maybe_BER(DataSource& source) 00056 { 00057 byte first_byte; 00058 if(!source.peek_byte(first_byte)) 00059 throw Stream_IO_Error("ASN1::maybe_BER: Source was empty"); 00060 00061 if(first_byte == (SEQUENCE | CONSTRUCTED)) 00062 return true; 00063 return false; 00064 } 00065 00066 } 00067 00068 }