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.
5 #ifndef NET_DER_PARSE_VALUES_H_
6 #define NET_DER_PARSE_VALUES_H_
8 #include "base/compiler_specific.h"
9 #include "net/base/net_export.h"
10 #include "net/der/input.h"
16 // Reads a DER-encoded ASN.1 BOOLEAN value from |in| and puts the resulting
17 // value in |out|. Returns whether the encoded value could successfully be
19 NET_EXPORT
bool ParseBool(const Input
& in
, bool* out
) WARN_UNUSED_RESULT
;
21 // Like ParseBool, except it is more relaxed in what inputs it accepts: Any
22 // value that is a valid BER encoding will be parsed successfully.
23 NET_EXPORT
bool ParseBoolRelaxed(const Input
& in
, bool* out
) WARN_UNUSED_RESULT
;
25 // Reads a DER-encoded ASN.1 INTEGER value from |in| and puts the resulting
26 // value in |out|. ASN.1 INTEGERs are arbitrary precision; this function is
27 // provided as a convenience when the caller knows that the value is unsigned
28 // and is between 0 and 2^63-1. This function does not support the full range of
29 // uint64_t. This function returns false if the value is too big to fit in a
30 // uint64_t, is negative, or if there is an error reading the integer.
31 NET_EXPORT
bool ParseUint64(const Input
& in
, uint64_t* out
) WARN_UNUSED_RESULT
;
33 // The BitString class is a helper for representing a valid parsed BIT STRING.
35 // * The bits are ordered within each octet of bytes() from most to least
36 // significant, as in the DER encoding.
38 // * There may be at most 7 unused bits.
39 class NET_EXPORT BitString
{
41 BitString() : unused_bits_(0) {}
43 // |unused_bits| represents the number of bits in the last octet of |bytes|,
44 // starting from the least significant bit, that are unused. It MUST be < 8.
45 // And if bytes is empty, then it MUST be 0.
46 BitString(const Input
& bytes
, uint8_t unused_bits
);
48 const Input
& bytes() const { return bytes_
; }
49 uint8_t unused_bits() const { return unused_bits_
; }
55 // Default assignment and copy constructor are OK.
58 // Reads a DER-encoded ASN.1 BIT STRING value from |in| and puts the resulting
59 // octet string and number of unused bits into |bit_string|
61 // Returns true on success, otherwise returns false and does not modify the
63 NET_EXPORT
bool ParseBitString(const Input
& in
,
64 BitString
* bit_string
) WARN_UNUSED_RESULT
;
66 struct GeneralizedTime
{
75 NET_EXPORT_PRIVATE
bool operator<(const GeneralizedTime
& lhs
,
76 const GeneralizedTime
& rhs
);
78 // Reads a DER-encoded ASN.1 UTCTime value from |in| and puts the resulting
79 // value in |out|, returning true if the UTCTime could be parsed successfully.
80 NET_EXPORT
bool ParseUTCTime(const Input
& in
,
81 GeneralizedTime
* out
) WARN_UNUSED_RESULT
;
83 // Like ParseUTCTime, but it is more lenient in what is accepted. DER requires
84 // a UTCTime to be in the format YYMMDDhhmmssZ; this function will accept both
85 // that and YYMMDDhhmmZ, which is a valid BER encoding of a UTCTime which
86 // sometimes incorrectly appears in X.509 certificates.
87 NET_EXPORT
bool ParseUTCTimeRelaxed(const Input
& in
,
88 GeneralizedTime
* out
) WARN_UNUSED_RESULT
;
90 // Reads a DER-encoded ASN.1 GeneralizedTime value from |in| and puts the
91 // resulting value in |out|, returning true if the GeneralizedTime could
92 // be parsed sucessfully. This function is even more restrictive than the
93 // DER rules - it follows the rules from RFC5280, which does not allow for
94 // fractional seconds.
95 NET_EXPORT
bool ParseGeneralizedTime(const Input
& in
,
96 GeneralizedTime
* out
) WARN_UNUSED_RESULT
;
102 #endif // NET_DER_PARSE_VALUES_H_