ASN1.cpp 6.6 KB

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