aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-12-19 01:01:46 -0500
committerJack Lloyd <[email protected]>2017-12-19 01:01:46 -0500
commitac754772afbf7be397f8631ebbb3d2921b0a7753 (patch)
tree357ec592ba996dea9c3affeb3d63109aeafcd37b /src/lib
parent395a8ab3b1d38f545e79f4bf169c71037fa6cc18 (diff)
Fix some other copy+paste code in BER_Decoder
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/asn1/alg_id.h4
-rw-r--r--src/lib/asn1/asn1_obj.h2
-rw-r--r--src/lib/asn1/ber_dec.cpp104
-rw-r--r--src/lib/asn1/ber_dec.h25
4 files changed, 58 insertions, 77 deletions
diff --git a/src/lib/asn1/alg_id.h b/src/lib/asn1/alg_id.h
index be862c5dd..fdb73b178 100644
--- a/src/lib/asn1/alg_id.h
+++ b/src/lib/asn1/alg_id.h
@@ -44,9 +44,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_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,