| 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 <cryptohi.h> | 7 #include <cryptohi.h> |
| 8 #include <keyhi.h> | 8 #include <keyhi.h> |
| 9 #include <pk11pub.h> | 9 #include <pk11pub.h> |
| 10 #include <secmod.h> | 10 #include <secmod.h> |
| 11 | 11 |
| 12 #include <list> | 12 #include <list> |
| 13 | 13 |
| 14 #include "base/debug/leak_annotations.h" | 14 #include "base/debug/leak_annotations.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "crypto/nss_util.h" | 18 #include "crypto/nss_util.h" |
| 19 #include "crypto/nss_util_internal.h" | 19 #include "crypto/nss_util_internal.h" |
| 20 #include "crypto/scoped_nss_types.h" | 20 #include "crypto/scoped_nss_types.h" |
| 21 | 21 |
| 22 // TODO(rafaelw): Consider refactoring common functions and definitions from | 22 // TODO(rafaelw): Consider using NSS's ASN.1 encoder. |
| 23 // rsa_private_key_win.cc or using NSS's ASN.1 encoder. | |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 static bool ReadAttribute(SECKEYPrivateKey* key, | 25 static bool ReadAttribute(SECKEYPrivateKey* key, |
| 27 CK_ATTRIBUTE_TYPE type, | 26 CK_ATTRIBUTE_TYPE type, |
| 28 std::vector<uint8>* output) { | 27 std::vector<uint8>* output) { |
| 29 SECItem item; | 28 SECItem item; |
| 30 SECStatus rv; | 29 SECStatus rv; |
| 31 rv = PK11_ReadRawAttribute(PK11_TypePrivKey, key, type, &item); | 30 rv = PK11_ReadRawAttribute(PK11_TypePrivKey, key, type, &item); |
| 32 if (rv != SECSuccess) { | 31 if (rv != SECSuccess) { |
| 33 NOTREACHED(); | 32 NOTREACHED(); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 copy->key_ = SECKEY_CopyPrivateKey(key); | 88 copy->key_ = SECKEY_CopyPrivateKey(key); |
| 90 copy->public_key_ = SECKEY_ConvertToPublicKey(key); | 89 copy->public_key_ = SECKEY_ConvertToPublicKey(key); |
| 91 if (!copy->key_ || !copy->public_key_) { | 90 if (!copy->key_ || !copy->public_key_) { |
| 92 NOTREACHED(); | 91 NOTREACHED(); |
| 93 delete copy; | 92 delete copy; |
| 94 return NULL; | 93 return NULL; |
| 95 } | 94 } |
| 96 return copy; | 95 return copy; |
| 97 } | 96 } |
| 98 | 97 |
| 98 #if defined(USE_NSS) |
| 99 // static | 99 // static |
| 100 RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfo( | 100 RSAPrivateKey* RSAPrivateKey::FindFromPublicKeyInfo( |
| 101 const std::vector<uint8>& input) { | 101 const std::vector<uint8>& input) { |
| 102 EnsureNSSInit(); | 102 EnsureNSSInit(); |
| 103 | 103 |
| 104 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); | 104 scoped_ptr<RSAPrivateKey> result(new RSAPrivateKey); |
| 105 | 105 |
| 106 // First, decode and save the public key. | 106 // First, decode and save the public key. |
| 107 SECItem key_der; | 107 SECItem key_der; |
| 108 key_der.type = siBuffer; | 108 key_der.type = siBuffer; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 result->key_ = PK11_FindKeyByKeyID(item->module->slots[i], | 146 result->key_ = PK11_FindKeyByKeyID(item->module->slots[i], |
| 147 ck_id.get(), NULL); | 147 ck_id.get(), NULL); |
| 148 if (result->key_) | 148 if (result->key_) |
| 149 return result.release(); | 149 return result.release(); |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 | 152 |
| 153 // We didn't find the key. | 153 // We didn't find the key. |
| 154 return NULL; | 154 return NULL; |
| 155 } | 155 } |
| 156 #endif |
| 156 | 157 |
| 157 RSAPrivateKey* RSAPrivateKey::Copy() const { | 158 RSAPrivateKey* RSAPrivateKey::Copy() const { |
| 158 RSAPrivateKey* copy = new RSAPrivateKey(); | 159 RSAPrivateKey* copy = new RSAPrivateKey(); |
| 159 copy->key_ = SECKEY_CopyPrivateKey(key_); | 160 copy->key_ = SECKEY_CopyPrivateKey(key_); |
| 160 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); | 161 copy->public_key_ = SECKEY_CopyPublicKey(public_key_); |
| 161 return copy; | 162 return copy; |
| 162 } | 163 } |
| 163 | 164 |
| 164 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { | 165 bool RSAPrivateKey::ExportPrivateKey(std::vector<uint8>* output) const { |
| 165 PrivateKeyInfoCodec private_key_info(true); | 166 PrivateKeyInfoCodec private_key_info(true); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); | 259 result->public_key_ = SECKEY_ConvertToPublicKey(result->key_); |
| 259 if (!result->public_key_) { | 260 if (!result->public_key_) { |
| 260 NOTREACHED(); | 261 NOTREACHED(); |
| 261 return NULL; | 262 return NULL; |
| 262 } | 263 } |
| 263 | 264 |
| 264 return result.release(); | 265 return result.release(); |
| 265 } | 266 } |
| 266 | 267 |
| 267 } // namespace crypto | 268 } // namespace crypto |
| OLD | NEW |