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 "crypto/ec_private_key.h" | 5 #include "crypto/ec_private_key.h" |
6 | 6 |
7 extern "C" { | 7 extern "C" { |
8 // Work around NSS missing SEC_BEGIN_PROTOS in secmodt.h. This must come before | 8 // Work around NSS missing SEC_BEGIN_PROTOS in secmodt.h. This must come before |
9 // other NSS headers. | 9 // other NSS headers. |
10 #include <secmodt.h> | 10 #include <secmodt.h> |
11 } | 11 } |
12 | 12 |
13 #include <cryptohi.h> | 13 #include <cryptohi.h> |
14 #include <keyhi.h> | 14 #include <keyhi.h> |
15 #include <pk11pub.h> | 15 #include <pk11pub.h> |
16 #include <secmod.h> | 16 #include <secmod.h> |
17 #include <stddef.h> | 17 #include <stddef.h> |
18 #include <stdint.h> | 18 #include <stdint.h> |
19 | 19 |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/scoped_ptr.h" |
22 #include "crypto/nss_util.h" | 22 #include "crypto/nss_util.h" |
23 #include "crypto/nss_util_internal.h" | 23 #include "crypto/nss_util_internal.h" |
24 #include "crypto/scoped_nss_types.h" | 24 #include "crypto/scoped_nss_types.h" |
25 #include "crypto/third_party/nss/chromium-nss.h" | 25 #include "crypto/third_party/nss/chromium-nss.h" |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // Copied from rsa_private_key_nss.cc. | 29 static bool AppendAttribute(SECKEYPrivateKey* key, |
30 static bool ReadAttribute(SECKEYPrivateKey* key, | 30 CK_ATTRIBUTE_TYPE type, |
31 CK_ATTRIBUTE_TYPE type, | 31 std::vector<uint8_t>* output) { |
32 std::vector<uint8_t>* output) { | |
33 SECItem item; | 32 SECItem item; |
34 SECStatus rv; | 33 SECStatus rv; |
35 rv = PK11_ReadRawAttribute(PK11_TypePrivKey, key, type, &item); | 34 rv = PK11_ReadRawAttribute(PK11_TypePrivKey, key, type, &item); |
36 if (rv != SECSuccess) { | 35 if (rv != SECSuccess) { |
37 DLOG(ERROR) << "PK11_ReadRawAttribute: " << PORT_GetError(); | 36 DLOG(ERROR) << "PK11_ReadRawAttribute: " << PORT_GetError(); |
38 return false; | 37 return false; |
39 } | 38 } |
40 | 39 |
41 output->assign(item.data, item.data + item.len); | 40 output->insert(output->end(), item.data, item.data + item.len); |
42 SECITEM_FreeItem(&item, PR_FALSE); | 41 SECITEM_FreeItem(&item, PR_FALSE); |
43 return true; | 42 return true; |
44 } | 43 } |
45 | 44 |
46 } // namespace | 45 } // namespace |
47 | 46 |
48 namespace crypto { | 47 namespace crypto { |
49 | 48 |
50 ECPrivateKey::~ECPrivateKey() { | 49 ECPrivateKey::~ECPrivateKey() { |
51 if (key_) | 50 if (key_) |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 const unsigned char* const data = public_key_->u.ec.publicValue.data; | 303 const unsigned char* const data = public_key_->u.ec.publicValue.data; |
305 const unsigned int len = public_key_->u.ec.publicValue.len; | 304 const unsigned int len = public_key_->u.ec.publicValue.len; |
306 if (len != kExpectedKeyLength || data[0] != 0x04) | 305 if (len != kExpectedKeyLength || data[0] != 0x04) |
307 return false; | 306 return false; |
308 | 307 |
309 output->assign(reinterpret_cast<const char*>(data + 1), | 308 output->assign(reinterpret_cast<const char*>(data + 1), |
310 kExpectedKeyLength - 1); | 309 kExpectedKeyLength - 1); |
311 return true; | 310 return true; |
312 } | 311 } |
313 | 312 |
314 bool ECPrivateKey::ExportValue(std::vector<uint8_t>* output) { | 313 bool ECPrivateKey::ExportValueForTesting(std::vector<uint8_t>* output) { |
315 return ReadAttribute(key_, CKA_VALUE, output); | 314 // This serialization format is purely for testing equality, so just |
316 } | 315 // concatenate the raw private key (always 32 bytes for P-256) with the |
317 | 316 // parameters. |
318 bool ECPrivateKey::ExportECParams(std::vector<uint8_t>* output) { | 317 output->clear(); |
319 return ReadAttribute(key_, CKA_EC_PARAMS, output); | 318 return AppendAttribute(key_, CKA_VALUE, output) && |
| 319 output->size() == 32 && |
| 320 AppendAttribute(key_, CKA_EC_PARAMS, output); |
320 } | 321 } |
321 | 322 |
322 ECPrivateKey::ECPrivateKey() : key_(NULL), public_key_(NULL) {} | 323 ECPrivateKey::ECPrivateKey() : key_(NULL), public_key_(NULL) {} |
323 | 324 |
324 } // namespace crypto | 325 } // namespace crypto |
OLD | NEW |