OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 #include "content/browser/media/dtls_identity_store.h" | |
6 | |
7 #include <cert.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/location.h" | |
11 #include "base/logging.h" | |
12 #include "base/rand_util.h" | |
13 #include "base/task_runner.h" | |
14 #include "base/threading/worker_pool.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "crypto/ec_private_key.h" | |
17 #include "googleurl/src/gurl.h" | |
18 #include "net/cert/x509_util_nss.h" | |
19 | |
20 namespace content { | |
21 | |
22 namespace { | |
23 | |
24 static void GenerateIdentityWorker( | |
25 const GURL& origin, | |
26 const std::string& identity_name, | |
27 const std::string& common_name, | |
28 const DTLSIdentityStore::OnCompleteCallback& callback) { | |
29 std::string certificate; | |
30 std::vector<uint8> private_key_info; | |
31 | |
32 int serial_number = base::RandInt(0, std::numeric_limits<int>::max()); | |
33 base::Time not_valid_before = base::Time::Now(); | |
34 base::Time not_valid_after = not_valid_before + base::TimeDelta::FromDays(30); | |
35 | |
36 scoped_ptr<crypto::ECPrivateKey> key(crypto::ECPrivateKey::Create()); | |
37 if (!key.get()) { | |
38 DLOG(ERROR) << "Unable to create key pair for client"; | |
39 return; | |
40 } | |
41 | |
42 CERTCertificate* cert = | |
43 net::x509_util::CreateSelfSignedCert(key->public_key(), | |
44 key->key(), | |
45 "CN=" + common_name, | |
46 serial_number, | |
47 not_valid_before, | |
48 not_valid_after); | |
49 if (cert == NULL) { | |
50 DLOG(ERROR) << "Unable to create x509 cert for client"; | |
51 return; | |
52 } | |
53 certificate.append(reinterpret_cast<char*>(cert->derCert.data), | |
54 cert->derCert.len); | |
55 CERT_DestroyCertificate(cert); | |
56 cert = NULL; | |
57 | |
58 if (!key->ExportEncryptedPrivateKey("", 1, &private_key_info)) { | |
59 DLOG(ERROR) << "Unable to export private key"; | |
60 return; | |
61 } | |
62 | |
63 std::string priveta_key(private_key_info.begin(), private_key_info.end()); | |
64 | |
65 DLOG(INFO) << "DTLSIdentityStore: a new identity is gnerated."; | |
66 BrowserThread::PostTask( | |
67 BrowserThread::IO, | |
68 FROM_HERE, | |
69 base::Bind(callback, certificate, priveta_key)); | |
Ami GONE FROM CHROMIUM
2013/06/06 18:28:51
typo: priveta_key
jiayl
2013/06/06 21:00:08
Done.
| |
70 } | |
71 | |
72 } // namespace | |
73 | |
74 DTLSIdentityStore* DTLSIdentityStore::GetInstance() { | |
75 return Singleton<DTLSIdentityStore>::get(); | |
76 } | |
77 | |
78 DTLSIdentityStore::DTLSIdentityStore() | |
79 : task_runner_(base::WorkerPool::GetTaskRunner(true)) { | |
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
81 } | |
82 | |
83 DTLSIdentityStore::DTLSIdentityStore( | |
84 const scoped_refptr<base::TaskRunner>& task_runner) | |
85 : task_runner_(task_runner) { | |
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
87 } | |
88 | |
89 DTLSIdentityStore::~DTLSIdentityStore() {} | |
90 | |
91 void DTLSIdentityStore::GetOrGenerateIdentity( | |
92 const GURL& origin, | |
93 const std::string& identity_name, | |
94 const std::string& common_name, | |
95 const OnCompleteCallback& callback) { | |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
97 | |
98 DLOG(INFO) << "DTLSIdentityStore: start generating identity."; | |
99 // TODO(jiayl): find the cert in the persistent store and generate a new one | |
100 // only when not found. | |
101 task_runner_->PostTask( | |
102 FROM_HERE, | |
103 base::Bind(&GenerateIdentityWorker, | |
104 origin, identity_name, common_name, callback)); | |
105 } | |
106 | |
107 } // namespace content | |
OLD | NEW |