]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-76-idstransport.c
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-76-idstransport.c
1 //
2 // secd-76-idstransport.c
3 // sec
4 //
5 //
6
7 /*
8 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
9 *
10 * @APPLE_LICENSE_HEADER_START@
11 *
12 * This file contains Original Code and/or Modifications of Original Code
13 * as defined in and that are subject to the Apple Public Source License
14 * Version 2.0 (the 'License'). You may not use this file except in
15 * compliance with the License. Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this
17 * file.
18 *
19 * The Original Code and all software distributed under the License are
20 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
21 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
22 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
24 * Please see the License for the specific language governing rights and
25 * limitations under the License.
26 *
27 * @APPLE_LICENSE_HEADER_END@
28 */
29
30
31 #include <stdio.h>
32 #include <Security/SecBase.h>
33 #include <Security/SecItem.h>
34
35 #include <Security/SecureObjectSync/SOSAccount.h>
36 #include <Security/SecureObjectSync/SOSCloudCircle.h>
37 #include <Security/SecureObjectSync/SOSInternal.h>
38 #include <Security/SecureObjectSync/SOSFullPeerInfo.h>
39 #include <Security/SecureObjectSync/SOSUserKeygen.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42
43 #include "secd_regressions.h"
44 #include "SOSTestDataSource.h"
45
46 #include "SOSRegressionUtilities.h"
47 #include <utilities/SecCFWrappers.h>
48
49 #include <securityd/SOSCloudCircleServer.h>
50 #include "SecdTestKeychainUtilities.h"
51 #include "SOSAccountTesting.h"
52 #include "SOSTransportTestTransports.h"
53 #include <Security/SecureObjectSync/SOSTransportMessageIDS.h>
54 #include <SOSCircle/CKBridge/SOSCloudKeychainConstants.h>
55 #include "SOSTestDevice.h"
56
57 static int kTestTestCount = 92;
58
59 static void tests()
60 {
61 CFErrorRef error = NULL;
62
63 CFMutableDictionaryRef changes = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
64 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
65 CFStringRef cfaccount = CFSTR("test@test.org");
66
67 SOSAccountRef alice_account = CreateAccountForLocalChanges(CFSTR("Alice"), CFSTR("ak"));
68 SOSAccountRef bob_account = CreateAccountForLocalChanges(CFSTR("Bob"), CFSTR("ak"));
69
70 ok(SOSAccountAssertUserCredentialsAndUpdate(bob_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
71
72 // Bob wins writing at this point, feed the changes back to alice.
73 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 1, "updates");
74
75 ok(SOSAccountAssertUserCredentialsAndUpdate(alice_account, cfaccount, cfpassword, &error), "Credential setting (%@)", error);
76 CFReleaseNull(cfpassword);
77 CFReleaseNull(error);
78
79 ok(NULL != alice_account, "Alice Created");
80 ok(NULL != bob_account, "Bob Created");
81
82 ok(SOSAccountResetToOffering_wTxn(alice_account, &error), "Reset to offering (%@)", error);
83 CFReleaseNull(error);
84
85 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
86
87 ok(SOSAccountJoinCircles_wTxn(bob_account, &error), "Bob Applies (%@)", error);
88 CFReleaseNull(error);
89
90 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
91
92 {
93 CFArrayRef applicants = SOSAccountCopyApplicants(alice_account, &error);
94
95 ok(applicants && CFArrayGetCount(applicants) == 1, "See one applicant %@ (%@)", applicants, error);
96 ok(SOSAccountAcceptApplicants(alice_account, applicants, &error), "Alice accepts (%@)", error);
97 CFReleaseNull(error);
98 CFReleaseNull(applicants);
99 }
100
101 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
102
103 accounts_agree("bob&alice pair", bob_account, alice_account);
104
105 CFArrayRef peers = SOSAccountCopyPeers(alice_account, &error);
106 ok(peers && CFArrayGetCount(peers) == 2, "See two peers %@ (%@)", peers, error);
107 CFReleaseNull(peers);
108
109 //creating test devices
110 CFIndex version = 0;
111
112 // Optionally prefix each peer with name to make them more unique.
113 CFArrayRef deviceIDs = CFArrayCreateForCFTypes(kCFAllocatorDefault,SOSAccountGetMyPeerID(alice_account), SOSAccountGetMyPeerID(bob_account), NULL);
114 CFSetRef views = SOSViewsCopyTestV2Default();
115 CFMutableArrayRef peerMetas = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault);
116 CFStringRef deviceID;
117 CFArrayForEachC(deviceIDs, deviceID) {
118 SOSPeerMetaRef peerMeta = SOSPeerMetaCreateWithComponents(deviceID, views, NULL);
119 CFArrayAppendValue(peerMetas, peerMeta);
120 CFReleaseNull(peerMeta);
121 }
122
123 CFReleaseNull(views);
124 CFArrayForEachC(deviceIDs, deviceID) {
125 SOSTestDeviceRef device = SOSTestDeviceCreateWithDbNamed(kCFAllocatorDefault, deviceID, deviceID);
126 SOSTestDeviceSetPeerIDs(device, peerMetas, version, NULL);
127
128 if(CFEqualSafe(deviceID, SOSAccountGetMyPeerID(alice_account))){
129 alice_account->factory = device->dsf;
130 SOSTestDeviceAddGenericItem(device, CFSTR("Alice"), CFSTR("Alice-add"));
131 }
132 else{
133 bob_account->factory = device->dsf;
134 SOSTestDeviceAddGenericItem(device, CFSTR("Bob"), CFSTR("Bob-add"));
135 }
136 CFReleaseNull(device);
137 }
138 CFReleaseNull(deviceIDs);
139 CFReleaseNull(peerMetas);
140
141 SOSUnregisterAllTransportMessages();
142 CFArrayRemoveAllValues(message_transports);
143
144 alice_account->ids_message_transport = (SOSTransportMessageRef)SOSTransportMessageIDSTestCreate(alice_account, CFSTR("Alice"), SOSCircleGetName(alice_account->trusted_circle), &error);
145 bob_account->ids_message_transport = (SOSTransportMessageRef)SOSTransportMessageIDSTestCreate(bob_account, CFSTR("Bob"), SOSCircleGetName(bob_account->trusted_circle), &error);
146
147 ok(alice_account->ids_message_transport != NULL, "Alice Account, Created IDS Test Transport");
148 ok(bob_account->ids_message_transport != NULL, "Bob Account, Created IDS Test Transport");
149
150 bool result = SOSAccountModifyCircle(alice_account, &error, ^bool(SOSCircleRef circle) {
151 CFErrorRef localError = NULL;
152
153 SOSFullPeerInfoUpdateTransportType(alice_account->my_identity, SOSTransportMessageTypeIDSV2, &localError);
154 SOSFullPeerInfoUpdateTransportPreference(alice_account->my_identity, kCFBooleanFalse, &localError);
155 SOSFullPeerInfoUpdateTransportFragmentationPreference(alice_account->my_identity, kCFBooleanTrue, &localError);
156
157 return SOSCircleHasPeer(circle, SOSFullPeerInfoGetPeerInfo(alice_account->my_identity), NULL);
158 });
159
160 ok(result, "Alice account update circle with transport type");
161
162 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
163
164 result = SOSAccountModifyCircle(bob_account, &error, ^bool(SOSCircleRef circle) {
165 CFErrorRef localError = NULL;
166
167 SOSFullPeerInfoUpdateTransportType(bob_account->my_identity, SOSTransportMessageTypeIDSV2, &localError);
168 SOSFullPeerInfoUpdateTransportPreference(bob_account->my_identity, kCFBooleanFalse, &localError);
169 SOSFullPeerInfoUpdateTransportFragmentationPreference(bob_account->my_identity, kCFBooleanTrue, &localError);
170
171 return SOSCircleHasPeer(circle, SOSFullPeerInfoGetPeerInfo(bob_account->my_identity), NULL);
172 });
173
174 ok(result, "Bob account update circle with transport type");
175 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 2, "updates");
176
177 CFStringRef alice_transportType =SOSPeerInfoCopyTransportType(SOSAccountGetMyPeerInfo(alice_account));
178 CFStringRef bob_accountTransportType = SOSPeerInfoCopyTransportType(SOSAccountGetMyPeerInfo(bob_account));
179 ok(CFEqualSafe(alice_transportType, CFSTR("IDS2.0")), "Alice transport type not IDS");
180 ok(CFEqualSafe(bob_accountTransportType, CFSTR("IDS2.0")), "Bob transport type not IDS");
181
182 CFReleaseNull(alice_transportType);
183 CFReleaseNull(bob_accountTransportType);
184
185 SOSTransportMessageIDSTestSetName(alice_account->ids_message_transport, CFSTR("Alice Account"));
186 ok(SOSTransportMessageIDSTestGetName(alice_account->ids_message_transport) != NULL, "retrieved getting account name");
187 ok(SOSAccountRetrieveDeviceIDFromIDSKeychainSyncingProxy(alice_account, &error) != false, "device ID from IDSKeychainSyncingProxy");
188
189 SOSTransportMessageIDSTestSetName(bob_account->ids_message_transport, CFSTR("Bob Account"));
190 ok(SOSTransportMessageIDSTestGetName(bob_account->ids_message_transport) != NULL, "retrieved getting account name");
191 ok(SOSAccountRetrieveDeviceIDFromIDSKeychainSyncingProxy(bob_account, &error) != false, "device ID from IDSKeychainSyncingProxy");
192
193 ok(SOSAccountSetMyDSID(alice_account, CFSTR("Alice"),&error), "Setting IDS device ID");
194 CFStringRef alice_dsid = SOSAccountCopyDeviceID(alice_account, &error);
195 ok(CFEqualSafe(alice_dsid, CFSTR("Alice")), "Getting IDS device ID");
196
197 ok(SOSAccountSetMyDSID(bob_account, CFSTR("Bob"),&error), "Setting IDS device ID");
198 CFStringRef bob_dsid = SOSAccountCopyDeviceID(bob_account, &error);
199 ok(CFEqualSafe(bob_dsid, CFSTR("Bob")), "Getting IDS device ID");
200
201 is(ProcessChangesUntilNoChange(changes, alice_account, bob_account, NULL), 3, "updates");
202
203 SOSTransportMessageIDSTestSetName(alice_account->ids_message_transport, CFSTR("Alice Account"));
204 ok(SOSTransportMessageIDSTestGetName(alice_account->ids_message_transport) != NULL, "retrieved getting account name");
205 ok(SOSAccountRetrieveDeviceIDFromIDSKeychainSyncingProxy(alice_account, &error) != false, "device ID from IDSKeychainSyncingProxy");
206
207 ok(SOSAccountSetMyDSID(alice_account, CFSTR("DSID"),&error), "Setting IDS device ID");
208 CFStringRef dsid = SOSAccountCopyDeviceID(alice_account, &error);
209 ok(CFEqualSafe(dsid, CFSTR("DSID")), "Getting IDS device ID");
210 CFReleaseNull(dsid);
211
212 ok(SOSAccountStartPingTest(alice_account, CFSTR("hai there!"), &error), "Ping test");
213 ok(CFDictionaryGetCount(SOSTransportMessageIDSTestGetChanges(alice_account->ids_message_transport)) != 0, "ping message made it to transport");
214 SOSTransportMessageIDSTestClearChanges(alice_account->ids_message_transport);
215
216 ok(SOSAccountSendIDSTestMessage(alice_account, CFSTR("hai again!"), &error), "Send Test Message");
217 ok(CFDictionaryGetCount(SOSTransportMessageIDSTestGetChanges(alice_account->ids_message_transport)) != 0, "ping message made it to transport");
218
219 CFStringRef dataKey = CFStringCreateWithCString(kCFAllocatorDefault, kMessageKeyIDSDataMessage, kCFStringEncodingASCII);
220 CFStringRef deviceIDKey = CFStringCreateWithCString(kCFAllocatorDefault, kMessageKeyDeviceID, kCFStringEncodingASCII);
221 CFStringRef sendersPeerIDKey = CFStringCreateWithCString(kCFAllocatorDefault, kMessageKeySendersPeerID, kCFStringEncodingASCII);
222
223 //test IDS message handling
224 CFMutableDictionaryRef messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
225 ok(SOSTransportMessageIDSHandleMessage(alice_account, messageDict, &error) == kHandleIDSMessageDontHandle, "sending empty message dictionary");
226
227 CFDictionaryAddValue(messageDict, deviceIDKey, CFSTR("Alice Account"));
228 ok(SOSTransportMessageIDSHandleMessage(alice_account, messageDict, &error) == kHandleIDSMessageDontHandle, "sending device ID only");
229
230 CFReleaseNull(messageDict);
231 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
232 CFDictionaryAddValue(messageDict, sendersPeerIDKey, CFSTR("Alice Account"));
233 ok(SOSTransportMessageIDSHandleMessage(alice_account, messageDict, &error) == kHandleIDSMessageDontHandle, "sending peer ID only");
234
235 CFReleaseNull(messageDict);
236 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
237 CFDataRef data = CFDataCreate(kCFAllocatorDefault, 0, 0);
238 CFDictionaryAddValue(messageDict, dataKey, data);
239 ok(SOSTransportMessageIDSHandleMessage(alice_account, messageDict, &error) == kHandleIDSMessageDontHandle, "sending data only");
240
241 CFReleaseNull(messageDict);
242 CFReleaseNull(data);
243 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
244 data = CFDataCreate(kCFAllocatorDefault, 0, 0);
245 CFDictionaryAddValue(messageDict, dataKey, data);
246 CFDictionaryAddValue(messageDict, sendersPeerIDKey, CFSTR("Alice Account"));
247 ok(SOSTransportMessageIDSHandleMessage(alice_account, messageDict, &error) == kHandleIDSMessageDontHandle, "sending data and peerid only");
248
249 CFReleaseNull(messageDict);
250 CFReleaseNull(data);
251 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
252 data = CFDataCreate(kCFAllocatorDefault, 0, 0);
253 CFDictionaryAddValue(messageDict, dataKey, data);
254 CFDictionaryAddValue(messageDict, deviceIDKey, CFSTR("Alice Account"));
255 ok(SOSTransportMessageIDSHandleMessage(alice_account, messageDict, &error) == kHandleIDSMessageDontHandle, "sending data and deviceid only");
256
257 CFReleaseNull(messageDict);
258 CFReleaseNull(data);
259 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
260 CFDictionaryAddValue(messageDict, deviceIDKey, CFSTR("Alice Account"));
261 CFDictionaryAddValue(messageDict, sendersPeerIDKey, CFSTR("Alice Account"));
262 ok(SOSTransportMessageIDSHandleMessage(alice_account, messageDict, &error) == kHandleIDSMessageDontHandle, "sending peerid and deviceid only");
263
264 CFReleaseNull(messageDict);
265 CFReleaseNull(data);
266 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
267 data = CFDataCreate(kCFAllocatorDefault, 0, 0);
268 CFDictionaryAddValue(messageDict, dataKey, data);
269 CFDictionaryAddValue(messageDict, deviceIDKey, CFSTR("Alice Account"));
270 CFDictionaryAddValue(messageDict, sendersPeerIDKey, SOSPeerInfoGetPeerID(SOSAccountGetMyPeerInfo(bob_account)));
271 ok(SOSTransportMessageIDSHandleMessage(alice_account, messageDict, &error) == kHandleIDSMessageDontHandle, "sending peerid and deviceid and data");
272
273 CFReleaseNull(messageDict);
274 CFReleaseNull(data);
275
276 messageDict = CFDictionaryCreateMutableForCFTypes(kCFAllocatorDefault);
277 data = CFDataCreate(kCFAllocatorDefault, 0, 0);
278 CFDictionaryAddValue(messageDict, dataKey, data);
279 CFStringRef BobDeviceID = SOSPeerInfoCopyDeviceID(SOSAccountGetMyPeerInfo(bob_account));
280 CFDictionaryAddValue(messageDict, deviceIDKey, BobDeviceID);
281 CFReleaseNull(BobDeviceID);
282 CFDictionaryAddValue(messageDict, sendersPeerIDKey, CFSTR("Alice Account"));
283 ok(SOSTransportMessageIDSHandleMessage(alice_account, messageDict, &error) == kHandleIDSMessageDontHandle, "sending peerid and deviceid and data");
284
285 CFReleaseNull(data);
286 CFReleaseNull(dataKey);
287 CFReleaseNull(deviceIDKey);
288 CFReleaseNull(sendersPeerIDKey);
289
290 CFReleaseNull(alice_account);
291 CFReleaseNull(bob_account);
292 CFReleaseNull(alice_dsid);
293 CFReleaseNull(bob_dsid);
294 CFReleaseNull(changes);
295
296 SOSUnregisterAllTransportMessages();
297 SOSUnregisterAllTransportCircles();
298 SOSUnregisterAllTransportKeyParameters();
299 CFArrayRemoveAllValues(key_transports);
300 CFArrayRemoveAllValues(circle_transports);
301 CFArrayRemoveAllValues(message_transports);
302
303 }
304 int secd_76_idstransport(int argc, char *const *argv)
305 {
306 plan_tests(kTestTestCount);
307
308 secd_test_setup_temp_keychain(__FUNCTION__, NULL);
309
310 tests();
311
312 return 0;
313 }