5 // Created by Richard Murphy on 3/3/15.
11 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
13 * @APPLE_LICENSE_HEADER_START@
15 * This file contains Original Code and/or Modifications of Original Code
16 * as defined in and that are subject to the Apple Public Source License
17 * Version 2.0 (the 'License'). You may not use this file except in
18 * compliance with the License. Please obtain a copy of the License at
19 * http://www.opensource.apple.com/apsl/ and read it before using this
22 * The Original Code and all software distributed under the License are
23 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
24 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
25 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
27 * Please see the License for the specific language governing rights and
28 * limitations under the License.
30 * @APPLE_LICENSE_HEADER_END@
35 #include <Security/SecBase.h>
36 #include <Security/SecItem.h>
37 #include <Security/SecKeyPriv.h>
39 #include "keychain/SecureObjectSync/SOSRing.h"
40 #include "keychain/SecureObjectSync/SOSRingTypes.h"
41 #include "keychain/SecureObjectSync/SOSRingUtils.h"
42 #include <Security/SecureObjectSync/SOSPeerInfo.h>
43 #include "keychain/SecureObjectSync/SOSInternal.h"
44 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
46 #include <utilities/SecCFWrappers.h>
48 #include <CoreFoundation/CoreFoundation.h>
53 #include "SOSCircle_regressions.h"
54 #include "SOSRegressionUtilities.h"
56 static SOSFullPeerInfoRef SOSCreateApplicantFullPeerInfoFromName(CFStringRef peerName,
57 SecKeyRef user_private_key,
58 SecKeyRef* outSigningKey,
59 SecKeyRef* outOctagonSigningKey,
60 SecKeyRef* outOctagonEncryptionKey,
63 SOSFullPeerInfoRef result = NULL;
64 SOSFullPeerInfoRef fullPeer = SOSCreateFullPeerInfoFromName(peerName, outSigningKey, outOctagonSigningKey, outOctagonEncryptionKey, error);
66 if (fullPeer && SOSFullPeerInfoPromoteToApplication(fullPeer, user_private_key, error))
67 CFTransferRetained(result, fullPeer);
69 CFReleaseNull(fullPeer);
73 static int kTestTestCount = 24;
74 static void tests(void)
77 //SecKeyRef publicKey = NULL;
78 SecKeyRef dev_a_key = NULL;
79 SecKeyRef dev_b_key = NULL;
80 SecKeyRef dev_c_key = NULL;
81 SecKeyRef oct_dev_as_key = NULL;
82 SecKeyRef oct_dev_aw_key = NULL;
83 SecKeyRef oct_dev_bs_key = NULL;
84 SecKeyRef oct_dev_bw_key = NULL;
85 SecKeyRef oct_dev_cs_key = NULL;
86 SecKeyRef oct_dev_cw_key = NULL;
87 CFErrorRef error = NULL;
88 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
90 ok(cfpassword, "no password");
92 CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error);
93 ok(parameters, "No parameters!");
94 ok(error == NULL, "Error: (%@)", error);
97 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error);
98 CFReleaseNull(parameters);
100 SecKeyRef user_pubkey = SecKeyCreatePublicFromPrivate(user_privkey);
103 SOSFullPeerInfoRef peer_a_full_info = SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer A"), user_privkey, &dev_a_key, &oct_dev_as_key, &oct_dev_aw_key, NULL);
104 SOSFullPeerInfoRef peer_b_full_info = SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer B"), user_privkey, &dev_b_key, &oct_dev_bs_key, &oct_dev_bw_key, NULL);
105 SOSFullPeerInfoRef peer_c_full_info = SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer C"), user_privkey, &dev_c_key, &oct_dev_cs_key, &oct_dev_cw_key, NULL);
106 CFStringRef peerID_a = SOSPeerInfoGetPeerID(SOSFullPeerInfoGetPeerInfo(peer_a_full_info));
107 CFStringRef peerID_b = SOSPeerInfoGetPeerID(SOSFullPeerInfoGetPeerInfo(peer_b_full_info));
108 SOSRingRef Ring = SOSRingCreate(CFSTR("TESTRING"), peerID_a, kSOSRingBase, NULL);
110 ok(Ring, "Ring creation");
113 ok(0 == SOSRingCountPeers(Ring), "Zero peers");
115 ok(SOSRingApply(Ring, user_pubkey, peer_a_full_info, NULL));
116 ok(SOSRingApply(Ring, user_pubkey, peer_b_full_info, NULL));
118 ok(2 == SOSRingCountPeers(Ring), "Two peers");
120 ok(SOSRingWithdraw(Ring, user_privkey, peer_b_full_info, NULL));
122 ok(1 == SOSRingCountPeers(Ring), "One peer");
124 ok(kSOSRingMember == SOSRingDeviceIsInRing(Ring, peerID_a), "peer_a is in Ring");
125 ok(kSOSRingNotInRing == SOSRingDeviceIsInRing(Ring, peerID_b), "peer_b is not in Ring");
126 CFStringRef lastmod = SOSRingGetLastModifier(Ring);
127 ok(CFEqual(lastmod, peerID_b), "peer_b_full_info did last mod");
129 ok(SOSRingResetToEmpty(Ring, peerID_a, NULL), "Reset the circle");
130 ok(kSOSRingNotInRing == SOSRingDeviceIsInRing(Ring, peerID_a), "peer_a is not in Ring");
132 ok(SOSRingResetToOffering(Ring, NULL, peer_a_full_info, NULL), "Reset Ring to Offering for PeerA");
133 ok(kSOSRingMember == SOSRingDeviceIsInRing(Ring, peerID_a), "peer_a is in Ring");
134 ok(kSOSRingNotInRing == SOSRingDeviceIsInRing(Ring, peerID_b), "peer_b is not in Ring");
136 CFDataRef ringDER = SOSRingCopyEncodedData(Ring, NULL);
137 ok(ringDER, "Successful encoding to DER of Ring");
138 SOSRingRef Ring2 = SOSRingCreateFromData(NULL, ringDER);
139 ok(Ring2, "Successful decoding of DER to Ring");
141 ok(CFEqualSafe(Ring, Ring2), "Compares");
143 ok(SOSRingApply(Ring, user_pubkey, peer_c_full_info, NULL));
144 ok(SOSRingApply(Ring, user_pubkey, peer_b_full_info, NULL));
146 CFReleaseNull(ringDER);
147 CFReleaseNull(Ring2);
148 ringDER = SOSRingCopyEncodedData(Ring, NULL);
149 Ring2 = SOSRingCreateFromData(NULL, ringDER);
150 ok(CFEqualSafe(Ring, Ring2), "Compares");
152 CFReleaseNull(ringDER);
153 CFReleaseNull(Ring2);
154 CFReleaseNull(dev_a_key);
155 CFReleaseNull(dev_b_key);
156 CFReleaseNull(dev_c_key);
157 CFReleaseNull(oct_dev_as_key);
158 CFReleaseNull(oct_dev_aw_key);
159 CFReleaseNull(oct_dev_bs_key);
160 CFReleaseNull(oct_dev_bw_key);
161 CFReleaseNull(oct_dev_cs_key);
162 CFReleaseNull(oct_dev_cw_key);
163 CFReleaseNull(cfpassword);
165 CFReleaseNull(user_privkey);
166 CFReleaseNull(user_pubkey);
168 CFReleaseNull(peer_a_full_info);
169 CFReleaseNull(peer_b_full_info);
170 CFReleaseNull(peer_c_full_info);
174 int sc_150_Ring(int argc, char *const *argv)
176 plan_tests(kTestTestCount);