diff options
author | Jack Lloyd <[email protected]> | 2018-05-24 11:50:40 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-05-24 13:46:15 -0400 |
commit | 14ea9c0395dd5861fbe457165c95fa789bd3acef (patch) | |
tree | e32d230b9fe019105251b43362a6df3a392c3ad6 /src/lib/asn1 | |
parent | cf40ae57ad5e59161316a217a1305d4e519ff25c (diff) |
Improve error message on BER decoding error
Diffstat (limited to 'src/lib/asn1')
-rw-r--r-- | src/lib/asn1/asn1_obj.cpp | 53 | ||||
-rw-r--r-- | src/lib/asn1/asn1_obj.h | 3 |
2 files changed, 50 insertions, 6 deletions
diff --git a/src/lib/asn1/asn1_obj.cpp b/src/lib/asn1/asn1_obj.cpp index 98f44d407..987c89d96 100644 --- a/src/lib/asn1/asn1_obj.cpp +++ b/src/lib/asn1/asn1_obj.cpp @@ -9,6 +9,7 @@ #include <botan/der_enc.h> #include <botan/data_src.h> #include <botan/internal/stl_util.h> +#include <sstream> namespace Botan { @@ -28,11 +29,34 @@ void BER_Object::assert_is_a(ASN1_Tag type_tag_, ASN1_Tag class_tag_, { if(this->is_a(type_tag_, class_tag_) == false) { - throw BER_Decoding_Error("Tag mismatch when decoding " + descr + " got " + - std::to_string(type_tag) + "/" + - std::to_string(class_tag) + " expected " + - std::to_string(type_tag_) + "/" + - std::to_string(class_tag_)); + std::stringstream msg; + + msg << "Tag mismatch when decoding " << descr << " got "; + + if(class_tag == UNIVERSAL || class_tag == CONSTRUCTED) + { + msg << asn1_tag_to_string(type_tag); + } + else + { + msg << std::to_string(type_tag); + } + + msg << "/" << asn1_class_to_string(class_tag); + msg << " expected "; + + if(class_tag_ == UNIVERSAL || class_tag_ == CONSTRUCTED) + { + msg << asn1_tag_to_string(type_tag_); + } + else + { + msg << std::to_string(type_tag_); + } + + msg << "/" << asn1_class_to_string(class_tag_); + + throw BER_Decoding_Error(msg.str()); } } @@ -52,6 +76,25 @@ void BER_Object::set_tagging(ASN1_Tag t, ASN1_Tag c) class_tag = c; } +std::string asn1_class_to_string(ASN1_Tag type) + { + switch(type) + { + case UNIVERSAL: + return "UNIVERSAL"; + case CONSTRUCTED: + return "CONSTRUCTED"; + case CONTEXT_SPECIFIC: + return "CONTEXT_SPECIFIC"; + case APPLICATION: + return "APPLICATION"; + case CONSTRUCTED | CONTEXT_SPECIFIC: + return "PRIVATE"; + default: + return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")"; + } + } + std::string asn1_tag_to_string(ASN1_Tag type) { switch(type) diff --git a/src/lib/asn1/asn1_obj.h b/src/lib/asn1/asn1_obj.h index b9477da09..7ae694977 100644 --- a/src/lib/asn1/asn1_obj.h +++ b/src/lib/asn1/asn1_obj.h @@ -56,7 +56,8 @@ enum ASN1_Tag : uint32_t { DIRECTORY_STRING = 0xFF01 }; -std::string BOTAN_DLL asn1_tag_to_string(ASN1_Tag type); +std::string BOTAN_UNSTABLE_API asn1_tag_to_string(ASN1_Tag type); +std::string BOTAN_UNSTABLE_API asn1_class_to_string(ASN1_Tag type); /** * Basic ASN.1 Object Interface |