1 #include <Security/SecKey.h>
2 #include <Security/SecKeychain.h>
3 #include <Security/SecKeychainSearch.h>
13 SecKeychainRef keychain
;
14 ok_status(SecKeychainCreate("test", 4, "test", FALSE
, NULL
, &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");
27 SecKeyRef pub_sign
= NULL
, prv_sign
= NULL
;
28 ok_status(SecKeyCreatePair(keychain
, CSSM_ALGID_RSA
, 256,
29 0 /* contextHandle */,
31 CSSM_KEYATTR_PERMANENT
| CSSM_KEYATTR_EXTRACTABLE
,
33 CSSM_KEYATTR_PERMANENT
| CSSM_KEYATTR_EXTRACTABLE
|
34 CSSM_KEYATTR_SENSITIVE
,
35 NULL
/* initialAccess */, &pub_sign
, &prv_sign
),
36 "generate signing keypair");
40 /* uint32 prv_class = CSSM_KEYCLASS_PRIVATE_KEY; */
41 SecKeychainAttribute attrs
[] =
43 { kSecKeyDecrypt
, sizeof(uint32
), &btrue
},
44 { kSecKeyEncrypt
, sizeof(uint32
), &bfalse
},
45 /* { kSecKeyKeyClass, sizeof(uint32), &prv_class } */
47 SecKeychainAttributeList attrList
= { sizeof(attrs
) / sizeof(*attrs
), attrs
};
48 SecKeychainSearchRef search
;
50 SecKeychainItemRef item
;
52 ok_status((result
= SecKeychainSearchCreateFromAttributes(keychain
,
53 CSSM_DL_DB_RECORD_PRIVATE_KEY
, &attrList
, &search
)), "create key search");
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?");
60 is_status(SecKeychainSearchCopyNext(search
, &item
),
61 errSecItemNotFound
, "get next key");
62 is((intptr_t)item
, 0, "no item returned");
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");
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?");
76 is_status(SecKeychainSearchCopyNext(search
, &item
),
77 errSecItemNotFound
, "get next key");
87 ok(tests_end(1), "cleanup");
90 int main(int argc
, char *const *argv
)
94 if (!tests_begin(argc
, argv
))
95 BAIL_OUT("tests_begin failed");