]>
Commit | Line | Data |
---|---|---|
5c19dc3a A |
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, CFErrorRef *error) | |
58 | { | |
59 | SOSFullPeerInfoRef result = NULL; | |
60 | SOSFullPeerInfoRef fullPeer = SOSCreateFullPeerInfoFromName(peerName, outSigningKey, 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 | CFErrorRef error = NULL; | |
78 | CFDataRef cfpassword = CFDataCreate(NULL, (uint8_t *) "FooFooFoo", 10); | |
79 | ||
80 | ok(cfpassword, "no password"); | |
81 | ||
82 | CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error); | |
83 | ok(parameters, "No parameters!"); | |
84 | ok(error == NULL, "Error: (%@)", error); | |
85 | CFReleaseNull(error); | |
86 | ||
87 | SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error); | |
88 | CFReleaseNull(parameters); | |
89 | ||
90 | SecKeyRef user_pubkey = SecKeyCreatePublicFromPrivate(user_privkey); | |
91 | ||
92 | ||
93 | SOSFullPeerInfoRef peer_a_full_info = SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer A"), user_privkey, &dev_a_key, NULL); | |
94 | SOSFullPeerInfoRef peer_b_full_info = SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer B"), user_privkey, &dev_b_key, NULL); | |
95 | SOSFullPeerInfoRef peer_c_full_info = SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer C"), user_privkey, &dev_c_key, NULL); | |
96 | CFStringRef peerID_a = SOSPeerInfoGetPeerID(SOSFullPeerInfoGetPeerInfo(peer_a_full_info)); | |
97 | CFStringRef peerID_b = SOSPeerInfoGetPeerID(SOSFullPeerInfoGetPeerInfo(peer_b_full_info)); | |
98 | SOSRingRef Ring = SOSRingCreate(CFSTR("TESTRING"), peerID_a, kSOSRingBase, NULL); | |
99 | ||
100 | ok(Ring, "Ring creation"); | |
101 | ||
102 | ||
103 | ok(0 == SOSRingCountPeers(Ring), "Zero peers"); | |
104 | ||
105 | ok(SOSRingApply(Ring, user_pubkey, peer_a_full_info, NULL)); | |
106 | ok(SOSRingApply(Ring, user_pubkey, peer_b_full_info, NULL)); | |
107 | ||
108 | ok(2 == SOSRingCountPeers(Ring), "Two peers"); | |
109 | ||
110 | ok(SOSRingWithdraw(Ring, user_privkey, peer_b_full_info, NULL)); | |
111 | ||
112 | ok(1 == SOSRingCountPeers(Ring), "One peer"); | |
113 | ||
114 | ok(kSOSRingMember == SOSRingDeviceIsInRing(Ring, peerID_a), "peer_a is in Ring"); | |
115 | ok(kSOSRingNotInRing == SOSRingDeviceIsInRing(Ring, peerID_b), "peer_b is not in Ring"); | |
116 | CFStringRef lastmod = SOSRingGetLastModifier(Ring); | |
117 | ok(CFEqual(lastmod, peerID_b), "peer_b_full_info did last mod"); | |
118 | ||
119 | ok(SOSRingResetToEmpty(Ring, peerID_a, NULL), "Reset the circle"); | |
120 | ok(kSOSRingNotInRing == SOSRingDeviceIsInRing(Ring, peerID_a), "peer_a is not in Ring"); | |
121 | ||
122 | ok(SOSRingResetToOffering(Ring, NULL, peer_a_full_info, NULL), "Reset Ring to Offering for PeerA"); | |
123 | ok(kSOSRingMember == SOSRingDeviceIsInRing(Ring, peerID_a), "peer_a is in Ring"); | |
124 | ok(kSOSRingNotInRing == SOSRingDeviceIsInRing(Ring, peerID_b), "peer_b is not in Ring"); | |
125 | ||
126 | CFDataRef ringDER = SOSRingCopyEncodedData(Ring, NULL); | |
127 | ok(ringDER, "Successful encoding to DER of Ring"); | |
128 | SOSRingRef Ring2 = SOSRingCreateFromData(NULL, ringDER); | |
129 | ok(Ring2, "Successful decoding of DER to Ring"); | |
130 | ||
131 | ok(CFEqualSafe(Ring, Ring2), "Compares"); | |
132 | ||
133 | ok(SOSRingApply(Ring, user_pubkey, peer_c_full_info, NULL)); | |
134 | ok(SOSRingApply(Ring, user_pubkey, peer_b_full_info, NULL)); | |
135 | ||
136 | CFReleaseNull(ringDER); | |
137 | CFReleaseNull(Ring2); | |
138 | ringDER = SOSRingCopyEncodedData(Ring, NULL); | |
139 | Ring2 = SOSRingCreateFromData(NULL, ringDER); | |
140 | ok(CFEqualSafe(Ring, Ring2), "Compares"); | |
141 | ||
142 | CFReleaseNull(ringDER); | |
143 | CFReleaseNull(Ring2); | |
144 | CFReleaseNull(dev_a_key); | |
fa7225c8 A |
145 | CFReleaseNull(dev_b_key); |
146 | CFReleaseNull(dev_c_key); | |
5c19dc3a A |
147 | CFReleaseNull(cfpassword); |
148 | ||
149 | CFReleaseNull(user_privkey); | |
150 | CFReleaseNull(user_pubkey); | |
fa7225c8 A |
151 | |
152 | CFReleaseNull(peer_a_full_info); | |
153 | CFReleaseNull(peer_b_full_info); | |
154 | CFReleaseNull(peer_c_full_info); | |
155 | CFReleaseNull(Ring); | |
5c19dc3a A |
156 | } |
157 | ||
158 | int sc_150_Ring(int argc, char *const *argv) | |
159 | { | |
160 | plan_tests(kTestTestCount); | |
161 | ||
162 | tests(); | |
163 | ||
164 | return 0; | |
165 | } |