2 * Copyright (c) 2016 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 #import <Foundation/Foundation.h>
27 #include "shared_regressions.h"
29 static void create_random_key_worker(id keyType, int keySize, bool permPub, bool permPriv) {
30 NSDictionary *params = nil;
34 (id)kSecAttrKeyType: keyType,
35 (id)kSecAttrKeySizeInBits: @(keySize),
36 (id)kSecAttrLabel: @"si-44-seckey-gen:0",
37 (id)kSecPublicKeyAttrs: @{
38 (id)kSecAttrIsPermanent: @(permPub),
40 (id)kSecPrivateKeyAttrs: @{
41 (id)kSecAttrIsPermanent: @(permPriv),
45 id privateKey = CFBridgingRelease(SecKeyCreateRandomKey((CFDictionaryRef)params, (void *)&error));
46 ok(privateKey != nil, "successfully generated keys");
49 (id)kSecClass: (id)kSecClassKey,
50 (id)kSecAttrKeyType: keyType,
51 (id)kSecAttrKeySizeInBits: @(keySize),
52 (id)kSecAttrLabel: @"si-44-seckey-gen:0",
53 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
54 (id)kSecReturnAttributes: @YES,
57 OSStatus expected = (permPub || permPriv) ? errSecSuccess : errSecItemNotFound;
58 is_status(SecItemCopyMatching((CFDictionaryRef)params, (void *)&items), expected, "keychain query for generated keys");
59 is((int)items.count, (permPub ? 1 : 0) + (permPriv ? 1 : 0), "found keys in the keychain");
61 if (items.count > 0) {
63 (id)kSecClass: (id)kSecClassKey,
64 (id)kSecAttrKeyType: keyType,
65 (id)kSecAttrKeySizeInBits: @(keySize),
66 (id)kSecAttrLabel: @"si-44-seckey-gen:0",
68 ok_status(SecItemDelete((CFDictionaryRef)params), "clear generated pair from keychain");
72 static void test_create_random_key() {
73 create_random_key_worker((id)kSecAttrKeyTypeRSA, 1024, false, false);
74 create_random_key_worker((id)kSecAttrKeyTypeRSA, 1024, true, false);
75 create_random_key_worker((id)kSecAttrKeyTypeRSA, 1024, false, true);
76 create_random_key_worker((id)kSecAttrKeyTypeRSA, 1024, true, true);
77 create_random_key_worker((id)kSecAttrKeyTypeECSECPrimeRandom, 256, false, false);
78 create_random_key_worker((id)kSecAttrKeyTypeECSECPrimeRandom, 256, true, false);
79 create_random_key_worker((id)kSecAttrKeyTypeECSECPrimeRandom, 256, false, true);
80 create_random_key_worker((id)kSecAttrKeyTypeECSECPrimeRandom, 256, true, true);
82 static const int TestCountCreateRandomKey = (3 * 4 + 1 * 3) * 2;
84 static const int TestCount = TestCountCreateRandomKey;
86 int si_44_seckey_gen(int argc, char *const *argv) {
87 plan_tests(TestCount);
90 test_create_random_key();