]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-12-item-create-keypair.c
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-12-item-create-keypair.c
1 #include <Security/SecKey.h>
2 #include <Security/SecKeychain.h>
3 #include <Security/SecKeychainSearch.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include "keychain_regressions.h"
8 #include "kc-helpers.h"
9
10 static void tests(void)
11 {
12 SecKeychainRef keychain = createNewKeychain("test", "test");
13 SecKeyRef pub_crypt = NULL, prv_crypt = NULL;
14 ok_status(SecKeyCreatePair(keychain, CSSM_ALGID_RSA, 256,
15 0 /* contextHandle */,
16 CSSM_KEYUSE_ENCRYPT | CSSM_KEYUSE_WRAP,
17 CSSM_KEYATTR_PERMANENT | CSSM_KEYATTR_EXTRACTABLE,
18 CSSM_KEYUSE_DECRYPT | CSSM_KEYUSE_UNWRAP,
19 CSSM_KEYATTR_PERMANENT | CSSM_KEYATTR_EXTRACTABLE |
20 CSSM_KEYATTR_SENSITIVE,
21 NULL /* initialAccess */, &pub_crypt, &prv_crypt),
22 "generate encryption keypair");
23
24 SecKeyRef pub_sign = NULL, prv_sign = NULL;
25 ok_status(SecKeyCreatePair(keychain, CSSM_ALGID_RSA, 256,
26 0 /* contextHandle */,
27 CSSM_KEYUSE_VERIFY,
28 CSSM_KEYATTR_PERMANENT | CSSM_KEYATTR_EXTRACTABLE,
29 CSSM_KEYUSE_SIGN,
30 CSSM_KEYATTR_PERMANENT | CSSM_KEYATTR_EXTRACTABLE |
31 CSSM_KEYATTR_SENSITIVE,
32 NULL /* initialAccess */, &pub_sign, &prv_sign),
33 "generate signing keypair");
34
35 uint32 btrue = 1;
36 uint32 bfalse = 0;
37 /* uint32 prv_class = CSSM_KEYCLASS_PRIVATE_KEY; */
38 SecKeychainAttribute attrs[] =
39 {
40 { kSecKeyDecrypt, sizeof(uint32), &btrue },
41 { kSecKeyEncrypt, sizeof(uint32), &bfalse },
42 /* { kSecKeyKeyClass, sizeof(uint32), &prv_class } */
43 };
44 SecKeychainAttributeList attrList = { sizeof(attrs) / sizeof(*attrs), attrs };
45 SecKeychainSearchRef search;
46 OSStatus result;
47 SecKeychainItemRef item;
48
49 ok_status((result = SecKeychainSearchCreateFromAttributes(keychain,
50 CSSM_DL_DB_RECORD_PRIVATE_KEY, &attrList, &search)), "create key search");
51 if (result == noErr)
52 {
53 ok_status(SecKeychainSearchCopyNext(search, &item), "get first key");
54 cmp_ok((intptr_t)prv_crypt, ==, (intptr_t)item, "is key found the right one?");
55 CFRelease(item);
56 item = NULL;
57 is_status(SecKeychainSearchCopyNext(search, &item),
58 errSecItemNotFound, "get next key");
59 is((intptr_t)item, 0, "no item returned");
60 CFRelease(search);
61 }
62
63 SecKeychainAttribute attrs2[] = { { kSecKeySign, sizeof(btrue), &btrue } };
64 SecKeychainAttributeList attrList2 = { sizeof(attrs2) / sizeof(*attrs2), attrs2 };
65 ok_status((result = SecKeychainSearchCreateFromAttributes(keychain,
66 CSSM_DL_DB_RECORD_PRIVATE_KEY, &attrList2, &search)), "create private signing key search");
67
68 if (result == noErr)
69 {
70 ok_status(SecKeychainSearchCopyNext(search, &item), "get first key");
71 cmp_ok((intptr_t)prv_sign, ==, (intptr_t)item, "is key found the right one?");
72 CFRelease(item);
73 is_status(SecKeychainSearchCopyNext(search, &item),
74 errSecItemNotFound, "get next key");
75 CFRelease(search);
76 }
77
78 CFRelease(pub_crypt);
79 CFRelease(prv_crypt);
80 CFRelease(pub_sign);
81 CFRelease(prv_sign);
82
83 ok_status(SecKeychainDelete(keychain), "%s: SecKeychainDelete", testName);
84 CFRelease(keychain);
85 }
86
87 int kc_17_item_find_key(int argc, char *const *argv)
88 {
89 plan_tests(13);
90
91 tests();
92
93 deleteTestFiles();
94 return 0;
95 }