OLD | NEW |
| (Empty) |
1 // Copyright (c) 2007-2010 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 | |
5 #ifndef NET_BASE_X509_TYPES_H_ | |
6 #define NET_BASE_X509_TYPES_H_ | |
7 | |
8 #include <string.h> | |
9 | |
10 #include <iostream> | |
11 #include <map> | |
12 #include <set> | |
13 #include <string> | |
14 #include <vector> | |
15 | |
16 #include "base/ref_counted.h" | |
17 #include "base/singleton.h" | |
18 #include "base/time.h" | |
19 #include "testing/gtest/include/gtest/gtest_prod.h" | |
20 | |
21 #if defined(OS_WIN) | |
22 #include <windows.h> | |
23 #include <wincrypt.h> | |
24 #elif defined(OS_MACOSX) | |
25 #include <Security/x509defs.h> | |
26 #elif defined(USE_NSS) | |
27 // Forward declaration; real one in <cert.h> | |
28 struct CERTCertificateStr; | |
29 #endif | |
30 | |
31 namespace net { | |
32 | |
33 class X509Certificate; | |
34 | |
35 // SHA-1 fingerprint (160 bits) of a certificate. | |
36 struct SHA1Fingerprint { | |
37 bool Equals(const SHA1Fingerprint& other) const { | |
38 return memcmp(data, other.data, sizeof(data)) == 0; | |
39 } | |
40 | |
41 unsigned char data[20]; | |
42 }; | |
43 | |
44 class SHA1FingerprintLessThan | |
45 : public std::binary_function<SHA1Fingerprint, SHA1Fingerprint, bool> { | |
46 public: | |
47 bool operator() (const SHA1Fingerprint& lhs, const SHA1Fingerprint& rhs) const
; | |
48 }; | |
49 | |
50 // CertPrincipal represents the issuer or subject field of an X.509 certificate. | |
51 struct CertPrincipal { | |
52 CertPrincipal() { } | |
53 explicit CertPrincipal(const std::string& name) : common_name(name) { } | |
54 | |
55 // Parses a BER-format DistinguishedName. | |
56 bool ParseDistinguishedName(const void* ber_name_data, size_t length); | |
57 | |
58 #if defined(OS_MACOSX) | |
59 // Parses a CSSM_X509_NAME struct. | |
60 void Parse(const CSSM_X509_NAME* name); | |
61 #endif | |
62 | |
63 // Returns true if all attributes of the two objects match, | |
64 // where "match" is defined in RFC 5280 sec. 7.1. | |
65 bool Matches(const CertPrincipal& against) const; | |
66 | |
67 // The different attributes for a principal. They may be "". | |
68 // Note that some of them can have several values. | |
69 | |
70 std::string common_name; | |
71 std::string locality_name; | |
72 std::string state_or_province_name; | |
73 std::string country_name; | |
74 | |
75 std::vector<std::string> street_addresses; | |
76 std::vector<std::string> organization_names; | |
77 std::vector<std::string> organization_unit_names; | |
78 std::vector<std::string> domain_components; | |
79 }; | |
80 | |
81 // Writes a human-readable description of a CertPrincipal, for debugging. | |
82 std::ostream& operator<<(std::ostream& s, const CertPrincipal& p); | |
83 | |
84 // This class is useful for maintaining policies about which certificates are | |
85 // permitted or forbidden for a particular purpose. | |
86 class CertPolicy { | |
87 public: | |
88 // The judgments this policy can reach. | |
89 enum Judgment { | |
90 // We don't have policy information for this certificate. | |
91 UNKNOWN, | |
92 | |
93 // This certificate is allowed. | |
94 ALLOWED, | |
95 | |
96 // This certificate is denied. | |
97 DENIED, | |
98 }; | |
99 | |
100 // Returns the judgment this policy makes about this certificate. | |
101 Judgment Check(X509Certificate* cert) const; | |
102 | |
103 // Causes the policy to allow this certificate. | |
104 void Allow(X509Certificate* cert); | |
105 | |
106 // Causes the policy to deny this certificate. | |
107 void Deny(X509Certificate* cert); | |
108 | |
109 // Returns true if this policy has allowed at least one certificate. | |
110 bool HasAllowedCert() const; | |
111 | |
112 // Returns true if this policy has denied at least one certificate. | |
113 bool HasDeniedCert() const; | |
114 | |
115 private: | |
116 // The set of fingerprints of allowed certificates. | |
117 std::set<SHA1Fingerprint, SHA1FingerprintLessThan> allowed_; | |
118 | |
119 // The set of fingerprints of denied certificates. | |
120 std::set<SHA1Fingerprint, SHA1FingerprintLessThan> denied_; | |
121 }; | |
122 | |
123 #if defined(OS_MACOSX) | |
124 // Compares two OIDs by value. | |
125 inline bool CSSMOIDEqual(const CSSM_OID* oid1, const CSSM_OID* oid2) { | |
126 return oid1->Length == oid2->Length && | |
127 (memcmp(oid1->Data, oid2->Data, oid1->Length) == 0); | |
128 } | |
129 #endif | |
130 | |
131 } // namespace net | |
132 | |
133 #endif // NET_BASE_X509_TYPES_H_ | |
OLD | NEW |