]> git.saurik.com Git - apple/security.git/blob - Security/sec/SOSCircle/Regressions/sc-51-persistentEC.c
Security-57031.1.35.tar.gz
[apple/security.git] / Security / sec / SOSCircle / Regressions / sc-51-persistentEC.c
1 /*
2 * sc-51-persistentEC.c
3 * Security
4 *
5 * Copyright (c) 2008-2010,2012-2014 Apple Inc. All Rights Reserved.
6 *
7 */
8
9 /*
10 Test code for SOSCircle keys
11 */
12
13 #include <CoreFoundation/CoreFoundation.h>
14 #include <Security/SecBase.h>
15 #include <Security/SecItem.h>
16 #include <Security/SecKey.h>
17 #if TARGET_OS_EMBEDDED
18 #include <Security/SecInternal.h>
19 #endif
20 #include <Security/SecItemPriv.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <utilities/SecCFWrappers.h>
24 #include <utilities/iOSforOSX.h>
25
26 #include "SOSCircle_regressions.h"
27
28 static SecKeyRef GeneratePermanentFullECKey(int keySize, CFStringRef name)
29 {
30 SecKeyRef public_key = NULL;
31 SecKeyRef full_key = NULL;
32
33 CFNumberRef key_size_num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &keySize);
34
35 CFDictionaryRef keygen_parameters = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
36 kSecAttrKeyType, kSecAttrKeyTypeEC,
37 kSecAttrKeySizeInBits, key_size_num,
38 kSecAttrIsPermanent, kCFBooleanTrue,
39 kSecAttrAccessible, kSecAttrAccessibleAlwaysThisDeviceOnly,
40 kSecAttrLabel, name,
41 NULL);
42
43 CFReleaseNull(key_size_num);
44 ok_status(SecKeyGeneratePair(keygen_parameters, &public_key, &full_key), "generate EC Key Pair");
45 CFReleaseNull(keygen_parameters);
46 CFReleaseNull(public_key);
47
48 return full_key;
49 }
50
51 static void tests(void)
52 {
53 CFStringRef ourAccountName = CFSTR("LjzZ2JteIrnHoHWf5hYb1WGqjI");
54 CFStringRef circleName = kSecAttrAccessibleWhenUnlocked;
55 CFStringRef keyName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("ID for %@-%@"), ourAccountName, circleName);
56 const int keySize = 256;
57
58 // Create it
59 SecKeyRef full_key = GeneratePermanentFullECKey(keySize, keyName);
60 ok(full_key, "EC Key generated");
61
62 // Now search for it
63 CFNumberRef key_size_num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &keySize);
64
65 CFDictionaryRef keysearch_parameters = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
66 kSecClass, kSecClassKey,
67 kSecReturnRef, kCFBooleanTrue,
68 kSecAttrKeySizeInBits, key_size_num,
69 kSecAttrLabel, keyName,
70 NULL);
71
72 CFReleaseNull(key_size_num);
73 CFReleaseNull(keyName);
74
75 CFTypeRef results = NULL;
76 ok_status(SecItemCopyMatching(keysearch_parameters, &results), "find EC key by attr");
77 ok(results && (CFGetTypeID(results) == SecKeyGetTypeID()), "Got a SecKeyRef");
78 CFReleaseNull(results);
79
80 ok_status(SecItemDelete(keysearch_parameters), "delete EC Key Pair");
81
82 CFRelease(keysearch_parameters);
83 }
84
85 int sc_51_persistentEC(int argc, char *const *argv)
86 {
87 plan_tests(5);
88 tests();
89
90 return 0;
91 }