2 * Copyright (c) 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 __RCSID("$Heimdal: ks_keychain.c 22084 2007-11-16 20:12:30Z lha $"
38 #ifdef HAVE_FRAMEWORK_SECURITY
40 #include <Security/Security.h>
42 /* Missing function decls in pre Leopard */
43 #ifdef NEED_SECKEYGETCSPHANDLE_PROTO
44 OSStatus
SecKeyGetCSPHandle(SecKeyRef
, CSSM_CSP_HANDLE
*);
45 OSStatus
SecKeyGetCredentials(SecKeyRef
, CSSM_ACL_AUTHORIZATION_TAG
,
46 int, const CSSM_ACCESS_CREDENTIALS
**);
47 #define kSecCredentialTypeDefault 0
52 getAttribute(SecKeychainItemRef itemRef
, SecItemAttr item
,
53 SecKeychainAttributeList
**attrs
)
55 SecKeychainAttributeInfo attrInfo
;
56 UInt32 attrFormat
= 0;
63 attrInfo
.format
= &attrFormat
;
65 ret
= SecKeychainItemCopyAttributesAndData(itemRef
, &attrInfo
, NULL
,
78 SecKeychainItemRef item
;
84 kc_rsa_public_encrypt(int flen
,
85 const unsigned char *from
,
94 kc_rsa_public_decrypt(int flen
,
95 const unsigned char *from
,
105 kc_rsa_private_encrypt(int flen
,
106 const unsigned char *from
,
111 struct kc_rsa
*kc
= RSA_get_app_data(rsa
);
115 const CSSM_ACCESS_CREDENTIALS
*creds
;
116 SecKeyRef privKeyRef
= (SecKeyRef
)kc
->item
;
117 CSSM_CSP_HANDLE cspHandle
;
118 const CSSM_KEY
*cssmKey
;
119 CSSM_CC_HANDLE sigHandle
= 0;
124 cret
= SecKeyGetCSSMKey(privKeyRef
, &cssmKey
);
127 cret
= SecKeyGetCSPHandle(privKeyRef
, &cspHandle
);
130 ret
= SecKeyGetCredentials(privKeyRef
, CSSM_ACL_AUTHORIZATION_SIGN
,
131 kSecCredentialTypeDefault
, &creds
);
134 ret
= CSSM_CSP_CreateSignatureContext(cspHandle
, CSSM_ALGID_RSA
,
135 creds
, cssmKey
, &sigHandle
);
138 in
.Data
= (uint8
*)from
;
141 sig
.Data
= (uint8
*)to
;
142 sig
.Length
= kc
->keysize
;
144 cret
= CSSM_SignData(sigHandle
, &in
, 1, CSSM_ALGID_NONE
, &sig
);
146 /* cssmErrorString(cret); */
152 CSSM_DeleteContext(sigHandle
);
158 kc_rsa_private_decrypt(int flen
, const unsigned char *from
, unsigned char *to
,
159 RSA
* rsa
, int padding
)
165 kc_rsa_init(RSA
*rsa
)
171 kc_rsa_finish(RSA
*rsa
)
173 struct kc_rsa
*kc_rsa
= RSA_get_app_data(rsa
);
174 CFRelease(kc_rsa
->item
);
175 memset(kc_rsa
, 0, sizeof(*kc_rsa
));
180 static const RSA_METHOD kc_rsa_pkcs1_method
= {
181 "hx509 Keychain PKCS#1 RSA",
182 kc_rsa_public_encrypt
,
183 kc_rsa_public_decrypt
,
184 kc_rsa_private_encrypt
,
185 kc_rsa_private_decrypt
,
197 set_private_key(hx509_context context
,
198 SecKeychainItemRef itemRef
,
202 hx509_private_key key
;
206 ret
= _hx509_private_key_init(&key
, NULL
, NULL
);
210 kc
= calloc(1, sizeof(*kc
));
212 _hx509_abort("out of memory");
218 _hx509_abort("out of memory");
220 /* Argh, fake modulus since OpenSSL API is on crack */
222 SecKeychainAttributeList
*attrs
= NULL
;
227 if (rsa
->n
== NULL
) abort();
229 ret
= getAttribute(itemRef
, kSecKeyKeySizeInBits
, &attrs
);
232 size
= *(uint32_t *)attrs
->attr
[0].data
;
233 SecKeychainItemFreeAttributesAndData(attrs
, NULL
);
235 kc
->keysize
= (size
+ 7) / 8;
237 data
= malloc(kc
->keysize
);
238 memset(data
, 0xe0, kc
->keysize
);
239 BN_bin2bn(data
, kc
->keysize
, rsa
->n
);
244 RSA_set_method(rsa
, &kc_rsa_pkcs1_method
);
245 ret
= RSA_set_app_data(rsa
, kc
);
247 _hx509_abort("RSA_set_app_data");
249 _hx509_private_key_assign_rsa(key
, rsa
);
250 _hx509_cert_assign_key(cert
, key
);
261 SecKeychainRef keychain
;
265 keychain_init(hx509_context context
,
266 hx509_certs certs
, void **data
, int flags
,
267 const char *residue
, hx509_lock lock
)
269 struct ks_keychain
*ctx
;
271 ctx
= calloc(1, sizeof(*ctx
));
273 hx509_clear_error_string(context
);
278 if (strcasecmp(residue
, "system-anchors") == 0) {
280 } else if (strncasecmp(residue
, "FILE:", 5) == 0) {
283 ret
= SecKeychainOpen(residue
+ 5, &ctx
->keychain
);
285 hx509_set_error_string(context
, 0, ENOENT
,
286 "Failed to open %s", residue
);
290 hx509_set_error_string(context
, 0, ENOENT
,
291 "Unknown subtype %s", residue
);
305 keychain_free(hx509_certs certs
, void *data
)
307 struct ks_keychain
*ctx
= data
;
309 CFRelease(ctx
->keychain
);
310 memset(ctx
, 0, sizeof(*ctx
));
322 SecKeychainSearchRef searchRef
;
326 keychain_iter_start(hx509_context context
,
327 hx509_certs certs
, void *data
, void **cursor
)
329 struct ks_keychain
*ctx
= data
;
332 iter
= calloc(1, sizeof(*iter
));
334 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
343 ret
= hx509_certs_init(context
, "MEMORY:ks-file-create",
344 0, NULL
, &iter
->certs
);
350 ret
= SecTrustCopyAnchorCertificates(&anchors
);
352 hx509_certs_free(&iter
->certs
);
354 hx509_set_error_string(context
, 0, ENOMEM
,
355 "Can't get trust anchors from Keychain");
358 for (i
= 0; i
< CFArrayGetCount(anchors
); i
++) {
359 SecCertificateRef cr
;
363 cr
= (SecCertificateRef
)CFArrayGetValueAtIndex(anchors
, i
);
365 SecCertificateGetData(cr
, &cssm
);
367 ret
= hx509_cert_init_data(context
, cssm
.Data
, cssm
.Length
, &cert
);
371 ret
= hx509_certs_add(context
, iter
->certs
, cert
);
372 hx509_cert_free(cert
);
379 ret
= hx509_certs_start_seq(context
, iter
->certs
, &iter
->cursor
);
381 hx509_certs_free(&iter
->certs
);
388 ret
= SecKeychainSearchCreateFromAttributes(ctx
->keychain
,
389 kSecCertificateItemClass
,
394 hx509_set_error_string(context
, 0, ret
,
395 "Failed to start search for attributes");
409 keychain_iter(hx509_context context
,
410 hx509_certs certs
, void *data
, void *cursor
, hx509_cert
*cert
)
412 SecKeychainAttributeList
*attrs
= NULL
;
413 SecKeychainAttributeInfo attrInfo
;
414 UInt32 attrFormat
[1] = { 0 };
415 SecKeychainItemRef itemRef
;
417 struct iter
*iter
= cursor
;
423 return hx509_certs_next_cert(context
, iter
->certs
, iter
->cursor
, cert
);
427 ret
= SecKeychainSearchCopyNext(iter
->searchRef
, &itemRef
);
428 if (ret
== errSecItemNotFound
)
434 * Pick out certificate and matching "keyid"
437 item
[0] = kSecPublicKeyHashItemAttr
;
441 attrInfo
.format
= attrFormat
;
443 ret
= SecKeychainItemCopyAttributesAndData(itemRef
, &attrInfo
, NULL
,
448 ret
= hx509_cert_init_data(context
, ptr
, len
, cert
);
453 * Find related private key if there is one by looking at
454 * kSecPublicKeyHashItemAttr == kSecKeyLabel
457 SecKeychainSearchRef search
;
458 SecKeychainAttribute attrKeyid
;
459 SecKeychainAttributeList attrList
;
461 attrKeyid
.tag
= kSecKeyLabel
;
462 attrKeyid
.length
= attrs
->attr
[0].length
;
463 attrKeyid
.data
= attrs
->attr
[0].data
;
466 attrList
.attr
= &attrKeyid
;
468 ret
= SecKeychainSearchCreateFromAttributes(NULL
,
469 CSSM_DL_DB_RECORD_PRIVATE_KEY
,
477 ret
= SecKeychainSearchCopyNext(search
, &itemRef
);
479 if (ret
== errSecItemNotFound
) {
486 set_private_key(context
, itemRef
, *cert
);
490 SecKeychainItemFreeAttributesAndData(attrs
, ptr
);
500 keychain_iter_end(hx509_context context
,
505 struct iter
*iter
= cursor
;
509 ret
= hx509_certs_end_seq(context
, iter
->certs
, iter
->cursor
);
510 hx509_certs_free(&iter
->certs
);
512 CFRelease(iter
->searchRef
);
515 memset(iter
, 0, sizeof(*iter
));
524 struct hx509_keyset_ops keyset_keychain
= {
537 #endif /* HAVE_FRAMEWORK_SECURITY */
544 _hx509_ks_keychain_register(hx509_context context
)
546 #ifdef HAVE_FRAMEWORK_SECURITY
547 _hx509_ks_register(context
, &keyset_keychain
);