1 // Copyright (c) 2012 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_CERT_X509_CERT_TYPES_H_
6 #define NET_CERT_X509_CERT_TYPES_H_
14 #include "base/logging.h"
15 #include "base/strings/string_piece.h"
16 #include "build/build_config.h"
17 #include "net/base/hash_value.h"
18 #include "net/base/net_export.h"
20 #if defined(OS_MACOSX) && !defined(OS_IOS)
21 #include <Security/x509defs.h>
30 class X509Certificate
;
32 // CertPrincipal represents the issuer or subject field of an X.509 certificate.
33 struct NET_EXPORT CertPrincipal
{
35 explicit CertPrincipal(const std::string
& name
);
38 #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN)
39 // Parses a BER-format DistinguishedName.
40 bool ParseDistinguishedName(const void* ber_name_data
, size_t length
);
43 #if defined(OS_MACOSX)
44 // Compare this CertPrincipal with |against|, returning true if they're
45 // equal enough to be a possible match. This should NOT be used for any
46 // security relevant decisions.
47 // TODO(rsleevi): Remove once Mac client auth uses NSS for name comparison.
48 bool Matches(const CertPrincipal
& against
) const;
51 // Returns a name that can be used to represent the issuer. It tries in this
52 // order: CN, O and OU and returns the first non-empty one found.
53 std::string
GetDisplayName() const;
55 // The different attributes for a principal, stored in UTF-8. They may be "".
56 // Note that some of them can have several values.
58 std::string common_name
;
59 std::string locality_name
;
60 std::string state_or_province_name
;
61 std::string country_name
;
63 std::vector
<std::string
> street_addresses
;
64 std::vector
<std::string
> organization_names
;
65 std::vector
<std::string
> organization_unit_names
;
66 std::vector
<std::string
> domain_components
;
69 // This class is useful for maintaining policies about which certificates are
70 // permitted or forbidden for a particular purpose.
71 class NET_EXPORT CertPolicy
{
73 // The judgments this policy can reach.
75 // We don't have policy information for this certificate.
78 // This certificate is allowed.
81 // This certificate is denied.
88 // Returns the judgment this policy makes about this certificate.
89 Judgment
Check(X509Certificate
* cert
) const;
91 // Causes the policy to allow this certificate.
92 void Allow(X509Certificate
* cert
);
94 // Causes the policy to deny this certificate.
95 void Deny(X509Certificate
* cert
);
97 // Returns true if this policy has allowed at least one certificate.
98 bool HasAllowedCert() const;
100 // Returns true if this policy has denied at least one certificate.
101 bool HasDeniedCert() const;
104 // The set of fingerprints of allowed certificates.
105 std::set
<SHA1HashValue
, SHA1HashValueLessThan
> allowed_
;
107 // The set of fingerprints of denied certificates.
108 std::set
<SHA1HashValue
, SHA1HashValueLessThan
> denied_
;
111 #if defined(OS_MACOSX) && !defined(OS_IOS)
112 // Compares two OIDs by value.
113 inline bool CSSMOIDEqual(const CSSM_OID
* oid1
, const CSSM_OID
* oid2
) {
114 return oid1
->Length
== oid2
->Length
&&
115 (memcmp(oid1
->Data
, oid2
->Data
, oid1
->Length
) == 0);
119 // A list of ASN.1 date/time formats that ParseCertificateDate() supports,
120 // encoded in the canonical forms specified in RFC 2459/3280/5280.
121 enum CertDateFormat
{
122 // UTCTime: Format is YYMMDDHHMMSSZ
123 CERT_DATE_FORMAT_UTC_TIME
,
125 // GeneralizedTime: Format is YYYYMMDDHHMMSSZ
126 CERT_DATE_FORMAT_GENERALIZED_TIME
,
129 // Attempts to parse |raw_date|, an ASN.1 date/time string encoded as
130 // |format|, and writes the result into |*time|. If an invalid date is
131 // specified, or if parsing fails, returns false, and |*time| will not be
133 bool ParseCertificateDate(const base::StringPiece
& raw_date
,
134 CertDateFormat format
,
138 #endif // NET_CERT_X509_CERT_TYPES_H_