DER.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Bitmap.h>
  27. #include <AK/Utf8View.h>
  28. #include <LibCrypto/ASN1/DER.h>
  29. namespace Crypto::ASN1 {
  30. Result<Tag, DecodeError> Decoder::read_tag()
  31. {
  32. auto byte_or_error = read_byte();
  33. if (byte_or_error.is_error())
  34. return byte_or_error.error();
  35. auto byte = byte_or_error.value();
  36. u8 class_ = byte & 0xc0;
  37. u8 type = byte & 0x20;
  38. u8 kind = byte & 0x1f;
  39. if (kind == 0x1f) {
  40. kind = 0;
  41. while (byte & 0x80) {
  42. auto byte_or_error = read_byte();
  43. if (byte_or_error.is_error())
  44. return byte_or_error.error();
  45. byte = byte_or_error.value();
  46. kind = (kind << 7) | (byte & 0x7f);
  47. }
  48. }
  49. return Tag { (Kind)kind, (Class)class_, (Type)type };
  50. }
  51. Result<size_t, DecodeError> Decoder::read_length()
  52. {
  53. auto byte_or_error = read_byte();
  54. if (byte_or_error.is_error())
  55. return byte_or_error.error();
  56. auto byte = byte_or_error.value();
  57. size_t length = byte;
  58. if (byte & 0x80) {
  59. auto count = byte & 0x7f;
  60. if (count == 0x7f)
  61. return DecodeError::InvalidInputFormat;
  62. auto data_or_error = read_bytes(count);
  63. if (data_or_error.is_error())
  64. return data_or_error.error();
  65. auto data = data_or_error.value();
  66. length = 0;
  67. if (data.size() > sizeof(size_t))
  68. return DecodeError::Overflow;
  69. for (auto&& byte : data)
  70. length = (length << 8) | (size_t)byte;
  71. }
  72. return length;
  73. }
  74. Result<u8, DecodeError> Decoder::read_byte()
  75. {
  76. if (m_stack.is_empty())
  77. return DecodeError::NoInput;
  78. auto& entry = m_stack.last();
  79. if (entry.is_empty())
  80. return DecodeError::NotEnoughData;
  81. auto byte = entry[0];
  82. entry = entry.slice(1);
  83. return byte;
  84. }
  85. Result<ReadonlyBytes, DecodeError> Decoder::read_bytes(size_t length)
  86. {
  87. if (m_stack.is_empty())
  88. return DecodeError::NoInput;
  89. auto& entry = m_stack.last();
  90. if (entry.size() < length)
  91. return DecodeError::NotEnoughData;
  92. auto bytes = entry.slice(0, length);
  93. entry = entry.slice(length);
  94. return bytes;
  95. }
  96. Result<bool, DecodeError> Decoder::decode_boolean(ReadonlyBytes data)
  97. {
  98. if (data.size() != 1)
  99. return DecodeError::InvalidInputFormat;
  100. return data[0] == 0;
  101. }
  102. Result<UnsignedBigInteger, DecodeError> Decoder::decode_arbitrary_sized_integer(ReadonlyBytes data)
  103. {
  104. if (data.size() < 1)
  105. return DecodeError::NotEnoughData;
  106. if (data.size() > 1
  107. && ((data[0] == 0xff && data[1] & 0x80)
  108. || (data[0] == 0x00 && !(data[1] & 0x80)))) {
  109. return DecodeError::InvalidInputFormat;
  110. }
  111. bool is_negative = data[0] & 0x80;
  112. if (is_negative)
  113. return DecodeError::UnsupportedFormat;
  114. return UnsignedBigInteger::import_data(data.data(), data.size());
  115. }
  116. Result<StringView, DecodeError> Decoder::decode_octet_string(ReadonlyBytes bytes)
  117. {
  118. return StringView { bytes.data(), bytes.size() };
  119. }
  120. Result<std::nullptr_t, DecodeError> Decoder::decode_null(ReadonlyBytes data)
  121. {
  122. if (data.size() != 0)
  123. return DecodeError::InvalidInputFormat;
  124. return nullptr;
  125. }
  126. Result<Vector<int>, DecodeError> Decoder::decode_object_identifier(ReadonlyBytes data)
  127. {
  128. Vector<int> result;
  129. result.append(0); // Reserved space.
  130. u32 value = 0;
  131. for (auto&& byte : data) {
  132. if (value == 0 && byte == 0x80)
  133. return DecodeError::InvalidInputFormat;
  134. value = (value << 7) | (byte & 0x7f);
  135. if (!(byte & 0x80)) {
  136. result.append(value);
  137. value = 0;
  138. }
  139. }
  140. if (result.size() == 1 || result[1] >= 1600)
  141. return DecodeError::InvalidInputFormat;
  142. result[0] = result[1] / 40;
  143. result[1] = result[1] % 40;
  144. return result;
  145. }
  146. Result<StringView, DecodeError> Decoder::decode_printable_string(ReadonlyBytes data)
  147. {
  148. Utf8View view { data };
  149. if (!view.validate())
  150. return DecodeError::InvalidInputFormat;
  151. return StringView { data };
  152. }
  153. Result<const BitmapView, DecodeError> Decoder::decode_bit_string(ReadonlyBytes data)
  154. {
  155. if (data.size() < 1)
  156. return DecodeError::InvalidInputFormat;
  157. auto unused_bits = data[0];
  158. return BitmapView { const_cast<u8*>(data.offset_pointer(1)), data.size() * 8 - unused_bits };
  159. }
  160. Result<Tag, DecodeError> Decoder::peek()
  161. {
  162. if (m_stack.is_empty())
  163. return DecodeError::NoInput;
  164. if (eof())
  165. return DecodeError::EndOfStream;
  166. if (m_current_tag.has_value())
  167. return m_current_tag.value();
  168. auto tag_or_error = read_tag();
  169. if (tag_or_error.is_error())
  170. return tag_or_error.error();
  171. m_current_tag = tag_or_error.value();
  172. return m_current_tag.value();
  173. }
  174. bool Decoder::eof() const
  175. {
  176. return m_stack.is_empty() || m_stack.last().is_empty();
  177. }
  178. Optional<DecodeError> Decoder::enter()
  179. {
  180. if (m_stack.is_empty())
  181. return DecodeError::NoInput;
  182. auto tag_or_error = peek();
  183. if (tag_or_error.is_error())
  184. return tag_or_error.error();
  185. auto tag = tag_or_error.value();
  186. if (tag.type != Type::Constructed)
  187. return DecodeError::EnteringNonConstructedTag;
  188. auto length_or_error = read_length();
  189. if (length_or_error.is_error())
  190. return length_or_error.error();
  191. auto length = length_or_error.value();
  192. auto data_or_error = read_bytes(length);
  193. if (data_or_error.is_error())
  194. return data_or_error.error();
  195. m_current_tag.clear();
  196. auto data = data_or_error.value();
  197. m_stack.append(data);
  198. return {};
  199. }
  200. Optional<DecodeError> Decoder::leave()
  201. {
  202. if (m_stack.is_empty())
  203. return DecodeError::NoInput;
  204. if (m_stack.size() == 1)
  205. return DecodeError::LeavingMainContext;
  206. m_stack.take_last();
  207. m_current_tag.clear();
  208. return {};
  209. }
  210. }
  211. void AK::Formatter<Crypto::ASN1::DecodeError>::format(FormatBuilder& fmtbuilder, Crypto::ASN1::DecodeError error)
  212. {
  213. using Crypto::ASN1::DecodeError;
  214. switch (error) {
  215. case DecodeError::NoInput:
  216. return Formatter<StringView>::format(fmtbuilder, "DecodeError(No input provided)");
  217. case DecodeError::NonConformingType:
  218. return Formatter<StringView>::format(fmtbuilder, "DecodeError(Tried to read with a non-conforming type)");
  219. case DecodeError::EndOfStream:
  220. return Formatter<StringView>::format(fmtbuilder, "DecodeError(End of stream)");
  221. case DecodeError::NotEnoughData:
  222. return Formatter<StringView>::format(fmtbuilder, "DecodeError(Not enough data)");
  223. case DecodeError::EnteringNonConstructedTag:
  224. return Formatter<StringView>::format(fmtbuilder, "DecodeError(Tried to enter a primitive tag)");
  225. case DecodeError::LeavingMainContext:
  226. return Formatter<StringView>::format(fmtbuilder, "DecodeError(Tried to leave the main context)");
  227. case DecodeError::InvalidInputFormat:
  228. return Formatter<StringView>::format(fmtbuilder, "DecodeError(Input data contained invalid syntax/data)");
  229. case DecodeError::Overflow:
  230. return Formatter<StringView>::format(fmtbuilder, "DecodeError(Construction would overflow)");
  231. case DecodeError::UnsupportedFormat:
  232. return Formatter<StringView>::format(fmtbuilder, "DecodeError(Input data format not supported by this parser)");
  233. default:
  234. return Formatter<StringView>::format(fmtbuilder, "DecodeError(Unknown)");
  235. }
  236. }