1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/cros/certificate_pattern.h"
#include <algorithm>
#include <list>
#include <string>
#include <vector>
#include <cert.h>
#include <pk11pub.h>
#include "base/logging.h"
#include "base/values.h"
#include "net/base/cert_database.h"
#include "net/base/net_errors.h"
#include "net/base/x509_certificate.h"
#include "net/base/x509_cert_types.h"
// To shorten some of those long lines below.
using std::vector;
using std::string;
using std::list;
using std::find;
using base::ListValue;
using base::DictionaryValue;
namespace chromeos {
namespace {
// Keys for converting classes below to/from dictionaries.
const char kCommonNameKey[] = "CommonName";
const char kLocalityKey[] = "Locality";
const char kOrganizationKey[] = "Organization";
const char kOrganizationalUnitKey[] = "OrganizationalUnit";
const char kIssuerCaRefKey[] = "IssuerCARef";
const char kIssuerKey[] = "Issuer";
const char kSubjectKey[] = "Subject";
const char kEnrollmentUriKey[] = "EnrollmentURI";
bool GetAsListOfStrings(const base::Value& value,
std::vector<std::string>* result) {
const base::ListValue* list = NULL;
if (!value.GetAsList(&list))
return false;
result->clear();
result->reserve(list->GetSize());
for (size_t i = 0; i < list->GetSize(); i++) {
std::string item;
if (!list->GetString(i, &item))
return false;
result->push_back(item);
}
return true;
}
ListValue* CreateListFromStrings(const vector<string>& strings) {
ListValue* new_list = new ListValue;
for (vector<string>::const_iterator iter = strings.begin();
iter != strings.end(); ++iter) {
new_list->Append(new StringValue(*iter));
}
return new_list;
}
// Functor to filter out non-matching issuers.
class IssuerFilter {
public:
explicit IssuerFilter(const IssuerSubjectPattern& issuer)
: issuer_(issuer) {}
bool operator()(const scoped_refptr<net::X509Certificate>& cert) const {
return !issuer_.Matches(cert.get()->issuer());
}
private:
const IssuerSubjectPattern& issuer_;
};
// Functor to filter out non-matching subjects.
class SubjectFilter {
public:
explicit SubjectFilter(const IssuerSubjectPattern& subject)
: subject_(subject) {}
bool operator()(const scoped_refptr<net::X509Certificate>& cert) const {
return !subject_.Matches(cert.get()->subject());
}
private:
const IssuerSubjectPattern& subject_;
};
// Functor to filter out certs that don't have private keys, or are invalid.
class PrivateKeyFilter {
public:
explicit PrivateKeyFilter(net::CertDatabase* cert_db) : cert_db_(cert_db) {}
bool operator()(const scoped_refptr<net::X509Certificate>& cert) const {
return cert_db_->CheckUserCert(cert.get()) != net::OK;
}
private:
net::CertDatabase* cert_db_;
};
// Functor to filter out certs that don't have an issuer in the associated
// IssuerCARef list.
class IssuerCaRefFilter {
public:
explicit IssuerCaRefFilter(const vector<string>& issuer_ca_ref_list)
: issuer_ca_ref_list_(issuer_ca_ref_list) {}
bool operator()(const scoped_refptr<net::X509Certificate>& cert) const {
// Find the certificate issuer for each certificate.
// TODO(gspencer): this functionality should be available from
// X509Certificate or CertDatabase.
CERTCertificate* issuer_cert = CERT_FindCertIssuer(
cert.get()->os_cert_handle(), PR_Now(), certUsageAnyCA);
if (issuer_cert && issuer_cert->nickname) {
// Separate the nickname stored in the certificate at the colon, since
// NSS likes to store it as token:nickname.
const char* delimiter =
::strchr(cert.get()->os_cert_handle()->nickname, ':');
if (delimiter) {
delimiter++; // move past the colon.
vector<string>::const_iterator pat_iter = issuer_ca_ref_list_.begin();
while (pat_iter != issuer_ca_ref_list_.end()) {
if (::strcmp(delimiter, pat_iter->c_str()) == 0)
return false;
++pat_iter;
}
}
}
return true;
}
private:
const vector<string>& issuer_ca_ref_list_;
};
} // namespace
////////////////////////////////////////////////////////////////////////////////
// IssuerSubjectPattern
IssuerSubjectPattern::IssuerSubjectPattern(const std::string& common_name,
const std::string& locality,
const std::string& organization,
const std::string& organizational_unit)
: common_name_(common_name),
locality_(locality),
organization_(organization),
organizational_unit_(organizational_unit) { }
IssuerSubjectPattern::IssuerSubjectPattern() {}
IssuerSubjectPattern::~IssuerSubjectPattern() {}
bool IssuerSubjectPattern::Matches(const net::CertPrincipal& principal) const {
if (!common_name_.empty() && common_name_ != principal.common_name)
return false;
if (!locality_.empty() && locality_ != principal.locality_name)
return false;
if (!organization_.empty()) {
if (find(principal.organization_names.begin(),
principal.organization_names.end(), organization_) ==
principal.organization_names.end()) {
return false;
}
}
if (!organizational_unit_.empty()) {
if (find(principal.organization_unit_names.begin(),
principal.organization_unit_names.end(),
organizational_unit_) == principal.organization_unit_names.end()) {
return false;
}
}
return true;
}
bool IssuerSubjectPattern::Empty() const {
return common_name_.empty() &&
locality_.empty() &&
organization_.empty() &&
organizational_unit_.empty();
}
void IssuerSubjectPattern::Clear() {
common_name_.clear();
locality_.clear();
organization_.clear();
organizational_unit_.clear();
}
DictionaryValue* IssuerSubjectPattern::CreateAsDictionary() const {
DictionaryValue* dict = new DictionaryValue;
if (!common_name_.empty())
dict->SetString(kCommonNameKey, common_name_);
if (!locality_.empty())
dict->SetString(kLocalityKey, locality_);
if (!organization_.empty())
dict->SetString(kOrganizationKey, organization_);
if (!organizational_unit_.empty())
dict->SetString(kOrganizationalUnitKey, organizational_unit_);
return dict;
}
bool IssuerSubjectPattern::CopyFromDictionary(const DictionaryValue& dict) {
Clear();
dict.GetString(kCommonNameKey, &common_name_);
dict.GetString(kLocalityKey, &locality_);
dict.GetString(kOrganizationKey, &organization_);
dict.GetString(kOrganizationalUnitKey, &organizational_unit_);
// If the dictionary wasn't empty, but we are, or vice versa, then something
// went wrong.
DCHECK(dict.empty() == Empty());
if (dict.empty() != Empty())
return false;
return true;
}
////////////////////////////////////////////////////////////////////////////////
// CertificatePattern
CertificatePattern::CertificatePattern() {}
CertificatePattern::~CertificatePattern() {}
bool CertificatePattern::Empty() const {
return issuer_ca_ref_list_.empty() &&
issuer_.Empty() &&
subject_.Empty();
}
void CertificatePattern::Clear() {
issuer_ca_ref_list_.clear();
issuer_.Clear();
subject_.Clear();
enrollment_uri_list_.clear();
}
scoped_refptr<net::X509Certificate> CertificatePattern::GetMatch() const {
typedef list<scoped_refptr<net::X509Certificate> > CertificateStlList;
// Start with all the certs, and narrow it down from there.
net::CertificateList all_certs;
CertificateStlList matching_certs;
net::CertDatabase cert_db;
cert_db.ListCerts(&all_certs);
if (all_certs.empty())
return NULL;
for (net::CertificateList::iterator iter = all_certs.begin();
iter != all_certs.end(); ++iter) {
matching_certs.push_back(*iter);
}
// Strip off any certs that don't have the right issuer and/or subject.
if (!issuer_.Empty()) {
matching_certs.remove_if(IssuerFilter(issuer_));
if (matching_certs.empty())
return NULL;
}
if (!subject_.Empty()) {
matching_certs.remove_if(SubjectFilter(subject_));
if (matching_certs.empty())
return NULL;
}
if (!issuer_ca_ref_list_.empty()) {
matching_certs.remove_if(IssuerCaRefFilter(issuer_ca_ref_list_));
if (matching_certs.empty())
return NULL;
}
// Eliminate any certs that don't have private keys associated with
// them. The CheckUserCert call in the filter is a little slow (because of
// underlying PKCS11 calls), so we do this last to reduce the number of times
// we have to call it.
PrivateKeyFilter private_filter(&cert_db);
matching_certs.remove_if(private_filter);
if (matching_certs.empty())
return NULL;
// We now have a list of certificates that match the pattern we're
// looking for. Now we find the one with the latest start date.
scoped_refptr<net::X509Certificate> latest(NULL);
// Iterate over the rest looking for the one that was issued latest.
for (CertificateStlList::iterator iter = matching_certs.begin();
iter != matching_certs.end(); ++iter) {
if (!latest.get() || (*iter)->valid_start() > latest->valid_start())
latest = *iter;
}
return latest;
}
DictionaryValue* CertificatePattern::CreateAsDictionary() const {
DictionaryValue* dict = new base::DictionaryValue;
if (!issuer_ca_ref_list_.empty())
dict->Set(kIssuerCaRefKey, CreateListFromStrings(issuer_ca_ref_list_));
if (!issuer_.Empty())
dict->Set(kIssuerKey, issuer_.CreateAsDictionary());
if (!subject_.Empty())
dict->Set(kSubjectKey, subject_.CreateAsDictionary());
if (!enrollment_uri_list_.empty())
dict->Set(kEnrollmentUriKey, CreateListFromStrings(enrollment_uri_list_));
return dict;
}
bool CertificatePattern::CopyFromDictionary(const DictionaryValue &dict) {
DictionaryValue* child_dict = NULL;
ListValue* child_list = NULL;
Clear();
// All of these are optional.
if (dict.GetList(kIssuerCaRefKey, &child_list) && child_list) {
if (!GetAsListOfStrings(*child_list, &issuer_ca_ref_list_))
return false;
}
if (dict.GetDictionary(kIssuerKey, &child_dict) && child_dict) {
if (!issuer_.CopyFromDictionary(*child_dict))
return false;
}
child_dict = NULL;
if (dict.GetDictionary(kSubjectKey, &child_dict) && child_dict) {
if (!subject_.CopyFromDictionary(*child_dict))
return false;
}
child_list = NULL;
if (dict.GetList(kEnrollmentUriKey, &child_list) && child_list) {
if (!GetAsListOfStrings(*child_list, &enrollment_uri_list_))
return false;
}
// If we didn't copy anything from the dictionary, then it had better be
// empty.
DCHECK(dict.empty() == Empty());
if (dict.empty() != Empty())
return false;
return true;
}
} // namespace chromeos
|