| 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 <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/base64.h" | 14 #include "base/base64.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "crypto/rsa_private_key.h" | 18 #include "crypto/rsa_private_key.h" |
| 19 #include "crypto/signature_creator.h" | 19 #include "crypto/signature_creator.h" |
| 20 #include "net/cert/x509_util.h" | 20 #include "net/cert/x509_util.h" |
| 21 | 21 |
| 22 namespace remoting { | 22 namespace remoting { |
| 23 | 23 |
| 24 RsaKeyPair::RsaKeyPair(scoped_ptr<crypto::RSAPrivateKey> key) | 24 RsaKeyPair::RsaKeyPair(std::unique_ptr<crypto::RSAPrivateKey> key) |
| 25 : key_(std::move(key)){ | 25 : key_(std::move(key)) { |
| 26 DCHECK(key_); | 26 DCHECK(key_); |
| 27 } | 27 } |
| 28 | 28 |
| 29 RsaKeyPair::~RsaKeyPair() {} | 29 RsaKeyPair::~RsaKeyPair() {} |
| 30 | 30 |
| 31 // static | 31 // static |
| 32 scoped_refptr<RsaKeyPair> RsaKeyPair::Generate() { | 32 scoped_refptr<RsaKeyPair> RsaKeyPair::Generate() { |
| 33 scoped_ptr<crypto::RSAPrivateKey> key(crypto::RSAPrivateKey::Create(2048)); | 33 std::unique_ptr<crypto::RSAPrivateKey> key( |
| 34 crypto::RSAPrivateKey::Create(2048)); |
| 34 if (!key) { | 35 if (!key) { |
| 35 LOG(ERROR) << "Cannot generate private key."; | 36 LOG(ERROR) << "Cannot generate private key."; |
| 36 return NULL; | 37 return NULL; |
| 37 } | 38 } |
| 38 return new RsaKeyPair(std::move(key)); | 39 return new RsaKeyPair(std::move(key)); |
| 39 } | 40 } |
| 40 | 41 |
| 41 // static | 42 // static |
| 42 scoped_refptr<RsaKeyPair> RsaKeyPair::FromString( | 43 scoped_refptr<RsaKeyPair> RsaKeyPair::FromString( |
| 43 const std::string& key_base64) { | 44 const std::string& key_base64) { |
| 44 std::string key_str; | 45 std::string key_str; |
| 45 if (!base::Base64Decode(key_base64, &key_str)) { | 46 if (!base::Base64Decode(key_base64, &key_str)) { |
| 46 LOG(ERROR) << "Failed to decode private key."; | 47 LOG(ERROR) << "Failed to decode private key."; |
| 47 return NULL; | 48 return NULL; |
| 48 } | 49 } |
| 49 | 50 |
| 50 std::vector<uint8_t> key_buf(key_str.begin(), key_str.end()); | 51 std::vector<uint8_t> key_buf(key_str.begin(), key_str.end()); |
| 51 scoped_ptr<crypto::RSAPrivateKey> key( | 52 std::unique_ptr<crypto::RSAPrivateKey> key( |
| 52 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf)); | 53 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_buf)); |
| 53 if (!key) { | 54 if (!key) { |
| 54 LOG(ERROR) << "Invalid private key."; | 55 LOG(ERROR) << "Invalid private key."; |
| 55 return NULL; | 56 return NULL; |
| 56 } | 57 } |
| 57 | 58 |
| 58 return new RsaKeyPair(std::move(key)); | 59 return new RsaKeyPair(std::move(key)); |
| 59 } | 60 } |
| 60 | 61 |
| 61 std::string RsaKeyPair::ToString() const { | 62 std::string RsaKeyPair::ToString() const { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 73 std::string RsaKeyPair::GetPublicKey() const { | 74 std::string RsaKeyPair::GetPublicKey() const { |
| 74 std::vector<uint8_t> public_key; | 75 std::vector<uint8_t> public_key; |
| 75 CHECK(key_->ExportPublicKey(&public_key)); | 76 CHECK(key_->ExportPublicKey(&public_key)); |
| 76 std::string public_key_str(public_key.begin(), public_key.end()); | 77 std::string public_key_str(public_key.begin(), public_key.end()); |
| 77 std::string public_key_base64; | 78 std::string public_key_base64; |
| 78 base::Base64Encode(public_key_str, &public_key_base64); | 79 base::Base64Encode(public_key_str, &public_key_base64); |
| 79 return public_key_base64; | 80 return public_key_base64; |
| 80 } | 81 } |
| 81 | 82 |
| 82 std::string RsaKeyPair::SignMessage(const std::string& message) const { | 83 std::string RsaKeyPair::SignMessage(const std::string& message) const { |
| 83 scoped_ptr<crypto::SignatureCreator> signature_creator( | 84 std::unique_ptr<crypto::SignatureCreator> signature_creator( |
| 84 crypto::SignatureCreator::Create(key_.get(), | 85 crypto::SignatureCreator::Create(key_.get(), |
| 85 crypto::SignatureCreator::SHA1)); | 86 crypto::SignatureCreator::SHA1)); |
| 86 signature_creator->Update(reinterpret_cast<const uint8_t*>(message.c_str()), | 87 signature_creator->Update(reinterpret_cast<const uint8_t*>(message.c_str()), |
| 87 message.length()); | 88 message.length()); |
| 88 std::vector<uint8_t> signature_buf; | 89 std::vector<uint8_t> signature_buf; |
| 89 signature_creator->Final(&signature_buf); | 90 signature_creator->Final(&signature_buf); |
| 90 std::string signature_str(signature_buf.begin(), signature_buf.end()); | 91 std::string signature_str(signature_buf.begin(), signature_buf.end()); |
| 91 std::string signature_base64; | 92 std::string signature_base64; |
| 92 base::Base64Encode(signature_str, &signature_base64); | 93 base::Base64Encode(signature_str, &signature_base64); |
| 93 return signature_base64; | 94 return signature_base64; |
| 94 } | 95 } |
| 95 | 96 |
| 96 std::string RsaKeyPair::GenerateCertificate() const { | 97 std::string RsaKeyPair::GenerateCertificate() const { |
| 97 std::string der_cert; | 98 std::string der_cert; |
| 98 // Certificates are SHA1-signed because |key_| has likely been used to sign | 99 // Certificates are SHA1-signed because |key_| has likely been used to sign |
| 99 // with SHA1 previously, and you should not re-use a key for signing data with | 100 // with SHA1 previously, and you should not re-use a key for signing data with |
| 100 // multiple signature algorithms. | 101 // multiple signature algorithms. |
| 101 net::x509_util::CreateSelfSignedCert( | 102 net::x509_util::CreateSelfSignedCert( |
| 102 key_.get(), | 103 key_.get(), |
| 103 net::x509_util::DIGEST_SHA1, | 104 net::x509_util::DIGEST_SHA1, |
| 104 "CN=chromoting", | 105 "CN=chromoting", |
| 105 base::RandInt(1, std::numeric_limits<int>::max()), | 106 base::RandInt(1, std::numeric_limits<int>::max()), |
| 106 base::Time::Now(), | 107 base::Time::Now(), |
| 107 base::Time::Now() + base::TimeDelta::FromDays(1), | 108 base::Time::Now() + base::TimeDelta::FromDays(1), |
| 108 &der_cert); | 109 &der_cert); |
| 109 return der_cert; | 110 return der_cert; |
| 110 } | 111 } |
| 111 | 112 |
| 112 } // namespace remoting | 113 } // namespace remoting |
| OLD | NEW |