diff options
author | lloyd <[email protected]> | 2014-11-12 01:23:55 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-11-12 01:23:55 +0000 |
commit | 8b0cbccc7b11e545ed27bc6d7bda04b5cf632e60 (patch) | |
tree | 7ea9368d6ccaa85337a63b55e8bd15efa46fd357 /src/cmd/asn1.cpp | |
parent | 67161b91163afad417f9483cb557b26c5f5f4bc0 (diff) |
Command line prog cleanup
Diffstat (limited to 'src/cmd/asn1.cpp')
-rw-r--r-- | src/cmd/asn1.cpp | 160 |
1 files changed, 79 insertions, 81 deletions
diff --git a/src/cmd/asn1.cpp b/src/cmd/asn1.cpp index 02b73e415..cb68b2fcc 100644 --- a/src/cmd/asn1.cpp +++ b/src/cmd/asn1.cpp @@ -1,6 +1,7 @@ #include "apps.h" #include <botan/bigint.h> +#include <botan/hex.h> #include <botan/der_enc.h> #include <botan/ber_dec.h> #include <botan/asn1_time.h> @@ -25,10 +26,6 @@ using namespace Botan; */ #define INITIAL_LEVEL 0 -void decode(BER_Decoder&, size_t); -void emit(const std::string&, size_t, size_t, const std::string& = ""); -std::string type_name(ASN1_Tag); - namespace { std::string url_encode(const std::vector<byte>& in) @@ -55,38 +52,63 @@ std::string url_encode(const std::vector<byte>& in) return out.str(); } -} - -int asn1_main(int argc, char* argv[]) +void emit(const std::string& type, size_t level, size_t length, const std::string& value = "") { - if(argc != 2) - { - std::cout << "Usage: " << argv[0] << " <file>\n"; - return 1; - } + const size_t LIMIT = 4*1024; + const size_t BIN_LIMIT = 1024; - try { - DataSource_Stream in(argv[1]); + std::ostringstream out; - if(!PEM_Code::matches(in)) - { - BER_Decoder decoder(in); - decode(decoder, INITIAL_LEVEL); - } - else - { - std::string label; // ignored - BER_Decoder decoder(PEM_Code::decode(in, label)); - decode(decoder, INITIAL_LEVEL); - } - } - catch(std::exception& e) + out << " d=" << std::setw(2) << level + << ", l=" << std::setw(4) << length << ": "; + + for(size_t i = INITIAL_LEVEL; i != level; ++i) + out << ' '; + + out << type; + + bool should_skip = false; + + if(value.length() > LIMIT) + should_skip = true; + + if((type == "OCTET STRING" || type == "BIT STRING") && value.length() > BIN_LIMIT) + should_skip = true; + + if(value != "" && !should_skip) { - std::cout << "Error: " << e.what() << "\n"; - return 2; + if(out.tellp() % 2 == 0) out << ' '; + + while(out.tellp() < 50) out << ' '; + + out << value; } - return 0; + std::cout << out.str() << "\n"; + } + +std::string type_name(ASN1_Tag type) + { + if(type == PRINTABLE_STRING) return "PRINTABLE STRING"; + if(type == NUMERIC_STRING) return "NUMERIC STRING"; + if(type == IA5_STRING) return "IA5 STRING"; + if(type == T61_STRING) return "T61 STRING"; + if(type == UTF8_STRING) return "UTF8 STRING"; + if(type == VISIBLE_STRING) return "VISIBLE STRING"; + if(type == BMP_STRING) return "BMP STRING"; + + if(type == UTC_TIME) return "UTC TIME"; + if(type == GENERALIZED_TIME) return "GENERALIZED TIME"; + + if(type == OCTET_STRING) return "OCTET STRING"; + if(type == BIT_STRING) return "BIT STRING"; + + if(type == ENUMERATED) return "ENUMERATED"; + if(type == INTEGER) return "INTEGER"; + if(type == NULL_TAG) return "NULL"; + if(type == OBJECT_ID) return "OBJECT"; + if(type == BOOLEAN) return "BOOLEAN"; + return "(UNKNOWN)"; } void decode(BER_Decoder& decoder, size_t level) @@ -184,7 +206,7 @@ void decode(BER_Decoder& decoder, size_t level) std::vector<byte> rep; /* If it's small, it's probably a number, not a hash */ - if(number.bits() <= 16) + if(number.bits() <= 20) rep = BigInt::encode(number, BigInt::Decimal); else rep = BigInt::encode(number, BigInt::Hexadecimal); @@ -283,62 +305,38 @@ void decode(BER_Decoder& decoder, size_t level) } } -void emit(const std::string& type, size_t level, size_t length, - const std::string& value) +int asn1(int argc, char* argv[]) { - const size_t LIMIT = 128; - const size_t BIN_LIMIT = 64; - - std::ostringstream out; - - out << " d=" << std::setw(2) << level - << ", l=" << std::setw(4) << length << ": "; - - for(size_t i = INITIAL_LEVEL; i != level; ++i) - out << ' '; - - out << type; - - bool should_skip = false; - - if(value.length() > LIMIT) - should_skip = true; - - if((type == "OCTET STRING" || type == "BIT STRING") && value.length() > BIN_LIMIT) - should_skip = true; - - if(value != "" && !should_skip) + if(argc != 2) { - if(out.tellp() % 2 == 0) out << ' '; + std::cout << "Usage: " << argv[0] << " <file>\n"; + return 1; + } - while(out.tellp() < 50) out << ' '; + try { + DataSource_Stream in(argv[1]); - out << value; + if(!PEM_Code::matches(in)) + { + BER_Decoder decoder(in); + decode(decoder, INITIAL_LEVEL); + } + else + { + std::string label; // ignored + BER_Decoder decoder(PEM_Code::decode(in, label)); + decode(decoder, INITIAL_LEVEL); + } + } + catch(std::exception& e) + { + std::cout << "Error: " << e.what() << "\n"; + return 2; } - std::cout << out.str() << "\n"; + return 0; } -std::string type_name(ASN1_Tag type) - { - if(type == PRINTABLE_STRING) return "PRINTABLE STRING"; - if(type == NUMERIC_STRING) return "NUMERIC STRING"; - if(type == IA5_STRING) return "IA5 STRING"; - if(type == T61_STRING) return "T61 STRING"; - if(type == UTF8_STRING) return "UTF8 STRING"; - if(type == VISIBLE_STRING) return "VISIBLE STRING"; - if(type == BMP_STRING) return "BMP STRING"; - - if(type == UTC_TIME) return "UTC TIME"; - if(type == GENERALIZED_TIME) return "GENERALIZED TIME"; - - if(type == OCTET_STRING) return "OCTET STRING"; - if(type == BIT_STRING) return "BIT STRING"; +REGISTER_APP(asn1); - if(type == ENUMERATED) return "ENUMERATED"; - if(type == INTEGER) return "INTEGER"; - if(type == NULL_TAG) return "NULL"; - if(type == OBJECT_ID) return "OBJECT"; - if(type == BOOLEAN) return "BOOLEAN"; - return "(UNKNOWN)"; - } +} |