]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/Regressions/sc-150-ring.m
Security-59306.80.4.tar.gz
[apple/security.git] / keychain / SecureObjectSync / Regressions / sc-150-ring.m
1 //
2 // sc-150-ring.c
3 // sec
4 //
5 // Created by Richard Murphy on 3/3/15.
6 //
7 //
8
9 #include <stdio.h>
10 /*
11 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
12 *
13 * @APPLE_LICENSE_HEADER_START@
14 *
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
20 * file.
21 *
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.
29 *
30 * @APPLE_LICENSE_HEADER_END@
31 */
32
33
34
35 #include <Security/SecBase.h>
36 #include <Security/SecItem.h>
37 #include <Security/SecKeyPriv.h>
38
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"
45
46 #include <utilities/SecCFWrappers.h>
47
48 #include <CoreFoundation/CoreFoundation.h>
49
50 #include <stdlib.h>
51 #include <unistd.h>
52
53 #include "SOSCircle_regressions.h"
54 #include "SOSRegressionUtilities.h"
55
56 static SOSFullPeerInfoRef SOSCreateApplicantFullPeerInfoFromName(CFStringRef peerName,
57 SecKeyRef user_private_key,
58 SecKeyRef* outSigningKey,
59 SecKeyRef* outOctagonSigningKey,
60 SecKeyRef* outOctagonEncryptionKey,
61 CFErrorRef *error)
62 {
63 SOSFullPeerInfoRef result = NULL;
64 SOSFullPeerInfoRef fullPeer = SOSCreateFullPeerInfoFromName(peerName, outSigningKey, outOctagonSigningKey, outOctagonEncryptionKey, error);
65
66 if (fullPeer && SOSFullPeerInfoPromoteToApplication(fullPeer, user_private_key, error))
67 CFTransferRetained(result, fullPeer);
68
69 CFReleaseNull(fullPeer);
70 return result;
71 }
72
73 static int kTestTestCount = 24;
74 static void tests(void)
75 {
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 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);
89
90 ok(cfpassword, "no password");
91
92 CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error);
93 ok(parameters, "No parameters!");
94 ok(error == NULL, "Error: (%@)", error);
95 CFReleaseNull(error);
96
97 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error);
98 CFReleaseNull(parameters);
99
100 SecKeyRef user_pubkey = SecKeyCreatePublicFromPrivate(user_privkey);
101
102
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);
109
110 ok(Ring, "Ring creation");
111
112
113 ok(0 == SOSRingCountPeers(Ring), "Zero peers");
114
115 ok(SOSRingApply(Ring, user_pubkey, peer_a_full_info, NULL));
116 ok(SOSRingApply(Ring, user_pubkey, peer_b_full_info, NULL));
117
118 ok(2 == SOSRingCountPeers(Ring), "Two peers");
119
120 ok(SOSRingWithdraw(Ring, user_privkey, peer_b_full_info, NULL));
121
122 ok(1 == SOSRingCountPeers(Ring), "One peer");
123
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");
128
129 ok(SOSRingResetToEmpty(Ring, peerID_a, NULL), "Reset the circle");
130 ok(kSOSRingNotInRing == SOSRingDeviceIsInRing(Ring, peerID_a), "peer_a is not in Ring");
131
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");
135
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");
140
141 ok(CFEqualSafe(Ring, Ring2), "Compares");
142
143 ok(SOSRingApply(Ring, user_pubkey, peer_c_full_info, NULL));
144 ok(SOSRingApply(Ring, user_pubkey, peer_b_full_info, NULL));
145
146 CFReleaseNull(ringDER);
147 CFReleaseNull(Ring2);
148 ringDER = SOSRingCopyEncodedData(Ring, NULL);
149 Ring2 = SOSRingCreateFromData(NULL, ringDER);
150 ok(CFEqualSafe(Ring, Ring2), "Compares");
151
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);
164
165 CFReleaseNull(user_privkey);
166 CFReleaseNull(user_pubkey);
167
168 CFReleaseNull(peer_a_full_info);
169 CFReleaseNull(peer_b_full_info);
170 CFReleaseNull(peer_c_full_info);
171 CFReleaseNull(Ring);
172 }
173
174 int sc_150_Ring(int argc, char *const *argv)
175 {
176 plan_tests(kTestTestCount);
177
178 tests();
179
180 return 0;
181 }