OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_CERT_INTERNAL_PATH_BUILDER_H_ | 5 #ifndef NET_CERT_INTERNAL_PATH_BUILDER_H_ |
6 #define NET_CERT_INTERNAL_PATH_BUILDER_H_ | 6 #define NET_CERT_INTERNAL_PATH_BUILDER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
14 #include "net/base/net_errors.h" | |
15 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
| 15 #include "net/cert/internal/cert_errors.h" |
16 #include "net/cert/internal/completion_status.h" | 16 #include "net/cert/internal/completion_status.h" |
17 #include "net/cert/internal/parsed_certificate.h" | 17 #include "net/cert/internal/parsed_certificate.h" |
18 #include "net/cert/internal/trust_store.h" | 18 #include "net/cert/internal/trust_store.h" |
19 #include "net/der/input.h" | 19 #include "net/der/input.h" |
20 #include "net/der/parse_values.h" | 20 #include "net/der/parse_values.h" |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 namespace der { | 24 namespace der { |
25 struct GeneralizedTime; | 25 struct GeneralizedTime; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 // | 57 // |
58 // WARNING: This implementation is currently experimental. Consult an OWNER | 58 // WARNING: This implementation is currently experimental. Consult an OWNER |
59 // before using it. | 59 // before using it. |
60 class NET_EXPORT CertPathBuilder { | 60 class NET_EXPORT CertPathBuilder { |
61 public: | 61 public: |
62 // Represents a single candidate path that was built. | 62 // Represents a single candidate path that was built. |
63 struct NET_EXPORT ResultPath { | 63 struct NET_EXPORT ResultPath { |
64 ResultPath(); | 64 ResultPath(); |
65 ~ResultPath(); | 65 ~ResultPath(); |
66 | 66 |
67 // Returns true if this path was successfully verified. | 67 // The (possibly partial) certificate path. Consumers must always test |
68 bool is_success() const { return error == OK; } | 68 // |valid| before using |path|. When |!valid| path.trust_anchor may be |
69 | 69 // nullptr, and the path may be otherwise incomplete/invalid. |
70 // The (possibly partial) certificate path. In the case of an | |
71 // error path.trust_anchor may be nullptr. | |
72 CertPath path; | 70 CertPath path; |
73 | 71 |
74 // A net error code result of attempting to verify this path. | 72 // The errors/warnings from this path. Note that the list of errors is |
75 // TODO(mattm): may want to have an independent result enum, which caller | 73 // independent of whether the path was |valid| (a valid path may |
76 // can map to a net error if they want. | 74 // contain errors/warnings, and vice versa an invalid path may not have |
77 int error = ERR_UNEXPECTED; | 75 // logged any errors). |
| 76 CertErrors errors; |
| 77 |
| 78 // True if |path| is a correct verified certificate chain. |
| 79 bool valid = false; |
78 }; | 80 }; |
79 | 81 |
80 // Provides the overall result of path building. This includes the paths that | 82 // Provides the overall result of path building. This includes the paths that |
81 // were attempted. | 83 // were attempted. |
82 struct NET_EXPORT Result { | 84 struct NET_EXPORT Result { |
83 Result(); | 85 Result(); |
84 ~Result(); | 86 ~Result(); |
85 | 87 |
86 // Returns true if there was a valid path. | 88 // Returns true if there was a valid path. |
87 bool is_success() const { return error() == OK; } | 89 bool HasValidPath() const; |
88 | 90 |
89 // Returns the net error code of the overall best result. | 91 // Returns the ResultPath for the best valid path, or nullptr if there |
90 int error() const { | 92 // was none. |
91 if (paths.empty()) | 93 const ResultPath* GetBestValidPath() const; |
92 return ERR_CERT_AUTHORITY_INVALID; | |
93 return paths[best_result_index]->error; | |
94 } | |
95 | 94 |
96 // List of paths that were attempted and the result for each. | 95 // List of paths that were attempted and the result for each. |
97 std::vector<std::unique_ptr<ResultPath>> paths; | 96 std::vector<std::unique_ptr<ResultPath>> paths; |
98 | 97 |
99 // Index into |paths|. Before use, |paths.empty()| must be checked. | 98 // Index into |paths|. Before use, |paths.empty()| must be checked. |
100 // NOTE: currently the definition of "best" is fairly limited. Successful is | 99 // NOTE: currently the definition of "best" is fairly limited. Valid is |
101 // better than unsuccessful, but otherwise nothing is guaranteed. | 100 // better than invalid, but otherwise nothing is guaranteed. |
102 size_t best_result_index = 0; | 101 size_t best_result_index = 0; |
103 | 102 |
104 private: | 103 private: |
105 DISALLOW_COPY_AND_ASSIGN(Result); | 104 DISALLOW_COPY_AND_ASSIGN(Result); |
106 }; | 105 }; |
107 | 106 |
108 // TODO(mattm): allow caller specified hook/callback to extend path | 107 // TODO(mattm): allow caller specified hook/callback to extend path |
109 // verification. | 108 // verification. |
110 // | 109 // |
111 // Creates a CertPathBuilder that attempts to find a path from |cert| to a | 110 // Creates a CertPathBuilder that attempts to find a path from |cert| to a |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 STATE_GET_NEXT_PATH, | 154 STATE_GET_NEXT_PATH, |
156 STATE_GET_NEXT_PATH_COMPLETE, | 155 STATE_GET_NEXT_PATH_COMPLETE, |
157 }; | 156 }; |
158 | 157 |
159 CompletionStatus DoLoop(bool allow_async); | 158 CompletionStatus DoLoop(bool allow_async); |
160 | 159 |
161 CompletionStatus DoGetNextPath(bool allow_async); | 160 CompletionStatus DoGetNextPath(bool allow_async); |
162 void HandleGotNextPath(); | 161 void HandleGotNextPath(); |
163 CompletionStatus DoGetNextPathComplete(); | 162 CompletionStatus DoGetNextPathComplete(); |
164 | 163 |
165 void AddResultPath(const CertPath& path, bool is_success); | 164 void AddResultPath(std::unique_ptr<ResultPath> result_path); |
166 | 165 |
167 base::Closure callback_; | 166 base::Closure callback_; |
168 | 167 |
169 std::unique_ptr<CertPathIter> cert_path_iter_; | 168 std::unique_ptr<CertPathIter> cert_path_iter_; |
170 const SignaturePolicy* signature_policy_; | 169 const SignaturePolicy* signature_policy_; |
171 const der::GeneralizedTime time_; | 170 const der::GeneralizedTime time_; |
172 | 171 |
173 // Stores the next complete path to attempt verification on. This is filled in | 172 // Stores the next complete path to attempt verification on. This is filled in |
174 // by |cert_path_iter_| during the STATE_GET_NEXT_PATH step, and thus should | 173 // by |cert_path_iter_| during the STATE_GET_NEXT_PATH step, and thus should |
175 // only be accessed during the STATE_GET_NEXT_PATH_COMPLETE step. | 174 // only be accessed during the STATE_GET_NEXT_PATH_COMPLETE step. |
176 // (Will be empty if all paths have been tried, otherwise will be a candidate | 175 // (Will be empty if all paths have been tried, otherwise will be a candidate |
177 // path starting with the target cert and ending with a | 176 // path starting with the target cert and ending with a |
178 // certificate issued by trust anchor.) | 177 // certificate issued by trust anchor.) |
179 CertPath next_path_; | 178 CertPath next_path_; |
180 State next_state_; | 179 State next_state_; |
181 | 180 |
182 Result* out_result_; | 181 Result* out_result_; |
183 | 182 |
184 DISALLOW_COPY_AND_ASSIGN(CertPathBuilder); | 183 DISALLOW_COPY_AND_ASSIGN(CertPathBuilder); |
185 }; | 184 }; |
186 | 185 |
187 } // namespace net | 186 } // namespace net |
188 | 187 |
189 #endif // NET_CERT_INTERNAL_PATH_BUILDER_H_ | 188 #endif // NET_CERT_INTERNAL_PATH_BUILDER_H_ |
OLD | NEW |