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