1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_MAC_SECURITY_WRAPPERS_H_
#define CHROME_BROWSER_MAC_SECURITY_WRAPPERS_H_
#include <Security/Security.h>
#include <Security/SecRequirement.h>
#include "base/basictypes.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/memory/scoped_ptr.h"
namespace chrome {
// Wraps SecKeychainSetUserInteractionAllowed, restoring the previous setting
// on destruction.
class ScopedSecKeychainSetUserInteractionAllowed {
public:
explicit ScopedSecKeychainSetUserInteractionAllowed(Boolean allowed);
~ScopedSecKeychainSetUserInteractionAllowed();
private:
Boolean old_allowed_;
DISALLOW_COPY_AND_ASSIGN(ScopedSecKeychainSetUserInteractionAllowed);
};
// Holds a paired SecKeychainItemRef and SecAccessRef, maintaining the
// association between the two, and managing their ownership by retaining
// the SecKeychainItemRef and SecAccessRef elements placed into a
// CrSKeychainItemAndAccess object. Suitable for use
// in standard C++ containers.
class CrSKeychainItemAndAccess {
public:
CrSKeychainItemAndAccess(SecKeychainItemRef item, SecAccessRef access);
CrSKeychainItemAndAccess(const CrSKeychainItemAndAccess& that);
~CrSKeychainItemAndAccess();
void operator=(const CrSKeychainItemAndAccess& that);
SecKeychainItemRef item() const { return item_; }
SecAccessRef access() const { return access_; }
private:
base::ScopedCFTypeRef<SecKeychainItemRef> item_;
base::ScopedCFTypeRef<SecAccessRef> access_;
};
// Holds the return value from CrSACLCopySimpleContents and an argument to
// CrSACLSetSimpleContents, managing ownership. Used in those wrappers to keep
// logically grouped data together.
struct CrSACLSimpleContents {
CrSACLSimpleContents();
~CrSACLSimpleContents();
base::ScopedCFTypeRef<CFArrayRef> application_list;
base::ScopedCFTypeRef<CFStringRef> description;
CSSM_ACL_KEYCHAIN_PROMPT_SELECTOR prompt_selector;
};
// Holds a SecKeychainAttributeInfo*, calling SecKeychainFreeAttributeInfo on
// destruction.
class ScopedSecKeychainAttributeInfo {
public:
explicit ScopedSecKeychainAttributeInfo(
SecKeychainAttributeInfo* attribute_info);
~ScopedSecKeychainAttributeInfo();
operator SecKeychainAttributeInfo*() const {
return attribute_info_;
}
private:
SecKeychainAttributeInfo* attribute_info_;
};
// Holds the return value from CrSKeychainItemCopyAttributesAndData and an
// argument to CrSKeychainItemCreateFromContent. Used in those wrappers to
// keep logically grouped data together.
struct CrSKeychainItemAttributesAndData {
SecItemClass item_class;
SecKeychainAttributeList* attribute_list;
UInt32 length;
void* data;
};
// Holds a CrSKeychainItemAttributesAndData*, calling
// CrSKeychainItemFreeAttributesAndData and freeing the owned
// CrSKeychainItemAttributesAndData* on destruction.
class ScopedCrSKeychainItemAttributesAndData {
public:
ScopedCrSKeychainItemAttributesAndData(
CrSKeychainItemAttributesAndData* attributes_and_data);
~ScopedCrSKeychainItemAttributesAndData();
CrSKeychainItemAttributesAndData* get() const {
return attributes_and_data_.get();
}
CrSKeychainItemAttributesAndData* release() {
return attributes_and_data_.release();
}
SecItemClass item_class() const {
return attributes_and_data_->item_class;
}
SecItemClass* item_class_ptr() const {
return &attributes_and_data_->item_class;
}
SecKeychainAttributeList* attribute_list() const {
return attributes_and_data_->attribute_list;
}
SecKeychainAttributeList** attribute_list_ptr() const {
return &attributes_and_data_->attribute_list;
}
UInt32 length() const {
return attributes_and_data_->length;
}
UInt32* length_ptr() const {
return &attributes_and_data_->length;
}
void* data() const {
return attributes_and_data_->data;
}
void** data_ptr() const {
return &attributes_and_data_->data;
}
private:
scoped_ptr<CrSKeychainItemAttributesAndData> attributes_and_data_;
};
// Wraps SecKeychainSearchCreateFromAttributes, returning NULL on error and a
// SecKeychainSearchRef owned by the caller on success.
SecKeychainSearchRef CrSKeychainSearchCreateFromAttributes(
CFTypeRef keychain_or_array,
SecItemClass item_class,
const SecKeychainAttributeList* attribute_list);
// Wraps SecKeychainSearchCopyNext, tolerating a NULL argument (resulting in
// a NULL return value but nothing logged), returning NULL on error and a
// SecKeychainItemRef owned by the caller on success.
SecKeychainItemRef CrSKeychainSearchCopyNext(SecKeychainSearchRef search);
// Wraps SecKeychainItemFreeAttributesAndData.
void CrSKeychainItemFreeAttributesAndData(
SecKeychainAttributeList* attribute_list,
void* data);
// Tests access to |item| by calling SecKeychainItemCopyAttributesAndData,
// taking care to properly free any returned data. Returns true if access to
// |item| is authorized. errSecAuthFailed is considered an "expected" error
// for which nothing will be logged, although false will be returned.
bool CrSKeychainItemTestAccess(SecKeychainItemRef item);
// Wraps SecKeychainItemCopyAccess, returning NULL on error and a SecAccessRef
// owned by the caller on success. errSecNoAccessForItem and errSecAuthFailed
// are considered "expected" errors for which nothing will be logged, although
// NULL will be returned.
SecAccessRef CrSKeychainItemCopyAccess(SecKeychainItemRef item);
// Wraps SecAccessCopyACLList, returning NULL on error and a CFArrayRef owned
// by the caller on success.
CFArrayRef CrSAccessCopyACLList(SecAccessRef access);
// Wraps SecACLCopySimpleContents, returning NULL on error and a
// CrSACLSimpleContents* owned by the caller on success. errSecACLNotSimple is
// considered an "expected" error for which nothing will be logged, although
// NULL will be returned.
CrSACLSimpleContents* CrSACLCopySimpleContents(SecACLRef acl);
// Wraps SecTrustedApplicationCopyRequirement, tolerating a NULL argument
// (resulting in a NULL return value but nothing logged) and returning NULL on
// error or a SecRequirementRef owned by the caller on success.
SecRequirementRef CrSTrustedApplicationCopyRequirement(
SecTrustedApplicationRef application);
// Wraps SecRequirementCopyString, tolerating a NULL argument (resulting in
// a NULL return value but nothing logged) and returning NULL on error or a
// CFStringRef owned by the caller on success.
CFStringRef CrSRequirementCopyString(SecRequirementRef requirement,
SecCSFlags flags);
// Wraps SecTrustedApplicationCreateFromPath, returning NULL on error or a
// SecTrustedApplicationRef owned by the caller on success.
SecTrustedApplicationRef CrSTrustedApplicationCreateFromPath(const char* path);
// Wraps SecACLSetSimpleContents, adapting it to the CrSACLSimpleContents
// argument, returning false on error or true on success.
bool CrSACLSetSimpleContents(SecACLRef acl,
const CrSACLSimpleContents& acl_simple_contents);
// Wraps SecKeychainItemCopyKeychain, returning NULL on error or a
// SecKeychainRef owned by the caller on success.
SecKeychainRef CrSKeychainItemCopyKeychain(SecKeychainItemRef item);
// Wraps SecKeychainAttributeInfoForItemID, returning NULL on error or a
// SecKeychainAttributeInfo* owned by the caller on success.
SecKeychainAttributeInfo* CrSKeychainAttributeInfoForItemID(
SecKeychainRef keychain,
UInt32 item_id);
// Wraps SecKeychainItemCopyAttributesAndData, returning NULL on error or a
// CrSKeychainItemAttributesAndData* owned by the caller on success.
CrSKeychainItemAttributesAndData* CrSKeychainItemCopyAttributesAndData(
SecKeychainRef keychain,
SecKeychainItemRef item);
// Wraps SecKeychainItemDelete, returning false on error or true on success.
bool CrSKeychainItemDelete(SecKeychainItemRef item);
// Wraps SecKeychainItemCreateFromContent, adapting it to the
// CrSKeychainItemAttributesAndData argument, returning NULL on error or a
// SecKeychainItemRef owned by the caller on success.
SecKeychainItemRef CrSKeychainItemCreateFromContent(
const CrSKeychainItemAttributesAndData& attributes_and_data,
SecKeychainRef keychain,
SecAccessRef access);
} // namespace chrome
#endif // CHROME_BROWSER_MAC_SECURITY_WRAPPERS_H_
|