| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/base/rsa_key_pair.h" | 5 #include "remoting/base/rsa_key_pair.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/base64.h" | 14 #include "base/base64.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "crypto/rsa_private_key.h" | 18 #include "crypto/rsa_private_key.h" |
| 18 #include "crypto/signature_creator.h" | 19 #include "crypto/signature_creator.h" |
| 19 #include "net/cert/x509_util.h" | 20 #include "net/cert/x509_util.h" |
| 20 | 21 |
| 21 namespace remoting { | 22 namespace remoting { |
| 22 | 23 |
| 23 RsaKeyPair::RsaKeyPair(scoped_ptr<crypto::RSAPrivateKey> key) | 24 RsaKeyPair::RsaKeyPair(scoped_ptr<crypto::RSAPrivateKey> key) |
| 24 : key_(key.Pass()){ | 25 : key_(std::move(key)){ |
| 25 DCHECK(key_); | 26 DCHECK(key_); |
| 26 } | 27 } |
| 27 | 28 |
| 28 RsaKeyPair::~RsaKeyPair() {} | 29 RsaKeyPair::~RsaKeyPair() {} |
| 29 | 30 |
| 30 // static | 31 // static |
| 31 scoped_refptr<RsaKeyPair> RsaKeyPair::Generate() { | 32 scoped_refptr<RsaKeyPair> RsaKeyPair::Generate() { |
| 32 scoped_ptr<crypto::RSAPrivateKey> key(crypto::RSAPrivateKey::Create(2048)); | 33 scoped_ptr<crypto::RSAPrivateKey> key(crypto::RSAPrivateKey::Create(2048)); |
| 33 if (!key) { | 34 if (!key) { |
| 34 LOG(ERROR) << "Cannot generate private key."; | 35 LOG(ERROR) << "Cannot generate private key."; |
| 35 return NULL; | 36 return NULL; |
| 36 } | 37 } |
| 37 return new RsaKeyPair(key.Pass()); | 38 return new RsaKeyPair(std::move(key)); |
| 38 } | 39 } |
| 39 | 40 |
| 40 // static | 41 // static |
| 41 scoped_refptr<RsaKeyPair> RsaKeyPair::FromString( | 42 scoped_refptr<RsaKeyPair> RsaKeyPair::FromString( |
| 42 const std::string& key_base64) { | 43 const std::string& key_base64) { |
| 43 std::string key_str; | 44 std::string key_str; |
| 44 if (!base::Base64Decode(key_base64, &key_str)) { | 45 if (!base::Base64Decode(key_base64, &key_str)) { |
| 45 LOG(ERROR) << "Failed to decode private key."; | 46 LOG(ERROR) << "Failed to decode private key."; |
| 46 return NULL; | 47 return NULL; |
| 47 } | 48 } |
| 48 | 49 |
| 49 std::vector<uint8_t> key_buf(key_str.begin(), key_str.end()); | 50 std::vector<uint8_t> key_buf(key_str.begin(), key_str.end()); |
| 50 scoped_ptr<crypto::RSAPrivateKey> key( | 51 scoped_ptr<crypto::RSAPrivateKey> key( |
| 51 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf)); | 52 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf)); |
| 52 if (!key) { | 53 if (!key) { |
| 53 LOG(ERROR) << "Invalid private key."; | 54 LOG(ERROR) << "Invalid private key."; |
| 54 return NULL; | 55 return NULL; |
| 55 } | 56 } |
| 56 | 57 |
| 57 return new RsaKeyPair(key.Pass()); | 58 return new RsaKeyPair(std::move(key)); |
| 58 } | 59 } |
| 59 | 60 |
| 60 std::string RsaKeyPair::ToString() const { | 61 std::string RsaKeyPair::ToString() const { |
| 61 // Check that the key initialized. | 62 // Check that the key initialized. |
| 62 DCHECK(key_.get() != NULL); | 63 DCHECK(key_.get() != NULL); |
| 63 | 64 |
| 64 std::vector<uint8_t> key_buf; | 65 std::vector<uint8_t> key_buf; |
| 65 CHECK(key_->ExportPrivateKey(&key_buf)); | 66 CHECK(key_->ExportPrivateKey(&key_buf)); |
| 66 std::string key_str(key_buf.begin(), key_buf.end()); | 67 std::string key_str(key_buf.begin(), key_buf.end()); |
| 67 std::string key_base64; | 68 std::string key_base64; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 net::x509_util::DIGEST_SHA1, | 103 net::x509_util::DIGEST_SHA1, |
| 103 "CN=chromoting", | 104 "CN=chromoting", |
| 104 base::RandInt(1, std::numeric_limits<int>::max()), | 105 base::RandInt(1, std::numeric_limits<int>::max()), |
| 105 base::Time::Now(), | 106 base::Time::Now(), |
| 106 base::Time::Now() + base::TimeDelta::FromDays(1), | 107 base::Time::Now() + base::TimeDelta::FromDays(1), |
| 107 &der_cert); | 108 &der_cert); |
| 108 return der_cert; | 109 return der_cert; |
| 109 } | 110 } |
| 110 | 111 |
| 111 } // namespace remoting | 112 } // namespace remoting |
| OLD | NEW |