OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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_CERT_INTERNAL_PATH_BUILDER_H_ | |
6 #define NET_CERT_INTERNAL_PATH_BUILDER_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" | |
13 #include "net/base/completion_callback.h" | |
14 #include "net/base/net_errors.h" | |
15 #include "net/base/net_export.h" | |
16 #include "net/cert/internal/completion_status.h" | |
17 #include "net/der/input.h" | |
18 #include "net/der/parse_values.h" | |
19 | |
20 namespace net { | |
21 | |
22 namespace der { | |
23 struct GeneralizedTime; | |
24 } | |
25 | |
26 class CertPathIter; | |
27 class CertIssuerSource; | |
28 class ParsedCertificate; | |
29 class TrustStore; | |
30 class SignaturePolicy; | |
31 using ParsedCertificateList = std::vector<scoped_refptr<ParsedCertificate>>; | |
eroman
2016/07/01 23:49:29
Same here -- I think #include is better than dupli
mattm
2016/07/02 02:21:51
Done.
| |
32 | |
33 // Checks whether a certificate is trusted by building candidate paths to trust | |
34 // anchors and verifying those paths according to RFC 5280. Each instance of | |
35 // CertPathBuilder is used for a single verification. | |
36 // | |
37 // WARNING: This implementation is currently experimental. Consult an OWNER | |
38 // before using it. | |
39 class NET_EXPORT CertPathBuilder { | |
40 public: | |
41 // Represents a single candidate path that was built. | |
42 struct NET_EXPORT ResultPath { | |
43 ResultPath(); | |
44 ~ResultPath(); | |
45 | |
46 // Returns true if this path was successfully verified. | |
47 bool is_success() const { return error == OK; } | |
48 | |
49 // The candidate path, in forward direction. | |
50 // * path[0] is the target certificate. | |
51 // * path[i+1] is a candidate issuer of path[i]. The subject matches | |
52 // path[i]'s issuer, but nothing else is guaranteed unless is_success() is | |
53 // true. | |
54 // * path[N-1] will be a trust anchor if is_success() is true, otherwise | |
55 // it may or may not be a trust anchor. | |
56 ParsedCertificateList path; | |
57 | |
58 // A net error code result of attempting to verify this path. | |
59 // TODO(mattm): may want to have an independent result enum, which caller | |
60 // can map to a net error if they want. | |
61 int error = ERR_UNEXPECTED; | |
62 }; | |
63 | |
64 // Provides the overall result of path building. This includes the paths that | |
65 // were attempted. | |
66 struct NET_EXPORT Result { | |
67 Result(); | |
68 ~Result(); | |
69 | |
70 // Returns true if there was a valid path. | |
71 bool is_success() const { return error() == OK; } | |
72 | |
73 // Returns the net error code of the overall best result. | |
74 int error() const { | |
75 if (paths.empty()) | |
76 return ERR_CERT_AUTHORITY_INVALID; | |
77 return paths[best_result_index]->error; | |
78 } | |
79 | |
80 // List of paths that were attempted and the result for each. | |
81 std::vector<std::unique_ptr<ResultPath>> paths; | |
82 | |
83 // Index into |paths|. Before use, |paths.empty()| must be checked. | |
84 // NOTE: currently the definition of "best" is fairly limited. Successful is | |
85 // better than unsuccessful, but otherwise nothing is guaranteed. | |
86 size_t best_result_index = 0; | |
87 | |
88 private: | |
89 DISALLOW_COPY_AND_ASSIGN(Result); | |
90 }; | |
91 | |
92 // TODO(mattm): allow caller specified hook/callback to extend path | |
93 // verification. | |
94 // | |
95 // Creates a CertPathBuilder that attempts to find a path from |cert| to a | |
96 // trust anchor in |trust_store|, which satisfies |signature_policy| and is | |
97 // valid at |time|. Details of attempted path(s) are stored in |*result|. | |
98 // | |
99 // The caller must keep |trust_store|, |signature_policy|, and |*result| valid | |
100 // for the lifetime of the CertPathBuilder. | |
101 CertPathBuilder(scoped_refptr<ParsedCertificate> cert, | |
102 const TrustStore* trust_store, | |
103 const SignaturePolicy* signature_policy, | |
104 const der::GeneralizedTime& time, | |
105 Result* result); | |
106 ~CertPathBuilder(); | |
107 | |
108 // Adds a CertIssuerSource to provide intermediates for use in path building. | |
109 // Multiple sources may be added. Must not be called after Run is called. | |
110 // The |*cert_issuer_source| must remain valid for the lifetime of the | |
111 // CertPathBuilder. | |
112 // | |
113 // (If no issuer sources are added, the target certificate will only verify if | |
114 // it is a trust anchor or is directly signed by a trust anchor.) | |
115 void AddCertIssuerSource(CertIssuerSource* cert_issuer_source); | |
116 | |
117 // Begins verification of the target certificate. | |
118 // | |
119 // If the return value is SYNC then the verification is complete and the | |
120 // |result| value can be inspected for the status, and |callback| will not be | |
121 // called. | |
122 // If the return value is ASYNC, the |callback| will be called asynchronously | |
123 // once the verification is complete. |result| should not be examined or | |
124 // modified until the |callback| is run. | |
125 // | |
126 // If |callback| is null, verification always completes synchronously, even if | |
127 // it fails to find a valid path and one could have been found asynchronously. | |
128 // | |
129 // The CertPathBuilder may be deleted while an ASYNC verification is pending, | |
130 // in which case the verification is cancelled, |callback| will not be called, | |
131 // and the output Result will be in an undefined state. | |
132 // It is safe to delete the CertPathBuilder during the |callback|. | |
133 // Run must not be called more than once on each CertPathBuilder instance. | |
134 CompletionStatus Run(const base::Closure& callback); | |
135 | |
136 private: | |
137 enum State { | |
138 STATE_NONE, | |
139 STATE_GET_NEXT_PATH, | |
140 STATE_GET_NEXT_PATH_COMPLETE, | |
141 }; | |
142 | |
143 CompletionStatus DoLoop(bool allow_async); | |
144 | |
145 CompletionStatus DoGetNextPath(bool allow_async); | |
146 void HandleGotNextPath(); | |
147 CompletionStatus DoGetNextPathComplete(); | |
148 | |
149 void AddResultPath(const ParsedCertificateList& path, bool is_success); | |
150 | |
151 base::Closure callback_; | |
152 | |
153 std::unique_ptr<CertPathIter> cert_path_iter_; | |
154 const TrustStore* trust_store_; | |
155 const SignaturePolicy* signature_policy_; | |
156 const der::GeneralizedTime time_; | |
157 | |
158 // Stores the next complete path to attempt verification on. This is filled in | |
159 // by |cert_path_iter_| during the STATE_GET_NEXT_PATH step, and thus should | |
160 // only be accessed during the STATE_GET_NEXT_PATH_COMPLETE step. | |
161 // (Will be empty if all paths have been tried, otherwise will be a candidate | |
162 // path starting with the target cert and ending with a trust anchor.) | |
163 ParsedCertificateList next_path_; | |
164 State next_state_; | |
165 | |
166 Result* out_result_; | |
167 | |
168 DISALLOW_COPY_AND_ASSIGN(CertPathBuilder); | |
169 }; | |
170 | |
171 } // namespace net | |
172 | |
173 #endif // NET_CERT_INTERNAL_PATH_BUILDER_H_ | |
OLD | NEW |