Chromium Code Reviews| 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 #ifndef CRYPTO_RSA_PRIVATE_KEY_H_ | 5 #ifndef CRYPTO_RSA_PRIVATE_KEY_H_ |
| 6 #define CRYPTO_RSA_PRIVATE_KEY_H_ | 6 #define CRYPTO_RSA_PRIVATE_KEY_H_ |
| 7 | 7 |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 | 9 |
| 10 #if defined(USE_OPENSSL) | |
| 11 // Forward declaration for openssl/*.h | |
| 12 typedef struct evp_pkey_st EVP_PKEY; | |
| 13 #elif defined(USE_NSS) | |
| 14 // Forward declaration. | |
| 15 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; | |
| 16 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; | |
| 17 #elif defined(OS_IOS) | |
| 18 #include <Security/Security.h> | |
| 19 #elif defined(OS_MACOSX) | |
| 20 #include <Security/cssm.h> | |
| 21 #endif | |
| 22 | |
| 23 #include <list> | 10 #include <list> |
| 24 #include <vector> | 11 #include <vector> |
| 25 | 12 |
| 26 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 27 #include "crypto/crypto_export.h" | 14 #include "crypto/crypto_export.h" |
| 28 | 15 |
| 29 #if defined(OS_WIN) | |
| 30 #include "crypto/scoped_capi_types.h" | |
| 31 #endif | |
| 32 #if defined(USE_NSS) | 16 #if defined(USE_NSS) |
| 33 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
| 34 #endif | 18 #endif |
| 35 | 19 |
| 20 #if defined(USE_OPENSSL) | |
| 21 // Forward declaration for openssl/*.h | |
| 22 typedef struct evp_pkey_st EVP_PKEY; | |
| 23 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) | |
| 24 // Forward declaration. | |
| 25 typedef struct SECKEYPrivateKeyStr SECKEYPrivateKey; | |
| 26 typedef struct SECKEYPublicKeyStr SECKEYPublicKey; | |
| 27 #endif | |
| 28 | |
| 29 | |
| 36 namespace crypto { | 30 namespace crypto { |
| 37 | 31 |
| 38 // Used internally by RSAPrivateKey for serializing and deserializing | 32 // Used internally by RSAPrivateKey for serializing and deserializing |
| 39 // PKCS #8 PrivateKeyInfo and PublicKeyInfo. | 33 // PKCS #8 PrivateKeyInfo and PublicKeyInfo. |
| 40 class PrivateKeyInfoCodec { | 34 class PrivateKeyInfoCodec { |
| 41 public: | 35 public: |
| 42 | 36 |
| 43 // ASN.1 encoding of the AlgorithmIdentifier from PKCS #8. | 37 // ASN.1 encoding of the AlgorithmIdentifier from PKCS #8. |
| 44 static const uint8 kRsaAlgorithmIdentifier[]; | 38 static const uint8 kRsaAlgorithmIdentifier[]; |
| 45 | 39 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 | 187 |
| 194 // Create a new instance by importing an existing private key. The format is | 188 // Create a new instance by importing an existing private key. The format is |
| 195 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if | 189 // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if |
| 196 // initialization fails. | 190 // initialization fails. |
| 197 // The created key is permanent and is not exportable in plaintext form. | 191 // The created key is permanent and is not exportable in plaintext form. |
| 198 // | 192 // |
| 199 // NOTE: Currently only available if USE_NSS is defined. | 193 // NOTE: Currently only available if USE_NSS is defined. |
| 200 static RSAPrivateKey* CreateSensitiveFromPrivateKeyInfo( | 194 static RSAPrivateKey* CreateSensitiveFromPrivateKeyInfo( |
| 201 const std::vector<uint8>& input); | 195 const std::vector<uint8>& input); |
| 202 | 196 |
| 203 #if defined(USE_NSS) | 197 #if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) |
| 204 // Create a new instance by referencing an existing private key | 198 // Create a new instance by referencing an existing private key |
| 205 // structure. Does not import the key. | 199 // structure. Does not import the key. |
| 206 static RSAPrivateKey* CreateFromKey(SECKEYPrivateKey* key); | 200 static RSAPrivateKey* CreateFromKey(SECKEYPrivateKey* key); |
| 207 #endif | 201 #endif |
| 208 | 202 |
| 209 // Import an existing public key, and then search for the private | 203 // Import an existing public key, and then search for the private |
| 210 // half in the key database. The format of the public key blob is is | 204 // half in the key database. The format of the public key blob is is |
| 211 // an X509 SubjectPublicKeyInfo block. This can return NULL if | 205 // an X509 SubjectPublicKeyInfo block. This can return NULL if |
| 212 // initialization fails or the private key cannot be found. The | 206 // initialization fails or the private key cannot be found. The |
| 213 // caller takes ownership of the returned object, but nothing new is | 207 // caller takes ownership of the returned object, but nothing new is |
| 214 // created in the key database. | 208 // created in the key database. |
| 215 // | 209 // |
| 216 // NOTE: Currently only available if USE_NSS is defined. | 210 // NOTE: Currently only available if USE_NSS is defined. |
| 217 static RSAPrivateKey* FindFromPublicKeyInfo( | 211 static RSAPrivateKey* FindFromPublicKeyInfo( |
| 218 const std::vector<uint8>& input); | 212 const std::vector<uint8>& input); |
| 219 | 213 |
| 220 #if defined(USE_OPENSSL) | 214 #if defined(USE_OPENSSL) |
| 221 EVP_PKEY* key() { return key_; } | 215 EVP_PKEY* key() { return key_; } |
| 222 #elif defined(USE_NSS) | 216 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) |
| 223 SECKEYPrivateKey* key() { return key_; } | 217 SECKEYPrivateKey* key() { return key_; } |
| 224 SECKEYPublicKey* public_key() { return public_key_; } | 218 SECKEYPublicKey* public_key() { return public_key_; } |
| 225 #elif defined(OS_WIN) | |
| 226 HCRYPTPROV provider() { return provider_; } | |
| 227 HCRYPTKEY key() { return key_; } | |
| 228 #elif defined(OS_IOS) | |
| 229 SecKeyRef key() { return key_; } | |
| 230 SecKeyRef public_key() { return public_key_; } | |
| 231 #elif defined(OS_MACOSX) | |
| 232 CSSM_KEY_PTR key() { return &key_; } | |
| 233 CSSM_KEY_PTR public_key() { return &public_key_; } | |
| 234 #endif | 219 #endif |
| 235 | 220 |
| 236 // Creates a copy of the object. | 221 // Creates a copy of the object. |
| 237 RSAPrivateKey* Copy() const; | 222 RSAPrivateKey* Copy() const; |
| 238 | 223 |
| 239 // Exports the private key to a PKCS #1 PrivateKey block. | 224 // Exports the private key to a PKCS #1 PrivateKey block. |
| 240 bool ExportPrivateKey(std::vector<uint8>* output) const; | 225 bool ExportPrivateKey(std::vector<uint8>* output) const; |
| 241 | 226 |
| 242 // Exports the public key to an X509 SubjectPublicKeyInfo block. | 227 // Exports the public key to an X509 SubjectPublicKeyInfo block. |
| 243 bool ExportPublicKey(std::vector<uint8>* output) const; | 228 bool ExportPublicKey(std::vector<uint8>* output) const; |
| 244 | 229 |
| 245 private: | 230 private: |
| 246 #if defined(USE_NSS) | 231 #if defined(USE_NSS) |
| 247 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FindFromPublicKey); | 232 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FindFromPublicKey); |
| 248 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FailedFindFromPublicKey); | 233 FRIEND_TEST_ALL_PREFIXES(RSAPrivateKeyNSSTest, FailedFindFromPublicKey); |
| 249 #endif | 234 #endif |
| 250 | 235 |
| 251 // Constructor is private. Use one of the Create*() or Find*() | 236 // Constructor is private. Use one of the Create*() or Find*() |
| 252 // methods above instead. | 237 // methods above instead. |
| 253 RSAPrivateKey(); | 238 RSAPrivateKey(); |
| 254 | 239 |
| 255 // Shared helper for Create() and CreateSensitive(). | 240 // Shared helper for Create() and CreateSensitive(). |
| 256 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a | 241 // TODO(cmasone): consider replacing |permanent| and |sensitive| with a |
| 257 // flags arg created by ORing together some enumerated values. | 242 // flags arg created by ORing together some enumerated values. |
| 258 static RSAPrivateKey* CreateWithParams(uint16 num_bits, | 243 static RSAPrivateKey* CreateWithParams(uint16 num_bits, |
| 259 bool permanent, | 244 bool permanent, |
|
wtc
2013/06/20 21:35:56
IMPORTANT: The |permanent| flag can't be supported
| |
| 260 bool sensitive); | 245 bool sensitive); |
| 261 | 246 |
| 262 // Shared helper for CreateFromPrivateKeyInfo() and | 247 // Shared helper for CreateFromPrivateKeyInfo() and |
| 263 // CreateSensitiveFromPrivateKeyInfo(). | 248 // CreateSensitiveFromPrivateKeyInfo(). |
| 264 static RSAPrivateKey* CreateFromPrivateKeyInfoWithParams( | 249 static RSAPrivateKey* CreateFromPrivateKeyInfoWithParams( |
| 265 const std::vector<uint8>& input, bool permanent, bool sensitive); | 250 const std::vector<uint8>& input, bool permanent, bool sensitive); |
| 266 | 251 |
| 267 #if defined(USE_OPENSSL) | 252 #if defined(USE_OPENSSL) |
| 268 EVP_PKEY* key_; | 253 EVP_PKEY* key_; |
| 269 #elif defined(USE_NSS) | 254 #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) |
| 270 SECKEYPrivateKey* key_; | 255 SECKEYPrivateKey* key_; |
| 271 SECKEYPublicKey* public_key_; | 256 SECKEYPublicKey* public_key_; |
| 272 #elif defined(OS_WIN) | |
| 273 bool InitProvider(); | |
| 274 | |
| 275 ScopedHCRYPTPROV provider_; | |
| 276 ScopedHCRYPTKEY key_; | |
| 277 #elif defined(OS_IOS) | |
| 278 SecKeyRef key_; | |
| 279 SecKeyRef public_key_; | |
| 280 #elif defined(OS_MACOSX) | |
| 281 CSSM_KEY key_; | |
| 282 CSSM_KEY public_key_; | |
| 283 #endif | 257 #endif |
| 284 | 258 |
| 285 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); | 259 DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey); |
| 286 }; | 260 }; |
| 287 | 261 |
| 288 } // namespace crypto | 262 } // namespace crypto |
| 289 | 263 |
| 290 #endif // CRYPTO_RSA_PRIVATE_KEY_H_ | 264 #endif // CRYPTO_RSA_PRIVATE_KEY_H_ |
| OLD | NEW |