Chromium Code Reviews| 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 { | |
|
jam
2013/06/06 16:22:32
put this in the content namespace as well, so that
jiayl
2013/06/06 17:07:37
Done.
| |
| 21 | |
| 22 static void GenerateIdentityWorker( | |
| 23 const GURL& origin, | |
| 24 const std::string& identity_name, | |
| 25 const std::string& common_name, | |
| 26 const content::DTLSIdentityStore::OnCompleteCallback& callback) { | |
| 27 std::string certificate; | |
| 28 std::vector<uint8> private_key_info; | |
| 29 | |
| 30 int serial_number = base::RandInt(0, std::numeric_limits<int>::max()); | |
| 31 base::Time not_valid_before = base::Time::Now(); | |
| 32 base::Time not_valid_after = not_valid_before + base::TimeDelta::FromDays(30); | |
| 33 | |
| 34 scoped_ptr<crypto::ECPrivateKey> key(crypto::ECPrivateKey::Create()); | |
| 35 if (!key.get()) { | |
| 36 DLOG(ERROR) << "Unable to create key pair for client"; | |
| 37 return; | |
| 38 } | |
| 39 | |
| 40 CERTCertificate* cert = | |
| 41 net::x509_util::CreateSelfSignedCert(key->public_key(), | |
| 42 key->key(), | |
| 43 "CN=" + common_name, | |
| 44 serial_number, | |
| 45 not_valid_before, | |
| 46 not_valid_after); | |
| 47 if (cert == NULL) { | |
| 48 DLOG(ERROR) << "Unable to create x509 cert for client"; | |
| 49 return; | |
| 50 } | |
| 51 certificate.append(reinterpret_cast<char*>(cert->derCert.data), | |
| 52 cert->derCert.len); | |
| 53 CERT_DestroyCertificate(cert); | |
| 54 cert = NULL; | |
| 55 | |
| 56 if (!key->ExportEncryptedPrivateKey("", 1, &private_key_info)) { | |
| 57 DLOG(ERROR) << "Unable to export private key"; | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 std::string priveta_key(private_key_info.begin(), private_key_info.end()); | |
| 62 | |
| 63 LOG(INFO) << "DTLSIdentityStore: a new identity is gnerated."; | |
|
jam
2013/06/06 16:22:32
here and below, does this need to be LOG and not D
jiayl
2013/06/06 17:07:37
Done.
| |
| 64 content::BrowserThread::PostTask( | |
| 65 content::BrowserThread::IO, | |
| 66 FROM_HERE, | |
| 67 base::Bind(callback, certificate, priveta_key)); | |
| 68 } | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 namespace content { | |
| 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 LOG(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 |