2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 #include "keychain/SecureObjectSync/SOSManifest.h"
26 #include "keychain/SecureObjectSync/SOSMessage.h"
28 #include "secd_regressions.h"
30 #include <utilities/SecCFWrappers.h>
31 #include <utilities/der_plist.h>
32 #include "keychain/SecureObjectSync/SOSDigestVector.h"
33 #include "keychain/securityd/SecDbItem.h"
34 #include "SOSAccountTesting.h"
38 static void testNullMessage(uint64_t msgid)
40 SOSMessageRef sentMessage = NULL;
41 SOSMessageRef rcvdMessage = NULL;
42 SOSManifestRef sender = NULL;
43 CFErrorRef error = NULL;
44 CFDataRef data = NULL;
47 ok(sender = SOSManifestCreateWithBytes(NULL, 0, &error), "empty sender manifest create: %@", error);
49 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, sender, NULL, NULL, false, &error), "sentMessage create: %@", error);
51 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@", error);
55 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
57 __block size_t numObjects = 0;
58 SOSMessageWithObjects(sentMessage, &error, ^(CFDataRef object, bool *stop) {
61 ok(numObjects == 0, "no objects");
63 // Check if we got what we started with
64 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
67 CFReleaseNull(sentMessage);
68 CFReleaseNull(rcvdMessage);
69 CFReleaseNull(sender);
72 __unused static void testFlaggedMessage(const char *test_directive, const char *test_reason, uint64_t msgid, SOSMessageFlags flags)
74 SOSMessageRef sentMessage = NULL;
75 SOSMessageRef rcvdMessage = NULL;
76 SOSManifestRef sender = NULL;
77 CFErrorRef error = NULL;
78 CFDataRef data = NULL;
80 ok(sender = SOSManifestCreateWithBytes(NULL, 0, &error), "empty sender manifest create: %@", error);
82 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, sender, NULL, NULL, false, &error), "sentMessage create: %@", error);
84 SOSMessageSetFlags(sentMessage, flags);
85 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@", error);
89 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
91 __block size_t numObjects = 0;
92 SOSMessageWithObjects(sentMessage, &error, ^(CFDataRef object, bool *stop) {
95 ok(numObjects == 0, "no objects");
97 is(SOSMessageGetFlags(sentMessage), flags, "flags match after roundtrip");
98 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
101 CFReleaseNull(sentMessage);
102 CFReleaseNull(rcvdMessage);
103 CFReleaseNull(sender);
106 __unused static void testDeltaManifestMessage(const char *test_directive, const char *test_reason, uint64_t msgid)
108 SOSMessageRef sentMessage = NULL;
109 SOSMessageRef rcvdMessage = NULL;
110 SOSManifestRef sender = NULL;
111 SOSManifestRef proposed = NULL;
112 SOSManifestRef base = NULL;
113 CFErrorRef error = NULL;
114 CFDataRef data = NULL;
116 struct SOSDigestVector dv = SOSDigestVectorInit;
117 SOSDigestVectorAppend(&dv, (const uint8_t *)"sha1 hash that is 20 bytes long or so and stuff");
118 SOSDigestVectorAppend(&dv, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff");
119 SOSDigestVectorSort(&dv);
120 base = SOSManifestCreateWithBytes((const uint8_t *)dv.digest, dv.count * SOSDigestSize, &error);
121 SOSDigestVectorAppend(&dv, (const uint8_t *)"so much more is good to see here is another one for me");
122 SOSDigestVectorAppend(&dv, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff!");
123 SOSDigestVectorAppend(&dv, (const uint8_t *)"so much for is good to see here is another one for me");
124 SOSDigestVectorSort(&dv);
126 proposed = SOSManifestCreateWithBytes((const uint8_t *)dv.digest, dv.count * SOSDigestSize, &error);
128 CFReleaseNull(error);
129 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, proposed, base, proposed, true, &error), "sentMessage create: %@", error);
131 CFReleaseNull(proposed);
132 CFReleaseNull(error);
133 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@ .. %@", error, sentMessage);
134 CFReleaseNull(error);
137 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
138 CFReleaseNull(error);
139 __block size_t numObjects = 0;
140 SOSMessageWithObjects(sentMessage, &error, ^(CFDataRef object, bool *stop) {
143 ok(numObjects == 0, "no objects");
145 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
148 CFReleaseNull(sentMessage);
149 CFReleaseNull(rcvdMessage);
150 CFReleaseNull(sender);
153 static CFDataRef testCopyAddedObject(SOSMessageRef message, CFPropertyListRef plist)
155 CFErrorRef error = NULL;
157 ok(der = CFPropertyListCreateDERData(kCFAllocatorDefault, plist, &error), "copy der: %@", error);
158 CFReleaseNull(error);
159 ok(SOSMessageAppendObject(message, der, &error), "likes object: %@", error);
160 CFReleaseNull(error);
164 __unused static void testObjectsMessage(const char *test_directive, const char *test_reason, uint64_t msgid)
166 SOSMessageRef sentMessage = NULL;
167 SOSMessageRef rcvdMessage = NULL;
168 SOSManifestRef sender = NULL;
169 SOSManifestRef proposed = NULL;
170 SOSManifestRef base = NULL;
171 CFErrorRef error = NULL;
172 CFDataRef data = NULL;
174 struct SOSDigestVector dv1 = SOSDigestVectorInit;
175 struct SOSDigestVector dv2 = SOSDigestVectorInit;
176 SOSDigestVectorAppend(&dv1, (const uint8_t *)"sha1 hash that is 20 bytes long or so and stuff");
177 SOSDigestVectorAppend(&dv2, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff");
178 SOSDigestVectorAppend(&dv1, (const uint8_t *)"so much more is good to see here is another one for me");
179 SOSDigestVectorAppend(&dv2, (const uint8_t *)"so much more is good to see here is another one for me");
180 SOSDigestVectorAppend(&dv1, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff");
181 SOSDigestVectorAppend(&dv1, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff!");
182 SOSDigestVectorAppend(&dv2, (const uint8_t *)"so much for is good to see here is another one for me");
183 SOSDigestVectorSort(&dv1);
184 SOSDigestVectorSort(&dv2);
185 base = SOSManifestCreateWithBytes((const uint8_t *)dv1.digest, dv1.count * SOSDigestSize, &error);
187 proposed = SOSManifestCreateWithBytes((const uint8_t *)dv2.digest, dv2.count * SOSDigestSize, &error);
188 CFReleaseNull(error);
189 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, proposed, base, proposed, true, &error), "sentMessage create: %@", error);
191 CFReleaseNull(proposed);
192 CFDataRef O0, O1, O2, O3;
193 CFDataRef o0 = CFDataCreate(kCFAllocatorDefault, NULL, 0);
194 O0 = testCopyAddedObject(sentMessage, o0);
195 CFDataRef o1 = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)"test", 4);
196 O1 = testCopyAddedObject(sentMessage, o1);
197 CFDataRef o2 = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)"what an object", 14);
198 O2 = testCopyAddedObject(sentMessage, o2);
199 CFDataRef o3 = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)"This one even has shiny stripe.", 31);
200 O3 = testCopyAddedObject(sentMessage, o3);
201 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@ .. %@", error, sentMessage);
202 CFReleaseNull(error);
205 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
206 CFReleaseNull(error);
207 __block size_t numObjects = 0;
208 __block bool f0, f1, f2, f3;
209 f0 = f1 = f2 = f3 = false;
210 if (rcvdMessage) SOSMessageWithObjects(rcvdMessage, &error, ^(CFDataRef object, bool *stop) {
211 if (CFEqualSafe(object, O0)) f0 = true;
212 if (CFEqualSafe(object, O1)) f1 = true;
213 if (CFEqualSafe(object, O2)) f2 = true;
214 if (CFEqualSafe(object, O3)) f3 = true;
222 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
233 CFReleaseNull(sentMessage);
234 CFReleaseNull(rcvdMessage);
235 CFReleaseNull(sender);
238 static void tests(void)
240 testNullMessage(0); // v0
243 testNullMessage(++msgid); // v2
244 testFlaggedMessage(test_directive, test_reason, ++msgid, 0x865);
245 testFlaggedMessage(test_directive, test_reason, ++msgid, 0xdeadbeef);
249 int secd_50_message(int argc, char *const *argv)