2 * Copyright (c) 2015 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 * This is to fool os services to not provide the Keychain manager
26 * interface tht doens't work since we don't have unified headers
27 * between iOS and OS X. rdar://23405418/
29 #define __KEYCHAINCORE__ 1
30 #import <Foundation/Foundation.h>
32 #include <CoreFoundation/CoreFoundation.h>
33 #include <Security/SecFramework.h>
34 #include <Security/SecBase.h>
35 #include <Security/SecItem.h>
36 #include <Security/SecItemPriv.h>
37 #include <Security/SecKey.h>
38 #include <Security/SecKeyPriv.h>
39 #include <Security/SecECKey.h>
40 #include <Security/SecAccessControl.h>
41 #include <Security/SecAccessControlPriv.h>
42 #include <Security/SecInternal.h>
43 #include <Security/SecCertificatePriv.h>
44 #include <utilities/SecFileLocations.h>
45 #include <utilities/SecCFWrappers.h>
46 #include <utilities/SecCFError.h>
47 #include <utilities/SecCFRelease.h>
48 #include <SecBase64.h>
50 #include <libaks_acl_cf_keys.h>
52 #include <ctkclient_test.h>
53 #include <coreauthd_spi.h>
55 #include "secd_regressions.h"
57 #include "SecdTestKeychainUtilities.h"
58 #include "SecKeybagSupport.h"
60 const char *cert1 = "MIIFQzCCBCugAwIBAgIBAjANBgkqhkiG9w0BAQsFADA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orBlock)(void));
68 static void test_item_add(void) {
70 static const UInt8 data[] = { 0x01, 0x02, 0x03, 0x04 };
71 CFDataRef valueData = CFDataCreate(NULL, data, sizeof(data));
72 static const UInt8 oid[] = { 0x05, 0x06, 0x07, 0x08 };
73 CFDataRef oidData = CFDataCreate(NULL, oid, sizeof(oid));
75 CFMutableDictionaryRef attrs = CFDictionaryCreateMutableForCFTypesWith(NULL,
76 kSecClass, kSecClassGenericPassword,
77 kSecAttrTokenID, CFSTR("tokenid"),
78 kSecAttrService, CFSTR("ctktest-service"),
79 kSecValueData, valueData,
80 kSecReturnAttributes, kCFBooleanTrue,
83 __block int phase = 0;
84 TKTokenTestSetHook(^(CFDictionaryRef attributes, TKTokenTestBlocks *blocks) {
86 eq_cf(CFDictionaryGetValue(attributes, kSecAttrTokenID), CFSTR("tokenid"));
88 blocks->createOrUpdateObject = ^CFDataRef(CFDataRef objectID, CFMutableDictionaryRef at, CFErrorRef *error) {
91 eq_cf(CFDictionaryGetValue(at, kSecClass), kSecClassGenericPassword);
92 eq_cf(CFDictionaryGetValue(at, kSecAttrService), CFDictionaryGetValue(attrs, kSecAttrService));
93 eq_cf(CFDictionaryGetValue(at, kSecAttrTokenID), CFSTR("tokenid"));
94 eq_cf(CFDictionaryGetValue(at, kSecValueData), valueData);
95 CFDictionaryRemoveValue(at, kSecValueData);
96 return CFRetainSafe(oidData);
99 blocks->copyObjectAccessControl = ^CFDataRef(CFDataRef oid, CFErrorRef *error) {
101 SecAccessControlRef ac = SecAccessControlCreate(NULL, NULL);
102 SecAccessControlSetProtection(ac, kSecAttrAccessibleAlwaysPrivate, NULL);
103 SecAccessControlAddConstraintForOperation(ac, kAKSKeyOpDefaultAcl, kCFBooleanTrue, NULL);
104 CFDataRef acData = SecAccessControlCopyData(ac);
109 blocks->copyObjectData = ^CFTypeRef(CFDataRef oid, CFErrorRef *error) {
111 return CFRetain(valueData);
115 CFTypeRef result = NULL;
116 ok_status(SecItemAdd(attrs, &result));
117 eq_cf(CFDictionaryGetValue(result, kSecAttrService), CFSTR("ctktest-service"));
118 eq_cf(CFDictionaryGetValue(result, kSecAttrTokenID), CFSTR("tokenid"));
119 is(CFDictionaryGetValue(result, kSecValueData), NULL);
120 CFReleaseNull(result);
125 CFDictionarySetValue(attrs, kSecReturnData, kCFBooleanTrue);
126 CFDictionarySetValue(attrs, kSecAttrService, CFSTR("ctktest-service1"));
127 ok_status(SecItemAdd(attrs, &result));
128 eq_cf(CFDictionaryGetValue(result, kSecAttrService), CFSTR("ctktest-service1"));
129 eq_cf(CFDictionaryGetValue(result, kSecAttrTokenID), CFSTR("tokenid"));
130 eq_cf(CFDictionaryGetValue(result, kSecValueData), valueData);
131 CFReleaseNull(result);
136 CFDictionaryRemoveValue(attrs, kSecReturnAttributes);
137 CFDictionarySetValue(attrs, kSecAttrAccount, CFSTR("2nd"));
138 ok_status(SecItemAdd(attrs, &result));
139 eq_cf(result, valueData);
140 CFReleaseNull(result);
144 CFRelease(valueData);
147 static const int kItemAddTestCount = 31;
149 static void test_item_query() {
150 static const UInt8 oid[] = { 0x05, 0x06, 0x07, 0x08 };
151 CFDataRef oidData = CFDataCreate(NULL, oid, sizeof(oid));
152 static const UInt8 data[] = { 0x01, 0x02, 0x03, 0x04 };
153 CFDataRef valueData = CFDataCreate(NULL, data, sizeof(data));
154 CFDataRef valueData2 = CFDataCreate(NULL, data, sizeof(data) - 1);
156 __block int phase = 0;
157 TKTokenTestSetHook(^(CFDictionaryRef attributes, TKTokenTestBlocks *blocks) {
159 eq_cf(CFDictionaryGetValue(attributes, kSecAttrTokenID), CFSTR("tokenid"));
161 blocks->copyObjectData = ^CFTypeRef(CFDataRef oid, CFErrorRef *error) {
163 return CFRetain(valueData);
167 // Add non-token item with the same service, to test queries returning mixed results.
168 CFMutableDictionaryRef attrs = CFDictionaryCreateMutableForCFTypesWith(NULL,
169 kSecClass, kSecClassGenericPassword,
170 kSecAttrService, CFSTR("ctktest-service"),
171 kSecValueData, valueData2,
173 ok_status(SecItemAdd(attrs, NULL));
176 // Query with service.
177 CFMutableDictionaryRef query;
178 query = CFDictionaryCreateMutableForCFTypesWith(NULL,
179 kSecClass, kSecClassGenericPassword,
180 kSecAttrService, CFSTR("ctktest-service"),
181 kSecReturnAttributes, kCFBooleanTrue,
182 kSecReturnData, kCFBooleanTrue,
186 CFTypeRef result = NULL;
187 ok_status(SecItemCopyMatching(query, &result));
189 is(CFGetTypeID(result), CFDictionaryGetTypeID());
190 eq_cf(CFDictionaryGetValue(result, kSecValueData), valueData);
191 is(CFGetTypeID(CFDictionaryGetValue(result, kSecAttrAccessControl)), SecAccessControlGetTypeID());
192 eq_cf(CFDictionaryGetValue(result, kSecAttrService), CFSTR("ctktest-service"));
193 CFReleaseSafe(result);
196 CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitAll);
197 ok_status(SecItemCopyMatching(query, &result));
199 is(CFGetTypeID(result), CFArrayGetTypeID());
200 is(CFArrayGetCount(result), 2);
201 CFReleaseSafe(result);
204 CFDictionaryRemoveValue(query, kSecMatchLimit);
205 CFDictionaryRemoveValue(query, kSecReturnData);
206 ok_status(SecItemCopyMatching(query, &result));
208 is(CFGetTypeID(result), CFDictionaryGetTypeID());
209 is(CFDictionaryGetValue(result, kSecValueData), NULL);
210 CFReleaseSafe(result);
213 CFDictionaryRemoveValue(query, kSecReturnAttributes);
214 CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue);
215 CFDictionarySetValue(query, kSecAttrTokenID, CFSTR("tokenid"));
216 ok_status(SecItemCopyMatching(query, &result));
218 eq_cf(result, valueData);
219 CFReleaseSafe(result);
222 CFRelease(valueData);
223 CFRelease(valueData2);
226 static const int kItemQueryTestCount = 21;
228 static void test_item_update() {
229 static const UInt8 data[] = { 0x01, 0x02, 0x03, 0x04 };
230 CFDataRef valueData2 = CFDataCreate(NULL, data, sizeof(data) - 1);
231 CFTypeRef result = NULL;
233 CFMutableDictionaryRef query, attrs;
236 __block int phase = 0;
237 __block bool store_value = false;
238 TKTokenTestSetHook(^(CFDictionaryRef attributes, TKTokenTestBlocks *blocks) {
240 eq_cf(CFDictionaryGetValue(attributes, kSecAttrTokenID), CFSTR("tokenid"));
242 blocks->createOrUpdateObject = ^CFDataRef(CFDataRef objectID, CFMutableDictionaryRef at, CFErrorRef *error) {
244 eq_cf(CFDictionaryGetValue(at, kSecValueData), valueData2);
246 CFDictionaryRemoveValue(at, kSecValueData);
248 return CFRetainSafe(objectID);
251 blocks->copyObjectAccessControl = ^CFDataRef(CFDataRef oid, CFErrorRef *error) {
253 SecAccessControlRef ac = SecAccessControlCreate(NULL, NULL);
254 SecAccessControlSetProtection(ac, kSecAttrAccessibleAlwaysPrivate, NULL);
255 SecAccessControlAddConstraintForOperation(ac, kAKSKeyOpDefaultAcl, kCFBooleanTrue, NULL);
256 CFDataRef acData = SecAccessControlCopyData(ac);
261 blocks->copyObjectData = ^CFTypeRef(CFDataRef oid, CFErrorRef *error) {
263 return CFRetain(valueData2);
267 query = CFDictionaryCreateMutableForCFTypesWith(NULL,
268 kSecClass, kSecClassGenericPassword,
269 kSecAttrTokenID, CFSTR("tokenid"),
270 kSecAttrService, CFSTR("ctktest-service"),
273 attrs = CFDictionaryCreateMutableForCFTypesWith(NULL,
274 kSecValueData, valueData2,
277 ok_status(SecItemUpdate(query, attrs));
281 CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue);
282 ok_status(SecItemCopyMatching(query, &result));
283 eq_cf(valueData2, result);
289 CFDictionaryRemoveValue(query, kSecReturnData);
290 ok_status(SecItemUpdate(query, attrs));
294 CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue);
295 ok_status(SecItemCopyMatching(query, &result));
296 eq_cf(valueData2, result);
301 CFDictionarySetValue(query, kSecAttrService, CFSTR("ctktest-service1"));
302 CFDictionaryRemoveValue(query, kSecReturnData);
303 ok_status(SecItemUpdate(query, attrs));
307 CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitAll);
308 CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue);
309 ok_status(SecItemCopyMatching(query, &result));
311 is(CFGetTypeID(result), CFArrayGetTypeID());
312 is(CFArrayGetCount(result), 2);
313 eq_cf(CFArrayGetValueAtIndex(result, 0), valueData2);
314 eq_cf(CFArrayGetValueAtIndex(result, 1), valueData2);
318 CFRelease(valueData2);
320 static const int kItemUpdateTestCount = 26;
322 static void test_item_delete(void) {
324 CFMutableDictionaryRef query;
327 __block int phase = 0;
328 __block CFErrorRef deleteError = NULL;
329 TKTokenTestSetHook(^(CFDictionaryRef attributes, TKTokenTestBlocks *blocks) {
331 eq_cf(CFDictionaryGetValue(attributes, kSecAttrTokenID), CFSTR("tokenid"));
333 blocks->copyObjectAccessControl = ^CFDataRef(CFDataRef oid, CFErrorRef *error) {
335 SecAccessControlRef ac = SecAccessControlCreate(NULL, NULL);
336 SecAccessControlSetProtection(ac, kSecAttrAccessibleAlwaysPrivate, NULL);
337 SecAccessControlAddConstraintForOperation(ac, kAKSKeyOpDefaultAcl, kCFBooleanTrue, NULL);
338 CFDataRef acData = SecAccessControlCopyData(ac);
343 blocks->deleteObject = ^bool(CFDataRef objectID, CFErrorRef *error) {
345 if (deleteError != NULL) {
346 CFAssignRetained(*error, deleteError);
354 query = CFDictionaryCreateMutableForCFTypesWith(NULL,
355 kSecClass, kSecClassGenericPassword,
356 kSecAttrTokenID, CFSTR("tokenid"),
357 kSecAttrService, CFSTR("ctktest-service"),
361 ok_status(SecItemDelete(query));
365 is_status(SecItemCopyMatching(query, &result), errSecItemNotFound);
369 CFDictionarySetValue(query, kSecAttrService, CFSTR("ctktest-service1"));
370 ok_status(SecItemCopyMatching(query, &result));
374 #if LA_CONTEXT_IMPLEMENTED
375 LASetErrorCodeBlock(^{ return (CFErrorRef)NULL; });
376 deleteError = CFErrorCreate(NULL, CFSTR(kTKErrorDomain), kTKErrorCodeAuthenticationFailed, NULL);
377 ok_status(SecItemDelete(query), "delete multiple token items");
378 is(phase, 6, "connect + delete-auth-fail + copyAccess + connect + delete + delete-2nd");
380 ok_status(SecItemDelete(query), "delete multiple token items");
381 is(phase, 3, "connect + delete + delete");
385 is_status(SecItemCopyMatching(query, &result), errSecItemNotFound);
388 is_status(SecItemDelete(query), errSecItemNotFound);
391 CFReleaseSafe(deleteError);
393 #if LA_CONTEXT_IMPLEMENTED
394 static const int kItemDeleteTestCount = 15;
396 static const int kItemDeleteTestCount = 14;
399 static void test_key_generate(void) {
401 __block int phase = 0;
402 TKTokenTestSetHook(^(CFDictionaryRef attributes, TKTokenTestBlocks *blocks) {
405 blocks->createOrUpdateObject = ^CFDataRef(CFDataRef objectID, CFMutableDictionaryRef at, CFErrorRef *error) {
408 CFDictionarySetValue(at, kSecClass, kSecClassKey);
409 SecKeyRef publicKey = NULL, privateKey = NULL;
410 CFMutableDictionaryRef params = CFDictionaryCreateMutableForCFTypesWith(NULL,
411 kSecAttrKeyType, kSecAttrKeyTypeEC,
412 kSecAttrKeySizeInBits, CFSTR("256"),
414 ok_status(SecKeyGeneratePair(params, &publicKey, &privateKey));
415 CFDictionaryRef privKeyAttrs = SecKeyCopyAttributeDictionary(privateKey);
416 CFRelease(privateKey);
417 CFRelease(publicKey);
419 CFDataRef oid = CFRetainSafe(CFDictionaryGetValue(privKeyAttrs, kSecValueData));
420 CFRelease(privKeyAttrs);
424 blocks->copyObjectAccessControl = ^CFDataRef(CFDataRef oid, CFErrorRef *error) {
426 SecAccessControlRef ac = SecAccessControlCreate(NULL, NULL);
427 SecAccessControlSetProtection(ac, kSecAttrAccessibleAlwaysPrivate, NULL);
428 SecAccessControlAddConstraintForOperation(ac, kAKSKeyOpDefaultAcl, kCFBooleanTrue, NULL);
429 CFDataRef acData = SecAccessControlCopyData(ac);
434 blocks->copyPublicKeyData = ^CFDataRef(CFDataRef objectID, CFErrorRef *error) {
436 SecKeyRef privKey = SecKeyCreateECPrivateKey(NULL, CFDataGetBytePtr(objectID), CFDataGetLength(objectID), kSecKeyEncodingBytes);
437 CFDataRef publicData;
438 ok_status(SecKeyCopyPublicBytes(privKey, &publicData));
443 blocks->copyObjectData = ^CFTypeRef(CFDataRef oid, CFErrorRef *error) {
449 CFDictionaryRef prk_params = CFDictionaryCreateForCFTypes(NULL,
450 kSecAttrIsPermanent, kCFBooleanTrue,
453 CFMutableDictionaryRef params = CFDictionaryCreateMutableForCFTypesWith(NULL,
454 kSecAttrKeyType, kSecAttrKeyTypeEC,
455 kSecAttrKeySizeInBits, CFSTR("256"),
456 kSecAttrTokenID, CFSTR("tokenid"),
457 kSecPrivateKeyAttrs, prk_params,
459 CFRelease(prk_params);
461 SecKeyRef publicKey = NULL, privateKey = NULL;
463 ok_status(SecKeyGeneratePair(params, &publicKey, &privateKey));
466 CFDictionaryRef query = CFDictionaryCreateForCFTypes(NULL,
467 kSecValueRef, privateKey,
468 kSecReturnAttributes, kCFBooleanTrue,
469 kSecReturnRef, kCFBooleanTrue,
470 kSecReturnData, kCFBooleanTrue,
473 CFDictionaryRef result = NULL, keyAttrs = NULL;
474 ok_status(SecItemCopyMatching(query, (CFTypeRef *)&result));
476 is(CFDictionaryGetValue(result, kSecValueData), NULL);
477 eq_cf(CFDictionaryGetValue(result, kSecAttrTokenID), CFSTR("tokenid"));
478 keyAttrs = SecKeyCopyAttributeDictionary((SecKeyRef)CFDictionaryGetValue(result, kSecValueRef));
479 eq_cf(CFDictionaryGetValue(keyAttrs, kSecAttrApplicationLabel), CFDictionaryGetValue(result, kSecAttrApplicationLabel));
480 CFAssignRetained(keyAttrs, SecKeyCopyAttributeDictionary(publicKey));
481 eq_cf(CFDictionaryGetValue(keyAttrs, kSecAttrApplicationLabel), CFDictionaryGetValue(result, kSecAttrApplicationLabel));
485 CFRelease(publicKey);
486 CFRelease(privateKey);
491 static const int kKeyGenerateTestCount = 14;
493 static void test_key_sign(void) {
495 static const UInt8 data[] = { 0x01, 0x02, 0x03, 0x04 };
496 CFDataRef valueData = CFDataCreate(NULL, data, sizeof(data));
498 __block int phase = 0;
499 __block CFErrorRef cryptoError = NULL;
500 __block SecKeyOperationType cryptoOperation = -1;
501 __block SecKeyAlgorithm cryptoAlgorithm = NULL;
502 TKTokenTestSetHook(^(CFDictionaryRef attributes, TKTokenTestBlocks *blocks) {
505 blocks->copyPublicKeyData = ^CFDataRef(CFDataRef objectID, CFErrorRef *error) {
507 SecKeyRef privKey = SecKeyCreateECPrivateKey(NULL, CFDataGetBytePtr(objectID), CFDataGetLength(objectID), kSecKeyEncodingBytes);
508 CFDataRef publicData;
509 ok_status(SecKeyCopyPublicBytes(privKey, &publicData));
514 blocks->copyObjectAccessControl = ^CFDataRef(CFDataRef oid, CFErrorRef *error) {
516 SecAccessControlRef ac = SecAccessControlCreate(NULL, NULL);
517 SecAccessControlSetProtection(ac, kSecAttrAccessibleAlwaysPrivate, NULL);
518 SecAccessControlAddConstraintForOperation(ac, kAKSKeyOpDefaultAcl, kCFBooleanTrue, NULL);
519 CFDataRef acData = SecAccessControlCopyData(ac);
524 blocks->copyObjectOperationAlgorithms = ^CFSetRef(CFDataRef oid, CFIndex operation, CFErrorRef *error) {
525 static NSSet *ops[] = {
526 [kSecKeyOperationTypeSign] = NULL,
527 [kSecKeyOperationTypeDecrypt] = NULL,
528 [kSecKeyOperationTypeKeyExchange] = NULL,
531 static dispatch_once_t onceToken;
532 dispatch_once(&onceToken, ^{
533 ops[kSecKeyOperationTypeSign] = [NSSet setWithArray:@[(id)kSecKeyAlgorithmECDSASignatureDigestX962]];
534 ops[kSecKeyOperationTypeDecrypt] = [NSSet setWithArray:@[(id)kSecKeyAlgorithmRSAEncryptionRaw]];
535 ops[kSecKeyOperationTypeKeyExchange] = [NSSet setWithArray:@[(id)kSecKeyAlgorithmECDHKeyExchangeCofactor]];
538 return CFBridgingRetain(ops[operation]);
541 blocks->copyOperationResult = ^CFTypeRef(CFDataRef objectID, CFIndex operation, CFArrayRef algorithms, CFIndex secKeyOperationMode, CFTypeRef in1, CFTypeRef in2, CFErrorRef *error) {
542 SecKeyAlgorithm algorithm = CFArrayGetValueAtIndex(algorithms, CFArrayGetCount(algorithms) - 1);
544 cryptoOperation = operation;
545 cryptoAlgorithm = algorithm;
546 if (cryptoError != NULL) {
547 CFAssignRetained(*error, cryptoError);
551 return CFRetainSafe(valueData);
554 blocks->copyObjectData = ^CFTypeRef(CFDataRef objectID, CFErrorRef *error) {
560 NSDictionary *query = @{ (id)kSecClass: (id)kSecClassKey, (id)kSecReturnRef: @YES };
563 SecKeyRef privateKey = NULL;
564 ok_status(SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&privateKey));
568 CFMutableDataRef sig = CFDataCreateMutable(NULL, 0);
569 CFDataSetLength(sig, 256);
570 size_t sigLen = CFDataGetLength(sig);
571 ok_status(SecKeyRawSign(privateKey, kSecPaddingPKCS1, data, sizeof(data), CFDataGetMutableBytePtr(sig), &sigLen));
573 is(cryptoAlgorithm, kSecKeyAlgorithmECDSASignatureDigestX962);
574 is(cryptoOperation, kSecKeyOperationTypeSign);
575 CFDataSetLength(sig, sigLen);
576 is(CFDataGetLength(sig), CFDataGetLength(valueData));
577 eq_cf(valueData, sig);
579 #if LA_CONTEXT_IMPLEMENTED
581 CFDataSetLength(sig, 256);
582 sigLen = CFDataGetLength(sig);
583 LASetErrorCodeBlock(^{ return (CFErrorRef)NULL; });
584 cryptoError = CFErrorCreate(NULL, CFSTR(kTKErrorDomain), kTKErrorCodeAuthenticationFailed, NULL);
585 ok_status(SecKeyRawSign(privateKey, kSecPaddingPKCS1, data, sizeof(data), CFDataGetMutableBytePtr(sig), &sigLen));
587 is(cryptoError, NULL);
588 CFDataSetLength(sig, sigLen);
589 is(CFDataGetLength(sig), CFDataGetLength(valueData));
590 eq_cf(valueData, sig);
595 result = CFBridgingRelease(SecKeyCreateDecryptedData(privateKey, kSecKeyAlgorithmRSAEncryptionRaw,
596 valueData, (void *)&error));
597 eq_cf((__bridge CFDataRef)result, valueData);
598 is(cryptoAlgorithm, kSecKeyAlgorithmRSAEncryptionRaw);
599 is(cryptoOperation, kSecKeyOperationTypeDecrypt);
601 NSDictionary *params = @{ (id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom, (id)kSecAttrKeySizeInBits: @256 };
602 SecKeyRef otherPrivKey = NULL, otherPubKey = NULL;
603 ok_status(SecKeyGeneratePair((CFDictionaryRef)params, &otherPubKey, &otherPrivKey));
606 result = CFBridgingRelease(SecKeyCopyKeyExchangeResult(privateKey, kSecKeyAlgorithmECDHKeyExchangeCofactor,
607 otherPubKey, (CFDictionaryRef)@{}, (void *)&error));
608 eq_cf((__bridge CFDataRef)result, valueData);
609 is(cryptoAlgorithm, kSecKeyAlgorithmECDHKeyExchangeCofactor);
610 is(cryptoOperation, kSecKeyOperationTypeKeyExchange);
612 CFReleaseSafe(otherPrivKey);
613 CFReleaseSafe(otherPubKey);
614 CFReleaseSafe(cryptoError);
616 CFRelease(privateKey);
619 #if LA_CONTEXT_IMPLEMENTED
620 static const int kKeySignTestCount = 20;
622 static const int kKeySignTestCount = 15;
625 static void test_key_generate_with_params(void) {
627 const UInt8 data[] = "foo";
628 CFDataRef cred_ref = CFDataCreate(NULL, data, 4);
629 __block int phase = 0;
630 TKTokenTestSetHook(^(CFDictionaryRef attributes, TKTokenTestBlocks *blocks) {
632 eq_cf(CFDictionaryGetValue(attributes, kSecUseOperationPrompt), CFSTR("prompt"));
633 is(CFDictionaryGetValue(attributes, kSecUseAuthenticationUI), NULL);
634 eq_cf(CFDictionaryGetValue(attributes, kSecUseCredentialReference), cred_ref);
636 blocks->createOrUpdateObject = ^CFDataRef(CFDataRef objectID, CFMutableDictionaryRef at, CFErrorRef *error) {
638 SecCFCreateError(-4 /* kTKErrorCodeCanceledByUser */, CFSTR(kTKErrorDomain), CFSTR(""), NULL, error);
643 CFDictionaryRef prk_params = CFDictionaryCreateForCFTypes(NULL,
644 kSecAttrIsPermanent, kCFBooleanTrue,
647 CFMutableDictionaryRef params = CFDictionaryCreateMutableForCFTypesWith(NULL,
648 kSecAttrKeyType, kSecAttrKeyTypeEC,
649 kSecAttrKeySizeInBits, CFSTR("256"),
650 kSecAttrTokenID, CFSTR("tokenid"),
651 kSecPrivateKeyAttrs, prk_params,
652 kSecUseOperationPrompt, CFSTR("prompt"),
653 kSecUseAuthenticationUI, kSecUseAuthenticationUIAllow,
654 kSecUseCredentialReference, cred_ref,
656 CFRelease(prk_params);
658 SecKeyRef publicKey = NULL, privateKey = NULL;
660 diag("This will produce an internal assert - on purpose");
661 is_status(SecKeyGeneratePair(params, &publicKey, &privateKey), errSecUserCanceled);
664 CFReleaseSafe(publicKey);
665 CFReleaseSafe(privateKey);
669 static const int kKeyGenerateWithParamsTestCount = 5;
671 static void test_error_codes(void) {
673 CFMutableDictionaryRef attrs = CFDictionaryCreateMutableForCFTypesWith(NULL,
674 kSecClass, kSecClassGenericPassword,
675 kSecAttrTokenID, CFSTR("tokenid"),
678 __block OSStatus ctk_error = 0;
679 TKTokenTestSetHook(^(CFDictionaryRef attributes, TKTokenTestBlocks *blocks) {
680 blocks->createOrUpdateObject = ^CFDataRef(CFDataRef objectID, CFMutableDictionaryRef at, CFErrorRef *error) {
681 SecCFCreateError(ctk_error, CFSTR(kTKErrorDomain), CFSTR(""), NULL, error);
686 ctk_error = kTKErrorCodeBadParameter;
687 is_status(SecItemAdd(attrs, NULL), errSecParam);
689 ctk_error = kTKErrorCodeNotImplemented;
690 is_status(SecItemAdd(attrs, NULL), errSecUnimplemented);
692 ctk_error = kTKErrorCodeCanceledByUser;
693 is_status(SecItemAdd(attrs, NULL), errSecUserCanceled);
697 static const int kErrorCodesCount = 3;
699 static CFDataRef copy_certificate_data(const char *base64Cert)
701 size_t size = SecBase64Decode(base64Cert, strnlen(base64Cert, 2048), NULL, 0);
703 CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, size);
704 CFDataSetLength(data, size);
705 size = SecBase64Decode(base64Cert, strnlen(base64Cert, 2048), (char*)CFDataGetMutableBytePtr(data), CFDataGetLength(data));
707 CFDataSetLength(data, size);
712 static CFMutableDictionaryRef copy_certificate_attributes(const char *base64Cert)
714 CFDataRef data = copy_certificate_data(base64Cert);
716 SecCertificateRef cert = SecCertificateCreateWithData(kCFAllocatorDefault, data);
718 CFDictionaryRef certAttributes = SecCertificateCopyAttributeDictionary(cert);
720 CFMutableDictionaryRef result = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, certAttributes);
724 CFRelease(certAttributes);
733 static CFDictionaryRef copy_certificate_query(const char *base64cert, CFStringRef label, CFStringRef oid, CFStringRef tokenID)
735 CFMutableDictionaryRef certAttributes = copy_certificate_attributes(base64cert);
737 CFDictionarySetValue(certAttributes, kSecAttrLabel, label);
738 CFDictionarySetValue(certAttributes, kSecAttrAccessible, kSecAttrAccessibleAlwaysPrivate);
739 CFDictionarySetValue(certAttributes, kSecAttrTokenOID, oid);
740 CFDictionaryRemoveValue(certAttributes, kSecValueData);
742 SecAccessControlRef acl = SecAccessControlCreate(kCFAllocatorDefault, NULL);
744 CFTypeRef key[] = { kSecAttrTokenID };
745 CFTypeRef value[] = { tokenID };
746 CFDictionaryRef protection = CFDictionaryCreate(kCFAllocatorDefault, key, value, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
747 ok(SecAccessControlSetProtection(acl, protection, NULL));
748 CFRelease(protection);
749 ok(SecAccessControlAddConstraintForOperation(acl, kAKSKeyOpDefaultAcl, kCFBooleanTrue, NULL));
750 CFDataRef aclData = SecAccessControlCopyData(acl);
753 CFDictionarySetValue(certAttributes, kSecAttrAccessControl, aclData);
760 return certAttributes;
763 static CFDictionaryRef copy_key_query(CFDictionaryRef certAttributes, CFStringRef label, CFStringRef oid, CFStringRef tokenID)
765 CFMutableDictionaryRef keyAttributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks) ;
767 CFDictionarySetValue(keyAttributes, kSecClass, kSecClassKey);
768 CFDictionarySetValue(keyAttributes, kSecAttrKeyClass, kSecAttrKeyClassPrivate);
769 CFDictionarySetValue(keyAttributes, kSecAttrKeyType, kSecAttrKeyTypeRSA);
770 CFNumberRef keySize = CFNumberCreateWithCFIndex(kCFAllocatorDefault, 2048);
771 CFDictionarySetValue(keyAttributes, kSecAttrKeySizeInBits, keySize);
774 CFDictionarySetValue(keyAttributes, kSecAttrCanDecrypt, kCFBooleanTrue);
775 CFDictionarySetValue(keyAttributes, kSecAttrCanSign, kCFBooleanTrue);
776 CFDictionarySetValue(keyAttributes, kSecAttrCanUnwrap, kCFBooleanTrue);
777 CFDictionarySetValue(keyAttributes, kSecAttrCanDerive, kCFBooleanFalse);
778 CFDictionarySetValue(keyAttributes, kSecAttrIsPrivate, kCFBooleanTrue);
780 CFDictionarySetValue(keyAttributes, kSecAttrLabel, label);
781 CFDictionarySetValue(keyAttributes, kSecAttrAccessible, kSecAttrAccessibleAlwaysPrivate);
782 CFDictionarySetValue(keyAttributes, kSecAttrTokenOID, oid);
783 CFDictionarySetValue(keyAttributes, kSecAttrApplicationLabel, CFDictionaryGetValue(certAttributes, kSecAttrPublicKeyHash));
785 SecAccessControlRef acl = SecAccessControlCreate(kCFAllocatorDefault, NULL);
787 CFTypeRef key[] = { kSecAttrTokenID };
788 CFTypeRef value[] = { tokenID };
789 CFDictionaryRef protection = CFDictionaryCreate(kCFAllocatorDefault, key, value, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
790 ok(SecAccessControlSetProtection(acl, protection, NULL));
791 CFRelease(protection);
792 ok(SecAccessControlAddConstraintForOperation(acl, kAKSKeyOpDefaultAcl, kCFBooleanTrue, NULL));
793 CFDataRef aclData = SecAccessControlCopyData(acl);
796 CFDictionarySetValue(keyAttributes, kSecAttrAccessControl, aclData);
803 return keyAttributes;
806 static void check_array_for_type_id(CFArrayRef array, CFTypeID typeID)
808 if (array && CFGetTypeID(array) == CFArrayGetTypeID()) {
809 for (CFIndex i = 0; i < CFArrayGetCount(array); ++i) {
810 ok(CFGetTypeID(CFArrayGetValueAtIndex(array, i)) == typeID);
815 static void test_propagate_token_items()
817 CFStringRef cert1OID = CFSTR("oid1");
818 CFStringRef cert2OID = CFSTR("oid2");
819 CFStringRef key1OID = CFSTR("oid3");
820 CFStringRef key2OID = CFSTR("oid4");
822 TKTokenTestSetHook(^(CFDictionaryRef attributes, TKTokenTestBlocks *blocks) {
823 blocks->copyObjectData = ^CFTypeRef(CFDataRef oid, CFErrorRef *error) {
824 if (CFEqual(oid, cert1OID)) {
825 return copy_certificate_data(cert1);
827 else if (CFEqual(oid, cert2OID)) {
828 return copy_certificate_data(cert2);
830 else if (CFEqual(oid, key1OID) || CFEqual(oid, key2OID)) {
839 CFStringRef tokenID = CFSTR("com.apple.secdtest:propagate_test_token");
841 CFMutableArrayRef items = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
843 CFDictionaryRef certQuery = copy_certificate_query(cert1, CFSTR("test_cert1"), cert1OID, tokenID);
845 CFDictionaryRef keyQuery = copy_key_query(certQuery, CFSTR("test_key1"), key1OID, tokenID);
848 CFArrayAppendValue(items, certQuery);
849 CFArrayAppendValue(items, keyQuery);
850 CFReleaseSafe(certQuery);
851 CFReleaseSafe(keyQuery);
853 certQuery = copy_certificate_query(cert2, CFSTR("test_cert2"), cert2OID, tokenID);
855 keyQuery = copy_key_query(certQuery, CFSTR("test_key2"), key2OID, tokenID);
858 CFArrayAppendValue(items, certQuery);
859 CFArrayAppendValue(items, keyQuery);
860 CFReleaseSafe(certQuery);
861 CFReleaseSafe(keyQuery);
864 ok_status(result = SecItemUpdateTokenItems(tokenID, NULL), "Failed to delete items.");
866 ok_status(result = SecItemUpdateTokenItems(tokenID, items), "Failed to propagate items.");
869 CFMutableDictionaryRef query = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
870 CFDictionarySetValue(query, kSecClass, kSecClassCertificate);
871 CFDictionarySetValue(query, kSecAttrAccessGroup, CFSTR("com.apple.token"));
872 CFDictionarySetValue(query, kSecReturnRef, kCFBooleanTrue);
873 CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitAll);
874 CFTypeRef queryResult;
875 ok_status(SecItemCopyMatching(query, &queryResult));
876 ok(queryResult && CFGetTypeID(queryResult) == CFArrayGetTypeID() && CFArrayGetCount(queryResult) == 2, "Expect array with two certs");
877 check_array_for_type_id(queryResult, SecCertificateGetTypeID());
878 CFReleaseNull(queryResult);
880 CFDictionarySetValue(query, kSecReturnRef, kCFBooleanFalse);
881 CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue);
882 ok_status(SecItemCopyMatching(query, &queryResult));
883 ok(queryResult && CFGetTypeID(queryResult) == CFArrayGetTypeID() && CFArrayGetCount(queryResult) == 2, "Expect array with two certs");
884 check_array_for_type_id(queryResult, CFDataGetTypeID());
885 CFReleaseNull(queryResult);
887 CFDictionarySetValue(query, kSecClass, kSecClassKey);
888 CFDictionarySetValue(query, kSecReturnRef, kCFBooleanTrue);
889 CFDictionarySetValue(query, kSecReturnData, kCFBooleanFalse);
890 ok_status(SecItemCopyMatching(query, &queryResult));
891 ok(queryResult && CFGetTypeID(queryResult) == CFArrayGetTypeID() && CFArrayGetCount(queryResult) == 2, "Expect array with two keys");
892 check_array_for_type_id(queryResult, SecKeyGetTypeID());
893 CFReleaseNull(queryResult);
895 CFDictionarySetValue(query, kSecReturnRef, kCFBooleanFalse);
896 CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue);
897 ok_status(SecItemCopyMatching(query, &queryResult));
898 ok(queryResult && CFGetTypeID(queryResult) == CFArrayGetTypeID() && CFArrayGetCount(queryResult) == 0, "Expect empty array");
899 CFReleaseNull(queryResult);
901 CFDictionarySetValue(query, kSecClass, kSecClassIdentity);
902 CFDictionarySetValue(query, kSecReturnRef, kCFBooleanTrue);
903 CFDictionarySetValue(query, kSecReturnData, kCFBooleanFalse);
904 ok_status(SecItemCopyMatching(query, &queryResult));
905 ok(queryResult && CFGetTypeID(queryResult) == CFArrayGetTypeID() && CFArrayGetCount(queryResult) == 2, "Expect array with two identities");
906 check_array_for_type_id(queryResult, SecIdentityGetTypeID());
907 CFReleaseNull(queryResult);
909 CFDictionarySetValue(query, kSecReturnRef, kCFBooleanFalse);
910 CFDictionarySetValue(query, kSecReturnData, kCFBooleanTrue);
911 ok_status(SecItemCopyMatching(query, &queryResult));
912 ok(queryResult && CFGetTypeID(queryResult) == CFArrayGetTypeID() && CFArrayGetCount(queryResult) == 0, "Expect empty array");
913 CFReleaseNull(queryResult);
915 ok_status(result = SecItemUpdateTokenItems(tokenID, NULL), "Failed to delete items.");
917 CFDictionarySetValue(query, kSecReturnRef, kCFBooleanTrue);
918 CFDictionarySetValue(query, kSecReturnData, kCFBooleanFalse);
919 is_status(SecItemCopyMatching(query, &queryResult), errSecItemNotFound);
920 CFReleaseNull(queryResult);
923 static const int kPropagateCount = 66;
925 static void test_identity_on_two_tokens() {
926 CFStringRef cert3OID = CFSTR("oid1");
927 TKTokenTestSetHook(^(CFDictionaryRef attributes, TKTokenTestBlocks *blocks) {
929 blocks->createOrUpdateObject = ^CFDataRef(CFDataRef objectID, CFMutableDictionaryRef at, CFErrorRef *error) {
931 return (__bridge_retained CFDataRef)[[NSData alloc] initWithBase64EncodedString:@"BAcrF9iBupEeZOE+c73JBfkqsv8Q9rp1lTnZbKzmALf8yTR02310uGlZuUBVp4HOSiziO43dzFuegH0ywLhu+gtJj81RD8Rt+nLR6oTARkL+0l2/fzrIouleaEYpYmEp0A==" options:NSDataBase64DecodingIgnoreUnknownCharacters];
934 blocks->copyPublicKeyData = ^CFDataRef(CFDataRef objectID, CFErrorRef *error) {
935 SecKeyRef privKey = SecKeyCreateECPrivateKey(NULL, CFDataGetBytePtr(objectID), CFDataGetLength(objectID), kSecKeyEncodingBytes);
937 CFDataRef publicData;
938 ok_status(SecKeyCopyPublicBytes(privKey, &publicData));
939 CFReleaseSafe(privKey);
943 blocks->copyObjectData = ^CFTypeRef(CFDataRef oid, CFErrorRef *error) {
944 if (CFEqual(oid, cert3OID))
945 return copy_certificate_data(cert3);
950 blocks->copyObjectAccessControl = ^CFDataRef(CFDataRef oid, CFErrorRef *error) {
951 SecAccessControlRef ac = SecAccessControlCreate(NULL, NULL);
952 SecAccessControlSetProtection(ac, kSecAttrAccessibleAlwaysPrivate, NULL);
953 SecAccessControlAddConstraintForOperation(ac, kAKSKeyOpDefaultAcl, kCFBooleanTrue, NULL);
954 CFDataRef acData = SecAccessControlCopyData(ac);
959 blocks->copyOperationResult = ^CFTypeRef(CFDataRef objectID, CFIndex operation, CFArrayRef algorithms, CFIndex secKeyOperationMode, CFTypeRef in1, CFTypeRef in2, CFErrorRef *error) {
961 SecKeyRef privKey = SecKeyCreateECPrivateKey(NULL, CFDataGetBytePtr(objectID), CFDataGetLength(objectID), kSecKeyEncodingBytes);
963 CFDataRef signature = SecKeyCreateSignature(privKey, kSecKeyAlgorithmECDSASignatureDigestX962, in1, error);
965 CFReleaseSafe(privKey);
972 NSString *tokenID1 = @"com.apple.secdtest:identity_test_token1";
973 NSString *tokenID2 = @"com.apple.secdtest:identity_test_token2";
975 NSDictionary *params = @{ (id)kSecAttrKeyType : (id)kSecAttrKeyTypeEC,
976 (id)kSecAttrKeySizeInBits : @"256",
977 (id)kSecAttrTokenID : tokenID1,
978 (id)kSecPrivateKeyAttrs : @{ (id)kSecAttrIsPermanent : @YES/*, (id)kSecAttrIsPrivate : @YES*/ }
981 SecKeyRef publicKey = NULL, privateKey = NULL;
982 ok_status(SecKeyGeneratePair((CFDictionaryRef)params, &publicKey, &privateKey));
984 NSDictionary *certQuery = CFBridgingRelease(copy_certificate_query(cert3, CFSTR("test_cert3"), cert3OID, (__bridge CFStringRef)tokenID2));
988 ok_status(result = SecItemUpdateTokenItems((__bridge CFStringRef)tokenID2, (__bridge CFArrayRef)@[certQuery]), "Failed to propagate items.");
990 NSData *pubKeyHash = CFBridgingRelease(SecKeyCopyPublicKeyHash(publicKey));
993 NSDictionary *query = @{ (id)kSecClass : (id)kSecClassKey, (id)kSecAttrApplicationLabel : pubKeyHash, (id)kSecReturnRef : @YES, (id)kSecReturnAttributes : @YES };
994 ok_status(result = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef*)&resultRef));
995 CFReleaseSafe(resultRef);
997 query = @{ (id)kSecClass : (id)kSecClassCertificate, (id)kSecAttrPublicKeyHash : pubKeyHash, (id)kSecReturnRef : @YES, (id)kSecReturnAttributes : @YES };
998 ok_status(result = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef*)&resultRef));
999 CFReleaseSafe(resultRef);
1001 query = @{ (id)kSecClass : (id)kSecClassIdentity, (id)kSecAttrApplicationLabel : pubKeyHash, (id)kSecReturnRef : @YES, (id)kSecReturnAttributes : @YES };
1002 ok_status(result = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef*)&resultRef));
1003 CFReleaseSafe(resultRef);
1006 static const int kIdentityonTwoTokensCount = 20;
1008 static void test_ies(SecKeyRef privateKey, SecKeyAlgorithm algorithm) {
1009 TKTokenTestSetHook(^(CFDictionaryRef attributes, TKTokenTestBlocks *blocks) {
1011 blocks->createOrUpdateObject = ^CFDataRef(CFDataRef objectID, CFMutableDictionaryRef at, CFErrorRef *error) {
1013 return CFBridgingRetain([@"oid" dataUsingEncoding:NSUTF8StringEncoding]);
1016 blocks->copyPublicKeyData = ^CFDataRef(CFDataRef objectID, CFErrorRef *error) {
1017 SecKeyRef publicKey = SecKeyCopyPublicKey(privateKey);
1018 CFDataRef data = SecKeyCopyExternalRepresentation(publicKey, error);
1019 CFReleaseNull(publicKey);
1023 blocks->copyObjectData = ^CFTypeRef(CFDataRef oid, CFErrorRef *error) {
1027 blocks->copyObjectAccessControl = ^CFDataRef(CFDataRef oid, CFErrorRef *error) {
1028 SecAccessControlRef ac = SecAccessControlCreate(NULL, NULL);
1029 SecAccessControlSetProtection(ac, kSecAttrAccessibleAlwaysPrivate, NULL);
1030 SecAccessControlAddConstraintForOperation(ac, kAKSKeyOpDefaultAcl, kCFBooleanTrue, NULL);
1031 CFDataRef acData = SecAccessControlCopyData(ac);
1036 blocks->copyOperationResult = ^CFTypeRef(CFDataRef objectID, CFIndex operation, CFArrayRef algorithms, CFIndex secKeyOperationMode, CFTypeRef in1, CFTypeRef in2, CFErrorRef *error) {
1037 CFTypeRef result = kCFNull;
1038 CFTypeRef algorithm = CFArrayGetValueAtIndex(algorithms, CFArrayGetCount(algorithms) - 1);
1039 switch (operation) {
1040 case kSecKeyOperationTypeKeyExchange: {
1041 if (CFEqual(algorithm, kSecKeyAlgorithmECDHKeyExchangeStandard) ||
1042 CFEqual(algorithm, kSecKeyAlgorithmECDHKeyExchangeCofactor)) {
1043 NSDictionary *attrs = CFBridgingRelease(SecKeyCopyAttributes(privateKey));
1044 NSDictionary *params = @{
1045 (id)kSecAttrKeyType: attrs[(id)kSecAttrKeyType],
1046 (id)kSecAttrKeySizeInBits: attrs[(id)kSecAttrKeySizeInBits],
1047 (id)kSecAttrKeyClass: (id)kSecAttrKeyClassPublic,
1049 SecKeyRef pubKey = SecKeyCreateWithData(in1, (CFDictionaryRef)params, error);
1050 if (pubKey == NULL) {
1053 result = SecKeyCopyKeyExchangeResult(privateKey, algorithm, pubKey, in2, error);
1054 CFReleaseSafe(pubKey);
1058 case kSecKeyOperationTypeDecrypt: {
1059 if (CFEqual(algorithm, kSecKeyAlgorithmRSAEncryptionRaw)) {
1060 result = SecKeyCreateDecryptedData(privateKey, algorithm, in1, error);
1071 NSDictionary *privateParams = CFBridgingRelease(SecKeyCopyAttributes(privateKey));
1072 NSDictionary *params = @{ (id)kSecAttrKeyType : privateParams[(id)kSecAttrKeyType],
1073 (id)kSecAttrKeySizeInBits : privateParams[(id)kSecAttrKeySizeInBits],
1074 (id)kSecAttrTokenID : @"tid-ies",
1075 (id)kSecPrivateKeyAttrs : @{ (id)kSecAttrIsPermanent : @YES }
1078 SecKeyRef tokenKey = SecKeyCreateRandomKey((CFDictionaryRef)params, (void *)&error);
1079 ok(tokenKey != NULL, "create token-based key (err %@)", error);
1080 SecKeyRef tokenPublicKey = SecKeyCopyPublicKey(tokenKey);
1082 NSData *plaintext = [@"plaintext" dataUsingEncoding:NSUTF8StringEncoding];
1085 NSData *ciphertext = CFBridgingRelease(SecKeyCreateEncryptedData(tokenPublicKey, algorithm, (CFDataRef)plaintext, (void *)&error));
1086 ok(ciphertext, "failed to encrypt IES, err %@", error);
1088 NSData *decrypted = CFBridgingRelease(SecKeyCreateDecryptedData(tokenKey, algorithm, (CFDataRef)ciphertext, (void *)&error));
1089 ok(decrypted, "failed to decrypt IES, err %@", error);
1091 eq_cf((__bridge CFDataRef)plaintext, (__bridge CFDataRef)decrypted, "decrypted(%@) != plaintext(%@)", decrypted, plaintext);
1093 CFReleaseNull(tokenKey);
1094 CFReleaseNull(tokenPublicKey);
1096 static const int kIESCount = 4;
1098 static void test_ecies() {
1100 SecKeyRef privateKey = SecKeyCreateRandomKey((CFDictionaryRef)@{(id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom, (id)kSecAttrKeySizeInBits: @256}, (void *)&error);
1101 ok(privateKey != NULL, "failed to generate CPU EC key: %@", error);
1103 test_ies(privateKey, kSecKeyAlgorithmECIESEncryptionCofactorX963SHA256AESGCM);
1105 CFReleaseNull(privateKey);
1107 static const int kECIESCount = kIESCount + 1;
1109 static void test_rsawrap() {
1111 SecKeyRef privateKey = SecKeyCreateRandomKey((CFDictionaryRef)@{(id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA, (id)kSecAttrKeySizeInBits: @2048}, (void *)&error);
1112 ok(privateKey != NULL, "failed to generate CPU RSA key: %@", error);
1114 test_ies(privateKey, kSecKeyAlgorithmRSAEncryptionOAEPSHA256AESGCM);
1116 CFReleaseNull(privateKey);
1118 static const int kRSAWrapCount = kIESCount + 1;
1120 static void tests(void) {
1121 /* custom keychain dir */
1122 secd_test_setup_temp_keychain("secd_33_keychain_ctk", NULL);
1128 test_key_generate();
1130 test_key_generate_with_params();
1132 test_propagate_token_items();
1133 test_identity_on_two_tokens();
1138 int secd_33_keychain_ctk(int argc, char *const *argv) {
1139 plan_tests(kItemAddTestCount +
1140 kItemQueryTestCount +
1141 kItemUpdateTestCount +
1142 kItemDeleteTestCount +
1143 kKeyGenerateTestCount +
1145 kKeyGenerateWithParamsTestCount +
1148 kIdentityonTwoTokensCount +
1151 kSecdTestSetupTestCount);