ASN1.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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(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. // RFC5280 section 4.1.2.5.1.
  102. auto full_year = year_in_century.value();
  103. full_year += (full_year < 50) ? 2000 : 1900;
  104. auto full_seconds = seconds.value_or(0);
  105. // FIXME: Handle offsets!
  106. if (offset_hours.has_value() || offset_minutes.has_value())
  107. dbgln("FIXME: Implement UTCTime with offset!");
  108. return Core::DateTime::create(full_year, month.value(), day.value(), hour.value(), minute.value(), full_seconds);
  109. }
  110. Optional<Core::DateTime> parse_generalized_time(StringView time)
  111. {
  112. // YYYYMMDDhh[mm[ss[.fff]]] or YYYYMMDDhh[mm[ss[.fff]]]Z or YYYYMMDDhh[mm[ss[.fff]]](+|-)hhmm
  113. GenericLexer lexer(time);
  114. auto year = lexer.consume(4).to_uint();
  115. auto month = lexer.consume(2).to_uint();
  116. auto day = lexer.consume(2).to_uint();
  117. auto hour = lexer.consume(2).to_uint();
  118. Optional<unsigned> minute, seconds, milliseconds, offset_hours, offset_minutes;
  119. [[maybe_unused]] bool negative_offset = false;
  120. if (!lexer.is_eof()) {
  121. if (lexer.consume_specific('Z'))
  122. goto done_parsing;
  123. if (!lexer.next_is(is_any_of("+-"))) {
  124. minute = lexer.consume(2).to_uint();
  125. if (!minute.has_value()) {
  126. return {};
  127. }
  128. if (lexer.consume_specific('Z'))
  129. goto done_parsing;
  130. }
  131. if (!lexer.next_is(is_any_of("+-"))) {
  132. seconds = lexer.consume(2).to_uint();
  133. if (!seconds.has_value()) {
  134. return {};
  135. }
  136. if (lexer.consume_specific('Z'))
  137. goto done_parsing;
  138. }
  139. if (lexer.consume_specific('.')) {
  140. milliseconds = lexer.consume(3).to_uint();
  141. if (!milliseconds.has_value()) {
  142. return {};
  143. }
  144. if (lexer.consume_specific('Z'))
  145. goto done_parsing;
  146. }
  147. if (lexer.next_is(is_any_of("+-"))) {
  148. negative_offset = lexer.consume() == '-';
  149. offset_hours = lexer.consume(2).to_uint();
  150. offset_minutes = lexer.consume(2).to_uint();
  151. if (!offset_hours.has_value() || !offset_minutes.has_value()) {
  152. return {};
  153. }
  154. } else {
  155. lexer.consume();
  156. }
  157. }
  158. done_parsing:;
  159. if (!year.has_value() || !month.has_value() || !day.has_value() || !hour.has_value()) {
  160. return {};
  161. }
  162. // FIXME: Handle offsets!
  163. if (offset_hours.has_value() || offset_minutes.has_value())
  164. dbgln("FIXME: Implement GeneralizedTime with offset!");
  165. // Unceremoniously drop the milliseconds on the floor.
  166. return Core::DateTime::create(year.value(), month.value(), day.value(), hour.value(), minute.value_or(0), seconds.value_or(0));
  167. }
  168. }