ASN1.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/GenericLexer.h>
  7. #include <LibCrypto/ASN1/ASN1.h>
  8. namespace Crypto::ASN1 {
  9. String kind_name(Kind kind)
  10. {
  11. switch (kind) {
  12. case Kind::Eol:
  13. return "EndOfList";
  14. case Kind::Boolean:
  15. return "Boolean";
  16. case Kind::Integer:
  17. return "Integer";
  18. case Kind::BitString:
  19. return "BitString";
  20. case Kind::OctetString:
  21. return "OctetString";
  22. case Kind::Null:
  23. return "Null";
  24. case Kind::ObjectIdentifier:
  25. return "ObjectIdentifier";
  26. case Kind::IA5String:
  27. return "IA5String";
  28. case Kind::PrintableString:
  29. return "PrintableString";
  30. case Kind::Utf8String:
  31. return "UTF8String";
  32. case Kind::UTCTime:
  33. return "UTCTime";
  34. case Kind::GeneralizedTime:
  35. return "GeneralizedTime";
  36. case Kind::Sequence:
  37. return "Sequence";
  38. case Kind::Set:
  39. return "Set";
  40. }
  41. return "InvalidKind";
  42. }
  43. String class_name(Class class_)
  44. {
  45. switch (class_) {
  46. case Class::Application:
  47. return "Application";
  48. case Class::Context:
  49. return "Context";
  50. case Class::Private:
  51. return "Private";
  52. case Class::Universal:
  53. return "Universal";
  54. }
  55. return "InvalidClass";
  56. }
  57. String type_name(Type type)
  58. {
  59. switch (type) {
  60. case Type::Constructed:
  61. return "Constructed";
  62. case Type::Primitive:
  63. return "Primitive";
  64. }
  65. return "InvalidType";
  66. }
  67. Optional<Core::DateTime> parse_utc_time(const StringView& time)
  68. {
  69. // YYMMDDhhmm[ss]Z or YYMMDDhhmm[ss](+|-)hhmm
  70. GenericLexer lexer(time);
  71. auto year_in_century = lexer.consume(2).to_uint();
  72. auto month = lexer.consume(2).to_uint();
  73. auto day = lexer.consume(2).to_uint();
  74. auto hour = lexer.consume(2).to_uint();
  75. auto minute = lexer.consume(2).to_uint();
  76. Optional<unsigned> seconds, offset_hours, offset_minutes;
  77. [[maybe_unused]] bool negative_offset = false;
  78. if (!lexer.next_is('Z')) {
  79. if (!lexer.next_is(is_any_of("+-"))) {
  80. seconds = lexer.consume(2).to_uint();
  81. if (!seconds.has_value()) {
  82. return {};
  83. }
  84. }
  85. if (lexer.next_is(is_any_of("+-"))) {
  86. negative_offset = lexer.consume() == '-';
  87. offset_hours = lexer.consume(2).to_uint();
  88. offset_minutes = lexer.consume(2).to_uint();
  89. if (!offset_hours.has_value() || !offset_minutes.has_value()) {
  90. return {};
  91. }
  92. } else {
  93. lexer.consume();
  94. }
  95. } else {
  96. lexer.consume();
  97. }
  98. if (!year_in_century.has_value() || !month.has_value() || !day.has_value() || !hour.has_value() || !minute.has_value()) {
  99. return {};
  100. }
  101. auto full_year = (Core::DateTime::now().year() / 100) * 100 + year_in_century.value();
  102. auto full_seconds = seconds.value_or(0);
  103. // FIXME: Handle offsets!
  104. if (offset_hours.has_value() || offset_minutes.has_value())
  105. dbgln("FIXME: Implement UTCTime with offset!");
  106. return Core::DateTime::create(full_year, month.value(), day.value(), hour.value(), minute.value(), full_seconds);
  107. }
  108. Optional<Core::DateTime> parse_generalized_time(const StringView& time)
  109. {
  110. // YYYYMMDDhh[mm[ss[.fff]]] or YYYYMMDDhh[mm[ss[.fff]]]Z or YYYYMMDDhh[mm[ss[.fff]]](+|-)hhmm
  111. GenericLexer lexer(time);
  112. auto year = lexer.consume(4).to_uint();
  113. auto month = lexer.consume(2).to_uint();
  114. auto day = lexer.consume(2).to_uint();
  115. auto hour = lexer.consume(2).to_uint();
  116. Optional<unsigned> minute, seconds, milliseconds, offset_hours, offset_minutes;
  117. [[maybe_unused]] bool negative_offset = false;
  118. if (!lexer.is_eof()) {
  119. if (lexer.consume_specific('Z'))
  120. goto done_parsing;
  121. if (!lexer.next_is(is_any_of("+-"))) {
  122. minute = lexer.consume(2).to_uint();
  123. if (!minute.has_value()) {
  124. return {};
  125. }
  126. if (lexer.consume_specific('Z'))
  127. goto done_parsing;
  128. }
  129. if (!lexer.next_is(is_any_of("+-"))) {
  130. seconds = lexer.consume(2).to_uint();
  131. if (!seconds.has_value()) {
  132. return {};
  133. }
  134. if (lexer.consume_specific('Z'))
  135. goto done_parsing;
  136. }
  137. if (lexer.consume_specific('.')) {
  138. milliseconds = lexer.consume(3).to_uint();
  139. if (!milliseconds.has_value()) {
  140. return {};
  141. }
  142. if (lexer.consume_specific('Z'))
  143. goto done_parsing;
  144. }
  145. if (lexer.next_is(is_any_of("+-"))) {
  146. negative_offset = lexer.consume() == '-';
  147. offset_hours = lexer.consume(2).to_uint();
  148. offset_minutes = lexer.consume(2).to_uint();
  149. if (!offset_hours.has_value() || !offset_minutes.has_value()) {
  150. return {};
  151. }
  152. } else {
  153. lexer.consume();
  154. }
  155. }
  156. done_parsing:;
  157. if (!year.has_value() || !month.has_value() || !day.has_value() || !hour.has_value()) {
  158. return {};
  159. }
  160. // FIXME: Handle offsets!
  161. if (offset_hours.has_value() || offset_minutes.has_value())
  162. dbgln("FIXME: Implement GeneralizedTime with offset!");
  163. // Unceremonially drop the milliseconds on the floor.
  164. return Core::DateTime::create(year.value(), month.value(), day.value(), hour.value(), minute.value_or(0), seconds.value_or(0));
  165. }
  166. }