]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/Regressions/sc-40-circle.c
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / SecureObjectSync / 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 "keychain/SecureObjectSync/SOSCircle.h"
31 #include <Security/SecureObjectSync/SOSCloudCircle.h>
32 #include <Security/SecureObjectSync/SOSPeerInfo.h>
33 #include "keychain/SecureObjectSync/SOSPeerInfoCollections.h"
34 #include "keychain/SecureObjectSync/SOSInternal.h"
35 #include "keychain/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 "keychain/securityd/SOSCloudCircleServer.h"
45
46 #include "SOSCircle_regressions.h"
47
48 #include "SOSRegressionUtilities.h"
49
50 #if SOS_ENABLED
51
52 static int kTestGenerationCount = 2;
53 static void test_generation(void)
54 {
55 SOSGenCountRef generation = SOSGenerationCreate();
56 SOSGenCountRef newerGeneration = SOSGenerationCreateWithBaseline(generation);
57 SOSGenCountRef evenNewerGeneration = SOSGenerationCreateWithBaseline(newerGeneration);
58
59 ok(SOSGenerationIsOlder(generation, newerGeneration), "should be older");
60 ok(SOSGenerationIsOlder(newerGeneration, evenNewerGeneration), "should be older");
61
62 CFReleaseNull(generation);
63 CFReleaseNull(newerGeneration);
64 CFReleaseNull(evenNewerGeneration);
65 }
66
67
68 static int kTestTestCount = 26;
69 static void tests(void)
70 {
71 SOSCircleRef circle = SOSCircleCreate(NULL, CFSTR("TEST DOMAIN"), NULL);
72
73 ok(NULL != circle, "Circle creation");
74
75 ok(0 == SOSCircleCountPeers(circle), "Zero peers");
76
77 //SecKeyRef publicKey = NULL;
78 SecKeyRef dev_a_key = NULL;
79 SecKeyRef dev_b_key = NULL;
80 SecKeyRef dev_c_key = NULL;
81 SecKeyRef dev_d_key = NULL;
82 SecKeyRef oct_dev_as_key = NULL;
83 SecKeyRef oct_dev_aw_key = NULL;
84 SecKeyRef oct_dev_bs_key = NULL;
85 SecKeyRef oct_dev_bw_key = NULL;
86 SecKeyRef oct_dev_cs_key = NULL;
87 SecKeyRef oct_dev_cw_key = NULL;
88 SecKeyRef oct_dev_ds_key = NULL;
89 SecKeyRef oct_dev_dw_key = NULL;
90 CFErrorRef error = NULL;
91 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
92
93 ok(cfpassword, "no password");
94
95 CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error);
96 ok(parameters, "No parameters!");
97 ok(error == NULL, "Error: (%@)", error);
98 CFReleaseNull(error);
99
100 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error);
101 CFReleaseNull(parameters);
102
103 SOSFullPeerInfoRef peer_a_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer A"), &dev_a_key, &oct_dev_as_key, &oct_dev_aw_key, NULL);
104
105 SOSFullPeerInfoRef peer_b_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer B"), &dev_b_key, &oct_dev_bs_key, &oct_dev_bw_key, NULL);
106
107 SOSFullPeerInfoRef peer_c_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer C"), &dev_c_key, &oct_dev_cs_key, &oct_dev_cw_key, NULL);
108
109 SOSFullPeerInfoRef peer_d_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer D"), &dev_d_key, &oct_dev_ds_key, &oct_dev_dw_key, NULL);
110
111 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
112 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
113 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
114
115 ok(SOSCircleAcceptRequest(circle, user_privkey, peer_a_full_info, SOSFullPeerInfoGetPeerInfo(peer_a_full_info), NULL));
116
117 ok(!SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
118 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_b_full_info, NULL));
119
120 ok(SOSCircleCountPeers(circle) == 1, "Peer count");
121
122 size_t size = SOSCircleGetDEREncodedSize(circle, &error);
123 uint8_t buffer[size];
124 uint8_t* start = SOSCircleEncodeToDER(circle, &error, buffer, buffer + sizeof(buffer));
125
126 ok(start, "successful encoding");
127 ok(start == buffer, "Used whole buffer");
128
129 const uint8_t *der = buffer;
130 SOSCircleRef inflated = SOSCircleCreateFromDER(NULL, &error, &der, buffer + sizeof(buffer));
131
132 ok(inflated, "inflated");
133 ok(CFEqualSafe(inflated, circle), "Compares");
134 CFReleaseNull(inflated);
135
136 ok(SOSCircleRemovePeer(circle, user_privkey, peer_a_full_info, SOSFullPeerInfoGetPeerInfo(peer_a_full_info), NULL));
137 ok(SOSCircleCountPeers(circle) == 0, "Peer count");
138
139 // Try multiple peer removal:
140
141 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
142 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_b_full_info, NULL));
143 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_c_full_info, NULL));
144
145 ok(SOSCircleAcceptRequests(circle, user_privkey, peer_a_full_info, NULL));
146
147 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_d_full_info, NULL));
148
149 CFArrayRef peer_array = CFArrayCreateForCFTypes(kCFAllocatorDefault,
150 SOSFullPeerInfoGetPeerInfo(peer_b_full_info),
151 SOSFullPeerInfoGetPeerInfo(peer_c_full_info),
152 SOSFullPeerInfoGetPeerInfo(peer_d_full_info),
153 NULL);
154
155 CFSetRef peers_to_remove = CFSetCreateMutableForSOSPeerInfosByIDWithArray(kCFAllocatorDefault, peer_array);
156 CFReleaseNull(peer_array);
157
158 ok(SOSCircleRemovePeers(circle, user_privkey, peer_a_full_info, peers_to_remove, NULL));
159 CFReleaseNull(peers_to_remove);
160
161 ok(SOSCircleCountPeers(circle) == 1);
162 ok(SOSCircleCountApplicants(circle) == 0);
163
164 CFReleaseNull(dev_a_key);
165 CFReleaseNull(dev_b_key);
166 CFReleaseNull(dev_c_key);
167 CFReleaseNull(dev_d_key);
168 CFReleaseNull(oct_dev_as_key);
169 CFReleaseNull(oct_dev_aw_key);
170 CFReleaseNull(oct_dev_bs_key);
171 CFReleaseNull(oct_dev_bw_key);
172 CFReleaseNull(oct_dev_cs_key);
173 CFReleaseNull(oct_dev_cw_key);
174 CFReleaseNull(oct_dev_ds_key);
175 CFReleaseNull(oct_dev_dw_key);
176
177 CFReleaseNull(cfpassword);
178
179 CFReleaseNull(peer_a_full_info);
180 CFReleaseNull(peer_b_full_info);
181 CFReleaseNull(peer_c_full_info);
182 CFReleaseNull(peer_d_full_info);
183
184 CFReleaseNull(user_privkey);
185 CFReleaseNull(circle);
186 }
187 #endif
188
189 int sc_40_circle(int argc, char *const *argv)
190 {
191 #if SOS_ENABLED
192 plan_tests(kTestGenerationCount + kTestTestCount);
193 test_generation();
194 tests();
195 #else
196 plan_tests(0);
197 #endif
198 return 0;
199 }