2 * Copyright (c) 2007-2009,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 <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>
36 #include "keychain_regressions.h"
37 #include "utilities/SecCFRelease.h"
38 #include "utilities/array_size.h"
41 static inline bool CFEqualSafe(CFTypeRef left, CFTypeRef right)
43 if (left == NULL || right == NULL)
46 return CFEqual(left, right);
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",
60 SecKeyRef pubKey = NULL, privKey = NULL, key = NULL;
61 ok_status(SecKeyGeneratePair((__bridge CFDictionaryRef)params, &pubKey, &privKey));
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,
70 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&key));
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,
81 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&key));
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,
92 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&key));
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,
103 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&key));
108 (id)kSecClass: (id)kSecClassKey,
109 (id)kSecAttrLabel: @"sectests:generate-no-legacy",
110 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
111 (id)kSecAttrNoLegacy: @YES,
113 ok_status(SecItemDelete((__bridge CFDictionaryRef)query));
114 is_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL), errSecItemNotFound);
116 CFReleaseNull(privKey);
117 CFReleaseNull(pubKey);
120 #if !RC_HIDE_J79 && !RC_HIDE_J80
121 static const int kTestGenerateAccessControlCount = 4;
122 static void test_generate_access_control() {
123 SecAccessControlRef ac = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleAlways,
124 0 /* | kSecAccessControlPrivateKeyUsage */, NULL);
125 NSDictionary *params = @{
126 (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
127 (id)kSecAttrKeySizeInBits: @1024,
128 (id)kSecAttrAccessControl: (__bridge id)ac,
129 (id)kSecAttrIsPermanent: @YES,
130 (id)kSecAttrLabel: @"sectests:generate-access-control",
133 SecKeyRef pubKey, privKey;
134 ok_status(SecKeyGeneratePair((__bridge CFDictionaryRef)params, &pubKey, &privKey));
136 NSDictionary *query = @{
137 (id)kSecClass: (id)kSecClassKey,
138 (id)kSecAttrLabel: @"sectests:generate-access-control",
139 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
140 (id)kSecAttrNoLegacy: @YES,
142 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL));
144 ok_status(SecItemDelete((__bridge CFDictionaryRef)query));
146 is_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL), errSecItemNotFound);
149 CFReleaseSafe(privKey);
150 CFReleaseSafe(pubKey);
153 static const int kTestGenerateAccessControlCount = 0;
156 static const int kTestAddIOSKeyCount = 6;
157 static void test_add_ios_key() {
158 NSDictionary *params = @{
159 (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
160 (id)kSecAttrKeySizeInBits: @1024,
161 (id)kSecAttrNoLegacy: @YES,
162 (id)kSecAttrIsPermanent: @NO,
165 SecKeyRef pubKey, privKey;
166 ok_status(SecKeyGeneratePair((__bridge CFDictionaryRef)params, &pubKey, &privKey));
168 NSDictionary *attrs = @{
169 (id)kSecValueRef: (__bridge id)privKey,
170 (id)kSecAttrLabel: @"sectests:add-ios-key",
172 ok_status(SecItemAdd((__bridge CFDictionaryRef)attrs, NULL));
174 NSDictionary *query = @{
175 (id)kSecClass: (id)kSecClassKey,
176 (id)kSecAttrLabel: @"sectests:add-ios-key",
177 (id)kSecAttrNoLegacy: @YES,
178 (id)kSecReturnRef: @YES,
180 SecKeyRef key = NULL;
181 ok_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&key));
186 (id)kSecClass: (id)kSecClassKey,
187 (id)kSecAttrLabel: @"sectests:add-ios-key",
188 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
189 (id)kSecAttrNoLegacy: @YES,
191 ok_status(SecItemDelete((__bridge CFDictionaryRef)query));
192 is_status(SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL), errSecItemNotFound);
194 CFReleaseNull(pubKey);
195 CFReleaseNull(privKey);
198 static const char *certDataBase64 = "\
199 MIIEQjCCAyqgAwIBAgIJAJdFadWqNIfiMA0GCSqGSIb3DQEBBQUAMHMxCzAJBgNVBAYTAkNaMQ8wDQYD\
200 VQQHEwZQcmFndWUxFTATBgNVBAoTDENvc21vcywgSW5jLjEXMBUGA1UEAxMOc3VuLmNvc21vcy5nb2Qx\
201 IzAhBgkqhkiG9w0BCQEWFHRoaW5nQHN1bi5jb3Ntb3MuZ29kMB4XDTE2MDIyNjE0NTQ0OVoXDTE4MTEy\
202 MjE0NTQ0OVowczELMAkGA1UEBhMCQ1oxDzANBgNVBAcTBlByYWd1ZTEVMBMGA1UEChMMQ29zbW9zLCBJ\
203 bmMuMRcwFQYDVQQDEw5zdW4uY29zbW9zLmdvZDEjMCEGCSqGSIb3DQEJARYUdGhpbmdAc3VuLmNvc21v\
204 cy5nb2QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC5u9gnYEDzQIVu7yC40VcXTZ01D9CJ\
205 oD/mH62tebEHEdfVPLWKeq+uAHnJ6fTIJQvksaISOxwiOosFjtI30mbe6LZ/oK22wYX+OUwKhAYjZQPy\
206 RYfuaJe/52F0zmfUSJ+KTbUZrXbVVFma4xPfpg4bptvtGkFJWnufvEEHimOGmO5O69lXA0Hit1yLU0/A\
207 MQrIMmZT8gb8LMZGPZearT90KhCbTHAxjcBfswZYeL8q3xuEV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GX8qg2TTrRR33ksgGbbyi1UJrWC3/TqWWTjbEY\
237 uU51OS3jvEQ3ImdjjM3EtPW7LqHSxUhjGZjvYMk7bZefrIGgkOHx2IRRkotcn9ynKURbD+mcE249beuc\
238 6cFTJVTrXGcFvqomPWtV895A2JzECQZvt1ja88uuu/i2YoHDQdGJAoGAL2TEgiMXiunb6PzYMMKKa+mx\
239 mFnagF0Ek3UJ9ByXKoLz3HFEl7cADIkqyenXFsAER/ifMyCoZp/PDBd6ZkpqLTdH0jQ2Yo4SllLykoiZ\
240 fBWMfjRu4iw9E0MbPB3blmtzfv53BtWKy0LUOlN4juvpqryA7TgaUlZkfMT+T1TC7xU=\
243 static const int kTestStoreCertToIOS = 5;
244 static void test_store_cert_to_ios() {
245 // Create certificate instance.
246 NSData *certData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:certDataBase64]
247 options:NSDataBase64DecodingIgnoreUnknownCharacters];
248 SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certData);
249 ok(cert != NULL, "create certificate from data");
251 // Store certificate to modern keychain.
252 NSDictionary *attrs = @{
253 (id)kSecValueRef: (__bridge id)cert,
254 (id)kSecAttrLabel: @"sectests:store_cert_to_ios",
255 (id)kSecAttrNoLegacy: @YES,
256 (id)kSecReturnPersistentRef: @YES,
259 ok_status(SecItemAdd((CFDictionaryRef)attrs, (void *)&persistentRef), "store certificate into iOS keychain");
261 // Query certificate, without specification of the keychain.
262 NSDictionary *query = @{
263 (id)kSecClass: (id)kSecClassCertificate,
264 (id)kSecAttrLabel: @"sectests:store_cert_to_ios",
265 (id)kSecReturnRef: @YES,
267 SecCertificateRef queriedCert = NULL;
268 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (void *)&queriedCert), "query certificate back");
269 eq_cf(cert, queriedCert, "stored and retrieved certificates are the same");
271 ok_status(SecItemDelete((CFDictionaryRef)@{ (id)kSecValuePersistentRef: persistentRef }),
272 "delete certificate from keychain");
276 static const int kTestStoreIdentityToIOS = 6;
277 static void test_store_identity_to_ios() {
278 // Create certificate instance.
279 NSData *certData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:certDataBase64]
280 options:NSDataBase64DecodingIgnoreUnknownCharacters];
281 SecCertificateRef certificate = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certData);
282 ok(certificate != NULL, "create certificate from data");
284 // Create private key instance.
285 NSData *keyData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:keyDataBase64]
286 options:NSDataBase64DecodingIgnoreUnknownCharacters];
287 NSDictionary *keyAttrs = @{ (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA, (id)kSecAttrKeySizeInBits: @2048,
288 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate };
289 SecKeyRef privateKey = SecKeyCreateWithData((CFDataRef)keyData, (CFDictionaryRef)keyAttrs, NULL);
290 ok(privateKey != NULL, "create private key from data");
292 // Create identity from certificate and private key.
293 SecIdentityRef identity = SecIdentityCreate(kCFAllocatorDefault, certificate, privateKey);
295 // Store identity to the iOS keychain.
296 NSDictionary *attrs = @{
297 (id)kSecValueRef: (__bridge id)identity,
298 (id)kSecAttrLabel: @"sectests:store_identity_to_ios",
299 (id)kSecAttrNoLegacy: @YES,
300 (id)kSecReturnPersistentRef: @YES,
303 ok_status(SecItemAdd((CFDictionaryRef)attrs, (void *)&persistentRef), "store identity into iOS keychain");
305 NSDictionary *query = @{
306 (id)kSecClass: (id)kSecClassIdentity,
307 (id)kSecAttrLabel: @"sectests:store_identity_to_ios",
308 (id)kSecReturnRef: @YES,
310 SecIdentityRef queriedIdentity = NULL;
311 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (void *)&queriedIdentity), "query identity from keychain");
312 eq_cf(identity, queriedIdentity, "stored and retrieved identities are identical");
315 ok_status(SecItemDelete((CFDictionaryRef)@{ (id)kSecValuePersistentRef: persistentRef}),
316 "delete identity from iOS keychain");
318 CFReleaseNull(identity);
319 CFReleaseNull(privateKey);
320 CFReleaseNull(certificate);
323 static const int kTestTransformWithIOSKey = 9;
324 static void test_transform_with_ioskey() {
325 // Create private key instance.
326 NSData *keyData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:keyDataBase64]
327 options:NSDataBase64DecodingIgnoreUnknownCharacters];
328 NSDictionary *keyAttrs = @{ (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA, (id)kSecAttrKeySizeInBits: @2048,
329 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate };
330 SecKeyRef privateKey = SecKeyCreateWithData((CFDataRef)keyData, (CFDictionaryRef)keyAttrs, NULL);
331 ok(privateKey != NULL, "create private key from data");
333 // Create signature transform with private key
334 NSData *testData = [NSData dataWithBytes:"test" length:4];
335 SecTransformRef signer = SecSignTransformCreate(privateKey, NULL);
336 ok(signer != NULL, "create signing transform");
337 ok(SecTransformSetAttribute(signer, kSecTransformInputAttributeName, (CFDataRef)testData, NULL),
338 "set input data to verify transform");
339 NSData *signature = (__bridge_transfer NSData *)SecTransformExecute(signer, NULL);
340 ok(signature != nil, "create signature with transform");
342 // Create verify transform with public key.
343 SecKeyRef publicKey = SecKeyCopyPublicKey(privateKey);
344 ok(publicKey != NULL, "get public key from private key");
345 SecTransformRef verifier = SecVerifyTransformCreate(publicKey, (CFDataRef)signature, NULL);
346 ok(verifier, "create verification transform");
347 ok(SecTransformSetAttribute(verifier, kSecTransformInputAttributeName, (CFDataRef)testData, NULL),
348 "set input data to verify transform");
350 NSNumber *result = (__bridge_transfer NSNumber *)SecTransformExecute(verifier, NULL);
351 ok(result != nil, "transform execution succeeded");
352 ok(result.boolValue, "transform verified signature");
354 CFReleaseNull(signer);
355 CFReleaseNull(verifier);
356 CFReleaseNull(publicKey);
357 CFReleaseNull(privateKey);
360 static const int kTestConvertKeyToPersistentRef = 11;
361 static void test_convert_key_to_persistent_ref() {
362 NSString *label = @"sectests:convert-key-to-persistent-ref";
365 SecKeyRef privKey = NULL;
367 NSDictionary *query = @{
368 (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
369 (id)kSecAttrKeySizeInBits: @1024,
370 (id)kSecAttrNoLegacy: @YES,
371 (id)kSecAttrIsPermanent: @NO,
373 SecKeyRef pubKey = NULL;
374 ok_status(SecKeyGeneratePair((CFDictionaryRef)query, &pubKey, &privKey));
375 CFReleaseNull(pubKey);
380 NSDictionary *query = @{
381 (id)kSecAttrLabel: label,
382 (id)kSecValueRef: (__bridge id)privKey,
383 (id)kSecAttrNoLegacy: @YES,
385 ok_status(SecItemAdd((CFDictionaryRef)query, NULL));
389 CFDataRef queriedPersistentKeyRef = NULL;
390 SecKeyRef queriedKeyRef = NULL;
392 NSDictionary *query = @{
393 (id)kSecValueRef: (__bridge id)privKey,
394 (id)kSecReturnPersistentRef: @YES,
395 (id)kSecAttrNoLegacy: @YES,
397 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedPersistentKeyRef));
399 NSDictionary *query = @{
400 (id)kSecValuePersistentRef: (__bridge id)queriedPersistentKeyRef,
401 (id)kSecReturnRef: @YES,
402 (id)kSecAttrNoLegacy: @YES,
404 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedKeyRef));
406 CFDataRef persistentKeyRef = NULL;
407 SecKeychainItemRef keyRef = NULL;
408 ok_status(SecKeychainItemCreatePersistentReference((SecKeychainItemRef)privKey, &persistentKeyRef));
409 ok_status(SecKeychainItemCopyFromPersistentReference(persistentKeyRef, &keyRef));
410 eq_cf(privKey, queriedKeyRef);
411 eq_cf(keyRef, privKey);
412 eq_cf(persistentKeyRef, queriedPersistentKeyRef);
413 CFReleaseNull(persistentKeyRef);
414 CFReleaseNull(keyRef);
416 CFReleaseNull(queriedPersistentKeyRef);
417 CFReleaseNull(queriedKeyRef);
420 CFReleaseNull(privKey);
422 NSDictionary *query = @{
423 (id)kSecClass: (id)kSecClassKey,
424 (id)kSecAttrLabel: label,
425 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
427 ok_status(SecItemDelete((CFDictionaryRef)query));
428 is_status(SecItemCopyMatching((CFDictionaryRef)query, NULL), errSecItemNotFound);
432 static const int kTestConvertCertToPersistentRef = 11;
433 static void test_convert_cert_to_persistent_ref() {
434 NSString *label = @"sectests:convert-cert-to-persistent-ref";
437 SecCertificateRef cert = NULL;
439 NSData *certData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:certDataBase64]
440 options:NSDataBase64DecodingIgnoreUnknownCharacters];
441 cert = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certData);
447 NSDictionary *query = @{
448 (id)kSecAttrLabel: label,
449 (id)kSecValueRef: (__bridge id)cert,
450 (id)kSecAttrNoLegacy: @YES,
452 ok_status(SecItemAdd((CFDictionaryRef)query, NULL));
456 CFDataRef queriedPersistentCertRef = NULL;
457 SecCertificateRef queriedCertRef = NULL;
459 NSDictionary *query = @{
460 (id)kSecValueRef: (__bridge id)cert,
461 (id)kSecReturnPersistentRef: @YES,
462 (id)kSecAttrNoLegacy: @YES,
464 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedPersistentCertRef));
466 NSDictionary *query = @{
467 (id)kSecValuePersistentRef: (__bridge id)queriedPersistentCertRef,
468 (id)kSecReturnRef: @YES,
469 (id)kSecAttrNoLegacy: @YES,
471 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedCertRef));
473 CFDataRef persistentCertRef = NULL;
474 SecKeychainItemRef certRef = NULL;
475 ok_status(SecKeychainItemCreatePersistentReference((SecKeychainItemRef)cert, &persistentCertRef));
476 ok_status(SecKeychainItemCopyFromPersistentReference(persistentCertRef, &certRef));
477 eq_cf(cert, queriedCertRef);
478 eq_cf(certRef, cert);
479 eq_cf(persistentCertRef, queriedPersistentCertRef);
480 CFReleaseNull(persistentCertRef);
481 CFReleaseNull(certRef);
483 CFReleaseNull(queriedPersistentCertRef);
484 CFReleaseNull(queriedCertRef);
489 NSDictionary *query = @{
490 (id)kSecClass: (id)kSecClassCertificate,
491 (id)kSecAttrLabel: label,
492 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
494 ok_status(SecItemDelete((CFDictionaryRef)query));
495 is_status(SecItemCopyMatching((CFDictionaryRef)query, NULL), errSecItemNotFound);
499 static const int kTestConvertIdentityToPersistentRef = 12;
500 static void test_convert_identity_to_persistent_ref() {
501 NSString *label = @"sectests:convert-identity-to-persistent-ref";
504 SecIdentityRef idnt = NULL;
506 NSData *certData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:certDataBase64]
507 options:NSDataBase64DecodingIgnoreUnknownCharacters];
508 SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certData);
510 NSData *keyData = [[NSData alloc] initWithBase64EncodedString:[NSString stringWithUTF8String:keyDataBase64]
511 options:NSDataBase64DecodingIgnoreUnknownCharacters];
512 NSDictionary *keyAttrs = @{ (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
513 (id)kSecAttrKeySizeInBits: @2048,
514 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPrivate };
515 SecKeyRef privKey = SecKeyCreateWithData((CFDataRef)keyData, (CFDictionaryRef)keyAttrs, NULL);
517 idnt = SecIdentityCreate(kCFAllocatorDefault, cert, privKey);
519 CFReleaseNull(privKey);
524 NSDictionary *query = @{
525 (id)kSecAttrLabel: label,
526 (id)kSecValueRef: (__bridge id)idnt,
527 (id)kSecAttrNoLegacy: @YES,
529 ok_status(SecItemAdd((CFDictionaryRef)query, NULL));
533 CFDataRef queriedPersistentIdntRef = NULL;
534 SecIdentityRef queriedIdntRef = NULL;
536 NSDictionary *query = @{
537 (id)kSecValueRef: (__bridge id)idnt,
538 (id)kSecReturnPersistentRef: @YES,
539 (id)kSecAttrNoLegacy: @YES,
541 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedPersistentIdntRef));
543 NSDictionary *query = @{
544 (id)kSecValuePersistentRef: (__bridge id)queriedPersistentIdntRef,
545 (id)kSecReturnRef: @YES,
546 (id)kSecAttrNoLegacy: @YES,
548 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&queriedIdntRef));
550 CFDataRef persistentIdntRef = NULL;
551 SecKeychainItemRef idntRef = NULL;
552 ok_status(SecKeychainItemCreatePersistentReference((SecKeychainItemRef)idnt, &persistentIdntRef));
553 ok_status(SecKeychainItemCopyFromPersistentReference(persistentIdntRef, &idntRef));
554 eq_cf(idnt, queriedIdntRef);
555 eq_cf(idntRef, idnt);
556 eq_cf(persistentIdntRef, queriedPersistentIdntRef);
557 CFReleaseNull(persistentIdntRef);
558 CFReleaseNull(idntRef);
560 CFReleaseNull(queriedPersistentIdntRef);
561 CFReleaseNull(queriedIdntRef);
565 NSDictionary *query = @{
566 // identities can't be filtered out using 'label', so we will use directly the ValueRef here:
567 (id)kSecValueRef: (__bridge id)idnt,
568 (id)kSecAttrNoLegacy: @YES,
570 ok_status(SecItemDelete((CFDictionaryRef)query));
571 is_status(SecItemCopyMatching((CFDictionaryRef)query, NULL), errSecItemNotFound);
576 static void test_cssm_from_ios_key_single(CFTypeRef keyType, int keySize) {
577 NSDictionary *params = @{
578 (id)kSecAttrKeyType: (__bridge id)keyType,
579 (id)kSecAttrKeySizeInBits: @(keySize),
580 (id)kSecAttrNoLegacy: @YES,
581 (id)kSecAttrIsPermanent: @NO,
582 (id)kSecAttrLabel: @"sectests:cssm-from-ios-key",
585 id privateKey = CFBridgingRelease(SecKeyCreateRandomKey((CFDictionaryRef)params, (void *)&error));
586 ok(privateKey != nil, "Failed to generate key, err %@", error);
587 id publicKey = CFBridgingRelease(SecKeyCopyPublicKey((SecKeyRef)privateKey));
588 ok(publicKey != nil, "Failed to get public key from private key");
590 const CSSM_KEY *cssmKey = NULL;
591 OSStatus status = SecKeyGetCSSMKey((SecKeyRef)publicKey, &cssmKey);
592 ok_status(status, "Failed to get CSSM key");
593 isnt(cssmKey, NULL, "Got NULL CSSM key");
595 CSSM_CSP_HANDLE cspHandle = 0;
596 status = SecKeyGetCSPHandle((SecKeyRef)publicKey, &cspHandle);
597 ok_status(status, "Failed to get CSP handle");
598 isnt(cssmKey, NULL, "Got 0 CSP handle");
600 static const int kTestCSSMFromIOSKeySingleCount = 5;
602 static void test_cssm_from_ios_key() {
603 test_cssm_from_ios_key_single(kSecAttrKeyTypeECSECPrimeRandom, 256);
604 test_cssm_from_ios_key_single(kSecAttrKeyTypeECSECPrimeRandom, 384);
605 test_cssm_from_ios_key_single(kSecAttrKeyTypeECSECPrimeRandom, 521);
606 test_cssm_from_ios_key_single(kSecAttrKeyTypeRSA, 1024);
607 test_cssm_from_ios_key_single(kSecAttrKeyTypeRSA, 2048);
609 static const int kTestCSSMFromIOSKeyCount = kTestCSSMFromIOSKeySingleCount * 5;
611 static const int kTestCount =
612 kTestGenerateNoLegacyCount +
613 kTestGenerateAccessControlCount +
614 kTestAddIOSKeyCount +
615 kTestStoreCertToIOS +
616 kTestStoreIdentityToIOS +
617 kTestTransformWithIOSKey +
618 kTestConvertKeyToPersistentRef +
619 kTestConvertCertToPersistentRef +
620 kTestConvertIdentityToPersistentRef +
621 kTestCSSMFromIOSKeyCount;
623 int kc_43_seckey_interop(int argc, char *const *argv) {
624 plan_tests(kTestCount);
626 test_generate_nolegacy();
627 #if !RC_HIDE_J79 && !RC_HIDE_J80
628 test_generate_access_control();
631 test_store_cert_to_ios();
632 test_store_identity_to_ios();
633 test_transform_with_ioskey();
634 test_convert_key_to_persistent_ref();
635 test_convert_cert_to_persistent_ref();
636 test_convert_identity_to_persistent_ref();
637 test_cssm_from_ios_key();