diff options
Diffstat (limited to 'src/asn1/der_enc.cpp')
-rw-r--r-- | src/asn1/der_enc.cpp | 65 |
1 files changed, 29 insertions, 36 deletions
diff --git a/src/asn1/der_enc.cpp b/src/asn1/der_enc.cpp index 06aaa3dfa..fa53aa921 100644 --- a/src/asn1/der_enc.cpp +++ b/src/asn1/der_enc.cpp @@ -28,16 +28,16 @@ SecureVector<byte> encode_tag(ASN1_Tag type_tag, ASN1_Tag class_tag) SecureVector<byte> encoded_tag; if(type_tag <= 30) - encoded_tag.append(static_cast<byte>(type_tag | class_tag)); + encoded_tag.push_back(static_cast<byte>(type_tag | class_tag)); else { u32bit blocks = high_bit(type_tag) + 6; blocks = (blocks - (blocks % 7)) / 7; - encoded_tag.append(class_tag | 0x1F); + encoded_tag.push_back(class_tag | 0x1F); for(u32bit k = 0; k != blocks - 1; ++k) - encoded_tag.append(0x80 | ((type_tag >> 7*(blocks-k-1)) & 0x7F)); - encoded_tag.append(type_tag & 0x7F); + encoded_tag.push_back(0x80 | ((type_tag >> 7*(blocks-k-1)) & 0x7F)); + encoded_tag.push_back(type_tag & 0x7F); } return encoded_tag; @@ -50,13 +50,13 @@ SecureVector<byte> encode_length(u32bit length) { SecureVector<byte> encoded_length; if(length <= 127) - encoded_length.append(static_cast<byte>(length)); + encoded_length.push_back(static_cast<byte>(length)); else { const u32bit top_byte = significant_bytes(length); - encoded_length.append(static_cast<byte>(0x80 | top_byte)); + encoded_length.push_back(static_cast<byte>(0x80 | top_byte)); for(u32bit j = 4-top_byte; j != 4; ++j) - encoded_length.append(get_byte(j, length)); + encoded_length.push_back(get_byte(j, length)); } return encoded_length; } @@ -70,24 +70,21 @@ SecureVector<byte> DER_Encoder::DER_Sequence::get_contents() { const ASN1_Tag real_class_tag = ASN1_Tag(class_tag | CONSTRUCTED); - SecureVector<byte> encoded_tag = encode_tag(type_tag, real_class_tag); - if(type_tag == SET) { std::sort(set_contents.begin(), set_contents.end()); for(u32bit j = 0; j != set_contents.size(); ++j) - contents.append(set_contents[j]); + contents += set_contents[j]; set_contents.clear(); } - SecureVector<byte> encoded_length = encode_length(contents.size()); + SecureVector<byte> result; + result += encode_tag(type_tag, real_class_tag); + result += encode_length(contents.size()); + result += contents; + contents.clear(); - SecureVector<byte> retval; - retval.append(encoded_tag); - retval.append(encoded_length); - retval.append(contents); - contents.destroy(); - return retval; + return result; } /* @@ -98,7 +95,7 @@ void DER_Encoder::DER_Sequence::add_bytes(const byte data[], u32bit length) if(type_tag == SET) set_contents.push_back(SecureVector<byte>(data, length)); else - contents.append(data, length); + contents += std::make_pair(data, length); } /* @@ -125,10 +122,9 @@ SecureVector<byte> DER_Encoder::get_contents() if(subsequences.size() != 0) throw Invalid_State("DER_Encoder: Sequence hasn't been marked done"); - SecureVector<byte> retval; - retval = contents; - contents.destroy(); - return retval; + SecureVector<byte> output; + std::swap(output, contents); + return output; } /* @@ -181,7 +177,7 @@ DER_Encoder& DER_Encoder::end_explicit() */ DER_Encoder& DER_Encoder::raw_bytes(const MemoryRegion<byte>& val) { - return raw_bytes(val.begin(), val.size()); + return raw_bytes(&val[0], val.size()); } /* @@ -192,7 +188,7 @@ DER_Encoder& DER_Encoder::raw_bytes(const byte bytes[], u32bit length) if(subsequences.size()) subsequences[subsequences.size()-1].add_bytes(bytes, length); else - contents.append(bytes, length); + contents += std::make_pair(bytes, length); return (*this); } @@ -235,7 +231,7 @@ DER_Encoder& DER_Encoder::encode(const BigInt& n) DER_Encoder& DER_Encoder::encode(const MemoryRegion<byte>& bytes, ASN1_Tag real_type) { - return encode(bytes.begin(), bytes.size(), + return encode(&bytes[0], bytes.size(), real_type, real_type, UNIVERSAL); } @@ -278,7 +274,7 @@ DER_Encoder& DER_Encoder::encode(const BigInt& n, bool extra_zero = (n.bits() % 8 == 0); SecureVector<byte> contents(extra_zero + n.bytes()); - BigInt::encode(contents.begin() + extra_zero, n); + BigInt::encode(&contents[extra_zero], n); if(n < 0) { for(u32bit j = 0; j != contents.size(); ++j) @@ -298,7 +294,7 @@ DER_Encoder& DER_Encoder::encode(const MemoryRegion<byte>& bytes, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag) { - return encode(bytes.begin(), bytes.size(), + return encode(&bytes[0], bytes.size(), real_type, type_tag, class_tag); } @@ -315,8 +311,8 @@ DER_Encoder& DER_Encoder::encode(const byte bytes[], u32bit length, if(real_type == BIT_STRING) { SecureVector<byte> encoded; - encoded.append(0); - encoded.append(bytes, length); + encoded.push_back(0); + encoded += std::make_pair(bytes, length); return add_object(type_tag, class_tag, encoded); } else @@ -348,13 +344,10 @@ DER_Encoder& DER_Encoder::encode(const ASN1_Object& obj) DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], u32bit length) { - SecureVector<byte> encoded_tag = encode_tag(type_tag, class_tag); - SecureVector<byte> encoded_length = encode_length(length); - SecureVector<byte> buffer; - buffer.append(encoded_tag); - buffer.append(encoded_length); - buffer.append(rep, length); + buffer += encode_tag(type_tag, class_tag); + buffer += encode_length(length); + buffer += std::make_pair(rep, length); return raw_bytes(buffer); } @@ -365,7 +358,7 @@ DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const MemoryRegion<byte>& rep_buf) { - const byte* rep = rep_buf.begin(); + const byte* rep = &rep_buf[0]; const u32bit rep_len = rep_buf.size(); return add_object(type_tag, class_tag, rep, rep_len); } |