| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/crypto/rsa_private_key.h" | 5 #include "base/crypto/rsa_private_key.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 | 8 |
| 9 #include "base/crypto/cssm_init.h" | 9 #include "base/crypto/cssm_init.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 return NULL; | 26 return NULL; |
| 27 } | 27 } |
| 28 | 28 |
| 29 CSSM_KEY public_key; | 29 CSSM_KEY public_key; |
| 30 memset(&public_key, 0, sizeof(CSSM_KEY)); | 30 memset(&public_key, 0, sizeof(CSSM_KEY)); |
| 31 CSSM_DATA label = { 9, | 31 CSSM_DATA label = { 9, |
| 32 const_cast<uint8*>(reinterpret_cast<const uint8*>("temp_key")) }; | 32 const_cast<uint8*>(reinterpret_cast<const uint8*>("temp_key")) }; |
| 33 crtn = CSSM_GenerateKeyPair(cc_handle, | 33 crtn = CSSM_GenerateKeyPair(cc_handle, |
| 34 CSSM_KEYUSE_VERIFY, | 34 CSSM_KEYUSE_VERIFY, |
| 35 CSSM_KEYATTR_RETURN_DATA | CSSM_KEYATTR_EXTRACTABLE, &label, | 35 CSSM_KEYATTR_RETURN_DATA | CSSM_KEYATTR_EXTRACTABLE, &label, |
| 36 &public_key, CSSM_KEYUSE_SIGN, | 36 result->public_key(), CSSM_KEYUSE_SIGN, |
| 37 CSSM_KEYATTR_RETURN_DATA | CSSM_KEYATTR_EXTRACTABLE, &label, NULL, | 37 CSSM_KEYATTR_RETURN_DATA | CSSM_KEYATTR_EXTRACTABLE, &label, NULL, |
| 38 result->key()); | 38 result->key()); |
| 39 CSSM_DeleteContext(cc_handle); | 39 CSSM_DeleteContext(cc_handle); |
| 40 if (crtn) { | 40 if (crtn) { |
| 41 NOTREACHED() << "CSSM_CSP_CreateKeyGenContext failed: " << crtn; | 41 NOTREACHED() << "CSSM_CSP_CreateKeyGenContext failed: " << crtn; |
| 42 return NULL; | 42 return NULL; |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Public key is not needed. | |
| 46 CSSM_FreeKey(GetSharedCSPHandle(), NULL, &public_key, CSSM_FALSE); | |
| 47 | |
| 48 return result.release(); | 45 return result.release(); |
| 49 } | 46 } |
| 50 | 47 |
| 51 // static | 48 // static |
| 52 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { | 49 RSAPrivateKey* RSAPrivateKey::CreateSensitive(uint16 num_bits) { |
| 53 NOTIMPLEMENTED(); | 50 NOTIMPLEMENTED(); |
| 54 return NULL; | 51 return NULL; |
| 55 } | 52 } |
| 56 | 53 |
| 57 // static | 54 // static |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 } | 121 } |
| 125 | 122 |
| 126 RSAPrivateKey::RSAPrivateKey() { | 123 RSAPrivateKey::RSAPrivateKey() { |
| 127 memset(&key_, 0, sizeof(key_)); | 124 memset(&key_, 0, sizeof(key_)); |
| 128 | 125 |
| 129 EnsureCSSMInit(); | 126 EnsureCSSMInit(); |
| 130 } | 127 } |
| 131 | 128 |
| 132 RSAPrivateKey::~RSAPrivateKey() { | 129 RSAPrivateKey::~RSAPrivateKey() { |
| 133 if (key_.KeyData.Data) { | 130 if (key_.KeyData.Data) { |
| 134 CSSM_FreeKey(GetSharedCSPHandle(), NULL, &key_, CSSM_FALSE); | 131 CSSM_CSP_HANDLE csp_handle = GetSharedCSPHandle(); |
| 132 CSSM_FreeKey(csp_handle, NULL, &key_, CSSM_FALSE); |
| 133 CSSM_FreeKey(csp_handle, NULL, &public_key_, CSSM_FALSE); |
| 135 } | 134 } |
| 136 } | 135 } |
| 137 | 136 |
| 138 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { | 137 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) { |
| 139 if (!key_.KeyData.Data || !key_.KeyData.Length) { | 138 if (!key_.KeyData.Data || !key_.KeyData.Length) { |
| 140 return false; | 139 return false; |
| 141 } | 140 } |
| 142 output->clear(); | 141 output->clear(); |
| 143 output->insert(output->end(), key_.KeyData.Data, | 142 output->insert(output->end(), key_.KeyData.Data, |
| 144 key_.KeyData.Data + key_.KeyData.Length); | 143 key_.KeyData.Data + key_.KeyData.Length); |
| 145 return true; | 144 return true; |
| 146 } | 145 } |
| 147 | 146 |
| 148 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) { | 147 bool RSAPrivateKey::ExportPublicKey(std::vector<uint8>* output) { |
| 149 PrivateKeyInfoCodec private_key_info(true); | 148 if (!public_key_.KeyData.Data || !public_key_.KeyData.Length) { |
| 150 std::vector<uint8> private_key_data; | 149 return false; |
| 151 private_key_data.assign(key_.KeyData.Data, | 150 } |
| 152 key_.KeyData.Data + key_.KeyData.Length); | 151 output->clear(); |
| 153 return (private_key_info.Import(private_key_data) && | 152 output->insert(output->end(), public_key_.KeyData.Data, |
| 154 private_key_info.ExportPublicKeyInfo(output)); | 153 public_key_.KeyData.Data + public_key_.KeyData.Length); |
| 154 return true; |
| 155 } | 155 } |
| 156 | 156 |
| 157 } // namespace base | 157 } // namespace base |
| OLD | NEW |