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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/*
* Copyright (C) 2011 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "config.h"
#import "KeychainItemShimMethods.h"
#if defined(BUILDING_ON_SNOW_LEOPARD)
#import "CoreIPCClientRunLoop.h"
#import "SecKeychainItemRequestData.h"
#import "SecKeychainItemResponseData.h"
#import "WebProcess.h"
#import "WebProcessProxyMessages.h"
#import "WebProcessShim.h"
#import <dlfcn.h>
#import <wtf/MainThread.h>
namespace WebKit {
// Methods to allow the shim to manage memory for its own AttributeList contents.
static HashSet<SecKeychainAttributeList*>& shimManagedAttributeLists()
{
DEFINE_STATIC_LOCAL(HashSet<SecKeychainAttributeList*>, set, ());
return set;
}
static void freeAttributeListContents(SecKeychainAttributeList* attrList)
{
ASSERT(shimManagedAttributeLists().contains(attrList));
ASSERT(attrList);
for (size_t i = 0; i < attrList->count; ++i)
free(attrList->attr[i].data);
shimManagedAttributeLists().remove(attrList);
}
static void allocateAttributeListContents(const Vector<KeychainAttribute>& attributes, SecKeychainAttributeList* attrList)
{
ASSERT(isMainThread());
if (!attrList)
return;
ASSERT(!shimManagedAttributeLists().contains(attrList));
ASSERT(attributes.size() == attrList->count);
shimManagedAttributeLists().add(attrList);
for (size_t i = 0; i < attrList->count; ++i) {
ASSERT(attributes[i].tag == attrList->attr[i].tag);
CFDataRef cfData = attributes[i].data.get();
if (!cfData) {
attrList->attr[i].length = 0;
attrList->attr[i].data = 0;
continue;
}
CFIndex length = CFDataGetLength(cfData);
attrList->attr[i].length = length;
attrList->attr[i].data = malloc(length);
CFDataGetBytes(cfData, CFRangeMake(0, length), static_cast<UInt8*>(attrList->attr[i].data));
}
}
// Methods to allow the shim to manage memory for its own KeychainItem content data.
static HashSet<void*>& shimManagedKeychainItemContents()
{
DEFINE_STATIC_LOCAL(HashSet<void*>, set, ());
return set;
}
static void allocateKeychainItemContentData(CFDataRef cfData, UInt32* length, void** data)
{
ASSERT(isMainThread());
ASSERT((length && data) || (!length && !data));
if (!data)
return;
if (!cfData) {
*data = 0;
*length = 0;
return;
}
*length = CFDataGetLength(cfData);
*data = malloc(*length);
CFDataGetBytes(cfData, CFRangeMake(0, *length), (UInt8*)*data);
shimManagedKeychainItemContents().add(*data);
}
// FIXME (https://bugs.webkit.org/show_bug.cgi?id=60975) - Once CoreIPC supports sync messaging from a secondary thread,
// we can remove FreeAttributeListContext, FreeKeychainItemDataContext, KeychainItemAPIContext, and these 5 main-thread methods,
// and we can have the shim methods call out directly from whatever thread they're called on.
struct FreeAttributeListContext {
SecKeychainAttributeList* attrList;
bool freed;
};
static void webFreeAttributeListContentOnMainThread(void* voidContext)
{
FreeAttributeListContext* context = (FreeAttributeListContext*)voidContext;
if (!shimManagedAttributeLists().contains(context->attrList)) {
context->freed = false;
return;
}
freeAttributeListContents(context->attrList);
context->freed = true;
}
static bool webFreeAttributeListContent(SecKeychainAttributeList* attrList)
{
FreeAttributeListContext context;
context.attrList = attrList;
callOnCoreIPCClientRunLoopAndWait(webFreeAttributeListContentOnMainThread, &context);
return context.freed;
}
struct FreeKeychainItemDataContext {
void* data;
bool freed;
};
static void webFreeKeychainItemContentOnMainThread(void* voidContext)
{
FreeKeychainItemDataContext* context = (FreeKeychainItemDataContext*)voidContext;
if (!shimManagedKeychainItemContents().contains(context->data)) {
context->freed = false;
return;
}
shimManagedKeychainItemContents().remove(context->data);
free(context->data);
context->freed = true;
}
static bool webFreeKeychainItemContent(void* data)
{
FreeKeychainItemDataContext context;
context.data = data;
callOnCoreIPCClientRunLoopAndWait(webFreeKeychainItemContentOnMainThread, &context);
return context.freed;
}
struct SecKeychainItemContext {
SecKeychainItemRef item;
SecKeychainAttributeList* attributeList;
SecItemClass initialItemClass;
UInt32 length;
const void* data;
SecItemClass* resultItemClass;
UInt32* resultLength;
void** resultData;
OSStatus resultCode;
};
static void webSecKeychainItemCopyContentOnMainThread(void* voidContext)
{
SecKeychainItemContext* context = (SecKeychainItemContext*)voidContext;
SecKeychainItemRequestData requestData(context->item, context->attributeList);
SecKeychainItemResponseData response;
if (!WebProcess::shared().connection()->sendSync(Messages::WebProcessProxy::SecKeychainItemCopyContent(requestData), Messages::WebProcessProxy::SecKeychainItemCopyContent::Reply(response), 0)) {
context->resultCode = errSecInteractionNotAllowed;
ASSERT_NOT_REACHED();
return;
}
allocateAttributeListContents(response.attributes(), context->attributeList);
allocateKeychainItemContentData(response.data(), context->resultLength, context->resultData);
if (context->resultItemClass)
*context->resultItemClass = response.itemClass();
context->resultCode = response.resultCode();
}
static OSStatus webSecKeychainItemCopyContent(SecKeychainItemRef item, SecItemClass* itemClass, SecKeychainAttributeList* attrList, UInt32* length, void** outData)
{
SecKeychainItemContext context;
memset(&context, 0, sizeof(SecKeychainItemContext));
context.item = item;
context.resultItemClass = itemClass;
context.attributeList = attrList;
context.resultLength = length;
context.resultData = outData;
callOnCoreIPCClientRunLoopAndWait(webSecKeychainItemCopyContentOnMainThread, &context);
// FIXME: should return context.resultCode. Returning noErr is a workaround for <rdar://problem/9520886>;
// the authentication should fail anyway, since on error no data will be returned.
return noErr;
}
static void webSecKeychainItemCreateFromContentOnMainThread(void* voidContext)
{
SecKeychainItemContext* context = (SecKeychainItemContext*)voidContext;
SecKeychainItemRequestData requestData(context->initialItemClass, context->attributeList, context->length, context->data);
SecKeychainItemResponseData response;
if (!WebProcess::shared().connection()->sendSync(Messages::WebProcessProxy::SecKeychainItemCreateFromContent(requestData), Messages::WebProcessProxy::SecKeychainItemCreateFromContent::Reply(response), 0)) {
context->resultCode = errSecInteractionNotAllowed;
ASSERT_NOT_REACHED();
return;
}
if (response.keychainItem())
CFRetain(response.keychainItem());
context->item = response.keychainItem();
context->resultCode = response.resultCode();
}
static OSStatus webSecKeychainItemCreateFromContent(SecItemClass itemClass, SecKeychainAttributeList* attrList, UInt32 length, const void* data, SecKeychainItemRef *item)
{
SecKeychainItemContext context;
memset(&context, 0, sizeof(SecKeychainItemContext));
context.initialItemClass = itemClass;
context.attributeList = attrList;
context.length = length;
context.data = data;
callOnCoreIPCClientRunLoopAndWait(webSecKeychainItemCreateFromContentOnMainThread, &context);
if (item)
*item = context.item;
else
CFRelease(context.item);
return context.resultCode;
}
static void webSecKeychainItemModifyContentOnMainThread(void* voidContext)
{
SecKeychainItemContext* context = (SecKeychainItemContext*)voidContext;
SecKeychainItemRequestData requestData(context->item, context->attributeList, context->length, context->data);
SecKeychainItemResponseData response;
if (!WebProcess::shared().connection()->sendSync(Messages::WebProcessProxy::SecKeychainItemModifyContent(requestData), Messages::WebProcessProxy::SecKeychainItemModifyContent::Reply(response), 0)) {
context->resultCode = errSecInteractionNotAllowed;
ASSERT_NOT_REACHED();
return;
}
context->resultCode = response.resultCode();
}
static OSStatus webSecKeychainItemModifyContent(SecKeychainItemRef itemRef, const SecKeychainAttributeList* attrList, UInt32 length, const void* data)
{
SecKeychainItemContext context;
context.item = itemRef;
context.attributeList = (SecKeychainAttributeList*)attrList;
context.length = length;
context.data = data;
callOnCoreIPCClientRunLoopAndWait(webSecKeychainItemModifyContentOnMainThread, &context);
return context.resultCode;
}
void initializeKeychainItemShim()
{
const WebProcessKeychainItemShimCallbacks callbacks = {
webSecKeychainItemCopyContent,
webSecKeychainItemCreateFromContent,
webSecKeychainItemModifyContent,
webFreeAttributeListContent,
webFreeKeychainItemContent
};
WebProcessKeychainItemShimInitializeFunc initializeFunction = reinterpret_cast<WebProcessKeychainItemShimInitializeFunc>(dlsym(RTLD_DEFAULT, "WebKitWebProcessKeychainItemShimInitialize"));
initializeFunction(callbacks);
}
} // namespace WebKit
#endif // defined(BUILDING_ON_SNOW_LEOPARD)
|