1 /* $NetBSD: ks_keychain.c,v 1.1.1.2 2014/04/24 12:45:42 pettai Exp $ */
4 * Copyright (c) 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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
48 #define CSSM_SIZE uint32_t
53 getAttribute(SecKeychainItemRef itemRef
, SecItemAttr item
,
54 SecKeychainAttributeList
**attrs
)
56 SecKeychainAttributeInfo attrInfo
;
57 UInt32 attrFormat
= 0;
64 attrInfo
.format
= &attrFormat
;
66 ret
= SecKeychainItemCopyAttributesAndData(itemRef
, &attrInfo
, NULL
,
79 SecKeychainItemRef item
;
85 kc_rsa_public_encrypt(int flen
,
86 const unsigned char *from
,
95 kc_rsa_public_decrypt(int flen
,
96 const unsigned char *from
,
106 kc_rsa_private_encrypt(int flen
,
107 const unsigned char *from
,
112 struct kc_rsa
*kc
= RSA_get_app_data(rsa
);
116 const CSSM_ACCESS_CREDENTIALS
*creds
;
117 SecKeyRef privKeyRef
= (SecKeyRef
)kc
->item
;
118 CSSM_CSP_HANDLE cspHandle
;
119 const CSSM_KEY
*cssmKey
;
120 CSSM_CC_HANDLE sigHandle
= 0;
124 if (padding
!= RSA_PKCS1_PADDING
)
127 cret
= SecKeyGetCSSMKey(privKeyRef
, &cssmKey
);
130 cret
= SecKeyGetCSPHandle(privKeyRef
, &cspHandle
);
133 ret
= SecKeyGetCredentials(privKeyRef
, CSSM_ACL_AUTHORIZATION_SIGN
,
134 kSecCredentialTypeDefault
, &creds
);
137 ret
= CSSM_CSP_CreateSignatureContext(cspHandle
, CSSM_ALGID_RSA
,
138 creds
, cssmKey
, &sigHandle
);
141 in
.Data
= (uint8
*)from
;
144 sig
.Data
= (uint8
*)to
;
145 sig
.Length
= kc
->keysize
;
147 cret
= CSSM_SignData(sigHandle
, &in
, 1, CSSM_ALGID_NONE
, &sig
);
149 /* cssmErrorString(cret); */
155 CSSM_DeleteContext(sigHandle
);
161 kc_rsa_private_decrypt(int flen
, const unsigned char *from
, unsigned char *to
,
162 RSA
* rsa
, int padding
)
164 struct kc_rsa
*kc
= RSA_get_app_data(rsa
);
168 const CSSM_ACCESS_CREDENTIALS
*creds
;
169 SecKeyRef privKeyRef
= (SecKeyRef
)kc
->item
;
170 CSSM_CSP_HANDLE cspHandle
;
171 const CSSM_KEY
*cssmKey
;
172 CSSM_CC_HANDLE handle
= 0;
173 CSSM_DATA out
, in
, rem
;
175 CSSM_SIZE outlen
= 0;
178 if (padding
!= RSA_PKCS1_PADDING
)
181 cret
= SecKeyGetCSSMKey(privKeyRef
, &cssmKey
);
184 cret
= SecKeyGetCSPHandle(privKeyRef
, &cspHandle
);
187 ret
= SecKeyGetCredentials(privKeyRef
, CSSM_ACL_AUTHORIZATION_DECRYPT
,
188 kSecCredentialTypeDefault
, &creds
);
192 ret
= CSSM_CSP_CreateAsymmetricContext (cspHandle
,
200 in
.Data
= (uint8
*)from
;
203 out
.Data
= (uint8
*)to
;
204 out
.Length
= kc
->keysize
;
206 rem
.Data
= (uint8
*)remdata
;
207 rem
.Length
= sizeof(remdata
);
209 cret
= CSSM_DecryptData(handle
, &in
, 1, &out
, 1, &outlen
, &rem
);
211 /* cssmErrorString(cret); */
217 CSSM_DeleteContext(handle
);
223 kc_rsa_init(RSA
*rsa
)
229 kc_rsa_finish(RSA
*rsa
)
231 struct kc_rsa
*kc_rsa
= RSA_get_app_data(rsa
);
232 CFRelease(kc_rsa
->item
);
233 memset(kc_rsa
, 0, sizeof(*kc_rsa
));
238 static const RSA_METHOD kc_rsa_pkcs1_method
= {
239 "hx509 Keychain PKCS#1 RSA",
240 kc_rsa_public_encrypt
,
241 kc_rsa_public_decrypt
,
242 kc_rsa_private_encrypt
,
243 kc_rsa_private_decrypt
,
255 set_private_key(hx509_context context
,
256 SecKeychainItemRef itemRef
,
260 hx509_private_key key
;
264 ret
= hx509_private_key_init(&key
, NULL
, NULL
);
268 kc
= calloc(1, sizeof(*kc
));
270 _hx509_abort("out of memory");
276 _hx509_abort("out of memory");
278 /* Argh, fake modulus since OpenSSL API is on crack */
280 SecKeychainAttributeList
*attrs
= NULL
;
285 if (rsa
->n
== NULL
) abort();
287 ret
= getAttribute(itemRef
, kSecKeyKeySizeInBits
, &attrs
);
290 size
= *(uint32_t *)attrs
->attr
[0].data
;
291 SecKeychainItemFreeAttributesAndData(attrs
, NULL
);
293 kc
->keysize
= (size
+ 7) / 8;
295 data
= malloc(kc
->keysize
);
296 memset(data
, 0xe0, kc
->keysize
);
297 BN_bin2bn(data
, kc
->keysize
, rsa
->n
);
302 RSA_set_method(rsa
, &kc_rsa_pkcs1_method
);
303 ret
= RSA_set_app_data(rsa
, kc
);
305 _hx509_abort("RSA_set_app_data");
307 hx509_private_key_assign_rsa(key
, rsa
);
308 _hx509_cert_assign_key(cert
, key
);
319 SecKeychainRef keychain
;
323 keychain_init(hx509_context context
,
324 hx509_certs certs
, void **data
, int flags
,
325 const char *residue
, hx509_lock lock
)
327 struct ks_keychain
*ctx
;
329 ctx
= calloc(1, sizeof(*ctx
));
331 hx509_clear_error_string(context
);
336 if (strcasecmp(residue
, "system-anchors") == 0) {
338 } else if (strncasecmp(residue
, "FILE:", 5) == 0) {
341 ret
= SecKeychainOpen(residue
+ 5, &ctx
->keychain
);
343 hx509_set_error_string(context
, 0, ENOENT
,
344 "Failed to open %s", residue
);
348 hx509_set_error_string(context
, 0, ENOENT
,
349 "Unknown subtype %s", residue
);
363 keychain_free(hx509_certs certs
, void *data
)
365 struct ks_keychain
*ctx
= data
;
367 CFRelease(ctx
->keychain
);
368 memset(ctx
, 0, sizeof(*ctx
));
380 SecKeychainSearchRef searchRef
;
384 keychain_iter_start(hx509_context context
,
385 hx509_certs certs
, void *data
, void **cursor
)
387 struct ks_keychain
*ctx
= data
;
390 iter
= calloc(1, sizeof(*iter
));
392 hx509_set_error_string(context
, 0, ENOMEM
, "out of memory");
401 ret
= hx509_certs_init(context
, "MEMORY:ks-file-create",
402 0, NULL
, &iter
->certs
);
408 ret
= SecTrustCopyAnchorCertificates(&anchors
);
410 hx509_certs_free(&iter
->certs
);
412 hx509_set_error_string(context
, 0, ENOMEM
,
413 "Can't get trust anchors from Keychain");
416 for (i
= 0; i
< CFArrayGetCount(anchors
); i
++) {
417 SecCertificateRef cr
;
421 cr
= (SecCertificateRef
)CFArrayGetValueAtIndex(anchors
, i
);
423 SecCertificateGetData(cr
, &cssm
);
425 ret
= hx509_cert_init_data(context
, cssm
.Data
, cssm
.Length
, &cert
);
429 ret
= hx509_certs_add(context
, iter
->certs
, cert
);
430 hx509_cert_free(cert
);
437 ret
= hx509_certs_start_seq(context
, iter
->certs
, &iter
->cursor
);
439 hx509_certs_free(&iter
->certs
);
446 ret
= SecKeychainSearchCreateFromAttributes(ctx
->keychain
,
447 kSecCertificateItemClass
,
452 hx509_set_error_string(context
, 0, ret
,
453 "Failed to start search for attributes");
467 keychain_iter(hx509_context context
,
468 hx509_certs certs
, void *data
, void *cursor
, hx509_cert
*cert
)
470 SecKeychainAttributeList
*attrs
= NULL
;
471 SecKeychainAttributeInfo attrInfo
;
472 UInt32 attrFormat
[1] = { 0 };
473 SecKeychainItemRef itemRef
;
475 struct iter
*iter
= cursor
;
481 return hx509_certs_next_cert(context
, iter
->certs
, iter
->cursor
, cert
);
485 ret
= SecKeychainSearchCopyNext(iter
->searchRef
, &itemRef
);
486 if (ret
== errSecItemNotFound
)
492 * Pick out certificate and matching "keyid"
495 item
[0] = kSecPublicKeyHashItemAttr
;
499 attrInfo
.format
= attrFormat
;
501 ret
= SecKeychainItemCopyAttributesAndData(itemRef
, &attrInfo
, NULL
,
506 ret
= hx509_cert_init_data(context
, ptr
, len
, cert
);
511 * Find related private key if there is one by looking at
512 * kSecPublicKeyHashItemAttr == kSecKeyLabel
515 SecKeychainSearchRef search
;
516 SecKeychainAttribute attrKeyid
;
517 SecKeychainAttributeList attrList
;
519 attrKeyid
.tag
= kSecKeyLabel
;
520 attrKeyid
.length
= attrs
->attr
[0].length
;
521 attrKeyid
.data
= attrs
->attr
[0].data
;
524 attrList
.attr
= &attrKeyid
;
526 ret
= SecKeychainSearchCreateFromAttributes(NULL
,
527 CSSM_DL_DB_RECORD_PRIVATE_KEY
,
535 ret
= SecKeychainSearchCopyNext(search
, &itemRef
);
537 if (ret
== errSecItemNotFound
) {
544 set_private_key(context
, itemRef
, *cert
);
548 SecKeychainItemFreeAttributesAndData(attrs
, ptr
);
558 keychain_iter_end(hx509_context context
,
563 struct iter
*iter
= cursor
;
566 hx509_certs_end_seq(context
, iter
->certs
, iter
->cursor
);
567 hx509_certs_free(&iter
->certs
);
569 CFRelease(iter
->searchRef
);
572 memset(iter
, 0, sizeof(*iter
));
581 struct hx509_keyset_ops keyset_keychain
= {
594 #endif /* HAVE_FRAMEWORK_SECURITY */
601 _hx509_ks_keychain_register(hx509_context context
)
603 #ifdef HAVE_FRAMEWORK_SECURITY
604 _hx509_ks_register(context
, &keyset_keychain
);