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
354
355
356
357
358
359
360
|
// Copyright (c) 2011 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 "net/base/x509_util.h"
#include "net/base/x509_util_nss.h"
#include <cert.h>
#include <cryptohi.h>
#include <pk11pub.h>
#include <prerror.h>
#include <secmod.h>
#include <secport.h>
#include "base/debug/leak_annotations.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "crypto/ec_private_key.h"
#include "crypto/nss_util.h"
#include "crypto/nss_util_internal.h"
#include "crypto/rsa_private_key.h"
#include "crypto/scoped_nss_types.h"
#include "crypto/third_party/nss/chromium-nss.h"
namespace {
class ObCertOIDWrapper {
public:
static ObCertOIDWrapper* GetInstance() {
// Instantiated as a leaky singleton to allow the singleton to be
// constructed on a worker thead that is not joined when a process
// shuts down.
return Singleton<ObCertOIDWrapper,
LeakySingletonTraits<ObCertOIDWrapper> >::get();
}
SECOidTag ob_cert_oid_tag() const {
return ob_cert_oid_tag_;
}
private:
friend struct DefaultSingletonTraits<ObCertOIDWrapper>;
ObCertOIDWrapper();
SECOidTag ob_cert_oid_tag_;
DISALLOW_COPY_AND_ASSIGN(ObCertOIDWrapper);
};
ObCertOIDWrapper::ObCertOIDWrapper(): ob_cert_oid_tag_(SEC_OID_UNKNOWN) {
// 1.3.6.1.4.1.11129.2.1.6
// (iso.org.dod.internet.private.enterprises.google.googleSecurity.
// certificateExtensions.originBoundCertificate)
static const uint8 kObCertOID[] = {
0x2b, 0x06, 0x01, 0x04, 0x01, 0xd6, 0x79, 0x02, 0x01, 0x06
};
SECOidData oid_data;
memset(&oid_data, 0, sizeof(oid_data));
oid_data.oid.data = const_cast<uint8*>(kObCertOID);
oid_data.oid.len = sizeof(kObCertOID);
oid_data.offset = SEC_OID_UNKNOWN;
oid_data.desc = "Origin Bound Certificate";
oid_data.mechanism = CKM_INVALID_MECHANISM;
oid_data.supportedExtension = SUPPORTED_CERT_EXTENSION;
ob_cert_oid_tag_ = SECOID_AddEntry(&oid_data);
if (ob_cert_oid_tag_ == SEC_OID_UNKNOWN)
LOG(ERROR) << "OB_CERT OID tag creation failed";
}
// Creates a Certificate object that may be passed to the SignCertificate
// method to generate an X509 certificate.
// Returns NULL if an error is encountered in the certificate creation
// process.
// Caller responsible for freeing returned certificate object.
CERTCertificate* CreateCertificate(
SECKEYPublicKey* public_key,
const std::string& subject,
uint32 serial_number,
base::Time not_valid_before,
base::Time not_valid_after) {
// Create info about public key.
CERTSubjectPublicKeyInfo* spki =
SECKEY_CreateSubjectPublicKeyInfo(public_key);
if (!spki)
return NULL;
// Create the certificate request.
CERTName* subject_name =
CERT_AsciiToName(const_cast<char*>(subject.c_str()));
CERTCertificateRequest* cert_request =
CERT_CreateCertificateRequest(subject_name, spki, NULL);
SECKEY_DestroySubjectPublicKeyInfo(spki);
if (!cert_request) {
PRErrorCode prerr = PR_GetError();
LOG(ERROR) << "Failed to create certificate request: " << prerr;
CERT_DestroyName(subject_name);
return NULL;
}
CERTValidity* validity = CERT_CreateValidity(
crypto::BaseTimeToPRTime(not_valid_before),
crypto::BaseTimeToPRTime(not_valid_after));
CERTCertificate* cert = CERT_CreateCertificate(serial_number, subject_name,
validity, cert_request);
if (!cert) {
PRErrorCode prerr = PR_GetError();
LOG(ERROR) << "Failed to create certificate: " << prerr;
}
// Cleanup for resources used to generate the cert.
CERT_DestroyName(subject_name);
CERT_DestroyValidity(validity);
CERT_DestroyCertificateRequest(cert_request);
return cert;
}
// Signs a certificate object, with |key| generating a new X509Certificate
// and destroying the passed certificate object (even when NULL is returned).
// The logic of this method references SignCert() in NSS utility certutil:
// http://mxr.mozilla.org/security/ident?i=SignCert.
// Returns true on success or false if an error is encountered in the
// certificate signing process.
bool SignCertificate(
CERTCertificate* cert,
SECKEYPrivateKey* key) {
// |arena| is used to encode the cert.
PLArenaPool* arena = cert->arena;
SECOidTag algo_id = SEC_GetSignatureAlgorithmOidTag(key->keyType,
SEC_OID_SHA1);
if (algo_id == SEC_OID_UNKNOWN)
return false;
SECStatus rv = SECOID_SetAlgorithmID(arena, &cert->signature, algo_id, 0);
if (rv != SECSuccess)
return false;
// Generate a cert of version 3.
*(cert->version.data) = 2;
cert->version.len = 1;
SECItem der;
der.len = 0;
der.data = NULL;
// Use ASN1 DER to encode the cert.
void* encode_result = SEC_ASN1EncodeItem(
arena, &der, cert, SEC_ASN1_GET(CERT_CertificateTemplate));
if (!encode_result)
return false;
// Allocate space to contain the signed cert.
SECItem* result = SECITEM_AllocItem(arena, NULL, 0);
if (!result)
return false;
// Sign the ASN1 encoded cert and save it to |result|.
rv = DerSignData(arena, result, &der, key, algo_id);
if (rv != SECSuccess) {
DLOG(ERROR) << "DerSignData: " << PORT_GetError();
return false;
}
// Save the signed result to the cert.
cert->derCert = *result;
return true;
}
bool CreateOriginBoundCertInternal(
SECKEYPublicKey* public_key,
SECKEYPrivateKey* private_key,
const std::string& origin,
uint32 serial_number,
base::Time not_valid_before,
base::Time not_valid_after,
std::string* der_cert) {
CERTCertificate* cert = CreateCertificate(public_key,
"CN=anonymous.invalid",
serial_number,
not_valid_before,
not_valid_after);
if (!cert)
return false;
// Create opaque handle used to add extensions later.
void* cert_handle;
if ((cert_handle = CERT_StartCertExtensions(cert)) == NULL) {
LOG(ERROR) << "Unable to get opaque handle for adding extensions";
CERT_DestroyCertificate(cert);
return false;
}
// Create SECItem for IA5String encoding.
SECItem origin_string_item = {
siAsciiString,
(unsigned char*)origin.data(),
origin.size()
};
// IA5Encode and arena allocate SECItem
SECItem* asn1_origin_string = SEC_ASN1EncodeItem(
cert->arena, NULL, &origin_string_item,
SEC_ASN1_GET(SEC_IA5StringTemplate));
if (asn1_origin_string == NULL) {
LOG(ERROR) << "Unable to get ASN1 encoding for origin in ob_cert extension";
CERT_DestroyCertificate(cert);
return false;
}
// Add the extension to the opaque handle
if (CERT_AddExtension(cert_handle,
ObCertOIDWrapper::GetInstance()->ob_cert_oid_tag(),
asn1_origin_string,
PR_TRUE, PR_TRUE) != SECSuccess){
LOG(ERROR) << "Unable to add origin bound cert extension to opaque handle";
CERT_DestroyCertificate(cert);
return false;
}
// Copy extension into x509 cert
if (CERT_FinishExtensions(cert_handle) != SECSuccess){
LOG(ERROR) << "Unable to copy extension to X509 cert";
CERT_DestroyCertificate(cert);
return false;
}
if (!SignCertificate(cert, private_key)) {
CERT_DestroyCertificate(cert);
return false;
}
DCHECK(cert->derCert.len);
// XXX copied from X509Certificate::GetDEREncoded
der_cert->clear();
der_cert->append(reinterpret_cast<char*>(cert->derCert.data),
cert->derCert.len);
CERT_DestroyCertificate(cert);
return true;
}
} // namespace
namespace net {
namespace x509_util {
CERTCertificate* CreateSelfSignedCert(
SECKEYPublicKey* public_key,
SECKEYPrivateKey* private_key,
const std::string& subject,
uint32 serial_number,
base::Time not_valid_before,
base::Time not_valid_after) {
CERTCertificate* cert = CreateCertificate(public_key,
subject,
serial_number,
not_valid_before,
not_valid_after);
if (!cert)
return NULL;
if (!SignCertificate(cert, private_key)) {
CERT_DestroyCertificate(cert);
return NULL;
}
return cert;
}
bool CreateOriginBoundCertRSA(
crypto::RSAPrivateKey* key,
const std::string& origin,
uint32 serial_number,
base::Time not_valid_before,
base::Time not_valid_after,
std::string* der_cert) {
DCHECK(key);
SECKEYPublicKey* public_key;
SECKEYPrivateKey* private_key;
#if defined(USE_NSS)
public_key = key->public_key();
private_key = key->key();
#else
crypto::ScopedSECKEYPublicKey scoped_public_key;
crypto::ScopedSECKEYPrivateKey scoped_private_key;
{
// Based on the NSS RSAPrivateKey::CreateFromPrivateKeyInfoWithParams.
// This method currently leaks some memory.
// See http://crbug.com/34742.
ANNOTATE_SCOPED_MEMORY_LEAK;
crypto::EnsureNSSInit();
std::vector<uint8> key_data;
key->ExportPrivateKey(&key_data);
crypto::ScopedPK11Slot slot(crypto::GetPrivateNSSKeySlot());
if (!slot.get())
return NULL;
SECItem der_private_key_info;
der_private_key_info.data = const_cast<unsigned char*>(&key_data[0]);
der_private_key_info.len = key_data.size();
// Allow the private key to be used for key unwrapping, data decryption,
// and signature generation.
const unsigned int key_usage = KU_KEY_ENCIPHERMENT | KU_DATA_ENCIPHERMENT |
KU_DIGITAL_SIGNATURE;
SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey(
slot.get(), &der_private_key_info, NULL, NULL, PR_FALSE, PR_FALSE,
key_usage, &private_key, NULL);
scoped_private_key.reset(private_key);
if (rv != SECSuccess) {
NOTREACHED();
return NULL;
}
public_key = SECKEY_ConvertToPublicKey(private_key);
if (!public_key) {
NOTREACHED();
return NULL;
}
scoped_public_key.reset(public_key);
}
#endif
return CreateOriginBoundCertInternal(public_key,
private_key,
origin,
serial_number,
not_valid_before,
not_valid_after,
der_cert);
}
bool CreateOriginBoundCertEC(
crypto::ECPrivateKey* key,
const std::string& origin,
uint32 serial_number,
base::Time not_valid_before,
base::Time not_valid_after,
std::string* der_cert) {
DCHECK(key);
return CreateOriginBoundCertInternal(key->public_key(),
key->key(),
origin,
serial_number,
not_valid_before,
not_valid_after,
der_cert);
}
} // namespace x509_util
} // namespace net
|