]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Regressions/sc-150-ring.m
Security-58286.1.32.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / 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 <Security/SecureObjectSync/SOSRing.h>
40 #include <Security/SecureObjectSync/SOSRingTypes.h>
41 #include <Security/SecureObjectSync/SOSRingUtils.h>
42 #include <Security/SecureObjectSync/SOSPeerInfo.h>
43 #include <Security/SecureObjectSync/SOSInternal.h>
44 #include <Security/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, SecKeyRef user_private_key,
57 SecKeyRef* outSigningKey, SecKeyRef* outOctagonSigningKey, CFErrorRef *error)
58 {
59 SOSFullPeerInfoRef result = NULL;
60 SOSFullPeerInfoRef fullPeer = SOSCreateFullPeerInfoFromName(peerName, outSigningKey, outOctagonSigningKey, error);
61
62 if (fullPeer && SOSFullPeerInfoPromoteToApplication(fullPeer, user_private_key, error))
63 CFTransferRetained(result, fullPeer);
64
65 CFReleaseNull(fullPeer);
66 return result;
67 }
68
69 static int kTestTestCount = 24;
70 static void tests(void)
71 {
72
73 //SecKeyRef publicKey = NULL;
74 SecKeyRef dev_a_key = NULL;
75 SecKeyRef dev_b_key = NULL;
76 SecKeyRef dev_c_key = NULL;
77 SecKeyRef oct_dev_a_key = NULL;
78 SecKeyRef oct_dev_b_key = NULL;
79 SecKeyRef oct_dev_c_key = NULL;
80 CFErrorRef error = NULL;
81 CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10);
82
83 ok(cfpassword, "no password");
84
85 CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error);
86 ok(parameters, "No parameters!");
87 ok(error == NULL, "Error: (%@)", error);
88 CFReleaseNull(error);
89
90 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error);
91 CFReleaseNull(parameters);
92
93 SecKeyRef user_pubkey = SecKeyCreatePublicFromPrivate(user_privkey);
94
95
96 SOSFullPeerInfoRef peer_a_full_info = SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer A"), user_privkey, &dev_a_key, &oct_dev_a_key, NULL);
97 SOSFullPeerInfoRef peer_b_full_info = SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer B"), user_privkey, &dev_b_key, &oct_dev_b_key, NULL);
98 SOSFullPeerInfoRef peer_c_full_info = SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer C"), user_privkey, &dev_c_key, &oct_dev_c_key, NULL);
99 CFStringRef peerID_a = SOSPeerInfoGetPeerID(SOSFullPeerInfoGetPeerInfo(peer_a_full_info));
100 CFStringRef peerID_b = SOSPeerInfoGetPeerID(SOSFullPeerInfoGetPeerInfo(peer_b_full_info));
101 SOSRingRef Ring = SOSRingCreate(CFSTR("TESTRING"), peerID_a, kSOSRingBase, NULL);
102
103 ok(Ring, "Ring creation");
104
105
106 ok(0 == SOSRingCountPeers(Ring), "Zero peers");
107
108 ok(SOSRingApply(Ring, user_pubkey, peer_a_full_info, NULL));
109 ok(SOSRingApply(Ring, user_pubkey, peer_b_full_info, NULL));
110
111 ok(2 == SOSRingCountPeers(Ring), "Two peers");
112
113 ok(SOSRingWithdraw(Ring, user_privkey, peer_b_full_info, NULL));
114
115 ok(1 == SOSRingCountPeers(Ring), "One peer");
116
117 ok(kSOSRingMember == SOSRingDeviceIsInRing(Ring, peerID_a), "peer_a is in Ring");
118 ok(kSOSRingNotInRing == SOSRingDeviceIsInRing(Ring, peerID_b), "peer_b is not in Ring");
119 CFStringRef lastmod = SOSRingGetLastModifier(Ring);
120 ok(CFEqual(lastmod, peerID_b), "peer_b_full_info did last mod");
121
122 ok(SOSRingResetToEmpty(Ring, peerID_a, NULL), "Reset the circle");
123 ok(kSOSRingNotInRing == SOSRingDeviceIsInRing(Ring, peerID_a), "peer_a is not in Ring");
124
125 ok(SOSRingResetToOffering(Ring, NULL, peer_a_full_info, NULL), "Reset Ring to Offering for PeerA");
126 ok(kSOSRingMember == SOSRingDeviceIsInRing(Ring, peerID_a), "peer_a is in Ring");
127 ok(kSOSRingNotInRing == SOSRingDeviceIsInRing(Ring, peerID_b), "peer_b is not in Ring");
128
129 CFDataRef ringDER = SOSRingCopyEncodedData(Ring, NULL);
130 ok(ringDER, "Successful encoding to DER of Ring");
131 SOSRingRef Ring2 = SOSRingCreateFromData(NULL, ringDER);
132 ok(Ring2, "Successful decoding of DER to Ring");
133
134 ok(CFEqualSafe(Ring, Ring2), "Compares");
135
136 ok(SOSRingApply(Ring, user_pubkey, peer_c_full_info, NULL));
137 ok(SOSRingApply(Ring, user_pubkey, peer_b_full_info, NULL));
138
139 CFReleaseNull(ringDER);
140 CFReleaseNull(Ring2);
141 ringDER = SOSRingCopyEncodedData(Ring, NULL);
142 Ring2 = SOSRingCreateFromData(NULL, ringDER);
143 ok(CFEqualSafe(Ring, Ring2), "Compares");
144
145 CFReleaseNull(ringDER);
146 CFReleaseNull(Ring2);
147 CFReleaseNull(dev_a_key);
148 CFReleaseNull(dev_b_key);
149 CFReleaseNull(dev_c_key);
150 CFReleaseNull(oct_dev_a_key);
151 CFReleaseNull(oct_dev_b_key);
152 CFReleaseNull(oct_dev_c_key);
153 CFReleaseNull(cfpassword);
154
155 CFReleaseNull(user_privkey);
156 CFReleaseNull(user_pubkey);
157
158 CFReleaseNull(peer_a_full_info);
159 CFReleaseNull(peer_b_full_info);
160 CFReleaseNull(peer_c_full_info);
161 CFReleaseNull(Ring);
162 }
163
164 int sc_150_Ring(int argc, char *const *argv)
165 {
166 plan_tests(kTestTestCount);
167
168 tests();
169
170 return 0;
171 }