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