[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 3bcd5790 | 2010-06-23 22:46:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/capi_util.h" |
[email protected] | 3bcd5790 | 2010-06-23 22:46:04 | [diff] [blame] | 6 | |
7 | #include "base/basictypes.h" | ||||
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 8 | #include "base/memory/singleton.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 9 | #include "base/synchronization/lock.h" |
[email protected] | 3bcd5790 | 2010-06-23 22:46:04 | [diff] [blame] | 10 | |
11 | namespace { | ||||
12 | |||||
13 | class CAPIUtilSingleton { | ||||
14 | public: | ||||
15 | static CAPIUtilSingleton* GetInstance() { | ||||
16 | return Singleton<CAPIUtilSingleton>::get(); | ||||
17 | } | ||||
18 | |||||
19 | // Returns a lock to guard calls to CryptAcquireContext with | ||||
20 | // CRYPT_DELETEKEYSET or CRYPT_NEWKEYSET. | ||||
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 21 | base::Lock& acquire_context_lock() { |
[email protected] | 3bcd5790 | 2010-06-23 22:46:04 | [diff] [blame] | 22 | return acquire_context_lock_; |
23 | } | ||||
24 | |||||
25 | private: | ||||
26 | friend class Singleton<CAPIUtilSingleton>; | ||||
27 | friend struct DefaultSingletonTraits<CAPIUtilSingleton>; | ||||
28 | |||||
29 | CAPIUtilSingleton() {} | ||||
30 | |||||
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 31 | base::Lock acquire_context_lock_; |
[email protected] | 3bcd5790 | 2010-06-23 22:46:04 | [diff] [blame] | 32 | |
33 | DISALLOW_COPY_AND_ASSIGN(CAPIUtilSingleton); | ||||
34 | }; | ||||
35 | |||||
36 | } // namespace | ||||
37 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 38 | namespace crypto { |
[email protected] | 3bcd5790 | 2010-06-23 22:46:04 | [diff] [blame] | 39 | |
40 | BOOL CryptAcquireContextLocked(HCRYPTPROV* prov, | ||||
[email protected] | 08ce4d4 | 2010-10-21 00:31:19 | [diff] [blame] | 41 | LPCWSTR container, |
42 | LPCWSTR provider, | ||||
[email protected] | 3bcd5790 | 2010-06-23 22:46:04 | [diff] [blame] | 43 | DWORD prov_type, |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 44 | DWORD flags) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 45 | base::AutoLock lock(CAPIUtilSingleton::GetInstance()->acquire_context_lock()); |
[email protected] | 3bcd5790 | 2010-06-23 22:46:04 | [diff] [blame] | 46 | return CryptAcquireContext(prov, container, provider, prov_type, flags); |
47 | } | ||||
48 | |||||
[email protected] | 68da887 | 2012-02-15 06:01:59 | [diff] [blame] | 49 | void* WINAPI CryptAlloc(size_t size) { |
50 | return malloc(size); | ||||
51 | } | ||||
52 | |||||
53 | void WINAPI CryptFree(void* p) { | ||||
54 | free(p); | ||||
55 | } | ||||
56 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 57 | } // namespace crypto |