]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-12-key-create-symmetric.c
Security-58286.51.6.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-12-key-create-symmetric.c
1 #include <Security/SecKeychain.h>
2 #include <Security/SecKeyPriv.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()
11 {
12 SecKeychainRef keychain = createNewKeychain("test", "test");
13
14 /* Symmetric key tests. */
15
16 ok_status(SecKeyGenerate(keychain, CSSM_ALGID_AES, 128,
17 0 /* contextHandle */,
18 CSSM_KEYUSE_DECRYPT | CSSM_KEYUSE_ENCRYPT,
19 CSSM_KEYATTR_EXTRACTABLE,
20 NULL, NULL), "SecKeyGenerate");
21
22 uint32 btrue = 1;
23 SecKeychainAttribute sym_attrs[] =
24 {
25 { kSecKeyEncrypt, sizeof(btrue), &btrue }
26 };
27 SecKeychainAttributeList sym_attr_list =
28 { sizeof(sym_attrs) / sizeof(*sym_attrs), sym_attrs };
29 SecKeychainSearchRef search = NULL;
30 ok_status(SecKeychainSearchCreateFromAttributes(keychain,
31 CSSM_DL_DB_RECORD_SYMMETRIC_KEY, &sym_attr_list, &search),
32 "create symmetric encryption key search");
33 SecKeychainItemRef item = NULL;
34 ok_status(SecKeychainSearchCopyNext(search, &item), "get first key");
35
36 if (item) CFRelease(item);
37 is_status(SecKeychainSearchCopyNext(search, &item),
38 errSecItemNotFound, "copy next returns no more keys");
39 CFRelease(search);
40
41 ok_status(SecKeychainSearchCreateFromAttributes(keychain,
42 CSSM_DL_DB_RECORD_ANY, NULL, &search),
43 "create any item search");
44 item = NULL;
45
46 ok_status(SecKeychainSearchCopyNext(search, &item), "get first key");
47
48 if (item) CFRelease(item);
49
50 is_status(SecKeychainSearchCopyNext(search, &item),
51 errSecItemNotFound, "copy next returns no more keys");
52 CFRelease(search);
53
54 SecKeyRef aes_key2 = NULL;
55 ok_status(SecKeyGenerate(keychain, CSSM_ALGID_AES, 128,
56 0 /* contextHandle */,
57 CSSM_KEYUSE_DECRYPT | CSSM_KEYUSE_ENCRYPT,
58 CSSM_KEYATTR_EXTRACTABLE,
59 NULL, &aes_key2), "SecKeyGenerate and get key");
60
61 is(CFGetRetainCount(aes_key2), 1, "retain count is 1");
62 CFRelease(aes_key2);
63
64
65 ok_status(SecKeychainDelete(keychain), "%s: SecKeychainDelete", testName);
66 CFRelease(keychain);
67 }
68
69 int kc_12_key_create_symmetric(int argc, char *const *argv)
70 {
71 plan_tests(11);
72
73 tests();
74
75 deleteTestFiles();
76 return 0;
77 }