]> git.saurik.com Git - apple/security.git/blob - OSX/shared_regressions/si-44-seckey-gen.m
Security-57740.20.22.tar.gz
[apple/security.git] / OSX / shared_regressions / si-44-seckey-gen.m
1 /*
2 * Copyright (c) 2016 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 #import <Foundation/Foundation.h>
26
27 #include "shared_regressions.h"
28
29 static void create_random_key_worker(id keyType, int keySize, bool permPub, bool permPriv) {
30 NSDictionary *params = nil;
31 NSError *error = nil;
32
33 params = @{
34 (id)kSecAttrKeyType: keyType,
35 (id)kSecAttrKeySizeInBits: @(keySize),
36 (id)kSecAttrLabel: @"si-44-seckey-gen:0",
37 (id)kSecPublicKeyAttrs: @{
38 (id)kSecAttrIsPermanent: @(permPub),
39 },
40 (id)kSecPrivateKeyAttrs: @{
41 (id)kSecAttrIsPermanent: @(permPriv),
42 },
43 };
44
45 id privateKey = CFBridgingRelease(SecKeyCreateRandomKey((CFDictionaryRef)params, (void *)&error));
46 ok(privateKey != nil, "successfully generated keys");
47
48 params = @{
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,
55 };
56 NSArray *items = nil;
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");
60
61 if (items.count > 0) {
62 params = @{
63 (id)kSecClass: (id)kSecClassKey,
64 (id)kSecAttrKeyType: keyType,
65 (id)kSecAttrKeySizeInBits: @(keySize),
66 (id)kSecAttrLabel: @"si-44-seckey-gen:0",
67 };
68 ok_status(SecItemDelete((CFDictionaryRef)params), "clear generated pair from keychain");
69 }
70 }
71
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);
81 }
82 static const int TestCountCreateRandomKey = (3 * 4 + 1 * 3) * 2;
83
84 static const int TestCount = TestCountCreateRandomKey;
85
86 int si_44_seckey_gen(int argc, char *const *argv) {
87 plan_tests(TestCount);
88
89 @autoreleasepool {
90 test_create_random_key();
91 }
92
93 return 0;
94 }