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_KEYCHAIN_MAC_H_
6#define CRYPTO_KEYCHAIN_MAC_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#if defined (OS_IOS)
15typedef void* SecKeychainRef;
16typedef void* SecKeychainItemRef;
17typedef void SecKeychainAttributeList;
18#endif
19
20namespace crypto {
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).
30class CRYPTO_EXPORT AppleKeychain {
31 public:
32  AppleKeychain();
33  virtual ~AppleKeychain();
34
35  virtual OSStatus FindGenericPassword(CFTypeRef keychainOrArray,
36                                       UInt32 serviceNameLength,
37                                       const char* serviceName,
38                                       UInt32 accountNameLength,
39                                       const char* accountName,
40                                       UInt32* passwordLength,
41                                       void** passwordData,
42                                       SecKeychainItemRef* itemRef) const;
43
44  virtual OSStatus ItemFreeContent(SecKeychainAttributeList* attrList,
45                                   void* data) const;
46
47  virtual OSStatus AddGenericPassword(SecKeychainRef keychain,
48                                      UInt32 serviceNameLength,
49                                      const char* serviceName,
50                                      UInt32 accountNameLength,
51                                      const char* accountName,
52                                      UInt32 passwordLength,
53                                      const void* passwordData,
54                                      SecKeychainItemRef* itemRef) const;
55
56#if !defined(OS_IOS)
57  virtual OSStatus ItemCopyAttributesAndData(
58      SecKeychainItemRef itemRef,
59      SecKeychainAttributeInfo* info,
60      SecItemClass* itemClass,
61      SecKeychainAttributeList** attrList,
62      UInt32* length,
63      void** outData) const;
64
65  virtual OSStatus ItemModifyAttributesAndData(
66      SecKeychainItemRef itemRef,
67      const SecKeychainAttributeList* attrList,
68      UInt32 length,
69      const void* data) const;
70
71  virtual OSStatus ItemFreeAttributesAndData(SecKeychainAttributeList* attrList,
72                                             void* data) const;
73
74  virtual OSStatus ItemDelete(SecKeychainItemRef itemRef) const;
75
76  virtual OSStatus SearchCreateFromAttributes(
77      CFTypeRef keychainOrArray,
78      SecItemClass itemClass,
79      const SecKeychainAttributeList* attrList,
80      SecKeychainSearchRef* searchRef) const;
81
82  virtual OSStatus SearchCopyNext(SecKeychainSearchRef searchRef,
83                                  SecKeychainItemRef* itemRef) const;
84
85  virtual OSStatus AddInternetPassword(SecKeychainRef keychain,
86                                       UInt32 serverNameLength,
87                                       const char* serverName,
88                                       UInt32 securityDomainLength,
89                                       const char* securityDomain,
90                                       UInt32 accountNameLength,
91                                       const char* accountName,
92                                       UInt32 pathLength, const char* path,
93                                       UInt16 port, SecProtocolType protocol,
94                                       SecAuthenticationType authenticationType,
95                                       UInt32 passwordLength,
96                                       const void* passwordData,
97                                       SecKeychainItemRef* itemRef) const;
98
99  // Calls CFRelease on the given ref, after checking that |ref| is non-NULL.
100  virtual void Free(CFTypeRef ref) const;
101#endif  // !defined(OS_IOS)
102
103 private:
104  DISALLOW_COPY_AND_ASSIGN(AppleKeychain);
105};
106
107}  // namespace crypto
108
109#endif  // CRYPTO_KEYCHAIN_MAC_H_
110