]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-43-seckey-interop.m
Security-58286.60.28.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-43-seckey-interop.m
1 /*
2 * Copyright (c) 2007-2009,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 <TargetConditionals.h>
26 #include <CoreFoundation/CoreFoundation.h>
27 #include <Foundation/Foundation.h>
28 #include <Security/Security.h>
29 #include <Security/SecRandom.h>
30 #include <Security/SecKeyPriv.h>
31 #include <Security/SecItem.h>
32 #include <Security/SecItemPriv.h>
33 #include <Security/SecIdentityPriv.h>
34 #include <Security/SecKeychainItem.h>
35
36 #include "keychain_regressions.h"
37 #include "utilities/SecCFRelease.h"
38 #include "utilities/array_size.h"
39
40 #if !TARGET_OS_IPHONE
41 static inline bool CFEqualSafe(CFTypeRef left, CFTypeRef right)
42 {
43 if (left == NULL || right == NULL)
44 return left == right;
45 else
46 return CFEqual(left, right);
47 }
48 #endif
49
50 static const int kTestGenerateNoLegacyCount = 11;
51 static void test_generate_nolegacy() {
52 NSDictionary *query, *params = @{
53 (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
54 (id)kSecAttrKeySizeInBits: @1024,
55 (id)kSecAttrNoLegacy: @YES,
56 (id)kSecAttrIsPermanent: @YES,
57 (id)kSecAttrLabel: @"sectests:generate-no-legacy",
58 };
59
60 SecKeyRef pubKey = NULL, privKey = NULL, key = NULL;
61 ok_status(SecKeyGeneratePair((__bridge CFDictionaryRef)params, &pubKey, &privKey));
62
63 query = @{
64 (id)kSecClass: (id)kSecClassKey,
65 (id)kSecAttrLabel: @"sectests:generate-no-legacy",
66 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPublic,
67 (id)kSecAttrNoLegacy: @YES,
68 (id)kSecReturnRef: @YES,
69 };
70 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&key));
71 eq_cf(key, pubKey);
72 CFReleaseNull(key);
73
74 query = @{
75 (id)kSecClass: (id)kSecClassKey,
76 (id)kSecAttrLabel: @"sectests:generate-no-legacy",
77 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate,
78 (id)kSecAttrNoLegacy: @YES,
79 (id)kSecReturnRef: @YES,
80 };
81 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&key));
82 eq_cf(key, privKey);
83 CFReleaseNull(key);
84
85 query = @{
86 (id)kSecClass: (id)kSecClassKey,
87 (id)kSecAttrLabel: @"sectests:generate-no-legacy",
88 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPublic,
89 (id)kSecAttrNoLegacy: @YES,
90 (id)kSecReturnRef: @YES,
91 };
92 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&key));
93 eq_cf(key, pubKey);
94 CFReleaseNull(key);
95
96 query = @{
97 (id)kSecClass: (id)kSecClassKey,
98 (id)kSecAttrLabel: @"sectests:generate-no-legacy",
99 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate,
100 (id)kSecAttrNoLegacy: @YES,
101 (id)kSecReturnRef: @YES,
102 };
103 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&key));
104 eq_cf(key, privKey);
105 CFReleaseNull(key);
106
107 query = @{
108 (id)kSecClass: (id)kSecClassKey,
109 (id)kSecAttrLabel: @"sectests:generate-no-legacy",
110 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
111 (id)kSecAttrNoLegacy: @YES,
112 };
113 ok_status(SecItemDelete((__bridge CFDictionaryRef)query));
114 is_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL), errSecItemNotFound);
115
116 CFReleaseNull(privKey);
117 CFReleaseNull(pubKey);
118 }
119
120 static const int kTestGenerateAccessControlCount = 4;
121 static void test_generate_access_control() {
122 SecAccessControlRef ac = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleAlways,
123 0 /* | kSecAccessControlPrivateKeyUsage */, NULL);
124 NSDictionary *params = @{
125 (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
126 (id)kSecAttrKeySizeInBits: @1024,
127 (id)kSecAttrAccessControl: (__bridge id)ac,
128 (id)kSecAttrIsPermanent: @YES,
129 (id)kSecAttrLabel: @"sectests:generate-access-control",
130 };
131
132 SecKeyRef pubKey, privKey;
133 ok_status(SecKeyGeneratePair((__bridge CFDictionaryRef)params, &pubKey, &privKey));
134
135 NSDictionary *query = @{
136 (id)kSecClass: (id)kSecClassKey,
137 (id)kSecAttrLabel: @"sectests:generate-access-control",
138 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
139 (id)kSecAttrNoLegacy: @YES,
140 };
141 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL));
142
143 ok_status(SecItemDelete((__bridge CFDictionaryRef)query));
144
145 is_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL), errSecItemNotFound);
146
147 CFReleaseSafe(ac);
148 CFReleaseSafe(privKey);
149 CFReleaseSafe(pubKey);
150 }
151
152 static const int kTestAddIOSKeyCount = 6;
153 static void test_add_ios_key() {
154 NSDictionary *params = @{
155 (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
156 (id)kSecAttrKeySizeInBits: @1024,
157 (id)kSecAttrNoLegacy: @YES,
158 (id)kSecAttrIsPermanent: @NO,
159 };
160
161 SecKeyRef pubKey, privKey;
162 ok_status(SecKeyGeneratePair((__bridge CFDictionaryRef)params, &pubKey, &privKey));
163
164 NSDictionary *attrs = @{
165 (id)kSecValueRef: (__bridge id)privKey,
166 (id)kSecAttrLabel: @"sectests:add-ios-key",
167 };
168 ok_status(SecItemAdd((__bridge CFDictionaryRef)attrs, NULL));
169
170 NSDictionary *query = @{
171 (id)kSecClass: (id)kSecClassKey,
172 (id)kSecAttrLabel: @"sectests:add-ios-key",
173 (id)kSecAttrNoLegacy: @YES,
174 (id)kSecReturnRef: @YES,
175 };
176 SecKeyRef key = NULL;
177 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&key));
178 eq_cf(key, privKey);
179 CFReleaseNull(key);
180
181 query = @{
182 (id)kSecClass: (id)kSecClassKey,
183 (id)kSecAttrLabel: @"sectests:add-ios-key",
184 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
185 (id)kSecAttrNoLegacy: @YES,
186 };
187 ok_status(SecItemDelete((__bridge CFDictionaryRef)query));
188 is_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL), errSecItemNotFound);
189
190 CFReleaseNull(pubKey);
191 CFReleaseNull(privKey);
192 }
193
194 static const char *certDataBase64 = "\
195 MIIEQjCCAyqgAwIBAgIJAJdFadWqNIfiMA0GCSqGSIb3DQEBBQUAMHMxCzAJBgNVBAYTAkNaMQ8wDQYD\
196 VQQHEwZQcmFndWUxFTATBgNVBAoTDENvc21vcywgSW5jLjEXMBUGA1UEAxMOc3VuLmNvc21vcy5nb2Qx\
197 IzAhBgkqhkiG9w0BCQEWFHRoaW5nQHN1bi5jb3Ntb3MuZ29kMB4XDTE2MDIyNjE0NTQ0OVoXDTE4MTEy\
198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Rh5+3bCPsPmRVb7h\
227 k/GreOriCjTZtyt2XGp8eIfstfirofB7c1lNBjT61BhgjJ8Moii5c2ksNIOOZnKtD53n47mf7hiarYkw\
228 xFEgU6ECgYEAxE8Js3gIPOBjsSw47XHuvsjP880nZZx/oiQ4IeJb/0rkoDMVJjU69WQu1HTNNAnMg4/u\
229 RXo31h+gDZOlE9t9vSXHdrn3at67KAVmoTbRknGxZ+8tYpRJpPj1hyufynBGcKwevv3eHJHnE5eDqbHx\
230 ynZFkXemzT9aMy3R4CCFMXsCgYAYyZpnG/m6WohE0zthMFaeoJ6dSLGvyboWVqDrzXjCbMf/4wllRlxv\
231 cm34T2NXjpJmlH2c7HQJVg9uiivwfYdyb5If3tHhP4VkdIM5dABnCWoVOWy/NvA7XtE+KF/fItuGqKRP\
232 WCGaiRHoEeqZ23SQm5VmvdF7OXNi/R5LiQ3o4QKBgAGX8qg2TTrRR33ksgGbbyi1UJrWC3/TqWWTjbEY\
233 uU51OS3jvEQ3ImdjjM3EtPW7LqHSxUhjGZjvYMk7bZefrIGgkOHx2IRRkotcn9ynKURbD+mcE249beuc\
234 6cFTJVTrXGcFvqomPWtV895A2JzECQZvt1ja88uuu/i2YoHDQdGJAoGAL2TEgiMXiunb6PzYMMKKa+mx\
235 mFnagF0Ek3UJ9ByXKoLz3HFEl7cADIkqyenXFsAER/ifMyCoZp/PDBd6ZkpqLTdH0jQ2Yo4SllLykoiZ\
236 fBWMfjRu4iw9E0MbPB3blmtzfv53BtWKy0LUOlN4juvpqryA7TgaUlZkfMT+T1TC7xU=\
237 ";
238
239 static const int kTestStoreCertToIOS = 5;
240 static void test_store_cert_to_ios() {
241 // Create certificate instance.
242 NSData *certData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:certDataBase64]
243 options:NSDataBase64DecodingIgnoreUnknownCharacters];
244 SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certData);
245 ok(cert != NULL, "create certificate from data");
246
247 // Store certificate to modern keychain.
248 NSDictionary *attrs = @{
249 (id)kSecValueRef: (__bridge id)cert,
250 (id)kSecAttrLabel: @"sectests:store_cert_to_ios",
251 (id)kSecAttrNoLegacy: @YES,
252 (id)kSecReturnPersistentRef: @YES,
253 };
254 id persistentRef;
255 ok_status(SecItemAdd((CFDictionaryRef)attrs, (void *)&persistentRef), "store certificate into iOS keychain");
256
257 // Query certificate, without specification of the keychain.
258 NSDictionary *query = @{
259 (id)kSecClass: (id)kSecClassCertificate,
260 (id)kSecAttrLabel: @"sectests:store_cert_to_ios",
261 (id)kSecReturnRef: @YES,
262 };
263 SecCertificateRef queriedCert = NULL;
264 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (void *)&queriedCert), "query certificate back");
265 eq_cf(cert, queriedCert, "stored and retrieved certificates are the same");
266
267 ok_status(SecItemDelete((CFDictionaryRef)@{ (id)kSecValuePersistentRef: persistentRef }),
268 "delete certificate from keychain");
269 CFReleaseNull(cert);
270 }
271
272 static const int kTestStoreIdentityToIOS = 6;
273 static void test_store_identity_to_ios() {
274 // Create certificate instance.
275 NSData *certData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:certDataBase64]
276 options:NSDataBase64DecodingIgnoreUnknownCharacters];
277 SecCertificateRef certificate = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certData);
278 ok(certificate != NULL, "create certificate from data");
279
280 // Create private key instance.
281 NSData *keyData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:keyDataBase64]
282 options:NSDataBase64DecodingIgnoreUnknownCharacters];
283 NSDictionary *keyAttrs = @{ (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA, (id)kSecAttrKeySizeInBits: @2048,
284 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate };
285 SecKeyRef privateKey = SecKeyCreateWithData((CFDataRef)keyData, (CFDictionaryRef)keyAttrs, NULL);
286 ok(privateKey != NULL, "create private key from data");
287
288 // Create identity from certificate and private key.
289 SecIdentityRef identity = SecIdentityCreate(kCFAllocatorDefault, certificate, privateKey);
290
291 // Store identity to the iOS keychain.
292 NSDictionary *attrs = @{
293 (id)kSecValueRef: (__bridge id)identity,
294 (id)kSecAttrLabel: @"sectests:store_identity_to_ios",
295 (id)kSecAttrNoLegacy: @YES,
296 (id)kSecReturnPersistentRef: @YES,
297 };
298 id persistentRef;
299 ok_status(SecItemAdd((CFDictionaryRef)attrs, (void *)&persistentRef), "store identity into iOS keychain");
300
301 NSDictionary *query = @{
302 (id)kSecClass: (id)kSecClassIdentity,
303 (id)kSecAttrLabel: @"sectests:store_identity_to_ios",
304 (id)kSecReturnRef: @YES,
305 };
306 SecIdentityRef queriedIdentity = NULL;
307 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (void *)&queriedIdentity), "query identity from keychain");
308 eq_cf(identity, queriedIdentity, "stored and retrieved identities are identical");
309
310 // Cleanup identity.
311 ok_status(SecItemDelete((CFDictionaryRef)@{ (id)kSecValuePersistentRef: persistentRef}),
312 "delete identity from iOS keychain");
313
314 CFReleaseNull(identity);
315 CFReleaseNull(privateKey);
316 CFReleaseNull(certificate);
317 }
318
319 static const int kTestTransformWithIOSKey = 9;
320 static void test_transform_with_ioskey() {
321 // Create private key instance.
322 NSData *keyData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:keyDataBase64]
323 options:NSDataBase64DecodingIgnoreUnknownCharacters];
324 NSDictionary *keyAttrs = @{ (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA, (id)kSecAttrKeySizeInBits: @2048,
325 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate };
326 SecKeyRef privateKey = SecKeyCreateWithData((CFDataRef)keyData, (CFDictionaryRef)keyAttrs, NULL);
327 ok(privateKey != NULL, "create private key from data");
328
329 // Create signature transform with private key
330 NSData *testData = [NSData dataWithBytes:"test" length:4];
331 SecTransformRef signer = SecSignTransformCreate(privateKey, NULL);
332 ok(signer != NULL, "create signing transform");
333 ok(SecTransformSetAttribute(signer, kSecTransformInputAttributeName, (CFDataRef)testData, NULL),
334 "set input data to verify transform");
335 NSData *signature = (__bridge_transfer NSData *)SecTransformExecute(signer, NULL);
336 ok(signature != nil, "create signature with transform");
337
338 // Create verify transform with public key.
339 SecKeyRef publicKey = SecKeyCopyPublicKey(privateKey);
340 ok(publicKey != NULL, "get public key from private key");
341 SecTransformRef verifier = SecVerifyTransformCreate(publicKey, (CFDataRef)signature, NULL);
342 ok(verifier, "create verification transform");
343 ok(SecTransformSetAttribute(verifier, kSecTransformInputAttributeName, (CFDataRef)testData, NULL),
344 "set input data to verify transform");
345
346 NSNumber *result = (__bridge_transfer NSNumber *)SecTransformExecute(verifier, NULL);
347 ok(result != nil, "transform execution succeeded");
348 ok(result.boolValue, "transform verified signature");
349
350 CFReleaseNull(signer);
351 CFReleaseNull(verifier);
352 CFReleaseNull(publicKey);
353 CFReleaseNull(privateKey);
354 }
355
356 static const int kTestConvertKeyToPersistentRef = 11;
357 static void test_convert_key_to_persistent_ref() {
358 NSString *label = @"sectests:convert-key-to-persistent-ref";
359
360 // Create
361 SecKeyRef privKey = NULL;
362 {
363 NSDictionary *query = @{
364 (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
365 (id)kSecAttrKeySizeInBits: @1024,
366 (id)kSecAttrNoLegacy: @YES,
367 (id)kSecAttrIsPermanent: @NO,
368 };
369 SecKeyRef pubKey = NULL;
370 ok_status(SecKeyGeneratePair((CFDictionaryRef)query, &pubKey, &privKey));
371 CFReleaseNull(pubKey);
372 }
373
374 // Store
375 {
376 NSDictionary *query = @{
377 (id)kSecAttrLabel: label,
378 (id)kSecValueRef: (__bridge id)privKey,
379 (id)kSecAttrNoLegacy: @YES,
380 };
381 ok_status(SecItemAdd((CFDictionaryRef)query, NULL));
382 }
383
384 // Convert & Compare
385 CFDataRef queriedPersistentKeyRef = NULL;
386 SecKeyRef queriedKeyRef = NULL;
387 {
388 NSDictionary *query = @{
389 (id)kSecValueRef: (__bridge id)privKey,
390 (id)kSecReturnPersistentRef: @YES,
391 (id)kSecAttrNoLegacy: @YES,
392 };
393 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedPersistentKeyRef));
394 }{
395 NSDictionary *query = @{
396 (id)kSecValuePersistentRef: (__bridge id)queriedPersistentKeyRef,
397 (id)kSecReturnRef: @YES,
398 (id)kSecAttrNoLegacy: @YES,
399 };
400 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedKeyRef));
401 }{
402 CFDataRef persistentKeyRef = NULL;
403 SecKeychainItemRef keyRef = NULL;
404 ok_status(SecKeychainItemCreatePersistentReference((SecKeychainItemRef)privKey, &persistentKeyRef));
405 ok_status(SecKeychainItemCopyFromPersistentReference(persistentKeyRef, &keyRef));
406 eq_cf(privKey, queriedKeyRef);
407 eq_cf(keyRef, privKey);
408 eq_cf(persistentKeyRef, queriedPersistentKeyRef);
409 CFReleaseNull(persistentKeyRef);
410 CFReleaseNull(keyRef);
411 }
412 CFReleaseNull(queriedPersistentKeyRef);
413 CFReleaseNull(queriedKeyRef);
414
415 // Cleanup
416 CFReleaseNull(privKey);
417 {
418 NSDictionary *query = @{
419 (id)kSecClass: (id)kSecClassKey,
420 (id)kSecAttrLabel: label,
421 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
422 };
423 ok_status(SecItemDelete((CFDictionaryRef)query));
424 is_status(SecItemCopyMatching((CFDictionaryRef)query, NULL), errSecItemNotFound);
425 }
426 }
427
428 static const int kTestConvertCertToPersistentRef = 11;
429 static void test_convert_cert_to_persistent_ref() {
430 NSString *label = @"sectests:convert-cert-to-persistent-ref";
431
432 // Create
433 SecCertificateRef cert = NULL;
434 {
435 NSData *certData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:certDataBase64]
436 options:NSDataBase64DecodingIgnoreUnknownCharacters];
437 cert = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certData);
438 ok(cert);
439 }
440
441 // Store
442 {
443 NSDictionary *query = @{
444 (id)kSecAttrLabel: label,
445 (id)kSecValueRef: (__bridge id)cert,
446 (id)kSecAttrNoLegacy: @YES,
447 };
448 ok_status(SecItemAdd((CFDictionaryRef)query, NULL));
449 }
450
451 // Convert & Compare
452 CFDataRef queriedPersistentCertRef = NULL;
453 SecCertificateRef queriedCertRef = NULL;
454 {
455 NSDictionary *query = @{
456 (id)kSecValueRef: (__bridge id)cert,
457 (id)kSecReturnPersistentRef: @YES,
458 (id)kSecAttrNoLegacy: @YES,
459 };
460 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedPersistentCertRef));
461 }{
462 NSDictionary *query = @{
463 (id)kSecValuePersistentRef: (__bridge id)queriedPersistentCertRef,
464 (id)kSecReturnRef: @YES,
465 (id)kSecAttrNoLegacy: @YES,
466 };
467 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedCertRef));
468 }{
469 CFDataRef persistentCertRef = NULL;
470 SecKeychainItemRef certRef = NULL;
471 ok_status(SecKeychainItemCreatePersistentReference((SecKeychainItemRef)cert, &persistentCertRef));
472 ok_status(SecKeychainItemCopyFromPersistentReference(persistentCertRef, &certRef));
473 eq_cf(cert, queriedCertRef);
474 eq_cf(certRef, cert);
475 eq_cf(persistentCertRef, queriedPersistentCertRef);
476 CFReleaseNull(persistentCertRef);
477 CFReleaseNull(certRef);
478 }
479 CFReleaseNull(queriedPersistentCertRef);
480 CFReleaseNull(queriedCertRef);
481
482 // Cleanup
483 CFReleaseNull(cert);
484 {
485 NSDictionary *query = @{
486 (id)kSecClass: (id)kSecClassCertificate,
487 (id)kSecAttrLabel: label,
488 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
489 };
490 ok_status(SecItemDelete((CFDictionaryRef)query));
491 is_status(SecItemCopyMatching((CFDictionaryRef)query, NULL), errSecItemNotFound);
492 }
493 }
494
495 static const int kTestConvertIdentityToPersistentRef = 12;
496 static void test_convert_identity_to_persistent_ref() {
497 NSString *label = @"sectests:convert-identity-to-persistent-ref";
498
499 // Create
500 SecIdentityRef idnt = NULL;
501 {
502 NSData *certData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:certDataBase64]
503 options:NSDataBase64DecodingIgnoreUnknownCharacters];
504 SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certData);
505 ok(cert);
506 NSData *keyData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:keyDataBase64]
507 options:NSDataBase64DecodingIgnoreUnknownCharacters];
508 NSDictionary *keyAttrs = @{ (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
509 (id)kSecAttrKeySizeInBits: @2048,
510 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate };
511 SecKeyRef privKey = SecKeyCreateWithData((CFDataRef)keyData, (CFDictionaryRef)keyAttrs, NULL);
512 ok(privKey);
513 idnt = SecIdentityCreate(kCFAllocatorDefault, cert, privKey);
514 CFReleaseNull(cert);
515 CFReleaseNull(privKey);
516 }
517
518 // Store
519 {
520 NSDictionary *query = @{
521 (id)kSecAttrLabel: label,
522 (id)kSecValueRef: (__bridge id)idnt,
523 (id)kSecAttrNoLegacy: @YES,
524 };
525 ok_status(SecItemAdd((CFDictionaryRef)query, NULL));
526 }
527
528 // Convert & Compare
529 CFDataRef queriedPersistentIdntRef = NULL;
530 SecIdentityRef queriedIdntRef = NULL;
531 {
532 NSDictionary *query = @{
533 (id)kSecValueRef: (__bridge id)idnt,
534 (id)kSecReturnPersistentRef: @YES,
535 (id)kSecAttrNoLegacy: @YES,
536 };
537 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedPersistentIdntRef));
538 }{
539 NSDictionary *query = @{
540 (id)kSecValuePersistentRef: (__bridge id)queriedPersistentIdntRef,
541 (id)kSecReturnRef: @YES,
542 (id)kSecAttrNoLegacy: @YES,
543 };
544 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedIdntRef));
545 }{
546 CFDataRef persistentIdntRef = NULL;
547 SecKeychainItemRef idntRef = NULL;
548 ok_status(SecKeychainItemCreatePersistentReference((SecKeychainItemRef)idnt, &persistentIdntRef));
549 ok_status(SecKeychainItemCopyFromPersistentReference(persistentIdntRef, &idntRef));
550 eq_cf(idnt, queriedIdntRef);
551 eq_cf(idntRef, idnt);
552 eq_cf(persistentIdntRef, queriedPersistentIdntRef);
553 CFReleaseNull(persistentIdntRef);
554 CFReleaseNull(idntRef);
555 }
556 CFReleaseNull(queriedPersistentIdntRef);
557 CFReleaseNull(queriedIdntRef);
558
559 // Cleanup
560 {
561 NSDictionary *query = @{
562 // identities can't be filtered out using 'label', so we will use directly the ValueRef here:
563 (id)kSecValueRef: (__bridge id)idnt,
564 (id)kSecAttrNoLegacy: @YES,
565 };
566 ok_status(SecItemDelete((CFDictionaryRef)query));
567 is_status(SecItemCopyMatching((CFDictionaryRef)query, NULL), errSecItemNotFound);
568 }
569 CFReleaseNull(idnt);
570 }
571
572 static void test_cssm_from_ios_key_single(CFTypeRef keyType, int keySize) {
573 NSDictionary *params = @{
574 (id)kSecAttrKeyType: (__bridge id)keyType,
575 (id)kSecAttrKeySizeInBits: @(keySize),
576 (id)kSecAttrNoLegacy: @YES,
577 (id)kSecAttrIsPermanent: @NO,
578 (id)kSecAttrLabel: @"sectests:cssm-from-ios-key",
579 };
580 NSError *error;
581 id privateKey = CFBridgingRelease(SecKeyCreateRandomKey((CFDictionaryRef)params, (void *)&error));
582 ok(privateKey != nil, "Failed to generate key, err %@", error);
583 id publicKey = CFBridgingRelease(SecKeyCopyPublicKey((SecKeyRef)privateKey));
584 ok(publicKey != nil, "Failed to get public key from private key");
585
586 const CSSM_KEY *cssmKey = NULL;
587 OSStatus status = SecKeyGetCSSMKey((SecKeyRef)publicKey, &cssmKey);
588 ok_status(status, "Failed to get CSSM key");
589 isnt(cssmKey, NULL, "Got NULL CSSM key");
590
591 CSSM_CSP_HANDLE cspHandle = 0;
592 status = SecKeyGetCSPHandle((SecKeyRef)publicKey, &cspHandle);
593 ok_status(status, "Failed to get CSP handle");
594 isnt(cssmKey, NULL, "Got 0 CSP handle");
595 }
596 static const int kTestCSSMFromIOSKeySingleCount = 5;
597
598 static void test_cssm_from_ios_key() {
599 test_cssm_from_ios_key_single(kSecAttrKeyTypeECSECPrimeRandom, 256);
600 test_cssm_from_ios_key_single(kSecAttrKeyTypeECSECPrimeRandom, 384);
601 test_cssm_from_ios_key_single(kSecAttrKeyTypeECSECPrimeRandom, 521);
602 test_cssm_from_ios_key_single(kSecAttrKeyTypeRSA, 1024);
603 test_cssm_from_ios_key_single(kSecAttrKeyTypeRSA, 2048);
604 }
605 static const int kTestCSSMFromIOSKeyCount = kTestCSSMFromIOSKeySingleCount * 5;
606
607 static const int kTestCount =
608 kTestGenerateNoLegacyCount +
609 kTestGenerateAccessControlCount +
610 kTestAddIOSKeyCount +
611 kTestStoreCertToIOS +
612 kTestStoreIdentityToIOS +
613 kTestTransformWithIOSKey +
614 kTestConvertKeyToPersistentRef +
615 kTestConvertCertToPersistentRef +
616 kTestConvertIdentityToPersistentRef +
617 kTestCSSMFromIOSKeyCount;
618
619 int kc_43_seckey_interop(int argc, char *const *argv) {
620 plan_tests(kTestCount);
621
622 test_generate_nolegacy();
623 test_generate_access_control();
624 test_add_ios_key();
625 test_store_cert_to_ios();
626 test_store_identity_to_ios();
627 test_transform_with_ioskey();
628 test_convert_key_to_persistent_ref();
629 test_convert_cert_to_persistent_ref();
630 test_convert_identity_to_persistent_ref();
631 test_cssm_from_ios_key();
632
633 return 0;
634 }