diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2021-04-18 13:39:50 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-18 14:18:16 +0200 |
commit | 65de2d236df19be626ad6ba3b4474fee65643088 (patch) | |
tree | d55785d7012e02dfed4a1b604f56335e72905729 /Userland | |
parent | 581f9ff6bbab3de172e27ede04266913d871534c (diff) | |
download | serenity-65de2d236df19be626ad6ba3b4474fee65643088.zip |
LibCrypto: Allow the user to override the DER read kind and class
This is useful for parsing non-universal types.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibCrypto/ASN1/DER.h | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/Userland/Libraries/LibCrypto/ASN1/DER.h b/Userland/Libraries/LibCrypto/ASN1/DER.h index 42df876554..3e092353ff 100644 --- a/Userland/Libraries/LibCrypto/ASN1/DER.h +++ b/Userland/Libraries/LibCrypto/ASN1/DER.h @@ -64,8 +64,42 @@ public: ValueType value; }; + Optional<DecodeError> drop() + { + if (m_stack.is_empty()) + return DecodeError::NoInput; + + if (eof()) + return DecodeError::EndOfStream; + + auto previous_position = m_stack; + + auto tag_or_error = peek(); + if (tag_or_error.is_error()) { + m_stack = move(previous_position); + return tag_or_error.error(); + } + + auto length_or_error = read_length(); + if (length_or_error.is_error()) { + m_stack = move(previous_position); + return length_or_error.error(); + } + + auto length = length_or_error.value(); + + auto bytes_result = read_bytes(length); + if (bytes_result.is_error()) { + m_stack = move(previous_position); + return bytes_result.error(); + } + + m_current_tag.clear(); + return {}; + } + template<typename ValueType> - Result<ValueType, DecodeError> read() + Result<ValueType, DecodeError> read(Optional<Class> class_override = {}, Optional<Kind> kind_override = {}) { if (m_stack.is_empty()) return DecodeError::NoInput; @@ -90,7 +124,7 @@ public: auto tag = tag_or_error.value(); auto length = length_or_error.value(); - auto value_or_error = read_value<ValueType>(tag.class_, tag.kind, length); + auto value_or_error = read_value<ValueType>(class_override.value_or(tag.class_), kind_override.value_or(tag.kind), length); if (value_or_error.is_error()) { m_stack = move(previous_position); return value_or_error.error(); |