diff options
Diffstat (limited to 'src/lib/asn1')
-rw-r--r-- | src/lib/asn1/alg_id.cpp | 32 | ||||
-rw-r--r-- | src/lib/asn1/alg_id.h | 23 | ||||
-rw-r--r-- | src/lib/asn1/asn1_attribute.cpp | 8 | ||||
-rw-r--r-- | src/lib/asn1/asn1_attribute.h | 17 | ||||
-rw-r--r-- | src/lib/asn1/asn1_obj.h | 2 | ||||
-rw-r--r-- | src/lib/asn1/ber_dec.cpp | 104 | ||||
-rw-r--r-- | src/lib/asn1/ber_dec.h | 25 | ||||
-rw-r--r-- | src/lib/asn1/der_enc.cpp | 33 | ||||
-rw-r--r-- | src/lib/asn1/der_enc.h | 19 |
9 files changed, 122 insertions, 141 deletions
diff --git a/src/lib/asn1/alg_id.cpp b/src/lib/asn1/alg_id.cpp index 0b84d2137..0637a8f8d 100644 --- a/src/lib/asn1/alg_id.cpp +++ b/src/lib/asn1/alg_id.cpp @@ -16,38 +16,46 @@ namespace Botan { * Create an AlgorithmIdentifier */ AlgorithmIdentifier::AlgorithmIdentifier(const OID& alg_id, - const std::vector<uint8_t>& param) : oid(alg_id), parameters(param) + const std::vector<uint8_t>& param) : + oid(alg_id), + parameters(param) {} /* * Create an AlgorithmIdentifier */ AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id, - const std::vector<uint8_t>& param) : oid(OIDS::lookup(alg_id)), parameters(param) + const std::vector<uint8_t>& param) : + oid(OIDS::lookup(alg_id)), + parameters(param) {} /* * Create an AlgorithmIdentifier */ AlgorithmIdentifier::AlgorithmIdentifier(const OID& alg_id, - Encoding_Option option) : oid(alg_id), parameters() + Encoding_Option option) : + oid(alg_id), + parameters() { const uint8_t DER_NULL[] = { 0x05, 0x00 }; if(option == USE_NULL_PARAM) - parameters += std::pair<const uint8_t*, size_t>(DER_NULL, sizeof(DER_NULL)); + parameters.assign(DER_NULL, DER_NULL + 2); } /* * Create an AlgorithmIdentifier */ AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id, - Encoding_Option option) : oid(OIDS::lookup(alg_id)), parameters() + Encoding_Option option) : + oid(OIDS::lookup(alg_id)), + parameters() { const uint8_t DER_NULL[] = { 0x05, 0x00 }; if(option == USE_NULL_PARAM) - parameters += std::pair<const uint8_t*, size_t>(DER_NULL, sizeof(DER_NULL)); + parameters.assign(DER_NULL, DER_NULL + 2); } /* @@ -66,14 +74,14 @@ bool param_null_or_empty(const std::vector<uint8_t>& p) bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) { - if(a1.oid != a2.oid) + if(a1.get_oid() != a2.get_oid()) return false; - if(param_null_or_empty(a1.parameters) && - param_null_or_empty(a2.parameters)) + if(param_null_or_empty(a1.get_parameters()) && + param_null_or_empty(a2.get_parameters())) return true; - return (a1.parameters == a2.parameters); + return (a1.get_parameters() == a2.get_parameters()); } /* @@ -90,8 +98,8 @@ bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const { codec.start_cons(SEQUENCE) - .encode(oid) - .raw_bytes(parameters) + .encode(get_oid()) + .raw_bytes(get_parameters()) .end_cons(); } diff --git a/src/lib/asn1/alg_id.h b/src/lib/asn1/alg_id.h index be862c5dd..b9af37c8a 100644 --- a/src/lib/asn1/alg_id.h +++ b/src/lib/asn1/alg_id.h @@ -27,16 +27,21 @@ class BOTAN_PUBLIC_API(2,0) AlgorithmIdentifier final : public ASN1_Object void decode_from(class BER_Decoder&) override; AlgorithmIdentifier() = default; - AlgorithmIdentifier(const OID&, Encoding_Option); - AlgorithmIdentifier(const std::string&, Encoding_Option); - AlgorithmIdentifier(const OID&, const std::vector<uint8_t>&); - AlgorithmIdentifier(const std::string&, const std::vector<uint8_t>&); + AlgorithmIdentifier(const OID& oid, Encoding_Option enc); + AlgorithmIdentifier(const std::string& oid_name, Encoding_Option enc); - // public member variable: - OID oid; + AlgorithmIdentifier(const OID& oid, const std::vector<uint8_t>& params); + AlgorithmIdentifier(const std::string& oid_name, const std::vector<uint8_t>& params); + + const OID& get_oid() const { return oid; } + const std::vector<uint8_t>& get_parameters() const { return parameters; } - // public member variable: + /* + * These values are public for historical reasons, but in a future release + * they will be made private. Do not access them. + */ + OID oid; std::vector<uint8_t> parameters; }; @@ -44,9 +49,9 @@ class BOTAN_PUBLIC_API(2,0) AlgorithmIdentifier final : public ASN1_Object * Comparison Operations */ bool BOTAN_PUBLIC_API(2,0) operator==(const AlgorithmIdentifier&, - const AlgorithmIdentifier&); + const AlgorithmIdentifier&); bool BOTAN_PUBLIC_API(2,0) operator!=(const AlgorithmIdentifier&, - const AlgorithmIdentifier&); + const AlgorithmIdentifier&); } diff --git a/src/lib/asn1/asn1_attribute.cpp b/src/lib/asn1/asn1_attribute.cpp index 3093f5025..8ecd8fd5f 100644 --- a/src/lib/asn1/asn1_attribute.cpp +++ b/src/lib/asn1/asn1_attribute.cpp @@ -15,14 +15,18 @@ namespace Botan { /* * Create an Attribute */ -Attribute::Attribute(const OID& attr_oid, const std::vector<uint8_t>& attr_value) : oid(attr_oid), parameters(attr_value) +Attribute::Attribute(const OID& attr_oid, const std::vector<uint8_t>& attr_value) : + oid(attr_oid), + parameters(attr_value) {} /* * Create an Attribute */ Attribute::Attribute(const std::string& attr_oid, - const std::vector<uint8_t>& attr_value) : oid(OIDS::lookup(attr_oid)), parameters(attr_value) + const std::vector<uint8_t>& attr_value) : + oid(OIDS::lookup(attr_oid)), + parameters(attr_value) {} /* diff --git a/src/lib/asn1/asn1_attribute.h b/src/lib/asn1/asn1_attribute.h index da0ee48d1..077850a65 100644 --- a/src/lib/asn1/asn1_attribute.h +++ b/src/lib/asn1/asn1_attribute.h @@ -23,15 +23,20 @@ class BOTAN_PUBLIC_API(2,0) Attribute final : public ASN1_Object void encode_into(class DER_Encoder& to) const override; void decode_from(class BER_Decoder& from) override; - // public member variable: - OID oid; - - // public member variable: - std::vector<uint8_t> parameters; - Attribute() = default; Attribute(const OID&, const std::vector<uint8_t>&); Attribute(const std::string&, const std::vector<uint8_t>&); + + const OID& get_oid() const { return oid; } + + const std::vector<uint8_t>& get_parameters() const { return parameters; } + + /* + * These values are public for historical reasons, but in a future release + * they will be made private. Do not access them. + */ + OID oid; + std::vector<uint8_t> parameters; }; } diff --git a/src/lib/asn1/asn1_obj.h b/src/lib/asn1/asn1_obj.h index a785a5807..0b9a1493e 100644 --- a/src/lib/asn1/asn1_obj.h +++ b/src/lib/asn1/asn1_obj.h @@ -88,7 +88,7 @@ class BOTAN_PUBLIC_API(2,0) ASN1_Object class BOTAN_PUBLIC_API(2,0) BER_Object final { public: - void assert_is_a(ASN1_Tag, ASN1_Tag); + void assert_is_a(ASN1_Tag type_tag, ASN1_Tag class_tag) const; // public member variable: ASN1_Tag type_tag, class_tag; diff --git a/src/lib/asn1/ber_dec.cpp b/src/lib/asn1/ber_dec.cpp index 515f7352f..225197224 100644 --- a/src/lib/asn1/ber_dec.cpp +++ b/src/lib/asn1/ber_dec.cpp @@ -150,7 +150,7 @@ size_t find_eoc(DataSource* ber, size_t allow_indef) /* * Check a type invariant on BER data */ -void BER_Object::assert_is_a(ASN1_Tag type_tag_, ASN1_Tag class_tag_) +void BER_Object::assert_is_a(ASN1_Tag type_tag_, ASN1_Tag class_tag_) const { if(type_tag != type_tag_ || class_tag != class_tag_) throw BER_Decoding_Error("Tag mismatch when decoding got " + @@ -181,34 +181,13 @@ BER_Decoder& BER_Decoder::verify_end() } /* -* Save all the bytes remaining in the source -*/ -BER_Decoder& BER_Decoder::raw_bytes(secure_vector<uint8_t>& out) - { - out.clear(); - uint8_t buf; - while(m_source->read_byte(buf)) - out.push_back(buf); - return (*this); - } - -BER_Decoder& BER_Decoder::raw_bytes(std::vector<uint8_t>& out) - { - out.clear(); - uint8_t buf; - while(m_source->read_byte(buf)) - out.push_back(buf); - return (*this); - } - -/* * Discard all the bytes remaining in the source */ BER_Decoder& BER_Decoder::discard_remaining() { uint8_t buf; while(m_source->read_byte(buf)) - ; + {} return (*this); } @@ -429,7 +408,8 @@ BER_Decoder& BER_Decoder::decode(bool& out, * Decode a small BER encoded INTEGER */ BER_Decoder& BER_Decoder::decode(size_t& out, - ASN1_Tag type_tag, ASN1_Tag class_tag) + ASN1_Tag type_tag, + ASN1_Tag class_tag) { BigInt integer; decode(integer, type_tag, class_tag); @@ -448,8 +428,8 @@ BER_Decoder& BER_Decoder::decode(size_t& out, * Decode a small BER encoded INTEGER */ uint64_t BER_Decoder::decode_constrained_integer(ASN1_Tag type_tag, - ASN1_Tag class_tag, - size_t T_bytes) + ASN1_Tag class_tag, + size_t T_bytes) { if(T_bytes > 8) throw BER_Decoding_Error("Can't decode small integer over 8 bytes"); @@ -471,7 +451,8 @@ uint64_t BER_Decoder::decode_constrained_integer(ASN1_Tag type_tag, * Decode a BER encoded INTEGER */ BER_Decoder& BER_Decoder::decode(BigInt& out, - ASN1_Tag type_tag, ASN1_Tag class_tag) + ASN1_Tag type_tag, + ASN1_Tag class_tag) { BER_Object obj = get_next_object(); obj.assert_is_a(type_tag, class_tag); @@ -500,37 +481,21 @@ BER_Decoder& BER_Decoder::decode(BigInt& out, return (*this); } -/* -* BER decode a BIT STRING or OCTET STRING -*/ -BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& out, ASN1_Tag real_type) - { - return decode(out, real_type, real_type, UNIVERSAL); - } - -/* -* BER decode a BIT STRING or OCTET STRING -*/ -BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& out, ASN1_Tag real_type) - { - return decode(out, real_type, real_type, UNIVERSAL); - } +namespace { -/* -* BER decode a BIT STRING or OCTET STRING -*/ -BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer, - ASN1_Tag real_type, - ASN1_Tag type_tag, ASN1_Tag class_tag) +template<typename Alloc> +void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer, + const BER_Object& obj, + ASN1_Tag real_type, + ASN1_Tag type_tag, + ASN1_Tag class_tag) { - if(real_type != OCTET_STRING && real_type != BIT_STRING) - throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type); - - BER_Object obj = get_next_object(); obj.assert_is_a(type_tag, class_tag); if(real_type == OCTET_STRING) - buffer = obj.value; + { + buffer.assign(obj.value.begin(), obj.value.end()); + } else { if(obj.value.empty()) @@ -543,33 +508,32 @@ BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer, if(obj.value.size() > 1) copy_mem(buffer.data(), &obj.value[1], obj.value.size() - 1); } - return (*this); } -BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer, +} + +/* +* BER decode a BIT STRING or OCTET STRING +*/ +BER_Decoder& BER_Decoder::decode(secure_vector<uint8_t>& buffer, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag) { if(real_type != OCTET_STRING && real_type != BIT_STRING) throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type); - BER_Object obj = get_next_object(); - obj.assert_is_a(type_tag, class_tag); - - if(real_type == OCTET_STRING) - buffer = unlock(obj.value); - else - { - if(obj.value.empty()) - throw BER_Decoding_Error("Invalid BIT STRING"); - if(obj.value[0] >= 8) - throw BER_Decoding_Error("Bad number of unused bits in BIT STRING"); + asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag); + return (*this); + } - buffer.resize(obj.value.size() - 1); +BER_Decoder& BER_Decoder::decode(std::vector<uint8_t>& buffer, + ASN1_Tag real_type, + ASN1_Tag type_tag, ASN1_Tag class_tag) + { + if(real_type != OCTET_STRING && real_type != BIT_STRING) + throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type); - if(obj.value.size() > 1) - copy_mem(buffer.data(), &obj.value[1], obj.value.size() - 1); - } + asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag); return (*this); } diff --git a/src/lib/asn1/ber_dec.h b/src/lib/asn1/ber_dec.h index fbe62e464..637cdc527 100644 --- a/src/lib/asn1/ber_dec.h +++ b/src/lib/asn1/ber_dec.h @@ -66,15 +66,32 @@ class BOTAN_PUBLIC_API(2,0) BER_Decoder final return (*this); } - BER_Decoder& raw_bytes(secure_vector<uint8_t>& v); - BER_Decoder& raw_bytes(std::vector<uint8_t>& v); + /* + * Save all the bytes remaining in the source + */ + template<typename Alloc> + BER_Decoder& raw_bytes(std::vector<uint8_t, Alloc>& out) + { + out.clear(); + uint8_t buf; + while(m_source->read_byte(buf)) + out.push_back(buf); + return (*this); + } BER_Decoder& decode_null(); BER_Decoder& decode(bool& v); BER_Decoder& decode(size_t& v); BER_Decoder& decode(class BigInt& v); - BER_Decoder& decode(std::vector<uint8_t>& v, ASN1_Tag type_tag); - BER_Decoder& decode(secure_vector<uint8_t>& v, ASN1_Tag type_tag); + + /* + * BER decode a BIT STRING or OCTET STRING + */ + template<typename Alloc> + BER_Decoder& decode(std::vector<uint8_t, Alloc>& out, ASN1_Tag real_type) + { + return decode(out, real_type, real_type, UNIVERSAL); + } BER_Decoder& decode(bool& v, ASN1_Tag type_tag, diff --git a/src/lib/asn1/der_enc.cpp b/src/lib/asn1/der_enc.cpp index 28a9bbc9a..1ed51d593 100644 --- a/src/lib/asn1/der_enc.cpp +++ b/src/lib/asn1/der_enc.cpp @@ -178,19 +178,6 @@ DER_Encoder& DER_Encoder::end_explicit() /* * Write raw bytes into the stream */ -DER_Encoder& DER_Encoder::raw_bytes(const secure_vector<uint8_t>& val) - { - return raw_bytes(val.data(), val.size()); - } - -DER_Encoder& DER_Encoder::raw_bytes(const std::vector<uint8_t>& val) - { - return raw_bytes(val.data(), val.size()); - } - -/* -* Write raw bytes into the stream -*/ DER_Encoder& DER_Encoder::raw_bytes(const uint8_t bytes[], size_t length) { if(m_subsequences.size()) @@ -234,26 +221,6 @@ DER_Encoder& DER_Encoder::encode(const BigInt& n) } /* -* DER encode an OCTET STRING or BIT STRING -*/ -DER_Encoder& DER_Encoder::encode(const secure_vector<uint8_t>& bytes, - ASN1_Tag real_type) - { - return encode(bytes.data(), bytes.size(), - real_type, real_type, UNIVERSAL); - } - -/* -* DER encode an OCTET STRING or BIT STRING -*/ -DER_Encoder& DER_Encoder::encode(const std::vector<uint8_t>& bytes, - ASN1_Tag real_type) - { - return encode(bytes.data(), bytes.size(), - real_type, real_type, UNIVERSAL); - } - -/* * Encode this object */ DER_Encoder& DER_Encoder::encode(const uint8_t bytes[], size_t length, diff --git a/src/lib/asn1/der_enc.h b/src/lib/asn1/der_enc.h index f1e85101c..f76391ae5 100644 --- a/src/lib/asn1/der_enc.h +++ b/src/lib/asn1/der_enc.h @@ -34,18 +34,29 @@ class BOTAN_PUBLIC_API(2,0) DER_Encoder final DER_Encoder& start_explicit(uint16_t type_tag); DER_Encoder& end_explicit(); + /** + * Insert raw bytes directly into the output stream + */ DER_Encoder& raw_bytes(const uint8_t val[], size_t len); - DER_Encoder& raw_bytes(const secure_vector<uint8_t>& val); - DER_Encoder& raw_bytes(const std::vector<uint8_t>& val); + + template<typename Alloc> + DER_Encoder& raw_bytes(const std::vector<uint8_t, Alloc>& val) + { + return raw_bytes(val.data(), val.size()); + } DER_Encoder& encode_null(); DER_Encoder& encode(bool b); DER_Encoder& encode(size_t s); DER_Encoder& encode(const BigInt& n); - DER_Encoder& encode(const secure_vector<uint8_t>& v, ASN1_Tag real_type); - DER_Encoder& encode(const std::vector<uint8_t>& v, ASN1_Tag real_type); DER_Encoder& encode(const uint8_t val[], size_t len, ASN1_Tag real_type); + template<typename Alloc> + DER_Encoder& encode(const std::vector<uint8_t, Alloc>& vec, ASN1_Tag real_type) + { + return encode(vec.data(), vec.size(), real_type); + } + DER_Encoder& encode(bool b, ASN1_Tag type_tag, ASN1_Tag class_tag = CONTEXT_SPECIFIC); |