forked from mozkeeler/ev-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathev-checker.cpp
317 lines (298 loc) · 9.84 KB
/
ev-checker.cpp
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include "EVCheckerTrustDomain.h"
#include "Util.h"
#include "nss.h"
#include "hasht.h"
#include "pk11pub.h"
#include "plbase64.h"
#include "plgetopt.h"
#include "prerror.h"
#include "secerr.h"
void
PrintUsage(const char* argv0)
{
std::cerr << "Usage: " << argv0 << " <-c certificate list file (PEM format)>";
std::cerr << " <-o dotted EV policy OID> <-h hostname>";
std::cerr << " [-d EV policy description]";
std::cerr << std::endl << std::endl;
std::cerr << "(the certificate list is expected to have the end-entity ";
std::cerr << "certificate first, followed by one or more intermediates, ";
std::cerr << "followed by the root certificate)" << std::endl;
std::cerr << "If -d is specified (with an EV policy description), then ";
std::cerr << argv0 << " will print out the information necessary to enable ";
std::cerr << "the given root for EV treatment in Firefox. Otherwise, ";
std::cerr << argv0 << " will simply print out 'Success!' or an error string ";
std::cerr << "describing an encountered failure." << std::endl;
}
inline void
SECITEM_FreeItem_true(SECItem* item)
{
SECITEM_FreeItem(item, true);
}
typedef mozilla::pkix::ScopedPtr<SECItem, SECITEM_FreeItem_true> ScopedSECItem;
CERTCertificate*
DecodeBase64Cert(const std::string& base64)
{
size_t derLen = (base64.length() * 3) / 4;
if (base64.length() < 2) {
return nullptr;
}
if (base64[base64.length() - 1] == '=') {
derLen--;
}
if (base64[base64.length() - 2] == '=') {
derLen--;
}
ScopedSECItem der(SECITEM_AllocItem(nullptr, nullptr, derLen));
if (!der) {
PrintPRError("SECITEM_AllocItem failed");
return nullptr;
}
if (!PL_Base64Decode(base64.data(), base64.length(),
reinterpret_cast<char*>(der->data))) {
PrintPRError("PL_Base64Decode failed");
return nullptr;
}
CERTCertificate* cert = CERT_NewTempCertificate(CERT_GetDefaultCertDB(),
der.get(), nullptr, false,
true);
if (!cert) {
PrintPRError("CERT_NewTempCertificate failed");
return nullptr;
}
return cert;
}
static const char PEM_HEADER[] = "-----BEGIN CERTIFICATE-----";
static const char PEM_FOOTER[] = "-----END CERTIFICATE-----";
CERTCertList*
ReadCertsFromFile(const char* filename)
{
CERTCertList* certs = CERT_NewCertList();
if (!certs) {
PrintPRError("CERT_NewCertList failed");
return nullptr;
}
std::string currentPem;
bool readingCertificate = false;
std::ifstream file(filename);
while (!file.eof()) {
std::string line;
std::getline(file, line);
// getline appears to strip off '\n' but not '\r'
// (maybe it's platform-dependent?)
if (line.length() > 0 && line.back() == '\r') {
line.pop_back();
}
if (line.compare(PEM_FOOTER) == 0) {
readingCertificate = false;
CERTCertificate* cert = DecodeBase64Cert(currentPem);
if (cert) {
if (CERT_AddCertToListTail(certs, cert) != SECSuccess) {
PrintPRError("CERT_AddCertToListTail failed");
}
}
currentPem.clear();
}
if (readingCertificate) {
currentPem += line;
}
if (line.compare(PEM_HEADER) == 0) {
readingCertificate = true;
}
}
return certs;
}
typedef uint8_t SHA256Buffer[SHA256_LENGTH];
SECStatus
HashBytes(SHA256Buffer& output, const SECItem& data)
{
if (PK11_HashBuf(SEC_OID_SHA256, output, data.data, data.len)
!= SECSuccess) {
PrintPRError("PK11_HashBuf failed");
return SECFailure;
}
return SECSuccess;
}
std::ostream&
HexPrint(std::ostream& output)
{
output.fill('0');
return output << "0x" << std::hex << std::uppercase << std::setw(2);
}
void
PrintSHA256HashOf(const SECItem& data)
{
SHA256Buffer hash;
if (HashBytes(hash, data) != SECSuccess) {
return;
}
// The format is:
// '{ <11 hex bytes>
// <11 hex bytes>
// <10 hex bytes> },'
std::cout << "{ ";
for (size_t i = 0; i < 11; i++) {
uint32_t val = hash[i];
std::cout << HexPrint << val << ", ";
}
std::cout << std::endl << " ";
for (size_t i = 11; i < 22; i++) {
uint32_t val = hash[i];
std::cout << HexPrint << val << ", ";
}
std::cout << std::endl << " ";
for (size_t i = 22; i < 31; i++) {
uint32_t val = hash[i];
std::cout << HexPrint << val << ", ";
}
uint32_t val = hash[31];
std::cout << HexPrint << val << " }," << std::endl;
}
void
PrintBase64Of(const SECItem& data)
{
std::string base64(PL_Base64Encode(reinterpret_cast<const char*>(data.data),
data.len, nullptr));
// The format is:
// '"<base64>"
// "<base64>",'
// where each line is limited to 64 characters of base64 data.
size_t lines = base64.length() / 64;
for (size_t line = 0; line < lines; line++) {
std::cout << "\"" << base64.substr(64 * line, 64) << "\"" << std::endl;
}
size_t remainder = base64.length() % 64;
std::cout << "\"" << base64.substr(base64.length() - remainder) << "\"," << std::endl;
}
typedef mozilla::pkix::ScopedPtr<PLOptState, PL_DestroyOptState>
ScopedPLOptState;
int main(int argc, char* argv[]) {
if (argc < 7) {
PrintUsage(argv[0]);
return 1;
}
if (NSS_NoDB_Init(nullptr) != SECSuccess) {
PrintPRError("NSS_NoDB_Init failed");
}
const char* certsFileName = nullptr;
const char* dottedOID = nullptr;
const char* oidDescription = nullptr;
const char* hostname = nullptr;
ScopedPLOptState opt(PL_CreateOptState(argc, argv, "c:o:d:h:"));
PLOptStatus os;
while ((os = PL_GetNextOpt(opt.get())) != PL_OPT_EOL) {
if (os == PL_OPT_BAD) {
continue;
}
switch (opt->option) {
case 'c':
certsFileName = opt->value;
break;
case 'o':
dottedOID = opt->value;
break;
case 'd':
oidDescription = opt->value;
break;
case 'h':
hostname = opt->value;
break;
default:
PrintUsage(argv[0]);
return 1;
}
}
if (!certsFileName || !dottedOID || !hostname) {
PrintUsage(argv[0]);
return 1;
}
mozilla::pkix::RegisterErrorTable();
ScopedCERTCertList certs(ReadCertsFromFile(certsFileName));
if (CERT_LIST_END(CERT_LIST_HEAD(certs), certs)) {
std::cerr << "Couldn't read certificates from '" << certsFileName << "'";
std::cerr << std::endl;
return 1;
}
CERTCertificate* root = CERT_LIST_TAIL(certs.get())->cert;
CERTCertificate* ee = CERT_LIST_HEAD(certs.get())->cert;
if (oidDescription) {
std::cout << "// " << root->subjectName << std::endl;
std::cout << "\"" << dottedOID << "\"," << std::endl;
std::cout << "\"" << oidDescription << "\"," << std::endl;
PrintSHA256HashOf(root->derCert);
PrintBase64Of(root->derIssuer);
PrintBase64Of(root->serialNumber);
}
EVCheckerTrustDomain trustDomain(CERT_DupCertificate(root));
if (trustDomain.Init(dottedOID, oidDescription) != SECSuccess) {
return 1;
}
ScopedCERTCertList results;
mozilla::pkix::CertPolicyId evPolicy;
if (trustDomain.GetFirstEVPolicyForCert(ee, evPolicy)
!= SECSuccess) {
PrintPRError("GetFirstEVPolicyForCert failed");
std::cerr << "This may mean that the specified EV Policy OID was not ";
std::cerr << "found in the end-entity certificate." << std::endl;
return 1;
}
mozilla::pkix::Input eeInput;
mozilla::pkix::Result rv = eeInput.Init(ee->derCert.data, ee->derCert.len);
if (rv != mozilla::pkix::Success) {
std::cerr << "Couldn't initialize Input from ee cert" << std::endl;
return 1;
}
rv = BuildCertChain(trustDomain, eeInput, mozilla::pkix::Now(),
mozilla::pkix::EndEntityOrCA::MustBeEndEntity,
mozilla::pkix::KeyUsage::noParticularKeyUsageRequired,
mozilla::pkix::KeyPurposeId::anyExtendedKeyUsage,
evPolicy, nullptr);
if (rv != mozilla::pkix::Success) {
PR_SetError(mozilla::pkix::MapResultToPRErrorCode(rv), 0);
PrintPRError("BuildCertChain failed");
PrintPRErrorString();
if (rv == mozilla::pkix::Result::ERROR_POLICY_VALIDATION_FAILED) {
std::cerr << "It appears be the case that the end-entity certificate ";
std::cerr << "was issued directly by the root. There should be at ";
std::cerr << "least one intermediate in the certificate issuance chain.";
std::cerr << std::endl;
} else if (rv == mozilla::pkix::Result::ERROR_CERT_BAD_ACCESS_LOCATION) {
std::cerr << "It appears to be the case that a certificate in the ";
std::cerr << "issuance chain has a malformed or missing OCSP AIA URI";
std::cerr << std::endl;
}
return 1;
}
mozilla::pkix::Input hostnameInput;
rv = hostnameInput.Init(reinterpret_cast<const uint8_t*>(hostname),
strlen(hostname));
if (rv != mozilla::pkix::Success) {
PrintPRError("Couldn't initialize Input from hostname");
return 1;
}
rv = CheckCertHostname(eeInput, hostnameInput);
if (rv != mozilla::pkix::Success) {
PR_SetError(mozilla::pkix::MapResultToPRErrorCode(rv), 0);
PrintPRError("CheckCertHostname failed");
PrintPRErrorString();
if (rv == mozilla::pkix::Result::ERROR_BAD_CERT_DOMAIN) {
std::cerr << "It appears that the end-entity certificate is not valid ";
std::cerr << "for the domain it is hosted at.";
std::cerr << std::endl;
} else if (rv == mozilla::pkix::Result::ERROR_BAD_DER) {
std::cerr << "It appears that the name information in the end-entity ";
std::cerr << "certificate does not conform to RFC 822, RFC 5280, or ";
std::cerr << "RFC 6125.";
std::cerr << std::endl;
}
return 1;
}
std::cout << "Success!" << std::endl;
return 0;
}