1 #include <Security/SecKey.h>
2 #include <Security/SecKeychain.h>
3 #include <Security/SecKeychainSearch.h>
7 #include "keychain_regressions.h"
8 #include "kc-helpers.h"
10 static void tests(void)
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");
24 SecKeyRef pub_sign
= NULL
, prv_sign
= NULL
;
25 ok_status(SecKeyCreatePair(keychain
, CSSM_ALGID_RSA
, 256,
26 0 /* contextHandle */,
28 CSSM_KEYATTR_PERMANENT
| CSSM_KEYATTR_EXTRACTABLE
,
30 CSSM_KEYATTR_PERMANENT
| CSSM_KEYATTR_EXTRACTABLE
|
31 CSSM_KEYATTR_SENSITIVE
,
32 NULL
/* initialAccess */, &pub_sign
, &prv_sign
),
33 "generate signing keypair");
37 /* uint32 prv_class = CSSM_KEYCLASS_PRIVATE_KEY; */
38 SecKeychainAttribute attrs
[] =
40 { kSecKeyDecrypt
, sizeof(uint32
), &btrue
},
41 { kSecKeyEncrypt
, sizeof(uint32
), &bfalse
},
42 /* { kSecKeyKeyClass, sizeof(uint32), &prv_class } */
44 SecKeychainAttributeList attrList
= { sizeof(attrs
) / sizeof(*attrs
), attrs
};
45 SecKeychainSearchRef search
;
47 SecKeychainItemRef item
;
49 ok_status((result
= SecKeychainSearchCreateFromAttributes(keychain
,
50 CSSM_DL_DB_RECORD_PRIVATE_KEY
, &attrList
, &search
)), "create key search");
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?");
57 is_status(SecKeychainSearchCopyNext(search
, &item
),
58 errSecItemNotFound
, "get next key");
59 is((intptr_t)item
, 0, "no item returned");
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");
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?");
73 is_status(SecKeychainSearchCopyNext(search
, &item
),
74 errSecItemNotFound
, "get next key");
83 ok_status(SecKeychainDelete(keychain
), "%s: SecKeychainDelete", testName
);
87 int kc_17_item_find_key(int argc
, char *const *argv
)