]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-50-message.m
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-50-message.m
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #include "keychain/SecureObjectSync/SOSManifest.h"
26 #include "keychain/SecureObjectSync/SOSMessage.h"
27
28 #include "secd_regressions.h"
29
30 #include <utilities/SecCFWrappers.h>
31 #include <utilities/der_plist.h>
32 #include "keychain/SecureObjectSync/SOSDigestVector.h"
33 #include <securityd/SecDbItem.h>
34
35 static void testNullMessage(uint64_t msgid)
36 {
37 SOSMessageRef sentMessage = NULL;
38 SOSMessageRef rcvdMessage = NULL;
39 SOSManifestRef sender = NULL;
40 CFErrorRef error = NULL;
41 CFDataRef data = NULL;
42
43 // Encode
44 ok(sender = SOSManifestCreateWithBytes(NULL, 0, &error), "empty sender manifest create: %@", error);
45 CFReleaseNull(error);
46 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, sender, NULL, NULL, false, &error), "sentMessage create: %@", error);
47 CFReleaseNull(error);
48 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@", error);
49 CFReleaseNull(error);
50
51 // Decode
52 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
53 CFReleaseNull(error);
54 __block size_t numObjects = 0;
55 SOSMessageWithObjects(sentMessage, &error, ^(CFDataRef object, bool *stop) {
56 numObjects++;
57 });
58 ok(numObjects == 0, "no objects");
59
60 // Check if we got what we started with
61 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
62
63 CFReleaseNull(data);
64 CFReleaseNull(sentMessage);
65 CFReleaseNull(rcvdMessage);
66 CFReleaseNull(sender);
67 }
68
69 __unused static void testFlaggedMessage(const char *test_directive, const char *test_reason, uint64_t msgid, SOSMessageFlags flags)
70 {
71 SOSMessageRef sentMessage = NULL;
72 SOSMessageRef rcvdMessage = NULL;
73 SOSManifestRef sender = NULL;
74 CFErrorRef error = NULL;
75 CFDataRef data = NULL;
76
77 ok(sender = SOSManifestCreateWithBytes(NULL, 0, &error), "empty sender manifest create: %@", error);
78 CFReleaseNull(error);
79 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, sender, NULL, NULL, false, &error), "sentMessage create: %@", error);
80 CFReleaseNull(error);
81 SOSMessageSetFlags(sentMessage, flags);
82 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@", error);
83 CFReleaseNull(error);
84
85 // Decode
86 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
87 CFReleaseNull(error);
88 __block size_t numObjects = 0;
89 SOSMessageWithObjects(sentMessage, &error, ^(CFDataRef object, bool *stop) {
90 numObjects++;
91 });
92 ok(numObjects == 0, "no objects");
93
94 is(SOSMessageGetFlags(sentMessage), flags, "flags match after roundtrip");
95 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
96
97 CFReleaseNull(data);
98 CFReleaseNull(sentMessage);
99 CFReleaseNull(rcvdMessage);
100 CFReleaseNull(sender);
101 }
102
103 __unused static void testDeltaManifestMessage(const char *test_directive, const char *test_reason, uint64_t msgid)
104 {
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;
112
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);
122 if (msgid)
123 proposed = SOSManifestCreateWithBytes((const uint8_t *)dv.digest, dv.count * SOSDigestSize, &error);
124
125 CFReleaseNull(error);
126 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, proposed, base, proposed, true, &error), "sentMessage create: %@", error);
127 CFReleaseNull(base);
128 CFReleaseNull(proposed);
129 CFReleaseNull(error);
130 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@ .. %@", error, sentMessage);
131 CFReleaseNull(error);
132
133 // Decode
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) {
138 numObjects++;
139 });
140 ok(numObjects == 0, "no objects");
141
142 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
143
144 CFReleaseNull(data);
145 CFReleaseNull(sentMessage);
146 CFReleaseNull(rcvdMessage);
147 CFReleaseNull(sender);
148 }
149
150 static CFDataRef testCopyAddedObject(SOSMessageRef message, CFPropertyListRef plist)
151 {
152 CFErrorRef error = NULL;
153 CFDataRef der;
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);
158 return der;
159 }
160
161 __unused static void testObjectsMessage(const char *test_directive, const char *test_reason, uint64_t msgid)
162 {
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;
170
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);
183 if (msgid)
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);
187 CFReleaseNull(base);
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);
200
201 // Decode
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;
212 numObjects++;
213 });
214 ok(f0, "got O0");
215 ok(f1, "got O1");
216 ok(f2, "got O2");
217 ok(f3, "got O3");
218
219 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
220
221 CFReleaseNull(o0);
222 CFReleaseNull(o1);
223 CFReleaseNull(o2);
224 CFReleaseNull(o3);
225 CFReleaseNull(O0);
226 CFReleaseNull(O1);
227 CFReleaseNull(O2);
228 CFReleaseNull(O3);
229 CFReleaseNull(data);
230 CFReleaseNull(sentMessage);
231 CFReleaseNull(rcvdMessage);
232 CFReleaseNull(sender);
233 }
234
235 static void tests(void)
236 {
237 testNullMessage(0); // v0
238
239 uint64_t msgid = 0;
240 testNullMessage(++msgid); // v2
241 testFlaggedMessage(test_directive, test_reason, ++msgid, 0x865);
242 testFlaggedMessage(test_directive, test_reason, ++msgid, 0xdeadbeef);
243 }
244
245 int secd_50_message(int argc, char *const *argv)
246 {
247 plan_tests(26);
248
249 tests();
250
251 return 0;
252 }