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 <securityd/SecDbItem.h>
35 static void testNullMessage(uint64_t msgid)
37 SOSMessageRef sentMessage = NULL;
38 SOSMessageRef rcvdMessage = NULL;
39 SOSManifestRef sender = NULL;
40 CFErrorRef error = NULL;
41 CFDataRef data = NULL;
44 ok(sender = SOSManifestCreateWithBytes(NULL, 0, &error), "empty sender manifest create: %@", error);
46 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, sender, NULL, NULL, false, &error), "sentMessage create: %@", error);
48 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@", error);
52 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
54 __block size_t numObjects = 0;
55 SOSMessageWithObjects(sentMessage, &error, ^(CFDataRef object, bool *stop) {
58 ok(numObjects == 0, "no objects");
60 // Check if we got what we started with
61 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
64 CFReleaseNull(sentMessage);
65 CFReleaseNull(rcvdMessage);
66 CFReleaseNull(sender);
69 __unused static void testFlaggedMessage(const char *test_directive, const char *test_reason, uint64_t msgid, SOSMessageFlags flags)
71 SOSMessageRef sentMessage = NULL;
72 SOSMessageRef rcvdMessage = NULL;
73 SOSManifestRef sender = NULL;
74 CFErrorRef error = NULL;
75 CFDataRef data = NULL;
77 ok(sender = SOSManifestCreateWithBytes(NULL, 0, &error), "empty sender manifest create: %@", error);
79 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, sender, NULL, NULL, false, &error), "sentMessage create: %@", error);
81 SOSMessageSetFlags(sentMessage, flags);
82 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@", error);
86 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
88 __block size_t numObjects = 0;
89 SOSMessageWithObjects(sentMessage, &error, ^(CFDataRef object, bool *stop) {
92 ok(numObjects == 0, "no objects");
94 is(SOSMessageGetFlags(sentMessage), flags, "flags match after roundtrip");
95 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
98 CFReleaseNull(sentMessage);
99 CFReleaseNull(rcvdMessage);
100 CFReleaseNull(sender);
103 __unused static void testDeltaManifestMessage(const char *test_directive, const char *test_reason, uint64_t msgid)
105 SOSMessageRef sentMessage = NULL;
106 SOSMessageRef rcvdMessage = NULL;
107 SOSManifestRef sender = NULL;
108 SOSManifestRef proposed = NULL;
109 SOSManifestRef base = NULL;
110 CFErrorRef error = NULL;
111 CFDataRef data = NULL;
113 struct SOSDigestVector dv = SOSDigestVectorInit;
114 SOSDigestVectorAppend(&dv, (const uint8_t *)"sha1 hash that is 20 bytes long or so and stuff");
115 SOSDigestVectorAppend(&dv, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff");
116 SOSDigestVectorSort(&dv);
117 base = SOSManifestCreateWithBytes((const uint8_t *)dv.digest, dv.count * SOSDigestSize, &error);
118 SOSDigestVectorAppend(&dv, (const uint8_t *)"so much more is good to see here is another one for me");
119 SOSDigestVectorAppend(&dv, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff!");
120 SOSDigestVectorAppend(&dv, (const uint8_t *)"so much for is good to see here is another one for me");
121 SOSDigestVectorSort(&dv);
123 proposed = SOSManifestCreateWithBytes((const uint8_t *)dv.digest, dv.count * SOSDigestSize, &error);
125 CFReleaseNull(error);
126 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, proposed, base, proposed, true, &error), "sentMessage create: %@", error);
128 CFReleaseNull(proposed);
129 CFReleaseNull(error);
130 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@ .. %@", error, sentMessage);
131 CFReleaseNull(error);
134 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
135 CFReleaseNull(error);
136 __block size_t numObjects = 0;
137 SOSMessageWithObjects(sentMessage, &error, ^(CFDataRef object, bool *stop) {
140 ok(numObjects == 0, "no objects");
142 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
145 CFReleaseNull(sentMessage);
146 CFReleaseNull(rcvdMessage);
147 CFReleaseNull(sender);
150 static CFDataRef testCopyAddedObject(SOSMessageRef message, CFPropertyListRef plist)
152 CFErrorRef error = NULL;
154 ok(der = CFPropertyListCreateDERData(kCFAllocatorDefault, plist, &error), "copy der: %@", error);
155 CFReleaseNull(error);
156 ok(SOSMessageAppendObject(message, der, &error), "likes object: %@", error);
157 CFReleaseNull(error);
161 __unused static void testObjectsMessage(const char *test_directive, const char *test_reason, uint64_t msgid)
163 SOSMessageRef sentMessage = NULL;
164 SOSMessageRef rcvdMessage = NULL;
165 SOSManifestRef sender = NULL;
166 SOSManifestRef proposed = NULL;
167 SOSManifestRef base = NULL;
168 CFErrorRef error = NULL;
169 CFDataRef data = NULL;
171 struct SOSDigestVector dv1 = SOSDigestVectorInit;
172 struct SOSDigestVector dv2 = SOSDigestVectorInit;
173 SOSDigestVectorAppend(&dv1, (const uint8_t *)"sha1 hash that is 20 bytes long or so and stuff");
174 SOSDigestVectorAppend(&dv2, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff");
175 SOSDigestVectorAppend(&dv1, (const uint8_t *)"so much more is good to see here is another one for me");
176 SOSDigestVectorAppend(&dv2, (const uint8_t *)"so much more is good to see here is another one for me");
177 SOSDigestVectorAppend(&dv1, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff");
178 SOSDigestVectorAppend(&dv1, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff!");
179 SOSDigestVectorAppend(&dv2, (const uint8_t *)"so much for is good to see here is another one for me");
180 SOSDigestVectorSort(&dv1);
181 SOSDigestVectorSort(&dv2);
182 base = SOSManifestCreateWithBytes((const uint8_t *)dv1.digest, dv1.count * SOSDigestSize, &error);
184 proposed = SOSManifestCreateWithBytes((const uint8_t *)dv2.digest, dv2.count * SOSDigestSize, &error);
185 CFReleaseNull(error);
186 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, proposed, base, proposed, true, &error), "sentMessage create: %@", error);
188 CFReleaseNull(proposed);
189 CFDataRef O0, O1, O2, O3;
190 CFDataRef o0 = CFDataCreate(kCFAllocatorDefault, NULL, 0);
191 O0 = testCopyAddedObject(sentMessage, o0);
192 CFDataRef o1 = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)"test", 4);
193 O1 = testCopyAddedObject(sentMessage, o1);
194 CFDataRef o2 = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)"what an object", 14);
195 O2 = testCopyAddedObject(sentMessage, o2);
196 CFDataRef o3 = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)"This one even has shiny stripe.", 31);
197 O3 = testCopyAddedObject(sentMessage, o3);
198 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@ .. %@", error, sentMessage);
199 CFReleaseNull(error);
202 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
203 CFReleaseNull(error);
204 __block size_t numObjects = 0;
205 __block bool f0, f1, f2, f3;
206 f0 = f1 = f2 = f3 = false;
207 if (rcvdMessage) SOSMessageWithObjects(rcvdMessage, &error, ^(CFDataRef object, bool *stop) {
208 if (CFEqualSafe(object, O0)) f0 = true;
209 if (CFEqualSafe(object, O1)) f1 = true;
210 if (CFEqualSafe(object, O2)) f2 = true;
211 if (CFEqualSafe(object, O3)) f3 = true;
219 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
230 CFReleaseNull(sentMessage);
231 CFReleaseNull(rcvdMessage);
232 CFReleaseNull(sender);
235 static void tests(void)
237 testNullMessage(0); // v0
240 testNullMessage(++msgid); // v2
241 testFlaggedMessage(test_directive, test_reason, ++msgid, 0x865);
242 testFlaggedMessage(test_directive, test_reason, ++msgid, 0xdeadbeef);
245 int secd_50_message(int argc, char *const *argv)