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/basictypes.h"
11#include "crypto/crypto_export.h"
12
13#if defined (OS_IOS)
14typedef void* SecKeychainRef;
15typedef void* SecKeychainItemRef;
16typedef void SecKeychainAttributeList;
17#endif
18
19namespace crypto {
20
21// Wraps the KeychainServices API in a very thin layer, to allow it to be
22// mocked out for testing.
23
24// See Keychain Services documentation for function documentation, as these call
25// through directly to their Keychain Services equivalents (Foo ->
26// SecKeychainFoo). The only exception is Free, which should be used for
27// anything returned from this class that would normally be freed with
28// CFRelease (to aid in testing).
29class CRYPTO_EXPORT AppleKeychain {
30 public:
31  AppleKeychain();
32  virtual ~AppleKeychain();
33
34  virtual OSStatus FindGenericPassword(CFTypeRef keychainOrArray,
35                                       UInt32 serviceNameLength,
36                                       const char* serviceName,
37                                       UInt32 accountNameLength,
38                                       const char* accountName,
39                                       UInt32* passwordLength,
40                                       void** passwordData,
41                                       SecKeychainItemRef* itemRef) const;
42
43  virtual OSStatus ItemFreeContent(SecKeychainAttributeList* attrList,
44                                   void* data) const;
45
46  virtual OSStatus AddGenericPassword(SecKeychainRef keychain,
47                                      UInt32 serviceNameLength,
48                                      const char* serviceName,
49                                      UInt32 accountNameLength,
50                                      const char* accountName,
51                                      UInt32 passwordLength,
52                                      const void* passwordData,
53                                      SecKeychainItemRef* itemRef) const;
54
55#if !defined(OS_IOS)
56  virtual OSStatus ItemCopyAttributesAndData(
57      SecKeychainItemRef itemRef,
58      SecKeychainAttributeInfo* info,
59      SecItemClass* itemClass,
60      SecKeychainAttributeList** attrList,
61      UInt32* length,
62      void** outData) const;
63
64  virtual OSStatus ItemModifyAttributesAndData(
65      SecKeychainItemRef itemRef,
66      const SecKeychainAttributeList* attrList,
67      UInt32 length,
68      const void* data) const;
69
70  virtual OSStatus ItemFreeAttributesAndData(SecKeychainAttributeList* attrList,
71                                             void* data) const;
72
73  virtual OSStatus ItemDelete(SecKeychainItemRef itemRef) const;
74
75  virtual OSStatus SearchCreateFromAttributes(
76      CFTypeRef keychainOrArray,
77      SecItemClass itemClass,
78      const SecKeychainAttributeList* attrList,
79      SecKeychainSearchRef* searchRef) const;
80
81  virtual OSStatus SearchCopyNext(SecKeychainSearchRef searchRef,
82                                  SecKeychainItemRef* itemRef) const;
83
84  virtual OSStatus AddInternetPassword(SecKeychainRef keychain,
85                                       UInt32 serverNameLength,
86                                       const char* serverName,
87                                       UInt32 securityDomainLength,
88                                       const char* securityDomain,
89                                       UInt32 accountNameLength,
90                                       const char* accountName,
91                                       UInt32 pathLength, const char* path,
92                                       UInt16 port, SecProtocolType protocol,
93                                       SecAuthenticationType authenticationType,
94                                       UInt32 passwordLength,
95                                       const void* passwordData,
96                                       SecKeychainItemRef* itemRef) const;
97
98  // Calls CFRelease on the given ref, after checking that |ref| is non-NULL.
99  virtual void Free(CFTypeRef ref) const;
100#endif  // !defined(OS_IOS)
101
102 private:
103  DISALLOW_COPY_AND_ASSIGN(AppleKeychain);
104};
105
106}  // namespace crypto
107
108#endif  // CRYPTO_KEYCHAIN_MAC_H_
109