2 * Copyright (c) 2011-2012,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@
27 #include "Security_regressions.h"
29 #include <CoreFoundation/CFData.h>
30 #include <Security/SecOTRSession.h>
31 #include <Security/SecInternal.h>
32 #include <Security/SecBasePriv.h>
34 static void RegressionsLogError(CFErrorRef error
) {
38 CFDictionaryRef tempDictionary
= CFErrorCopyUserInfo(error
);
39 CFIndex errorCode
= CFErrorGetCode(error
);
40 CFStringRef errorDomain
= CFErrorGetDomain(error
);
41 CFStringRef errorString
= CFDictionaryGetValue(tempDictionary
, kCFErrorDescriptionKey
);
42 CFErrorRef previousError
= (CFErrorRef
)CFDictionaryGetValue(tempDictionary
, kCFErrorUnderlyingErrorKey
);
43 if (previousError
!= NULL
) {
44 RegressionsLogError(previousError
);
46 char errorDomainStr
[1024];
47 char errorStringStr
[1024];
49 CFStringGetCString(errorDomain
, errorDomainStr
, 1024, kCFStringEncodingUTF8
);
50 CFStringGetCString(errorString
, errorStringStr
, 1024, kCFStringEncodingUTF8
);
51 printf("OTR: %s (%ld) -- %s\n", errorDomainStr
, errorCode
, errorStringStr
);
52 CFReleaseSafe(tempDictionary
);
55 static int kTestTestCount
= 18;
56 static void tests(void)
58 CFErrorRef testError
= NULL
;
60 SecOTRFullIdentityRef idToPurge
= SecOTRFullIdentityCreate(kCFAllocatorDefault
, &testError
);
61 ok(idToPurge
!= NULL
, "Make Identity: %@", testError
);
62 RegressionsLogError(testError
);
63 CFReleaseNull(testError
);
65 CFMutableDataRef purgeExport
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
67 ok(SecOTRFIAppendSerialization(idToPurge
, purgeExport
, &testError
), "First export: %@", testError
);
68 RegressionsLogError(testError
);
69 CFReleaseNull(testError
);
71 SecOTRFullIdentityRef purgeIdInflate
= SecOTRFullIdentityCreateFromData(kCFAllocatorDefault
, purgeExport
, &testError
);
72 ok(purgeIdInflate
!= NULL
, "Inflate Identity: %@", testError
);
73 RegressionsLogError(testError
);
74 CFReleaseNull(testError
);
76 SecOTRFIPurgeFromKeychain(idToPurge
, &testError
);
77 RegressionsLogError(testError
);
78 CFReleaseNull(testError
);
80 SecOTRFullIdentityRef failIDInflate
= SecOTRFullIdentityCreateFromData(kCFAllocatorDefault
, purgeExport
, &testError
);
81 ok(failIDInflate
== NULL
, "Should fail: %@", testError
);
82 RegressionsLogError(testError
);
83 CFReleaseNull(testError
);
86 CFReleaseSafe(idToPurge
);
89 idToPurge
= SecOTRFullIdentityCreate(kCFAllocatorDefault
, &testError
);
90 ok(idToPurge
!= NULL
, "Make Identity again: %@", testError
);
91 RegressionsLogError(testError
);
92 CFReleaseNull(testError
);
94 SecOTRFIPurgeAllFromKeychain(&testError
);
95 RegressionsLogError(testError
);
96 CFReleaseNull(testError
);
98 SecOTRFullIdentityRef failIDInflate2
= SecOTRFullIdentityCreateFromData(kCFAllocatorDefault
, purgeExport
, &testError
);
99 ok(failIDInflate2
== NULL
, "Should fail 2: %@", testError
);
100 RegressionsLogError(testError
);
101 CFReleaseNull(testError
);
103 SecOTRFullIdentityRef id
= SecOTRFullIdentityCreate(kCFAllocatorDefault
, &testError
);
104 ok(id
!= NULL
, "Make Identity: %@", testError
);
105 RegressionsLogError(testError
);
106 CFReleaseNull(testError
);
108 CFMutableDataRef firstExport
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
110 ok(SecOTRFIAppendSerialization(id
, firstExport
, &testError
), "First export: %@", testError
);
111 RegressionsLogError(testError
);
112 CFReleaseNull(testError
);
114 SecOTRFullIdentityRef idInflate
= SecOTRFullIdentityCreateFromData(kCFAllocatorDefault
, firstExport
, &testError
);
115 ok(idInflate
!= NULL
, "Inflate Identity: %@", testError
);
116 RegressionsLogError(testError
);
117 CFReleaseNull(testError
);
119 CFMutableDataRef secondExport
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
121 ok(SecOTRFIAppendSerialization(idInflate
, secondExport
, &testError
), "second export: %@", testError
);
122 RegressionsLogError(testError
);
123 CFReleaseNull(testError
);
125 ok(CFDataGetLength(firstExport
) == CFDataGetLength(secondExport
)
126 && 0 == memcmp(CFDataGetBytePtr(firstExport
), CFDataGetBytePtr(secondExport
), (size_t)CFDataGetLength(firstExport
)), "Different exports");
128 SecOTRPublicIdentityRef pubID
= SecOTRPublicIdentityCopyFromPrivate(kCFAllocatorDefault
, id
, &testError
);
129 ok(id
!= NULL
, "Failed to copy public identity: %@", testError
);
130 RegressionsLogError(testError
);
131 CFReleaseNull(testError
);
133 CFMutableDataRef firstPublicExport
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
135 ok(SecOTRPIAppendSerialization(pubID
, firstPublicExport
, &testError
), "failed first public export: %@", testError
);
136 RegressionsLogError(testError
);
137 CFReleaseNull(testError
);
139 SecOTRPublicIdentityRef pubIDInflate
= SecOTRPublicIdentityCreateFromData(kCFAllocatorDefault
, firstPublicExport
, &testError
);
140 ok(pubIDInflate
!= NULL
, "Pub inflate failed: %@", testError
);
141 RegressionsLogError(testError
);
142 CFReleaseNull(testError
);
144 CFMutableDataRef secondPublicExport
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
146 ok(SecOTRPIAppendSerialization(pubID
, secondPublicExport
, &testError
), "failed second public export: %@", testError
);
147 RegressionsLogError(testError
);
148 CFReleaseNull(testError
);
150 ok(CFDataGetLength(firstPublicExport
) == CFDataGetLength(secondPublicExport
)
151 && 0 == memcmp(CFDataGetBytePtr(firstPublicExport
), CFDataGetBytePtr(secondPublicExport
), (size_t)CFDataGetLength(firstPublicExport
)), "Different public exports");
153 uint8_t sampleByteString
[] = {
154 0x30, 0x81, 0xf6, 0x81, 0x43, 0x00, 0x41, 0x04, 0xc6, 0x8a, 0x2a, 0x5c, 0x29, 0xa4, 0xb7, 0x58,
155 0xe1, 0x3c, 0x07, 0x19, 0x20, 0xf3, 0x0b, 0xb8, 0xb3, 0x40, 0x41, 0x29, 0x4a, 0xa6, 0x7a, 0x56,
156 0x28, 0x6d, 0x10, 0x85, 0x2b, 0x14, 0x83, 0xaa, 0x1f, 0x6a, 0x47, 0xbc, 0x19, 0x26, 0x39, 0x1c,
157 0xd4, 0xbb, 0x8c, 0xd6, 0x94, 0x24, 0x79, 0x60, 0xfb, 0x8e, 0x4b, 0xf4, 0x0f, 0xbf, 0x38, 0x81,
158 0x78, 0xce, 0x1d, 0xd9, 0x03, 0xec, 0x65, 0xcd, 0x82, 0x81, 0xae, 0x00, 0xac, 0x30, 0x81, 0xa9,
159 0x02, 0x81, 0xa1, 0x00, 0xd2, 0xf4, 0x40, 0x8b, 0x2f, 0x09, 0x75, 0x2c, 0x68, 0x12, 0x76, 0xb9,
160 0xfb, 0x1b, 0x02, 0x91, 0x6d, 0xd7, 0x86, 0x49, 0xdc, 0xef, 0x38, 0xf3, 0x50, 0x58, 0xb5, 0xff,
161 0x5c, 0x02, 0x8a, 0xb0, 0xcd, 0xb3, 0x3d, 0x94, 0x71, 0x7d, 0x32, 0x53, 0xed, 0x43, 0xfb, 0xde,
162 0xbc, 0x20, 0x21, 0x33, 0xe3, 0xeb, 0x93, 0x48, 0xe8, 0xd1, 0x32, 0x2f, 0x40, 0x40, 0x47, 0x1f,
163 0xeb, 0x7e, 0xf6, 0x43, 0x81, 0x51, 0xd6, 0x4f, 0xe0, 0x57, 0xbf, 0x12, 0xeb, 0x18, 0x2e, 0x81,
164 0x0b, 0x3a, 0x04, 0xf1, 0xeb, 0x3c, 0xe1, 0xb9, 0xf4, 0x87, 0x37, 0x83, 0x5a, 0x2e, 0x09, 0xf8,
165 0xd5, 0xa0, 0x12, 0xfb, 0x35, 0xe4, 0xd4, 0x3f, 0xef, 0x24, 0x3e, 0x6c, 0xff, 0xb1, 0x35, 0x7e,
166 0x9f, 0xe7, 0x6d, 0x2f, 0xf8, 0x0d, 0xc6, 0xbc, 0x19, 0xe2, 0x78, 0xb3, 0x71, 0xe1, 0x35, 0xe7,
167 0xc7, 0x22, 0x6b, 0x4d, 0x92, 0xc4, 0x10, 0x75, 0x1a, 0x9b, 0x9f, 0x7f, 0xac, 0x2d, 0xfb, 0xc9,
168 0x64, 0x1e, 0x80, 0x11, 0x7f, 0x75, 0x8a, 0x86, 0x7e, 0x09, 0x44, 0xc4, 0x71, 0xbf, 0xd4, 0xfa,
169 0x8b, 0x6a, 0xb8, 0x9f, 0x02, 0x03, 0x01, 0x00,
172 CFDataRef testInteropImport
= CFDataCreate(kCFAllocatorDefault
, sampleByteString
, sizeof(sampleByteString
));
173 SecOTRPublicIdentityRef interopIDInflate
= SecOTRPublicIdentityCreateFromData(kCFAllocatorDefault
, testInteropImport
, &testError
);
174 RegressionsLogError(testError
);
175 CFReleaseNull(testError
);
176 ok(interopIDInflate
!= NULL
, "Interop inflate failed");
178 /* cleanup keychain */
179 ok(SecOTRFIPurgeAllFromKeychain(&testError
),"cleanup keychain");
180 RegressionsLogError(testError
);
181 CFReleaseNull(testError
);
183 CFReleaseSafe(pubID
);
184 CFReleaseSafe(pubIDInflate
);
185 CFReleaseSafe(firstPublicExport
);
186 CFReleaseSafe(secondPublicExport
);
188 CFReleaseSafe(idToPurge
);
189 CFReleaseSafe(idInflate
);
190 CFReleaseSafe(firstExport
);
191 CFReleaseSafe(secondExport
);
192 CFReleaseSafe(purgeExport
);
193 CFReleaseSafe(purgeIdInflate
);
194 CFReleaseSafe(failIDInflate
);
195 CFReleaseSafe(failIDInflate2
);
196 CFReleaseSafe(testInteropImport
);
197 CFReleaseSafe(interopIDInflate
);
200 int otr_00_identity(int argc
, char *const *argv
)
202 plan_tests(kTestTestCount
);