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