]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-50-message.m
Security-58286.1.32.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 <Security/SecureObjectSync/SOSManifest.h>
26 #include <Security/SecureObjectSync/SOSMessage.h>
27
28 #include "secd_regressions.h"
29
30 #include <utilities/SecCFWrappers.h>
31 #include <utilities/der_plist.h>
32 #include <Security/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(error);
128 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@ .. %@", error, sentMessage);
129 CFReleaseNull(error);
130
131 // Decode
132 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
133 CFReleaseNull(error);
134 __block size_t numObjects = 0;
135 SOSMessageWithObjects(sentMessage, &error, ^(CFDataRef object, bool *stop) {
136 numObjects++;
137 });
138 ok(numObjects == 0, "no objects");
139
140 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
141
142 CFReleaseNull(data);
143 CFReleaseNull(sentMessage);
144 CFReleaseNull(rcvdMessage);
145 CFReleaseNull(sender);
146 }
147
148 static CFDataRef testCopyAddedObject(SOSMessageRef message, CFPropertyListRef plist)
149 {
150 CFErrorRef error = NULL;
151 CFDataRef der;
152 ok(der = CFPropertyListCreateDERData(kCFAllocatorDefault, plist, &error), "copy der: %@", error);
153 CFReleaseNull(error);
154 ok(SOSMessageAppendObject(message, der, &error), "likes object: %@", error);
155 CFReleaseNull(error);
156 return der;
157 }
158
159 __unused static void testObjectsMessage(const char *test_directive, const char *test_reason, uint64_t msgid)
160 {
161 SOSMessageRef sentMessage = NULL;
162 SOSMessageRef rcvdMessage = NULL;
163 SOSManifestRef sender = NULL;
164 SOSManifestRef proposed = NULL;
165 SOSManifestRef base = NULL;
166 CFErrorRef error = NULL;
167 CFDataRef data = NULL;
168
169 struct SOSDigestVector dv1 = SOSDigestVectorInit;
170 struct SOSDigestVector dv2 = SOSDigestVectorInit;
171 SOSDigestVectorAppend(&dv1, (const uint8_t *)"sha1 hash that is 20 bytes long or so and stuff");
172 SOSDigestVectorAppend(&dv2, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff");
173 SOSDigestVectorAppend(&dv1, (const uint8_t *)"so much more is good to see here is another one for me");
174 SOSDigestVectorAppend(&dv2, (const uint8_t *)"so much more is good to see here is another one for me");
175 SOSDigestVectorAppend(&dv1, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff");
176 SOSDigestVectorAppend(&dv1, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff!");
177 SOSDigestVectorAppend(&dv2, (const uint8_t *)"so much for is good to see here is another one for me");
178 SOSDigestVectorSort(&dv1);
179 SOSDigestVectorSort(&dv2);
180 base = SOSManifestCreateWithBytes((const uint8_t *)dv1.digest, dv1.count * SOSDigestSize, &error);
181 if (msgid)
182 proposed = SOSManifestCreateWithBytes((const uint8_t *)dv2.digest, dv2.count * SOSDigestSize, &error);
183 CFReleaseNull(error);
184 ok(sentMessage = SOSMessageCreateWithManifests(kCFAllocatorDefault, proposed, base, proposed, true, &error), "sentMessage create: %@", error);
185 CFDataRef O0, O1, O2, O3;
186 CFDataRef o0 = CFDataCreate(kCFAllocatorDefault, NULL, 0);
187 O0 = testCopyAddedObject(sentMessage, o0);
188 CFDataRef o1 = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)"test", 4);
189 O1 = testCopyAddedObject(sentMessage, o1);
190 CFDataRef o2 = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)"what an object", 14);
191 O2 = testCopyAddedObject(sentMessage, o2);
192 CFDataRef o3 = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)"This one even has shiny stripe.", 31);
193 O3 = testCopyAddedObject(sentMessage, o3);
194 ok(data = SOSMessageCreateData(sentMessage, msgid, &error), "sentMessage data create: %@ .. %@", error, sentMessage);
195 CFReleaseNull(error);
196
197 // Decode
198 ok(rcvdMessage = SOSMessageCreateWithData(kCFAllocatorDefault, data, &error), "rcvdMessage create: %@", error);
199 CFReleaseNull(error);
200 __block size_t numObjects = 0;
201 __block bool f0, f1, f2, f3;
202 f0 = f1 = f2 = f3 = false;
203 if (rcvdMessage) SOSMessageWithObjects(rcvdMessage, &error, ^(CFDataRef object, bool *stop) {
204 if (CFEqualSafe(object, O0)) f0 = true;
205 if (CFEqualSafe(object, O1)) f1 = true;
206 if (CFEqualSafe(object, O2)) f2 = true;
207 if (CFEqualSafe(object, O3)) f3 = true;
208 numObjects++;
209 });
210 ok(f0, "got O0");
211 ok(f1, "got O1");
212 ok(f2, "got O2");
213 ok(f3, "got O3");
214
215 ok(sentMessage && rcvdMessage && CFEqual(sentMessage, rcvdMessage), "sent %@ == rcvd %@", sentMessage, rcvdMessage);
216
217 CFReleaseNull(o0);
218 CFReleaseNull(o1);
219 CFReleaseNull(o2);
220 CFReleaseNull(o3);
221 CFReleaseNull(O0);
222 CFReleaseNull(O1);
223 CFReleaseNull(O2);
224 CFReleaseNull(O3);
225 CFReleaseNull(data);
226 CFReleaseNull(sentMessage);
227 CFReleaseNull(rcvdMessage);
228 CFReleaseNull(sender);
229 }
230
231 static void tests(void)
232 {
233 testNullMessage(0); // v0
234
235 uint64_t msgid = 0;
236 testNullMessage(++msgid); // v2
237 testFlaggedMessage(test_directive, test_reason, ++msgid, 0x865);
238 testFlaggedMessage(test_directive, test_reason, ++msgid, 0xdeadbeef);
239 }
240
241 int secd_50_message(int argc, char *const *argv)
242 {
243 plan_tests(26);
244
245 tests();
246
247 return 0;
248 }