mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 18:57:35 +00:00
LibCrypto: Implement custom BitStringView for ASN.1 decoder
The ASN.1 decoder was originally using AK::BitmapView for decoded BitStrings, however the specification requires that the bits are stored in a byte from the most significant to the least significant. Storing three bits '110' would result in a byte '1100 0000', i.e. 0xC0. However, AK::BitmapView expects the bits to be stored at the bottom like '0000 0110', i.e. 0x06. For the current uses the data was always a multiple of eight bits, resulting in complete bytes, which could directly be interpreted correctly. For the implementation of the key usage extension of certificates the correct implementation of the BitString is required.
This commit is contained in:
parent
b16b61f6bc
commit
a6e465fba2
3 changed files with 33 additions and 7 deletions
|
@ -26,6 +26,32 @@ enum class DecodeError {
|
|||
UnsupportedFormat,
|
||||
};
|
||||
|
||||
class BitStringView {
|
||||
public:
|
||||
BitStringView(ReadonlyBytes data, size_t unused_bits)
|
||||
: m_data(data)
|
||||
, m_unused_bits(unused_bits)
|
||||
{
|
||||
}
|
||||
|
||||
ReadonlyBytes raw_bytes() const
|
||||
{
|
||||
VERIFY(m_unused_bits == 0);
|
||||
return m_data;
|
||||
}
|
||||
|
||||
bool get(size_t index)
|
||||
{
|
||||
if (index >= 8 * m_data.size() - m_unused_bits)
|
||||
return false;
|
||||
return 0 != (m_data[index / 8] & (1u << (7 - (index % 8))));
|
||||
}
|
||||
|
||||
private:
|
||||
ReadonlyBytes m_data;
|
||||
size_t m_unused_bits;
|
||||
};
|
||||
|
||||
class Decoder {
|
||||
public:
|
||||
Decoder(ReadonlyBytes data)
|
||||
|
@ -194,7 +220,7 @@ private:
|
|||
static Result<std::nullptr_t, DecodeError> decode_null(ReadonlyBytes);
|
||||
static Result<Vector<int>, DecodeError> decode_object_identifier(ReadonlyBytes);
|
||||
static Result<StringView, DecodeError> decode_printable_string(ReadonlyBytes);
|
||||
static Result<const BitmapView, DecodeError> decode_bit_string(ReadonlyBytes);
|
||||
static Result<BitStringView, DecodeError> decode_bit_string(ReadonlyBytes);
|
||||
|
||||
Vector<ReadonlyBytes> m_stack;
|
||||
Optional<Tag> m_current_tag;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue