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