Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "crypto/rsa_private_key.h" | 5 #include "crypto/rsa_private_key.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "crypto/cssm_init.h" | 11 #include "crypto/cssm_init.h" |
| 12 | 12 |
| 13 namespace { | |
| 14 | |
| 15 bool CopyCssmKey(const CSSM_KEY& source, CSSM_KEY* destination) { | |
|
wtc
2011/11/30 00:58:10
Nit: CopyCssmKey => CopyCSSMKey
rsleevi: is there
Ryan Sleevi
2011/11/30 02:05:09
No.
Further, it's not necessarily safe to copy Ke
Sergey Ulanov
2011/11/30 22:30:03
Removed this function
| |
| 16 destination->KeyHeader = source.KeyHeader; | |
| 17 destination->KeyData.Length = source.KeyData.Length; | |
| 18 destination->KeyData.Data = | |
| 19 reinterpret_cast<uint8*>(crypto::CSSMMalloc(source.KeyData.Length)); | |
| 20 if (!destination->KeyData.Data) { | |
| 21 NOTREACHED() << "CSSMMalloc failed"; | |
| 22 return false; | |
| 23 } | |
| 24 memcpy(destination->KeyData.Data, source.KeyData.Data, source.KeyData.Length); | |
| 25 return true; | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 13 namespace crypto { | 30 namespace crypto { |
| 14 | 31 |
| 15 // static | 32 // static |
| 16 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { | 33 RSAPrivateKey* RSAPrivateKey::Create(uint16 num_bits) { |
| 17 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 34 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 18 | 35 |
| 19 CSSM_CC_HANDLE cc_handle; | 36 CSSM_CC_HANDLE cc_handle; |
| 20 CSSM_RETURN crtn; | 37 CSSM_RETURN crtn; |
| 21 crtn = CSSM_CSP_CreateKeyGenContext(GetSharedCSPHandle(), CSSM_ALGID_RSA, | 38 crtn = CSSM_CSP_CreateKeyGenContext(GetSharedCSPHandle(), CSSM_ALGID_RSA, |
| 22 num_bits, NULL, NULL, NULL, NULL, NULL, | 39 num_bits, NULL, NULL, NULL, NULL, NULL, |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 | 184 |
| 168 RSAPrivateKey::~RSAPrivateKey() { | 185 RSAPrivateKey::~RSAPrivateKey() { |
| 169 if (key_.KeyData.Data) { | 186 if (key_.KeyData.Data) { |
| 170 CSSM_FreeKey(GetSharedCSPHandle(), NULL, &key_, CSSM_FALSE); | 187 CSSM_FreeKey(GetSharedCSPHandle(), NULL, &key_, CSSM_FALSE); |
| 171 } | 188 } |
| 172 if (public_key_.KeyData.Data) { | 189 if (public_key_.KeyData.Data) { |
| 173 CSSM_FreeKey(GetSharedCSPHandle(), NULL, &public_key_, CSSM_FALSE); | 190 CSSM_FreeKey(GetSharedCSPHandle(), NULL, &public_key_, CSSM_FALSE); |
| 174 } | 191 } |
| 175 } | 192 } |
| 176 | 193 |
| 194 RSAPrivateKey* RSAPrivateKey::Copy() const { | |
| 195 scoped_ptr<RSAPrivateKey> copy(new RSAPrivateKey()); | |
| 196 if (!CopyCssmKey(key_, ©->key_) || | |
| 197 !CopyCssmKey(public_key_, ©->public_key_)) { | |
| 198 return NULL; | |
| 199 } | |
| 200 return copy.release(); | |
| 201 } | |
| 202 | |
| 177 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { | 203 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { |
| 178 if (!key_.KeyData.Data || !key_.KeyData.Length) { | 204 if (!key_.KeyData.Data || !key_.KeyData.Length) { |
| 179 return false; | 205 return false; |
| 180 } | 206 } |
| 181 output->clear(); | 207 output->clear(); |
| 182 output->insert(output->end(), key_.KeyData.Data, | 208 output->insert(output->end(), key_.KeyData.Data, |
| 183 key_.KeyData.Data + key_.KeyData.Length); | 209 key_.KeyData.Data + key_.KeyData.Length); |
| 184 return true; | 210 return true; |
| 185 } | 211 } |
| 186 | 212 |
| 187 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) { | 213 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) { |
| 188 PrivateKeyInfoCodec private_key_info(true); | 214 PrivateKeyInfoCodec private_key_info(true); |
| 189 std::vector<uint8> private_key_data; | 215 std::vector<uint8> private_key_data; |
| 190 private_key_data.assign(key_.KeyData.Data, | 216 private_key_data.assign(key_.KeyData.Data, |
| 191 key_.KeyData.Data + key_.KeyData.Length); | 217 key_.KeyData.Data + key_.KeyData.Length); |
| 192 return (private_key_info.Import(private_key_data) && | 218 return (private_key_info.Import(private_key_data) && |
| 193 private_key_info.ExportPublicKeyInfo(output)); | 219 private_key_info.ExportPublicKeyInfo(output)); |
| 194 } | 220 } |
| 195 | 221 |
| 196 } // namespace crypto | 222 } // namespace crypto |
| OLD | NEW |