]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Regressions/sc-40-circle.c
Security-58286.1.32.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / Regressions / sc-40-circle.c
1 /*
2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
28 #include <Security/SecKey.h>
29
30 #include <Security/SecureObjectSync/SOSCircle.h>
31 #include <Security/SecureObjectSync/SOSCloudCircle.h>
32 #include <Security/SecureObjectSync/SOSPeerInfo.h>
33 #include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
34 #include <Security/SecureObjectSync/SOSInternal.h>
35 #include <Security/SecureObjectSync/SOSUserKeygen.h>
36
37 #include <utilities/SecCFWrappers.h>
38
39 #include <CoreFoundation/CoreFoundation.h>
40
41 #include <stdlib.h>
42 #include <unistd.h>
43
44 #include <securityd/SOSCloudCircleServer.h>
45
46 #include "SOSCircle_regressions.h"
47
48 #include "SOSRegressionUtilities.h"
49
50 static int kTestGenerationCount = 2;
51 static void test_generation(void)
52 {
53 SOSGenCountRef generation = SOSGenerationCreate();
54 SOSGenCountRef newerGeneration = SOSGenerationCreateWithBaseline(generation);
55 SOSGenCountRef evenNewerGeneration = SOSGenerationCreateWithBaseline(newerGeneration);
56
57 ok(SOSGenerationIsOlder(generation, newerGeneration), "should be older");
58 ok(SOSGenerationIsOlder(newerGeneration, evenNewerGeneration), "should be older");
59
60 CFReleaseNull(generation);
61 CFReleaseNull(newerGeneration);
62 CFReleaseNull(evenNewerGeneration);
63 }
64
65
66 static int kTestTestCount = 26;
67 static void tests(void)
68 {
69 SOSCircleRef circle = SOSCircleCreate(NULL, CFSTR("TEST DOMAIN"), NULL);
70
71 ok(NULL != circle, "Circle creation");
72
73 ok(0 == SOSCircleCountPeers(circle), "Zero peers");
74
75 //SecKeyRef publicKey = NULL;
76 SecKeyRef dev_a_key = NULL;
77 SecKeyRef dev_b_key = NULL;
78 SecKeyRef dev_c_key = NULL;
79 SecKeyRef dev_d_key = NULL;
80 SecKeyRef oct_dev_a_key = NULL;
81 SecKeyRef oct_dev_b_key = NULL;
82 SecKeyRef oct_dev_c_key = NULL;
83 SecKeyRef oct_dev_d_key = NULL;
84 CFErrorRef error = NULL;
85 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
86
87 ok(cfpassword, "no password");
88
89 CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error);
90 ok(parameters, "No parameters!");
91 ok(error == NULL, "Error: (%@)", error);
92 CFReleaseNull(error);
93
94 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error);
95 CFReleaseNull(parameters);
96
97 SOSFullPeerInfoRef peer_a_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer A"), &dev_a_key, &oct_dev_a_key, NULL);
98
99 SOSFullPeerInfoRef peer_b_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer B"), &dev_b_key, &oct_dev_b_key, NULL);
100
101 SOSFullPeerInfoRef peer_c_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer C"), &dev_c_key, &oct_dev_c_key, NULL);
102
103 SOSFullPeerInfoRef peer_d_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer D"), &dev_d_key, &oct_dev_d_key, NULL);
104
105 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
106 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
107 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
108
109 ok(SOSCircleAcceptRequest(circle, user_privkey, peer_a_full_info, SOSFullPeerInfoGetPeerInfo(peer_a_full_info), NULL));
110
111 ok(!SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
112 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_b_full_info, NULL));
113
114 ok(SOSCircleCountPeers(circle) == 1, "Peer count");
115
116 size_t size = SOSCircleGetDEREncodedSize(circle, &error);
117 uint8_t buffer[size];
118 uint8_t* start = SOSCircleEncodeToDER(circle, &error, buffer, buffer + sizeof(buffer));
119
120 ok(start, "successful encoding");
121 ok(start == buffer, "Used whole buffer");
122
123 const uint8_t *der = buffer;
124 SOSCircleRef inflated = SOSCircleCreateFromDER(NULL, &error, &der, buffer + sizeof(buffer));
125
126 ok(inflated, "inflated");
127 ok(CFEqualSafe(inflated, circle), "Compares");
128 CFReleaseNull(inflated);
129
130 ok(SOSCircleRemovePeer(circle, user_privkey, peer_a_full_info, SOSFullPeerInfoGetPeerInfo(peer_a_full_info), NULL));
131 ok(SOSCircleCountPeers(circle) == 0, "Peer count");
132
133 // Try multiple peer removal:
134
135 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
136 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_b_full_info, NULL));
137 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_c_full_info, NULL));
138
139 ok(SOSCircleAcceptRequests(circle, user_privkey, peer_a_full_info, NULL));
140
141 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_d_full_info, NULL));
142
143 CFArrayRef peer_array = CFArrayCreateForCFTypes(kCFAllocatorDefault,
144 SOSFullPeerInfoGetPeerInfo(peer_b_full_info),
145 SOSFullPeerInfoGetPeerInfo(peer_c_full_info),
146 SOSFullPeerInfoGetPeerInfo(peer_d_full_info),
147 NULL);
148
149 CFSetRef peers_to_remove = CFSetCreateMutableForSOSPeerInfosByIDWithArray(kCFAllocatorDefault, peer_array);
150 CFReleaseNull(peer_array);
151
152 ok(SOSCircleRemovePeers(circle, user_privkey, peer_a_full_info, peers_to_remove, NULL));
153 CFReleaseNull(peers_to_remove);
154
155 ok(SOSCircleCountPeers(circle) == 1);
156 ok(SOSCircleCountApplicants(circle) == 0);
157
158 CFReleaseNull(dev_a_key);
159 CFReleaseNull(dev_b_key);
160 CFReleaseNull(dev_c_key);
161 CFReleaseNull(dev_d_key);
162 CFReleaseNull(oct_dev_a_key);
163 CFReleaseNull(oct_dev_b_key);
164 CFReleaseNull(oct_dev_c_key);
165 CFReleaseNull(oct_dev_d_key);
166
167 CFReleaseNull(cfpassword);
168
169 CFReleaseNull(peer_a_full_info);
170 CFReleaseNull(peer_b_full_info);
171 CFReleaseNull(peer_c_full_info);
172 CFReleaseNull(peer_d_full_info);
173
174 CFReleaseNull(user_privkey);
175 CFReleaseNull(circle);
176 }
177
178 int sc_40_circle(int argc, char *const *argv)
179 {
180 plan_tests(kTestGenerationCount + kTestTestCount);
181
182 test_generation();
183
184 tests();
185
186 return 0;
187 }