2 * Copyright (c) 2002-2015 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <Security/SecIdentity.h>
25 #include <Security/SecIdentityPriv.h>
26 #include <Security/SecKeychainItemPriv.h>
27 #include <Security/SecItem.h>
28 #include <Security/SecIdentityPriv.h>
29 #include <Security/SecCertificatePriv.h>
31 #include "SecBridge.h"
32 #include <security_keychain/Certificate.h>
33 #include <security_keychain/Identity.h>
34 #include <security_keychain/KeyItem.h>
35 #include <security_keychain/KCCursor.h>
36 #include <security_cdsa_utilities/Schema.h>
37 #include <security_utilities/simpleprefs.h>
38 #include <utilities/SecCFRelease.h>
39 #include <sys/param.h>
41 #include <os/activity.h>
43 /* private function declarations */
45 SecIdentityFindPreferenceItemWithNameAndKeyUsage(
46 CFTypeRef keychainOrArray
,
49 SecKeychainItemRef
*itemRef
);
51 OSStatus
SecIdentityDeletePreferenceItemWithNameAndKeyUsage(
52 CFTypeRef keychainOrArray
,
57 CSSM_KEYUSE
ConvertArrayToKeyUsage(CFArrayRef usage
)
60 CSSM_KEYUSE result
= (CSSM_KEYUSE
) 0;
62 if ((NULL
== usage
) || (0 == (count
= CFArrayGetCount(usage
))))
67 for (CFIndex iCnt
= 0; iCnt
< count
; iCnt
++)
69 CFStringRef keyUsageStr
= NULL
;
70 keyUsageStr
= (CFStringRef
)CFArrayGetValueAtIndex(usage
,iCnt
);
71 if (NULL
!= keyUsageStr
)
73 if (kCFCompareEqualTo
== CFStringCompare((CFStringRef
)kSecAttrCanEncrypt
, keyUsageStr
, 0))
75 result
|= CSSM_KEYUSE_ENCRYPT
;
77 else if (kCFCompareEqualTo
== CFStringCompare((CFStringRef
)kSecAttrCanDecrypt
, keyUsageStr
, 0))
79 result
|= CSSM_KEYUSE_DECRYPT
;
81 else if (kCFCompareEqualTo
== CFStringCompare((CFStringRef
)kSecAttrCanDerive
, keyUsageStr
, 0))
83 result
|= CSSM_KEYUSE_DERIVE
;
85 else if (kCFCompareEqualTo
== CFStringCompare((CFStringRef
)kSecAttrCanSign
, keyUsageStr
, 0))
87 result
|= CSSM_KEYUSE_SIGN
;
89 else if (kCFCompareEqualTo
== CFStringCompare((CFStringRef
)kSecAttrCanVerify
, keyUsageStr
, 0))
91 result
|= CSSM_KEYUSE_VERIFY
;
93 else if (kCFCompareEqualTo
== CFStringCompare((CFStringRef
)kSecAttrCanWrap
, keyUsageStr
, 0))
95 result
|= CSSM_KEYUSE_WRAP
;
97 else if (kCFCompareEqualTo
== CFStringCompare((CFStringRef
)kSecAttrCanUnwrap
, keyUsageStr
, 0))
99 result
|= CSSM_KEYUSE_UNWRAP
;
109 SecIdentityGetTypeID(void)
112 os_activity_t activity
= os_activity_create("SecIdentityGetTypeID", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
113 os_activity_scope(activity
);
114 os_release(activity
);
116 return gTypes().Identity
.typeID
;
118 END_SECAPI1(_kCFRuntimeNotATypeID
)
123 SecIdentityCopyCertificate(
124 SecIdentityRef identityRef
,
125 SecCertificateRef
*certificateRef
)
128 os_activity_t activity
= os_activity_create("SecIdentityCopyCertificate", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
129 os_activity_scope(activity
);
130 os_release(activity
);
132 if (!identityRef
|| !certificateRef
) {
135 CFTypeID itemType
= CFGetTypeID(identityRef
);
136 if (itemType
== SecIdentityGetTypeID()) {
137 SecPointer
<Certificate
> certificatePtr(Identity::required(identityRef
)->certificate());
138 Required(certificateRef
) = certificatePtr
->handle();
140 /* convert outgoing certificate item to a unified SecCertificateRef */
141 CssmData certData
= certificatePtr
->data();
142 CFDataRef data
= NULL
;
143 if (certData
.Data
&& certData
.Length
) {
144 data
= CFDataCreate(NULL
, certData
.Data
, certData
.Length
);
147 *certificateRef
= NULL
;
148 syslog(LOG_ERR
, "ERROR: SecIdentityCopyCertificate failed to retrieve certificate data (length=%ld, data=0x%lX)",
149 (long)certData
.Length
, (uintptr_t)certData
.Data
);
150 return errSecInternal
;
152 SecCertificateRef tmpRef
= *certificateRef
;
153 *certificateRef
= SecCertificateCreateWithKeychainItem(NULL
, data
, tmpRef
);
161 else if (itemType
== SecCertificateGetTypeID()) {
163 // reconstituting a persistent identity reference could return the certificate
164 SecCertificateRef certificate
= (SecCertificateRef
)identityRef
;
166 /* convert outgoing certificate item to a unified SecCertificateRef, if needed */
167 if (SecCertificateIsItemImplInstance(certificate
)) {
168 *certificateRef
= SecCertificateCreateFromItemImplInstance(certificate
);
171 *certificateRef
= (SecCertificateRef
) CFRetain(certificate
);
173 return errSecSuccess
;
184 SecIdentityCopyPrivateKey(
185 SecIdentityRef identityRef
,
186 SecKeyRef
*privateKeyRef
)
189 os_activity_t activity
= os_activity_create("SecIdentityCopyPrivateKey", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
190 os_activity_scope(activity
);
191 os_release(activity
);
193 Required(privateKeyRef
) = (SecKeyRef
)CFRetain(Identity::required(identityRef
)->privateKeyRef());
199 SecIdentityCreateWithCertificate(
200 CFTypeRef keychainOrArray
,
201 SecCertificateRef certificate
,
202 SecIdentityRef
*identityRef
)
204 // This macro converts a new-style SecCertificateRef to an old-style ItemImpl
207 SecPointer
<Certificate
> certificatePtr(Certificate::required(__itemImplRef
));
208 StorageManager::KeychainList keychains
;
209 globals().storageManager
.optionalSearchList(keychainOrArray
, keychains
);
210 SecPointer
<Identity
> identityPtr(new Identity(keychains
, certificatePtr
));
211 Required(identityRef
) = identityPtr
->handle();
218 CFAllocatorRef allocator
,
219 SecCertificateRef certificate
,
220 SecKeyRef privateKey
)
222 SecIdentityRef identityRef
= NULL
;
223 OSStatus __secapiresult
;
224 SecCertificateRef __itemImplRef
= NULL
;
225 if (SecCertificateIsItemImplInstance(certificate
)) {
226 __itemImplRef
=(SecCertificateRef
)CFRetain(certificate
);
228 if (!__itemImplRef
&& certificate
) {
229 __itemImplRef
=(SecCertificateRef
)SecCertificateCopyKeychainItem(certificate
);
231 if (!__itemImplRef
&& certificate
) {
232 __itemImplRef
=SecCertificateCreateItemImplInstance(certificate
);
233 (void)SecCertificateSetKeychainItem(certificate
,__itemImplRef
);
236 SecPointer
<Certificate
> certificatePtr(Certificate::required(__itemImplRef
));
237 SecPointer
<Identity
> identityPtr(new Identity(privateKey
, certificatePtr
));
238 identityRef
= identityPtr
->handle();
240 __secapiresult
=errSecSuccess
;
242 catch (const MacOSError
&err
) { __secapiresult
=err
.osStatus(); }
243 catch (const CommonError
&err
) { __secapiresult
=SecKeychainErrFromOSStatus(err
.osStatus()); }
244 catch (const std::bad_alloc
&) { __secapiresult
=errSecAllocate
; }
245 catch (...) { __secapiresult
=errSecInternalComponent
; }
246 if (__itemImplRef
) { CFRelease(__itemImplRef
); }
252 SecIdentityRef identity1
,
253 SecIdentityRef identity2
,
254 CFOptionFlags compareOptions
)
256 if (!identity1
|| !identity2
)
258 if (identity1
== identity2
)
259 return kCFCompareEqualTo
;
260 else if (identity1
< identity2
)
261 return kCFCompareLessThan
;
263 return kCFCompareGreaterThan
;
267 SecPointer
<Identity
> id1(Identity::required(identity1
));
268 SecPointer
<Identity
> id2(Identity::required(identity2
));
271 return kCFCompareEqualTo
;
273 return kCFCompareLessThan
;
275 return kCFCompareGreaterThan
;
279 return kCFCompareGreaterThan
;
283 CFArrayRef
_SecIdentityCopyPossiblePaths(
286 // utility function to build and return an array of possible paths for the given name.
287 // if name is not a URL, this returns a single-element array.
288 // if name is a URL, the array may contain 1..N elements, one for each level of the path hierarchy.
290 CFMutableArrayRef names
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
294 CFIndex oldLength
= CFStringGetLength(name
);
295 CFArrayAppendValue(names
, name
);
297 CFURLRef url
= CFURLCreateWithString(NULL
, name
, NULL
);
299 if (CFURLCanBeDecomposed(url
)) {
300 // first, remove the query portion of this URL, if any
301 CFStringRef qs
= CFURLCopyQueryString(url
, NULL
);
303 CFMutableStringRef newName
= CFStringCreateMutableCopy(NULL
, oldLength
, name
);
305 CFIndex qsLength
= CFStringGetLength(qs
) + 1; // include the '?'
306 CFStringDelete(newName
, CFRangeMake(oldLength
-qsLength
, qsLength
));
308 url
= CFURLCreateWithString(NULL
, newName
, NULL
);
309 CFArraySetValueAtIndex(names
, 0, newName
);
314 // now add an entry for each level of the path
316 CFURLRef parent
= CFURLCreateCopyDeletingLastPathComponent(NULL
, url
);
318 CFStringRef parentURLString
= CFURLGetString(parent
);
319 if (parentURLString
) {
320 CFIndex newLength
= CFStringGetLength(parentURLString
);
321 // check that string length has decreased as expected; for file URLs,
322 // CFURLCreateCopyDeletingLastPathComponent can insert './' or '../'
323 if ((newLength
>= oldLength
) || (!CFStringHasPrefix(name
, parentURLString
))) {
328 oldLength
= newLength
;
329 CFArrayAppendValue(names
, parentURLString
);
340 // finally, add wildcard entries for each subdomain
341 url
= CFURLCreateWithString(NULL
, name
, NULL
);
343 if (CFURLCanBeDecomposed(url
)) {
344 CFStringRef netLocString
= CFURLCopyNetLocation(url
);
346 // first strip off port number, if present
347 CFStringRef tmpLocString
= netLocString
;
348 CFArrayRef hostnameArray
= CFStringCreateArrayBySeparatingStrings(NULL
, netLocString
, CFSTR(":"));
349 tmpLocString
= (CFStringRef
)CFRetain((CFStringRef
)CFArrayGetValueAtIndex(hostnameArray
, 0));
350 CFRelease(netLocString
);
351 CFRelease(hostnameArray
);
352 netLocString
= tmpLocString
;
353 // split remaining string into domain components
354 hostnameArray
= CFStringCreateArrayBySeparatingStrings(NULL
, netLocString
, CFSTR("."));
355 CFIndex subdomainCount
= CFArrayGetCount(hostnameArray
);
357 while (++i
< subdomainCount
) {
359 CFMutableStringRef wildcardString
= CFStringCreateMutable(NULL
, 0);
360 if (wildcardString
) {
361 CFStringAppendCString(wildcardString
, "*", kCFStringEncodingUTF8
);
362 while (j
< subdomainCount
) {
363 CFStringRef domainString
= (CFStringRef
)CFArrayGetValueAtIndex(hostnameArray
, j
++);
364 if (CFStringGetLength(domainString
) > 0) {
365 CFStringAppendCString(wildcardString
, ".", kCFStringEncodingUTF8
);
366 CFStringAppend(wildcardString
, domainString
);
369 if (CFStringGetLength(wildcardString
) > 1) {
370 CFArrayAppendValue(names
, wildcardString
);
372 CFRelease(wildcardString
);
375 CFRelease(hostnameArray
);
376 CFRelease(netLocString
);
386 OSStatus
_SecIdentityCopyPreferenceMatchingName(
388 CSSM_KEYUSE keyUsage
,
389 CFArrayRef validIssuers
,
390 SecIdentityRef
*identity
)
392 // this is NOT exported, and called only from SecIdentityCopyPreference (below), so no BEGIN/END macros here;
393 // caller must handle exceptions
395 StorageManager::KeychainList keychains
;
396 globals().storageManager
.getSearchList(keychains
);
397 KCCursor
cursor(keychains
, kSecGenericPasswordItemClass
, NULL
);
399 char idUTF8
[MAXPATHLEN
];
401 if (!CFStringGetCString(name
, idUTF8
, sizeof(idUTF8
)-1, kCFStringEncodingUTF8
))
402 idUTF8
[0] = (char)'\0';
403 CssmData
service(const_cast<char *>(idUTF8
), strlen(idUTF8
));
404 FourCharCode itemType
= 'iprf';
405 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecServiceItemAttr
), service
);
406 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecTypeItemAttr
), itemType
);
408 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecScriptCodeItemAttr
), (sint32
)keyUsage
);
411 if (!cursor
->next(prefItem
))
412 return errSecItemNotFound
;
414 // get persistent certificate reference
415 SecKeychainAttribute itemAttrs
[] = { { kSecGenericItemAttr
, 0, NULL
} };
416 SecKeychainAttributeList itemAttrList
= { sizeof(itemAttrs
) / sizeof(itemAttrs
[0]), itemAttrs
};
417 prefItem
->getContent(NULL
, &itemAttrList
, NULL
, NULL
);
419 // find certificate, given persistent reference data
420 CFDataRef pItemRef
= CFDataCreateWithBytesNoCopy(NULL
, (const UInt8
*)itemAttrs
[0].data
, itemAttrs
[0].length
, kCFAllocatorNull
);
421 SecKeychainItemRef certItemRef
= nil
;
422 OSStatus status
= SecKeychainItemCopyFromPersistentReference(pItemRef
, &certItemRef
); //%%% need to make this a method of ItemImpl
423 prefItem
->freeContent(&itemAttrList
, NULL
);
429 // filter on valid issuers, if provided
434 // create identity reference, given certificate
435 status
= SecIdentityCreateWithCertificate(NULL
, (SecCertificateRef
)certItemRef
, identity
);
437 CFRelease(certItemRef
);
443 SecIdentityRef
SecIdentityCopyPreferred(CFStringRef name
, CFArrayRef keyUsage
, CFArrayRef validIssuers
)
445 // This function will look for a matching preference in the following order:
446 // - matches the name and the supplied key use
447 // - matches the name and the special 'ANY' key use
448 // - matches the name with no key usage constraint
450 SecIdentityRef identityRef
= NULL
;
451 CSSM_KEYUSE keyUse
= ConvertArrayToKeyUsage(keyUsage
);
452 OSStatus status
= SecIdentityCopyPreference(name
, keyUse
, validIssuers
, &identityRef
);
453 if (status
!= errSecSuccess
&& keyUse
!= CSSM_KEYUSE_ANY
)
454 status
= SecIdentityCopyPreference(name
, CSSM_KEYUSE_ANY
, validIssuers
, &identityRef
);
455 if (status
!= errSecSuccess
&& keyUse
!= 0)
456 status
= SecIdentityCopyPreference(name
, 0, validIssuers
, &identityRef
);
461 OSStatus
SecIdentityCopyPreference(
463 CSSM_KEYUSE keyUsage
,
464 CFArrayRef validIssuers
,
465 SecIdentityRef
*identity
)
467 // The original implementation of SecIdentityCopyPreference matches the exact string only.
468 // That implementation has been moved to _SecIdentityCopyPreferenceMatchingName (above),
469 // and this function is a wrapper which calls it, so that existing clients will get the
470 // extended behavior of server domain matching for items that specify URLs.
471 // (Note that behavior is unchanged if the specified name is not a URL.)
474 os_activity_t activity
= os_activity_create("SecIdentityCopyPreference", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
475 os_activity_scope(activity
);
476 os_release(activity
);
478 CFTypeRef val
= (CFTypeRef
)CFPreferencesCopyValue(CFSTR("LogIdentityPreferenceLookup"),
479 CFSTR("com.apple.security"),
480 kCFPreferencesCurrentUser
,
481 kCFPreferencesAnyHost
);
482 Boolean logging
= false;
484 if (CFGetTypeID(val
) == CFBooleanGetTypeID()) {
485 logging
= CFBooleanGetValue((CFBooleanRef
)val
);
490 OSStatus status
= errSecItemNotFound
;
491 CFArrayRef names
= _SecIdentityCopyPossiblePaths(name
);
496 CFIndex idx
, total
= CFArrayGetCount(names
);
497 for (idx
= 0; idx
< total
; idx
++) {
498 CFStringRef aName
= (CFStringRef
)CFArrayGetValueAtIndex(names
, idx
);
500 status
= _SecIdentityCopyPreferenceMatchingName(aName
, keyUsage
, validIssuers
, identity
);
502 catch (...) { status
= errSecItemNotFound
; }
505 // get identity label
506 CFStringRef labelString
= NULL
;
507 if (!status
&& identity
&& *identity
) {
509 SecPointer
<Certificate
> cert(Identity::required(*identity
)->certificate());
510 cert
->inferLabel(false, &labelString
);
512 catch (...) { labelString
= NULL
; };
514 char *labelBuf
= NULL
;
515 CFIndex labelBufSize
= (labelString
) ? CFStringGetLength(labelString
) * 4 : 4;
516 labelBuf
= (char *)malloc(labelBufSize
);
517 if (!labelString
|| !CFStringGetCString(labelString
, labelBuf
, labelBufSize
, kCFStringEncodingUTF8
)) {
521 CFRelease(labelString
);
525 char *serviceBuf
= NULL
;
526 CFIndex serviceBufSize
= CFStringGetLength(aName
) * 4;
527 serviceBuf
= (char *)malloc(serviceBufSize
);
528 if (!CFStringGetCString(aName
, serviceBuf
, serviceBufSize
, kCFStringEncodingUTF8
)) {
532 syslog(LOG_NOTICE
, "preferred identity: \"%s\" found for \"%s\"\n", labelBuf
, serviceBuf
);
533 if (!status
&& name
) {
534 char *nameBuf
= NULL
;
535 CFIndex nameBufSize
= CFStringGetLength(name
) * 4;
536 nameBuf
= (char *)malloc(nameBufSize
);
537 if (!CFStringGetCString(name
, nameBuf
, nameBufSize
, kCFStringEncodingUTF8
)) {
540 syslog(LOG_NOTICE
, "lookup complete; will use: \"%s\" for \"%s\"\n", labelBuf
, nameBuf
);
548 if (status
== errSecSuccess
) {
549 break; // match found
559 OSStatus
SecIdentitySetPreference(
560 SecIdentityRef identity
,
562 CSSM_KEYUSE keyUsage
)
568 // treat NULL identity as a request to clear the preference
569 // (note: if keyUsage is 0, this clears all key usage prefs for name)
570 return SecIdentityDeletePreferenceItemWithNameAndKeyUsage(NULL
, name
, keyUsage
);
574 os_activity_t activity
= os_activity_create("SecIdentitySetPreference", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
575 os_activity_scope(activity
);
576 os_release(activity
);
578 CFRef
<SecCertificateRef
> certRef
;
579 OSStatus status
= SecIdentityCopyCertificate(identity
, certRef
.take());
580 if(status
!= errSecSuccess
) {
581 MacOSError::throwMe(status
);
584 // determine the account attribute
586 // This attribute must be synthesized from certificate label + pref item type + key usage,
587 // as only the account and service attributes can make a generic keychain item unique.
588 // For 'iprf' type items (but not 'cprf'), we append a trailing space. This insures that
589 // we can save a certificate preference if an identity preference already exists for the
590 // given service name, and vice-versa.
591 // If the key usage is 0 (i.e. the normal case), we omit the appended key usage string.
593 CFStringRef labelStr
= nil
;
594 SecCertificateInferLabel(certRef
.get(), &labelStr
);
596 MacOSError::throwMe(errSecDataTooLarge
); // data is "in a format which cannot be displayed"
598 CFIndex accountUTF8Len
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(labelStr
), kCFStringEncodingUTF8
) + 1;
599 const char *templateStr
= "%s [key usage 0x%X]";
600 const int keyUsageMaxStrLen
= 8;
601 accountUTF8Len
+= strlen(templateStr
) + keyUsageMaxStrLen
;
602 char accountUTF8
[accountUTF8Len
];
603 if (!CFStringGetCString(labelStr
, accountUTF8
, accountUTF8Len
-1, kCFStringEncodingUTF8
))
604 accountUTF8
[0] = (char)'\0';
606 snprintf(accountUTF8
, accountUTF8Len
-1, templateStr
, accountUTF8
, keyUsage
);
607 snprintf(accountUTF8
, accountUTF8Len
-1, "%s ", accountUTF8
);
608 CssmData
account(const_cast<char *>(accountUTF8
), strlen(accountUTF8
));
611 // service attribute (name provided by the caller)
612 CFIndex serviceUTF8Len
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(name
), kCFStringEncodingUTF8
) + 1;;
613 char serviceUTF8
[serviceUTF8Len
];
614 if (!CFStringGetCString(name
, serviceUTF8
, serviceUTF8Len
-1, kCFStringEncodingUTF8
))
615 serviceUTF8
[0] = (char)'\0';
616 CssmData
service(const_cast<char *>(serviceUTF8
), strlen(serviceUTF8
));
618 // look for existing identity preference item, in case this is an update
619 StorageManager::KeychainList keychains
;
620 globals().storageManager
.getSearchList(keychains
);
621 KCCursor
cursor(keychains
, kSecGenericPasswordItemClass
, NULL
);
622 FourCharCode itemType
= 'iprf';
623 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecServiceItemAttr
), service
);
624 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecTypeItemAttr
), itemType
);
626 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecScriptCodeItemAttr
), (sint32
)keyUsage
);
629 Item
item(kSecGenericPasswordItemClass
, 'aapl', 0, NULL
, false);
630 bool add
= (!cursor
->next(item
));
631 // at this point, we either have a new item to add or an existing item to update
633 // set item attribute values
634 item
->setAttribute(Schema::attributeInfo(kSecServiceItemAttr
), service
);
635 item
->setAttribute(Schema::attributeInfo(kSecTypeItemAttr
), itemType
);
636 item
->setAttribute(Schema::attributeInfo(kSecAccountItemAttr
), account
);
637 item
->setAttribute(Schema::attributeInfo(kSecScriptCodeItemAttr
), (sint32
)keyUsage
);
638 item
->setAttribute(Schema::attributeInfo(kSecLabelItemAttr
), service
);
640 // generic attribute (store persistent certificate reference)
641 CFDataRef pItemRef
= nil
;
642 SecKeychainItemCreatePersistentReference((SecKeychainItemRef
)certRef
.get(), &pItemRef
);
644 MacOSError::throwMe(errSecInvalidItemRef
);
646 const UInt8
*dataPtr
= CFDataGetBytePtr(pItemRef
);
647 CFIndex dataLen
= CFDataGetLength(pItemRef
);
648 CssmData
pref(const_cast<void *>(reinterpret_cast<const void *>(dataPtr
)), dataLen
);
649 item
->setAttribute(Schema::attributeInfo(kSecGenericItemAttr
), pref
);
653 Keychain keychain
= nil
;
655 keychain
= globals().storageManager
.defaultKeychain();
656 if (!keychain
->exists())
657 MacOSError::throwMe(errSecNoSuchKeychain
); // Might be deleted or not available at this time.
660 keychain
= globals().storageManager
.defaultKeychainUI(item
);
666 catch (const MacOSError
&err
) {
667 if (err
.osStatus() != errSecDuplicateItem
)
668 throw; // if item already exists, fall through to update
677 SecIdentitySetPreferred(SecIdentityRef identity
, CFStringRef name
, CFArrayRef keyUsage
)
679 CSSM_KEYUSE keyUse
= ConvertArrayToKeyUsage(keyUsage
);
680 return SecIdentitySetPreference(identity
, name
, keyUse
);
684 SecIdentityFindPreferenceItem(
685 CFTypeRef keychainOrArray
,
686 CFStringRef idString
,
687 SecKeychainItemRef
*itemRef
)
690 os_activity_t activity
= os_activity_create("SecIdentityFindPreferenceItem", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
691 os_activity_scope(activity
);
692 os_release(activity
);
694 StorageManager::KeychainList keychains
;
695 globals().storageManager
.optionalSearchList(keychainOrArray
, keychains
);
696 KCCursor
cursor(keychains
, kSecGenericPasswordItemClass
, NULL
);
698 char idUTF8
[MAXPATHLEN
];
699 idUTF8
[0] = (char)'\0';
702 if (!CFStringGetCString(idString
, idUTF8
, sizeof(idUTF8
)-1, kCFStringEncodingUTF8
))
703 idUTF8
[0] = (char)'\0';
705 size_t idUTF8Len
= strlen(idUTF8
);
707 MacOSError::throwMe(errSecParam
);
709 CssmData
service(const_cast<char *>(idUTF8
), idUTF8Len
);
710 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecServiceItemAttr
), service
);
711 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecTypeItemAttr
), (FourCharCode
)'iprf');
714 if (!cursor
->next(item
))
715 MacOSError::throwMe(errSecItemNotFound
);
718 *itemRef
=item
->handle();
724 SecIdentityFindPreferenceItemWithNameAndKeyUsage(
725 CFTypeRef keychainOrArray
,
728 SecKeychainItemRef
*itemRef
)
731 os_activity_t activity
= os_activity_create("SecIdentityFindPreferenceItemWithNameAndKeyUsage", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
732 os_activity_scope(activity
);
733 os_release(activity
);
735 StorageManager::KeychainList keychains
;
736 globals().storageManager
.optionalSearchList(keychainOrArray
, keychains
);
737 KCCursor
cursor(keychains
, kSecGenericPasswordItemClass
, NULL
);
739 char idUTF8
[MAXPATHLEN
];
740 idUTF8
[0] = (char)'\0';
743 if (!CFStringGetCString(name
, idUTF8
, sizeof(idUTF8
)-1, kCFStringEncodingUTF8
))
744 idUTF8
[0] = (char)'\0';
746 size_t idUTF8Len
= strlen(idUTF8
);
748 MacOSError::throwMe(errSecParam
);
750 CssmData
service(const_cast<char *>(idUTF8
), idUTF8Len
);
751 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecServiceItemAttr
), service
);
752 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecTypeItemAttr
), (FourCharCode
)'iprf');
754 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecScriptCodeItemAttr
), (sint32
)keyUsage
);
757 if (!cursor
->next(item
))
758 MacOSError::throwMe(errSecItemNotFound
);
761 *itemRef
=item
->handle();
766 OSStatus
SecIdentityDeletePreferenceItemWithNameAndKeyUsage(
767 CFTypeRef keychainOrArray
,
771 // when a specific key usage is passed, we'll only match & delete that pref;
772 // when a key usage of 0 is passed, all matching prefs should be deleted.
773 // maxUsages represents the most matches there could theoretically be, so
774 // cut things off at that point if we're still finding items (if they can't
775 // be deleted for some reason, we'd never break out of the loop.)
777 OSStatus status
= errSecInternalError
;
778 SecKeychainItemRef item
= NULL
;
779 int count
= 0, maxUsages
= 12;
780 while (++count
<= maxUsages
&&
781 (status
= SecIdentityFindPreferenceItemWithNameAndKeyUsage(keychainOrArray
, name
, keyUsage
, &item
)) == errSecSuccess
) {
782 status
= SecKeychainItemDelete(item
);
787 // it's not an error if the item isn't found
788 return (status
== errSecItemNotFound
) ? errSecSuccess
: status
;
793 OSStatus
_SecIdentityAddPreferenceItemWithName(
794 SecKeychainRef keychainRef
,
795 SecIdentityRef identityRef
,
796 CFStringRef idString
,
797 SecKeychainItemRef
*itemRef
)
799 // this is NOT exported, and called only from SecIdentityAddPreferenceItem (below), so no BEGIN/END macros here;
800 // caller must handle exceptions
802 if (!identityRef
|| !idString
)
804 SecPointer
<Certificate
> cert(Identity::required(identityRef
)->certificate());
805 Item
item(kSecGenericPasswordItemClass
, 'aapl', 0, NULL
, false);
808 // determine the account attribute
810 // This attribute must be synthesized from certificate label + pref item type + key usage,
811 // as only the account and service attributes can make a generic keychain item unique.
812 // For 'iprf' type items (but not 'cprf'), we append a trailing space. This insures that
813 // we can save a certificate preference if an identity preference already exists for the
814 // given service name, and vice-versa.
815 // If the key usage is 0 (i.e. the normal case), we omit the appended key usage string.
817 CFStringRef labelStr
= nil
;
818 cert
->inferLabel(false, &labelStr
);
820 return errSecDataTooLarge
; // data is "in a format which cannot be displayed"
822 CFIndex accountUTF8Len
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(labelStr
), kCFStringEncodingUTF8
) + 1;
823 const char *templateStr
= "%s [key usage 0x%X]";
824 const int keyUsageMaxStrLen
= 8;
825 accountUTF8Len
+= strlen(templateStr
) + keyUsageMaxStrLen
;
826 char accountUTF8
[accountUTF8Len
];
827 if (!CFStringGetCString(labelStr
, accountUTF8
, accountUTF8Len
-1, kCFStringEncodingUTF8
))
828 accountUTF8
[0] = (char)'\0';
830 snprintf(accountUTF8
, accountUTF8Len
-1, templateStr
, accountUTF8
, keyUsage
);
831 snprintf(accountUTF8
, accountUTF8Len
-1, "%s ", accountUTF8
);
832 CssmData
account(const_cast<char *>(accountUTF8
), strlen(accountUTF8
));
835 // service attribute (name provided by the caller)
836 CFIndex serviceUTF8Len
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(idString
), kCFStringEncodingUTF8
) + 1;;
837 char serviceUTF8
[serviceUTF8Len
];
838 if (!CFStringGetCString(idString
, serviceUTF8
, serviceUTF8Len
-1, kCFStringEncodingUTF8
))
839 serviceUTF8
[0] = (char)'\0';
840 CssmData
service(const_cast<char *>(serviceUTF8
), strlen(serviceUTF8
));
842 // set item attribute values
843 item
->setAttribute(Schema::attributeInfo(kSecServiceItemAttr
), service
);
844 item
->setAttribute(Schema::attributeInfo(kSecLabelItemAttr
), service
);
845 item
->setAttribute(Schema::attributeInfo(kSecTypeItemAttr
), (FourCharCode
)'iprf');
846 item
->setAttribute(Schema::attributeInfo(kSecAccountItemAttr
), account
);
847 item
->setAttribute(Schema::attributeInfo(kSecScriptCodeItemAttr
), keyUsage
);
849 // generic attribute (store persistent certificate reference)
850 CFDataRef pItemRef
= nil
;
851 OSStatus status
= SecKeychainItemCreatePersistentReference((SecKeychainItemRef
)cert
->handle(), &pItemRef
);
853 status
= errSecInvalidItemRef
;
856 const UInt8
*dataPtr
= CFDataGetBytePtr(pItemRef
);
857 CFIndex dataLen
= CFDataGetLength(pItemRef
);
858 CssmData
pref(const_cast<void *>(reinterpret_cast<const void *>(dataPtr
)), dataLen
);
859 item
->setAttribute(Schema::attributeInfo(kSecGenericItemAttr
), pref
);
862 Keychain keychain
= nil
;
864 keychain
= Keychain::optional(keychainRef
);
865 if (!keychain
->exists())
866 MacOSError::throwMe(errSecNoSuchKeychain
); // Might be deleted or not available at this time.
869 keychain
= globals().storageManager
.defaultKeychainUI(item
);
875 catch (const MacOSError
&err
) {
876 if (err
.osStatus() != errSecDuplicateItem
)
877 throw; // if item already exists, fall through to update
883 *itemRef
= item
->handle();
888 OSStatus
SecIdentityAddPreferenceItem(
889 SecKeychainRef keychainRef
,
890 SecIdentityRef identityRef
,
891 CFStringRef idString
,
892 SecKeychainItemRef
*itemRef
)
894 // The original implementation of SecIdentityAddPreferenceItem adds the exact string only.
895 // That implementation has been moved to _SecIdentityAddPreferenceItemWithName (above),
896 // and this function is a wrapper which calls it, so that existing clients will get the
897 // extended behavior of server domain matching for items that specify URLs.
898 // (Note that behavior is unchanged if the specified idString is not a URL.)
901 os_activity_t activity
= os_activity_create("SecIdentityAddPreferenceItem", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
902 os_activity_scope(activity
);
903 os_release(activity
);
905 OSStatus status
= errSecInternalComponent
;
906 CFArrayRef names
= _SecIdentityCopyPossiblePaths(idString
);
911 CFIndex total
= CFArrayGetCount(names
);
913 // add item for name (first element in array)
914 CFStringRef aName
= (CFStringRef
)CFArrayGetValueAtIndex(names
, 0);
916 status
= _SecIdentityAddPreferenceItemWithName(keychainRef
, identityRef
, aName
, itemRef
);
918 catch (const MacOSError
&err
) { status
=err
.osStatus(); }
919 catch (const CommonError
&err
) { status
=SecKeychainErrFromOSStatus(err
.osStatus()); }
920 catch (const std::bad_alloc
&) { status
=errSecAllocate
; }
921 catch (...) { status
=errSecInternalComponent
; }
924 Boolean setDomainDefaultIdentity
= FALSE
;
925 CFTypeRef val
= (CFTypeRef
)CFPreferencesCopyValue(CFSTR("SetDomainDefaultIdentity"),
926 CFSTR("com.apple.security.identities"),
927 kCFPreferencesCurrentUser
,
928 kCFPreferencesAnyHost
);
930 if (CFGetTypeID(val
) == CFBooleanGetTypeID())
931 setDomainDefaultIdentity
= CFBooleanGetValue((CFBooleanRef
)val
) ? TRUE
: FALSE
;
934 if (setDomainDefaultIdentity
) {
935 // add item for domain (second-to-last element in array, e.g. "*.apple.com")
936 OSStatus tmpStatus
= errSecSuccess
;
937 CFStringRef aName
= (CFStringRef
)CFArrayGetValueAtIndex(names
, total
-2);
939 tmpStatus
= _SecIdentityAddPreferenceItemWithName(keychainRef
, identityRef
, aName
, itemRef
);
941 catch (const MacOSError
&err
) { tmpStatus
=err
.osStatus(); }
942 catch (const CommonError
&err
) { tmpStatus
=SecKeychainErrFromOSStatus(err
.osStatus()); }
943 catch (const std::bad_alloc
&) { tmpStatus
=errSecAllocate
; }
944 catch (...) { tmpStatus
=errSecInternalComponent
; }
954 /* deprecated in 10.5 */
955 OSStatus
SecIdentityUpdatePreferenceItem(
956 SecKeychainItemRef itemRef
,
957 SecIdentityRef identityRef
)
960 os_activity_t activity
= os_activity_create("SecIdentityUpdatePreferenceItem", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
961 os_activity_scope(activity
);
962 os_release(activity
);
964 if (!itemRef
|| !identityRef
)
965 MacOSError::throwMe(errSecParam
);
966 SecPointer
<Certificate
> certificate(Identity::required(identityRef
)->certificate());
967 Item prefItem
= ItemImpl::required(itemRef
);
969 // get the current key usage value for this item
972 SecKeychainAttribute attr
= { kSecScriptCodeItemAttr
, sizeof(sint32
), &keyUsage
};
974 prefItem
->getAttribute(attr
, &actLen
);
980 // set the account attribute
982 // This attribute must be synthesized from certificate label + pref item type + key usage,
983 // as only the account and service attributes can make a generic keychain item unique.
984 // For 'iprf' type items (but not 'cprf'), we append a trailing space. This insures that
985 // we can save a certificate preference if an identity preference already exists for the
986 // given service name, and vice-versa.
987 // If the key usage is 0 (i.e. the normal case), we omit the appended key usage string.
989 CFStringRef labelStr
= nil
;
990 certificate
->inferLabel(false, &labelStr
);
992 MacOSError::throwMe(errSecDataTooLarge
); // data is "in a format which cannot be displayed"
994 CFIndex accountUTF8Len
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(labelStr
), kCFStringEncodingUTF8
) + 1;
995 const char *templateStr
= "%s [key usage 0x%X]";
996 const int keyUsageMaxStrLen
= 8;
997 accountUTF8Len
+= strlen(templateStr
) + keyUsageMaxStrLen
;
998 char accountUTF8
[accountUTF8Len
];
999 if (!CFStringGetCString(labelStr
, accountUTF8
, accountUTF8Len
-1, kCFStringEncodingUTF8
))
1000 accountUTF8
[0] = (char)'\0';
1002 snprintf(accountUTF8
, accountUTF8Len
-1, templateStr
, accountUTF8
, keyUsage
);
1003 snprintf(accountUTF8
, accountUTF8Len
-1, "%s ", accountUTF8
);
1004 CssmData
account(const_cast<char *>(accountUTF8
), strlen(accountUTF8
));
1005 prefItem
->setAttribute(Schema::attributeInfo(kSecAccountItemAttr
), account
);
1006 CFRelease(labelStr
);
1008 // generic attribute (store persistent certificate reference)
1009 CFDataRef pItemRef
= nil
;
1010 OSStatus status
= SecKeychainItemCreatePersistentReference((SecKeychainItemRef
)certificate
->handle(), &pItemRef
);
1012 status
= errSecInvalidItemRef
;
1014 MacOSError::throwMe(status
);
1015 const UInt8
*dataPtr
= CFDataGetBytePtr(pItemRef
);
1016 CFIndex dataLen
= CFDataGetLength(pItemRef
);
1017 CssmData
pref(const_cast<void *>(reinterpret_cast<const void *>(dataPtr
)), dataLen
);
1018 prefItem
->setAttribute(Schema::attributeInfo(kSecGenericItemAttr
), pref
);
1019 CFRelease(pItemRef
);
1026 OSStatus
SecIdentityCopyFromPreferenceItem(
1027 SecKeychainItemRef itemRef
,
1028 SecIdentityRef
*identityRef
)
1031 os_activity_t activity
= os_activity_create("SecIdentityCopyFromPreferenceItem", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
1032 os_activity_scope(activity
);
1033 os_release(activity
);
1035 if (!itemRef
|| !identityRef
)
1036 MacOSError::throwMe(errSecParam
);
1037 Item prefItem
= ItemImpl::required(itemRef
);
1039 // get persistent certificate reference
1040 SecKeychainAttribute itemAttrs
[] = { { kSecGenericItemAttr
, 0, NULL
} };
1041 SecKeychainAttributeList itemAttrList
= { sizeof(itemAttrs
) / sizeof(itemAttrs
[0]), itemAttrs
};
1042 prefItem
->getContent(NULL
, &itemAttrList
, NULL
, NULL
);
1044 // find certificate, given persistent reference data
1045 CFDataRef pItemRef
= CFDataCreateWithBytesNoCopy(NULL
, (const UInt8
*)itemAttrs
[0].data
, itemAttrs
[0].length
, kCFAllocatorNull
);
1046 SecKeychainItemRef certItemRef
= nil
;
1047 OSStatus status
= SecKeychainItemCopyFromPersistentReference(pItemRef
, &certItemRef
); //%%% need to make this a method of ItemImpl
1048 prefItem
->freeContent(&itemAttrList
, NULL
);
1050 CFRelease(pItemRef
);
1054 // create identity reference, given certificate
1055 StorageManager::KeychainList keychains
;
1056 globals().storageManager
.optionalSearchList((CFTypeRef
)NULL
, keychains
);
1057 Item certItem
= ItemImpl::required(SecKeychainItemRef(certItemRef
));
1058 SecPointer
<Certificate
> certificate(static_cast<Certificate
*>(certItem
.get()));
1059 SecPointer
<Identity
> identity(new Identity(keychains
, certificate
));
1061 CFRelease(certItemRef
);
1063 Required(identityRef
) = identity
->handle();
1069 * System Identity Support.
1072 /* plist domain (in /Library/Preferences) */
1073 #define IDENTITY_DOMAIN "com.apple.security.systemidentities"
1076 * Our plist is a dictionary whose entries have the following format:
1077 * key = domain name as CFString
1078 * value = public key hash as CFData
1081 #define SYSTEM_KEYCHAIN_PATH kSystemKeychainDir "/" kSystemKeychainName
1084 * All accesses to system identities and its associated plist are
1085 * protected by this lock.
1087 ModuleNexus
<Mutex
> systemIdentityLock
;
1089 OSStatus
SecIdentityCopySystemIdentity(
1091 SecIdentityRef
*idRef
,
1092 CFStringRef
*actualDomain
) /* optional */
1095 os_activity_t activity
= os_activity_create("SecIdentityCopySystemIdentity", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
1096 os_activity_scope(activity
);
1097 os_release(activity
);
1099 StLock
<Mutex
> _(systemIdentityLock());
1100 auto_ptr
<Dictionary
> identDict
;
1102 /* get top-level dictionary - if not present, we're done */
1103 Dictionary
* d
= Dictionary::CreateDictionary(IDENTITY_DOMAIN
, Dictionary::US_System
);
1106 return errSecNotAvailable
;
1111 /* see if there's an entry for specified domain */
1112 CFDataRef entryValue
= identDict
->getDataValue(domain
);
1113 if(entryValue
== NULL
) {
1114 /* try for default entry if we're not already looking for default */
1115 if(!CFEqual(domain
, kSecIdentityDomainDefault
)) {
1116 entryValue
= identDict
->getDataValue(kSecIdentityDomainDefault
);
1118 if(entryValue
== NULL
) {
1119 /* no default identity */
1120 MacOSError::throwMe(errSecItemNotFound
);
1123 /* remember that we're not fetching the requested domain */
1124 domain
= kSecIdentityDomainDefault
;
1127 /* open system keychain - error here is fatal */
1128 Keychain systemKc
= globals().storageManager
.make(SYSTEM_KEYCHAIN_PATH
, false);
1129 CFRef
<SecKeychainRef
> systemKcRef(systemKc
->handle());
1130 StorageManager::KeychainList keychains
;
1131 globals().storageManager
.optionalSearchList(systemKcRef
, keychains
);
1133 /* search for specified cert */
1134 SecKeychainAttributeList attrList
;
1135 SecKeychainAttribute attr
;
1136 attr
.tag
= kSecPublicKeyHashItemAttr
;
1137 attr
.length
= (UInt32
)CFDataGetLength(entryValue
);
1138 attr
.data
= (void *)CFDataGetBytePtr(entryValue
);
1140 attrList
.attr
= &attr
;
1142 KCCursor
cursor(keychains
, kSecCertificateItemClass
, &attrList
);
1144 if(!cursor
->next(certItem
)) {
1145 MacOSError::throwMe(errSecItemNotFound
);
1148 /* found the cert; try matching with key to cook up identity */
1149 SecPointer
<Certificate
> certificate(static_cast<Certificate
*>(certItem
.get()));
1150 SecPointer
<Identity
> identity(new Identity(keychains
, certificate
));
1152 Required(idRef
) = identity
->handle();
1154 *actualDomain
= domain
;
1155 CFRetain(*actualDomain
);
1161 OSStatus
SecIdentitySetSystemIdentity(
1163 SecIdentityRef idRef
)
1166 os_activity_t activity
= os_activity_create("SecIdentitySetSystemIdentity", OS_ACTIVITY_CURRENT
, OS_ACTIVITY_FLAG_IF_NONE_PRESENT
);
1167 os_activity_scope(activity
);
1168 os_release(activity
);
1170 StLock
<Mutex
> _(systemIdentityLock());
1171 if(geteuid() != 0) {
1172 MacOSError::throwMe(errSecAuthFailed
);
1175 auto_ptr
<MutableDictionary
> identDict
;
1176 MutableDictionary
*d
= MutableDictionary::CreateMutableDictionary(IDENTITY_DOMAIN
, Dictionary::US_System
);
1184 /* nothing there, nothing to set - done */
1185 return errSecSuccess
;
1187 identDict
.reset(new MutableDictionary());
1191 /* Just delete the possible entry for this domain */
1192 identDict
->removeValue(domain
);
1195 /* obtain public key hash of identity's cert */
1196 SecPointer
<Identity
> identity(Identity::required(idRef
));
1197 SecPointer
<Certificate
> cert
= identity
->certificate();
1198 const CssmData
&pubKeyHash
= cert
->publicKeyHash();
1199 CFRef
<CFDataRef
> pubKeyHashData(CFDataCreate(NULL
, pubKeyHash
.Data
,
1200 pubKeyHash
.Length
));
1202 /* add/replace to dictionary */
1203 identDict
->setValue(domain
, pubKeyHashData
);
1207 if(!identDict
->writePlistToPrefs(IDENTITY_DOMAIN
, Dictionary::US_System
)) {
1208 MacOSError::throwMe(errSecIO
);
1214 const CFStringRef kSecIdentityDomainDefault
= CFSTR("com.apple.systemdefault");
1215 const CFStringRef kSecIdentityDomainKerberosKDC
= CFSTR("com.apple.kerberos.kdc");