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
3 // found in the LICENSE file.
4 
5 #ifndef CRYPTO_APPLE_KEYCHAIN_H_
6 #define CRYPTO_APPLE_KEYCHAIN_H_
7 
8 #include <Security/Security.h>
9 
10 #include "base/macros.h"
11 #include "build/build_config.h"
12 #include "crypto/crypto_export.h"
13 
14 namespace crypto {
15 
16 #if defined(OS_IOS)
17 using AppleSecKeychainItemRef = void*;
18 #else
19 using AppleSecKeychainItemRef = SecKeychainItemRef;
20 #endif
21 
22 // Wraps the KeychainServices API in a very thin layer, to allow it to be
23 // mocked out for testing.
24 
25 // See Keychain Services documentation for function documentation, as these call
26 // through directly to their Keychain Services equivalents (Foo ->
27 // SecKeychainFoo). The only exception is Free, which should be used for
28 // anything returned from this class that would normally be freed with
29 // CFRelease (to aid in testing).
30 class CRYPTO_EXPORT AppleKeychain {
31  public:
32   AppleKeychain();
33   virtual ~AppleKeychain();
34 
35   virtual OSStatus FindGenericPassword(UInt32 serviceNameLength,
36                                        const char* serviceName,
37                                        UInt32 accountNameLength,
38                                        const char* accountName,
39                                        UInt32* passwordLength,
40                                        void** passwordData,
41                                        AppleSecKeychainItemRef* itemRef) const;
42 
43   virtual OSStatus ItemFreeContent(void* data) const;
44 
45   virtual OSStatus AddGenericPassword(UInt32 serviceNameLength,
46                                       const char* serviceName,
47                                       UInt32 accountNameLength,
48                                       const char* accountName,
49                                       UInt32 passwordLength,
50                                       const void* passwordData,
51                                       AppleSecKeychainItemRef* itemRef) const;
52 
53 #if !defined(OS_IOS)
54   virtual OSStatus ItemDelete(AppleSecKeychainItemRef itemRef) const;
55 #endif  // !defined(OS_IOS)
56 
57  private:
58   DISALLOW_COPY_AND_ASSIGN(AppleKeychain);
59 };
60 
61 }  // namespace crypto
62 
63 #endif  // CRYPTO_APPLE_KEYCHAIN_H_
64