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 // Checks the validity of a DER-encoded ASN.1 INTEGER value from |in|, and
26 // determines the sign of the number. Returns true on success and
27 // fills |negative|. Otherwise returns false and does not modify the out
30 // in: The value portion of an INTEGER.
31 // negative: Out parameter that is set to true if the number is negative
32 // and false otherwise (zero is non-negative).
33 NET_EXPORT
bool IsValidInteger(const Input
& in
,
34 bool* negative
) WARN_UNUSED_RESULT
;
36 // Reads a DER-encoded ASN.1 INTEGER value from |in| and puts the resulting
37 // value in |out|. ASN.1 INTEGERs are arbitrary precision; this function is
38 // provided as a convenience when the caller knows that the value is unsigned
39 // and is between 0 and 2^64-1. This function returns false if the value is too
40 // big to fit in a uint64_t, is negative, or if there is an error reading the
42 NET_EXPORT
bool ParseUint64(const Input
& in
, uint64_t* out
) WARN_UNUSED_RESULT
;
44 // The BitString class is a helper for representing a valid parsed BIT STRING.
46 // * The bits are ordered within each octet of bytes() from most to least
47 // significant, as in the DER encoding.
49 // * There may be at most 7 unused bits.
50 class NET_EXPORT BitString
{
52 BitString() : unused_bits_(0) {}
54 // |unused_bits| represents the number of bits in the last octet of |bytes|,
55 // starting from the least significant bit, that are unused. It MUST be < 8.
56 // And if bytes is empty, then it MUST be 0.
57 BitString(const Input
& bytes
, uint8_t unused_bits
);
59 const Input
& bytes() const { return bytes_
; }
60 uint8_t unused_bits() const { return unused_bits_
; }
66 // Default assignment and copy constructor are OK.
69 // Reads a DER-encoded ASN.1 BIT STRING value from |in| and puts the resulting
70 // octet string and number of unused bits into |bit_string|
72 // Returns true on success, otherwise returns false and does not modify the
74 NET_EXPORT
bool ParseBitString(const Input
& in
,
75 BitString
* bit_string
) WARN_UNUSED_RESULT
;
77 struct GeneralizedTime
{
86 NET_EXPORT_PRIVATE
bool operator<(const GeneralizedTime
& lhs
,
87 const GeneralizedTime
& rhs
);
89 // Reads a DER-encoded ASN.1 UTCTime value from |in| and puts the resulting
90 // value in |out|, returning true if the UTCTime could be parsed successfully.
91 NET_EXPORT
bool ParseUTCTime(const Input
& in
,
92 GeneralizedTime
* out
) WARN_UNUSED_RESULT
;
94 // Like ParseUTCTime, but it is more lenient in what is accepted. DER requires
95 // a UTCTime to be in the format YYMMDDhhmmssZ; this function will accept both
96 // that and YYMMDDhhmmZ, which is a valid BER encoding of a UTCTime which
97 // sometimes incorrectly appears in X.509 certificates.
98 NET_EXPORT
bool ParseUTCTimeRelaxed(const Input
& in
,
99 GeneralizedTime
* out
) WARN_UNUSED_RESULT
;
101 // Reads a DER-encoded ASN.1 GeneralizedTime value from |in| and puts the
102 // resulting value in |out|, returning true if the GeneralizedTime could
103 // be parsed sucessfully. This function is even more restrictive than the
104 // DER rules - it follows the rules from RFC5280, which does not allow for
105 // fractional seconds.
106 NET_EXPORT
bool ParseGeneralizedTime(const Input
& in
,
107 GeneralizedTime
* out
) WARN_UNUSED_RESULT
;
113 #endif // NET_DER_PARSE_VALUES_H_