parse_values.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_DER_PARSE_VALUES_H_
  5. #define NET_DER_PARSE_VALUES_H_
  6. #include <stdint.h>
  7. #include "net/base/net_export.h"
  8. #include "net/der/input.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. namespace net::der {
  11. // Reads a DER-encoded ASN.1 BOOLEAN value from |in| and puts the resulting
  12. // value in |out|. Returns whether the encoded value could successfully be
  13. // read.
  14. [[nodiscard]] NET_EXPORT bool ParseBool(const Input& in, bool* out);
  15. // Like ParseBool, except it is more relaxed in what inputs it accepts: Any
  16. // value that is a valid BER encoding will be parsed successfully.
  17. [[nodiscard]] NET_EXPORT bool ParseBoolRelaxed(const Input& in, bool* out);
  18. // Checks the validity of a DER-encoded ASN.1 INTEGER value from |in|, and
  19. // determines the sign of the number. Returns true on success and
  20. // fills |negative|. Otherwise returns false and does not modify the out
  21. // parameter.
  22. //
  23. // in: The value portion of an INTEGER.
  24. // negative: Out parameter that is set to true if the number is negative
  25. // and false otherwise (zero is non-negative).
  26. [[nodiscard]] NET_EXPORT bool IsValidInteger(const Input& in, bool* negative);
  27. // Reads a DER-encoded ASN.1 INTEGER value from |in| and puts the resulting
  28. // value in |out|. ASN.1 INTEGERs are arbitrary precision; this function is
  29. // provided as a convenience when the caller knows that the value is unsigned
  30. // and is between 0 and 2^64-1. This function returns false if the value is too
  31. // big to fit in a uint64_t, is negative, or if there is an error reading the
  32. // integer.
  33. [[nodiscard]] NET_EXPORT bool ParseUint64(const Input& in, uint64_t* out);
  34. // Same as ParseUint64() but for a uint8_t.
  35. [[nodiscard]] NET_EXPORT bool ParseUint8(const Input& in, uint8_t* out);
  36. // The BitString class is a helper for representing a valid parsed BIT STRING.
  37. //
  38. // * The bits are ordered within each octet of bytes() from most to least
  39. // significant, as in the DER encoding.
  40. //
  41. // * There may be at most 7 unused bits.
  42. class NET_EXPORT BitString {
  43. public:
  44. BitString() = default;
  45. // |unused_bits| represents the number of bits in the last octet of |bytes|,
  46. // starting from the least significant bit, that are unused. It MUST be < 8.
  47. // And if bytes is empty, then it MUST be 0.
  48. BitString(const Input& bytes, uint8_t unused_bits);
  49. const Input& bytes() const { return bytes_; }
  50. uint8_t unused_bits() const { return unused_bits_; }
  51. // Returns true if the bit string contains 1 at the specified position.
  52. // Otherwise returns false.
  53. //
  54. // A return value of false can mean either:
  55. // * The bit value at |bit_index| is 0.
  56. // * There is no bit at |bit_index| (index is beyond the end).
  57. [[nodiscard]] bool AssertsBit(size_t bit_index) const;
  58. private:
  59. Input bytes_;
  60. uint8_t unused_bits_ = 0;
  61. // Default assignment and copy constructor are OK.
  62. };
  63. // Reads a DER-encoded ASN.1 BIT STRING value from |in| and returns the
  64. // resulting octet string and number of unused bits.
  65. //
  66. // On failure, returns absl::nullopt.
  67. [[nodiscard]] NET_EXPORT absl::optional<BitString> ParseBitString(
  68. const Input& in);
  69. struct NET_EXPORT GeneralizedTime {
  70. uint16_t year;
  71. uint8_t month;
  72. uint8_t day;
  73. uint8_t hours;
  74. uint8_t minutes;
  75. uint8_t seconds;
  76. // Returns true if the value is in UTCTime's range.
  77. bool InUTCTimeRange() const;
  78. };
  79. NET_EXPORT_PRIVATE bool operator<(const GeneralizedTime& lhs,
  80. const GeneralizedTime& rhs);
  81. NET_EXPORT_PRIVATE bool operator<=(const GeneralizedTime& lhs,
  82. const GeneralizedTime& rhs);
  83. NET_EXPORT_PRIVATE bool operator>(const GeneralizedTime& lhs,
  84. const GeneralizedTime& rhs);
  85. NET_EXPORT_PRIVATE bool operator>=(const GeneralizedTime& lhs,
  86. const GeneralizedTime& rhs);
  87. // Reads a DER-encoded ASN.1 UTCTime value from |in| and puts the resulting
  88. // value in |out|, returning true if the UTCTime could be parsed successfully.
  89. [[nodiscard]] NET_EXPORT bool ParseUTCTime(const Input& in,
  90. GeneralizedTime* out);
  91. // Reads a DER-encoded ASN.1 GeneralizedTime value from |in| and puts the
  92. // resulting value in |out|, returning true if the GeneralizedTime could
  93. // be parsed successfully. This function is even more restrictive than the
  94. // DER rules - it follows the rules from RFC5280, which does not allow for
  95. // fractional seconds.
  96. [[nodiscard]] NET_EXPORT bool ParseGeneralizedTime(const Input& in,
  97. GeneralizedTime* out);
  98. // Reads a DER-encoded ASN.1 IA5String value from |in| and stores the result in
  99. // |out| as ASCII, returning true if successful.
  100. [[nodiscard]] NET_EXPORT bool ParseIA5String(Input in, std::string* out);
  101. // Reads a DER-encoded ASN.1 VisibleString value from |in| and stores the result
  102. // in |out| as ASCII, returning true if successful.
  103. [[nodiscard]] NET_EXPORT bool ParseVisibleString(Input in, std::string* out);
  104. // Reads a DER-encoded ASN.1 PrintableString value from |in| and stores the
  105. // result in |out| as ASCII, returning true if successful.
  106. [[nodiscard]] NET_EXPORT bool ParsePrintableString(Input in, std::string* out);
  107. // Reads a DER-encoded ASN.1 TeletexString value from |in|, treating it as
  108. // Latin-1, and stores the result in |out| as UTF-8, returning true if
  109. // successful.
  110. //
  111. // This is for compatibility with legacy implementations that would use Latin-1
  112. // encoding but tag it as TeletexString.
  113. [[nodiscard]] NET_EXPORT bool ParseTeletexStringAsLatin1(Input in,
  114. std::string* out);
  115. // Reads a DER-encoded ASN.1 UniversalString value from |in| and stores the
  116. // result in |out| as UTF-8, returning true if successful.
  117. [[nodiscard]] NET_EXPORT bool ParseUniversalString(Input in, std::string* out);
  118. // Reads a DER-encoded ASN.1 BMPString value from |in| and stores the
  119. // result in |out| as UTF-8, returning true if successful.
  120. [[nodiscard]] NET_EXPORT bool ParseBmpString(Input in, std::string* out);
  121. } // namespace net::der
  122. #endif // NET_DER_PARSE_VALUES_H_