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" |
11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // ASN.1 encoding of the AlgorithmIdentifier from PKCS #8. | 15 // ASN.1 encoding of the AlgorithmIdentifier from PKCS #8. |
16 const uint8 kRsaAlgorithmIdentifier[] = { | 16 const uint8 kRsaAlgorithmIdentifier[] = { |
17 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, | 17 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, |
18 0x05, 0x00 | 18 0x05, 0x00 |
19 }; | 19 }; |
20 | 20 |
21 // ASN.1 tags for some types we use. | 21 // ASN.1 tags for some types we use. |
22 const uint8 kSequenceTag = 0x30; | 22 const uint8 kSequenceTag = 0x30; |
23 const uint8 kIntegerTag = 0x02; | 23 const uint8 kIntegerTag = 0x02; |
24 const uint8 kNullTag = 0x05; | 24 const uint8 kNullTag = 0x05; |
25 const uint8 kOctetStringTag = 0x04; | 25 const uint8 kOctetStringTag = 0x04; |
26 const uint8 kBitStringTag = 0x03; | 26 const uint8 kBitStringTag = 0x03; |
27 | 27 |
28 // TODO(hawk): Move the App* functions into a shared location, | |
29 // perhaps cssm_init.cc. | |
30 void* AppMalloc(CSSM_SIZE size, void *alloc_ref) { | |
31 return malloc(size); | |
32 } | |
33 | |
34 void AppFree(void* mem_ptr, void* alloc_ref) { | |
35 free(mem_ptr); | |
36 } | |
37 | |
38 void* AppRealloc(void* ptr, CSSM_SIZE size, void* alloc_ref) { | |
39 return realloc(ptr, size); | |
40 } | |
41 | |
42 void* AppCalloc(uint32 num, CSSM_SIZE size, void* alloc_ref) { | |
43 return calloc(num, size); | |
44 } | |
45 | |
46 const CSSM_API_MEMORY_FUNCS mem_funcs = { | |
47 AppMalloc, | |
48 AppFree, | |
49 AppRealloc, | |
50 AppCalloc, | |
51 NULL | |
52 }; | |
53 | |
54 // Helper for error handling during key import. | 28 // Helper for error handling during key import. |
55 #define READ_ASSERT(truth) \ | 29 #define READ_ASSERT(truth) \ |
56 if (!(truth)) { \ | 30 if (!(truth)) { \ |
57 NOTREACHED(); \ | 31 NOTREACHED(); \ |
58 return false; \ | 32 return false; \ |
59 } | 33 } |
60 | 34 |
61 static void PrependBytesInOrder(uint8* val, int start, int num_bytes, | 35 static void PrependBytesInOrder(uint8* val, int start, int num_bytes, |
62 std::list<uint8>* data) { | 36 std::list<uint8>* data) { |
63 while(num_bytes > start) { | 37 while(num_bytes > start) { |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 return result.release(); | 284 return result.release(); |
311 } | 285 } |
312 | 286 |
313 RSAPrivateKey::RSAPrivateKey() : csp_handle_(0) { | 287 RSAPrivateKey::RSAPrivateKey() : csp_handle_(0) { |
314 memset(&key_, 0, sizeof(key_)); | 288 memset(&key_, 0, sizeof(key_)); |
315 | 289 |
316 EnsureCSSMInit(); | 290 EnsureCSSMInit(); |
317 | 291 |
318 static CSSM_VERSION version = {2, 0}; | 292 static CSSM_VERSION version = {2, 0}; |
319 CSSM_RETURN crtn; | 293 CSSM_RETURN crtn; |
320 crtn = CSSM_ModuleAttach(&gGuidAppleCSP, &version, &mem_funcs, 0, | 294 crtn = CSSM_ModuleAttach(&gGuidAppleCSP, &version, &kCssmMemoryFunctions, 0, |
321 CSSM_SERVICE_CSP, 0, CSSM_KEY_HIERARCHY_NONE, | 295 CSSM_SERVICE_CSP, 0, CSSM_KEY_HIERARCHY_NONE, |
322 NULL, 0, NULL, &csp_handle_); | 296 NULL, 0, NULL, &csp_handle_); |
323 DCHECK(crtn == CSSM_OK); | 297 DCHECK(crtn == CSSM_OK); |
324 } | 298 } |
325 | 299 |
326 RSAPrivateKey::~RSAPrivateKey() { | 300 RSAPrivateKey::~RSAPrivateKey() { |
327 if (csp_handle_) { | 301 if (csp_handle_) { |
328 if (key_.KeyData.Data) { | 302 if (key_.KeyData.Data) { |
329 CSSM_FreeKey(csp_handle_, NULL, &key_, CSSM_FALSE); | 303 CSSM_FreeKey(csp_handle_, NULL, &key_, CSSM_FALSE); |
330 } | 304 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 | 363 |
390 // Copy everything into the output. | 364 // Copy everything into the output. |
391 output->reserve(content.size()); | 365 output->reserve(content.size()); |
392 for (std::list<uint8>::iterator i = content.begin(); i != content.end(); ++i) | 366 for (std::list<uint8>::iterator i = content.begin(); i != content.end(); ++i) |
393 output->push_back(*i); | 367 output->push_back(*i); |
394 | 368 |
395 return true; | 369 return true; |
396 } | 370 } |
397 | 371 |
398 } // namespace base | 372 } // namespace base |
OLD | NEW |