2 * Copyright (c) 2002-2016 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/SecCertificate.h>
25 #include <Security/SecCertificatePriv.h>
26 #include <security_keychain/Certificate.h>
27 #include <security_keychain/Item.h>
28 #include <security_keychain/KCCursor.h>
29 #include <Security/cssmapi.h>
30 #include <Security/cssmapple.h>
31 #include <security_cdsa_client/cspclient.h>
32 #include <security_cdsa_client/clclient.h>
33 #include <security_cdsa_client/tpclient.h>
34 #include <Security/cssmtype.h>
36 #include "SecBridge.h"
38 // %%% used by SecCertificate{Copy,Set}Preference
39 #include <Security/SecKeychainItemPriv.h>
40 #include <Security/SecIdentityPriv.h>
41 #include <Security/SecItemPriv.h>
42 #include <security_keychain/KCCursor.h>
43 #include <security_cdsa_utilities/Schema.h>
44 #include <security_cdsa_utils/cuCdsaUtils.h>
45 #include <sys/param.h>
47 #include "CertificateValues.h"
49 OSStatus
SecCertificateGetCLHandle_legacy(SecCertificateRef certificate
, CSSM_CL_HANDLE
*clHandle
);
50 extern CSSM_KEYUSE
ConvertArrayToKeyUsage(CFArrayRef usage
);
55 using namespace CssmClient
;
57 CFTypeID
static SecCertificateGetTypeID_osx(void)
61 return gTypes().Certificate
.typeID
;
63 END_SECAPI1(_kCFRuntimeNotATypeID
)
67 SecCertificateIsItemImplInstance(SecCertificateRef certificate
)
69 if (certificate
== NULL
) {
73 CFTypeID typeID
= CFGetTypeID(certificate
);
75 #if 0 /* debug code to verify type IDs */
76 syslog(LOG_ERR
, "SecCertificate typeID=%d [STU=%d, OSX=%d, SKI=%d]",
78 (int)SecCertificateGetTypeID(),
79 (int)SecCertificateGetTypeID_osx(),
80 (int)SecKeychainItemGetTypeID());
82 if (typeID
== _kCFRuntimeNotATypeID
) {
86 return (typeID
== SecCertificateGetTypeID_osx() ||
87 typeID
== SecKeychainItemGetTypeID()) ? true : false;
90 /* convert a new-world SecCertificateRef to an old-world ItemImpl instance */
92 SecCertificateCreateItemImplInstance(SecCertificateRef certificate
)
97 SecCertificateRef implCertRef
= (SecCertificateRef
) SecCertificateCopyKeychainItem(certificate
);
101 CFDataRef data
= SecCertificateCopyData(certificate
);
106 CSSM_DATA cssmCertData
;
107 cssmCertData
.Length
= (data
) ? (CSSM_SIZE
)CFDataGetLength(data
) : 0;
108 cssmCertData
.Data
= (data
) ? (uint8
*)CFDataGetBytePtr(data
) : NULL
;
110 SecPointer
<Certificate
> certificatePtr(new Certificate(cssmCertData
, CSSM_CERT_X_509v3
, CSSM_CERT_ENCODING_DER
));
111 implCertRef
= certificatePtr
->handle();
118 /* convert an old-world ItemImpl instance to a new-world SecCertificateRef */
120 SecCertificateCreateFromItemImplInstance(SecCertificateRef certificate
)
125 SecCertificateRef result
= NULL
;
126 CFDataRef data
= NULL
;
128 CssmData certData
= Certificate::required(certificate
)->data();
129 if (certData
.Data
&& certData
.Length
) {
130 data
= CFDataCreate(NULL
, certData
.Data
, certData
.Length
);
133 if (certData
.Data
&& !certData
.Length
) {
134 /* zero-length certs can exist, so don't bother logging this */
137 syslog(LOG_ERR
, "WARNING: SecKeychainSearchCopyNext failed to retrieve certificate data (length=%ld, data=0x%lX)",
138 (long)certData
.Length
, (uintptr_t)certData
.Data
);
145 result
= SecCertificateCreateWithKeychainItem(NULL
, data
, certificate
);
152 /* OS X only: DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER */
154 SecCertificateCreateFromData(const CSSM_DATA
*data
, CSSM_CERT_TYPE type
, CSSM_CERT_ENCODING encoding
, SecCertificateRef
*certificate
)
156 /* bridge to support old functionality */
157 if (!data
|| !data
->Data
|| !data
->Length
|| !certificate
) {
160 SecCertificateRef certRef
= NULL
;
162 // <rdar://problem/24403998> REG: Adobe {Photoshop, InDesign} CC(2015) crashes on launch
163 // If you take the length that SecKeychainItemCopyContent gives you (a Uint32) and assign it incorrectly
164 // to a CSSM_DATA Length field (a CSSM_SIZE, i.e., a size_t), the upper 32 bits aren't set. If those bits
165 // are non-zero, the length is incredibly wrong.
167 // Assume that there will not exist a certificate > 4GiB, and fake this length field.
168 CSSM_SIZE length
= data
->Length
& 0xfffffffful
;
170 CFDataRef dataRef
= CFDataCreate(NULL
, data
->Data
, length
);
172 certRef
= SecCertificateCreateWithData(NULL
, dataRef
);
175 *certificate
= certRef
;
176 return (certRef
) ? errSecSuccess
: errSecUnknownFormat
;
179 /* OS X only: __OSX_AVAILABLE_STARTING(__MAC_10_3, __IPHONE_NA) */
181 SecCertificateAddToKeychain(SecCertificateRef certificate
, SecKeychainRef keychain
)
183 // This macro creates an ItemImpl certificate if it does not exist
186 Item
item(Certificate::required(__itemImplRef
));
187 Keychain::optional(keychain
)->add(item
);
192 /* OS X only: DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER */
194 SecCertificateGetData(SecCertificateRef certificate
, CSSM_DATA_PTR data
)
198 if (!certificate
|| !data
) {
199 __secapiresult
=errSecParam
;
201 else if (SecCertificateIsItemImplInstance(certificate
)) {
202 Required(data
) = Certificate::required(certificate
)->data();
205 data
->Length
= (CSSM_SIZE
)SecCertificateGetLength(certificate
);
206 data
->Data
= (uint8
*)SecCertificateGetBytePtr(certificate
);
212 /* OS X only: DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER */
214 SecCertificateGetType(SecCertificateRef certificate
, CSSM_CERT_TYPE
*certificateType
)
216 // This macro creates an ItemImpl certificate if it does not exist
219 Required(certificateType
) = Certificate::required(__itemImplRef
)->type();
224 /* OS X only: DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER */
226 SecCertificateGetSubject(SecCertificateRef certificate
, const CSSM_X509_NAME
**subject
)
228 // This macro creates an ItemImpl certificate if it does not exist
231 Required(subject
) = Certificate::required(__itemImplRef
)->subjectName();
236 /* OS X only: DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER */
238 SecCertificateGetIssuer(SecCertificateRef certificate
, const CSSM_X509_NAME
**issuer
)
240 // This macro creates an ItemImpl certificate if it does not exist
243 Required(issuer
) = Certificate::required(__itemImplRef
)->issuerName();
248 /* OS X only: DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER */
250 SecCertificateGetCLHandle(SecCertificateRef certificate
, CSSM_CL_HANDLE
*clHandle
)
252 // This macro creates an ItemImpl certificate if it does not exist
255 Required(clHandle
) = Certificate::required(__itemImplRef
)->clHandle();
260 /* private function; assumes input is old-style ItemImpl certificate reference,
261 and does not release that certificate reference!
264 SecCertificateGetCLHandle_legacy(SecCertificateRef certificate
, CSSM_CL_HANDLE
*clHandle
)
268 Required(clHandle
) = Certificate::required(certificate
)->clHandle();
274 * Private API to infer a display name for a SecCertificateRef which
275 * may or may not be in a keychain.
280 SecCertificateInferLabel(SecCertificateRef certificate
, CFStringRef
*label
)
282 // This macro creates an ItemImpl certificate if it does not exist
285 Certificate::required(__itemImplRef
)->inferLabel(false, &Required(label
));
290 /* OS X only (note: iOS version has different arguments and return value) */
292 SecCertificateCopyPublicKey(SecCertificateRef certificate
, SecKeyRef
*key
)
294 // This macro creates an ItemImpl certificate if it does not exist
297 Required(key
) = Certificate::required(__itemImplRef
)->publicKey()->handle();
302 /* OS X only: DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER */
304 SecCertificateGetAlgorithmID(SecCertificateRef certificate
, const CSSM_X509_ALGORITHM_IDENTIFIER
**algid
)
306 // This macro creates an ItemImpl certificate if it does not exist
309 Required(algid
) = Certificate::required(__itemImplRef
)->algorithmID();
316 SecCertificateCopySubjectComponent(SecCertificateRef certificate
, const CSSM_OID
*component
, CFStringRef
*result
)
318 // This macro creates an ItemImpl certificate if it does not exist
321 Required(result
) = Certificate::required(__itemImplRef
)->distinguishedName(&CSSMOID_X509V1SubjectNameCStruct
, component
);
326 /* OS X only; deprecated SPI */
328 SecCertificateGetCommonName(SecCertificateRef certificate
, CFStringRef
*commonName
)
330 // deprecated SPI signature; replaced by SecCertificateCopyCommonName
331 return SecCertificateCopyCommonName(certificate
, commonName
);
334 /* OS X only; deprecated SPI */
336 SecCertificateGetEmailAddress(SecCertificateRef certificate
, CFStringRef
*emailAddress
)
338 // This macro creates an ItemImpl certificate if it does not exist
341 Required(emailAddress
) = Certificate::required(__itemImplRef
)->copyFirstEmailAddress();
346 /* Return a zero terminated list of CSSM_DATA_PTR's with the values of the field specified by field.
347 * Caller must call releaseFieldValues to free the storage allocated by this call.
352 SecCertificateCopyFieldValues(SecCertificateRef certificate
, const CSSM_OID
*field
, CSSM_DATA_PTR
**fieldValues
)
354 // This macro creates an ItemImpl certificate if it does not exist
357 Required(fieldValues
) = Certificate::required(__itemImplRef
)->copyFieldValues(Required(field
));
364 SecCertificateReleaseFieldValues(SecCertificateRef certificate
, const CSSM_OID
*field
, CSSM_DATA_PTR
*fieldValues
)
366 // This macro creates an ItemImpl certificate if it does not exist
369 Certificate::required(__itemImplRef
)->releaseFieldValues(Required(field
), fieldValues
);
376 SecCertificateCopyFirstFieldValue(SecCertificateRef certificate
, const CSSM_OID
*field
, CSSM_DATA_PTR
*fieldValue
)
378 // This macro creates an ItemImpl certificate if it does not exist
381 Required(fieldValue
) = Certificate::required(__itemImplRef
)->copyFirstFieldValue(Required(field
));
388 SecCertificateReleaseFirstFieldValue(SecCertificateRef certificate
, const CSSM_OID
*field
, CSSM_DATA_PTR fieldValue
)
390 // This macro creates an ItemImpl certificate if it does not exist
393 Certificate::required(__itemImplRef
)->releaseFieldValue(Required(field
), fieldValue
);
400 SecCertificateFindByIssuerAndSN(CFTypeRef keychainOrArray
,const CSSM_DATA
*issuer
,
401 const CSSM_DATA
*serialNumber
, SecCertificateRef
*certificate
)
403 if (issuer
&& serialNumber
) {
404 CFRef
<CFMutableDictionaryRef
> query
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
405 CFDictionarySetValue(query
, kSecClass
, kSecClassCertificate
);
406 CFDictionarySetValue(query
, kSecReturnRef
, kCFBooleanTrue
);
407 CFDictionarySetValue(query
, kSecUseDataProtectionKeychain
, kCFBooleanTrue
);
409 CFRef
<CFDataRef
> issuerData
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, (const UInt8
*)issuer
->Data
, issuer
->Length
, kCFAllocatorNull
);
410 CFDictionarySetValue(query
, kSecAttrIssuer
, issuerData
);
412 CFRef
<CFDataRef
> serialNumberData
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, (const UInt8
*)serialNumber
->Data
, serialNumber
->Length
, kCFAllocatorNull
);
413 CFDictionarySetValue(query
, kSecAttrSerialNumber
, serialNumberData
);
415 OSStatus status
= SecItemCopyMatching(query
, (CFTypeRef
*)certificate
);
416 if (status
== errSecSuccess
) {
423 StorageManager::KeychainList keychains
;
424 globals().storageManager
.optionalSearchList(keychainOrArray
, keychains
);
425 Required(certificate
) = Certificate::findByIssuerAndSN(keychains
, CssmData::required(issuer
), CssmData::required(serialNumber
))->handle();
427 // convert ItemImpl-based SecCertificateRef to new-world version before returning
428 CssmData certData
= Certificate::required(*certificate
)->data();
429 CFRef
<CFDataRef
> cfData(CFDataCreate(NULL
, certData
.Data
, certData
.Length
));
430 SecCertificateRef tmpRef
= *certificate
;
431 *certificate
= SecCertificateCreateWithData(NULL
, cfData
);
439 SecCertificateFindBySubjectKeyID(CFTypeRef keychainOrArray
, const CSSM_DATA
*subjectKeyID
,
440 SecCertificateRef
*certificate
)
443 CFRef
<CFMutableDictionaryRef
> query
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
444 CFDictionarySetValue(query
, kSecClass
, kSecClassCertificate
);
445 CFDictionarySetValue(query
, kSecReturnRef
, kCFBooleanTrue
);
446 CFDictionarySetValue(query
, kSecUseDataProtectionKeychain
, kCFBooleanTrue
);
448 CFRef
<CFDataRef
> subjectKeyIDData
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, (const UInt8
*)subjectKeyID
->Data
, subjectKeyID
->Length
, kCFAllocatorNull
);
449 CFDictionarySetValue(query
, kSecAttrSubjectKeyID
, subjectKeyIDData
);
451 OSStatus status
= SecItemCopyMatching(query
, (CFTypeRef
*)certificate
);
452 if (status
== errSecSuccess
) {
459 StorageManager::KeychainList keychains
;
460 globals().storageManager
.optionalSearchList(keychainOrArray
, keychains
);
461 Required(certificate
) = Certificate::findBySubjectKeyID(keychains
, CssmData::required(subjectKeyID
))->handle();
463 // convert ItemImpl-based SecCertificateRef to new-world version before returning
464 CssmData certData
= Certificate::required(*certificate
)->data();
465 CFRef
<CFDataRef
> cfData(CFDataCreate(NULL
, certData
.Data
, certData
.Length
));
466 SecCertificateRef tmpRef
= *certificate
;
467 *certificate
= SecCertificateCreateWithData(NULL
, cfData
);
475 SecCertificateFindByEmail(CFTypeRef keychainOrArray
, const char *emailAddress
, SecCertificateRef
*certificate
)
478 CFRef
<CFMutableDictionaryRef
> query
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
479 CFDictionarySetValue(query
, kSecClass
, kSecClassCertificate
);
480 CFDictionarySetValue(query
, kSecReturnRef
, kCFBooleanTrue
);
481 CFDictionarySetValue(query
, kSecUseDataProtectionKeychain
, kCFBooleanTrue
);
483 CFRef
<CFStringRef
> emailAddressString
= CFStringCreateWithCString(kCFAllocatorDefault
, emailAddress
, kCFStringEncodingUTF8
);
484 CFTypeRef keys
[] = { kSecPolicyName
};
485 CFTypeRef values
[] = { emailAddressString
};
486 CFRef
<CFDictionaryRef
> properties
= CFDictionaryCreate(kCFAllocatorDefault
, keys
, values
, 1, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
487 CFRef
<SecPolicyRef
> policy
= SecPolicyCreateWithProperties(kSecPolicyAppleSMIME
, properties
);
488 CFDictionarySetValue(query
, kSecMatchPolicy
, policy
);
490 OSStatus status
= SecItemCopyMatching(query
, (CFTypeRef
*)certificate
);
491 if (status
== errSecSuccess
) {
498 StorageManager::KeychainList keychains
;
499 globals().storageManager
.optionalSearchList(keychainOrArray
, keychains
);
500 Required(certificate
) = Certificate::findByEmail(keychains
, emailAddress
)->handle();
502 // convert ItemImpl-based SecCertificateRef to new-world version before returning
503 CssmData certData
= Certificate::required(*certificate
)->data();
504 CFRef
<CFDataRef
> cfData(CFDataCreate(NULL
, certData
.Data
, certData
.Length
));
505 SecCertificateRef tmpRef
= *certificate
;
506 *certificate
= SecCertificateCreateWithData(NULL
, cfData
);
514 SecKeychainSearchCreateForCertificateByIssuerAndSN(CFTypeRef keychainOrArray
, const CSSM_DATA
*issuer
,
515 const CSSM_DATA
*serialNumber
, SecKeychainSearchRef
*searchRef
)
521 StorageManager::KeychainList keychains
;
522 globals().storageManager
.optionalSearchList(keychainOrArray
, keychains
);
523 KCCursor
cursor(Certificate::cursorForIssuerAndSN(keychains
, CssmData::required(issuer
), CssmData::required(serialNumber
)));
524 *searchRef
= cursor
->handle();
531 SecKeychainSearchCreateForCertificateByIssuerAndSN_CF(CFTypeRef keychainOrArray
, CFDataRef issuer
,
532 CFDataRef serialNumber
, SecKeychainSearchRef
*searchRef
)
538 StorageManager::KeychainList keychains
;
539 globals().storageManager
.optionalSearchList(keychainOrArray
, keychains
);
541 Required(serialNumber
);
542 KCCursor
cursor(Certificate::cursorForIssuerAndSN_CF(keychains
, issuer
, serialNumber
));
543 *searchRef
= cursor
->handle();
550 SecKeychainSearchCreateForCertificateBySubjectKeyID(CFTypeRef keychainOrArray
, const CSSM_DATA
*subjectKeyID
,
551 SecKeychainSearchRef
*searchRef
)
557 StorageManager::KeychainList keychains
;
558 globals().storageManager
.optionalSearchList(keychainOrArray
, keychains
);
559 KCCursor
cursor(Certificate::cursorForSubjectKeyID(keychains
, CssmData::required(subjectKeyID
)));
560 *searchRef
= cursor
->handle();
567 SecKeychainSearchCreateForCertificateByEmail(CFTypeRef keychainOrArray
, const char *emailAddress
,
568 SecKeychainSearchRef
*searchRef
)
574 StorageManager::KeychainList keychains
;
575 globals().storageManager
.optionalSearchList(keychainOrArray
, keychains
);
576 KCCursor
cursor(Certificate::cursorForEmail(keychains
, emailAddress
));
577 *searchRef
= cursor
->handle();
584 SecDigestGetData (CSSM_ALGORITHMS alg
, CSSM_DATA
* digest
, const CSSM_DATA
* data
)
588 if (!digest
|| !digest
->Data
|| !digest
->Length
|| !data
|| !data
->Data
|| !data
->Length
)
591 CSP
csp(gGuidAppleCSP
);
592 Digest
context(csp
, alg
);
593 CssmData
input(data
->Data
, data
->Length
);
594 CssmData
output(digest
->Data
, digest
->Length
);
596 context
.digest(input
, output
);
597 digest
->Length
= output
.length();
603 /* OS X only: DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER */
605 SecCertificateCopyPreference(
607 CSSM_KEYUSE keyUsage
,
608 SecCertificateRef
*certificate
)
613 Required(certificate
);
614 StorageManager::KeychainList keychains
;
615 globals().storageManager
.getSearchList(keychains
);
616 KCCursor
cursor(keychains
, kSecGenericPasswordItemClass
, NULL
);
618 char idUTF8
[MAXPATHLEN
];
619 if (!CFStringGetCString(name
, idUTF8
, sizeof(idUTF8
)-1, kCFStringEncodingUTF8
))
620 idUTF8
[0] = (char)'\0';
621 CssmData
service(const_cast<char *>(idUTF8
), strlen(idUTF8
));
622 FourCharCode itemType
= 'cprf';
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 if (!cursor
->next(prefItem
))
630 MacOSError::throwMe(errSecItemNotFound
);
632 // get persistent certificate reference
633 SecKeychainAttribute itemAttrs
[] = { { kSecGenericItemAttr
, 0, NULL
} };
634 SecKeychainAttributeList itemAttrList
= { sizeof(itemAttrs
) / sizeof(itemAttrs
[0]), itemAttrs
};
635 prefItem
->getContent(NULL
, &itemAttrList
, NULL
, NULL
);
637 // find certificate, given persistent reference data
638 CFDataRef pItemRef
= CFDataCreateWithBytesNoCopy(NULL
, (const UInt8
*)itemAttrs
[0].data
, itemAttrs
[0].length
, kCFAllocatorNull
);
639 SecKeychainItemRef certItemRef
= nil
;
640 OSStatus status
= SecKeychainItemCopyFromPersistentReference(pItemRef
, &certItemRef
); //%%% need to make this a method of ItemImpl
641 prefItem
->freeContent(&itemAttrList
, NULL
);
647 *certificate
= (SecCertificateRef
)certItemRef
;
649 if (certItemRef
&& (CFGetTypeID(certItemRef
) == SecIdentityGetTypeID())) {
650 // SecKeychainItemCopyFromPersistentReference handed out an identity reference
652 status
= SecIdentityCopyCertificate((SecIdentityRef
)certItemRef
, certificate
);
653 CFRelease(certItemRef
);
662 SecCertificateCopyPreferred(
666 // This function will look for a matching preference in the following order:
667 // - matches the name and the supplied key use
668 // - matches the name and the special 'ANY' key use
669 // - matches the name with no key usage constraint
671 SecCertificateRef certRef
= NULL
;
672 CSSM_KEYUSE keyUse
= ConvertArrayToKeyUsage(keyUsage
);
673 OSStatus status
= SecCertificateCopyPreference(name
, keyUse
, &certRef
);
674 if (status
!= errSecSuccess
&& keyUse
!= CSSM_KEYUSE_ANY
)
675 status
= SecCertificateCopyPreference(name
, CSSM_KEYUSE_ANY
, &certRef
);
676 if (status
!= errSecSuccess
&& keyUse
!= 0)
677 status
= SecCertificateCopyPreference(name
, 0, &certRef
);
682 /* OS X only; not exported */
684 SecCertificateFindPreferenceItemWithNameAndKeyUsage(
685 CFTypeRef keychainOrArray
,
688 SecKeychainItemRef
*itemRef
)
692 StorageManager::KeychainList keychains
;
693 globals().storageManager
.optionalSearchList(keychainOrArray
, keychains
);
694 KCCursor
cursor(keychains
, kSecGenericPasswordItemClass
, NULL
);
696 char idUTF8
[MAXPATHLEN
];
697 idUTF8
[0] = (char)'\0';
700 if (!CFStringGetCString(name
, idUTF8
, sizeof(idUTF8
)-1, kCFStringEncodingUTF8
))
701 idUTF8
[0] = (char)'\0';
703 size_t idUTF8Len
= strlen(idUTF8
);
705 MacOSError::throwMe(errSecParam
);
707 CssmData
service(const_cast<char *>(idUTF8
), idUTF8Len
);
708 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecServiceItemAttr
), service
);
709 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecTypeItemAttr
), (FourCharCode
)'cprf');
711 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecScriptCodeItemAttr
), (sint32
)keyUsage
);
714 if (!cursor
->next(item
))
715 MacOSError::throwMe(errSecItemNotFound
);
718 *itemRef
=item
->handle();
723 /* OS X only; not exported */
725 OSStatus
SecCertificateDeletePreferenceItemWithNameAndKeyUsage(
726 CFTypeRef keychainOrArray
,
730 // when a specific key usage is passed, we'll only match & delete that pref;
731 // when a key usage of 0 is passed, all matching prefs should be deleted.
732 // maxUsages represents the most matches there could theoretically be, so
733 // cut things off at that point if we're still finding items (if they can't
734 // be deleted for some reason, we'd never break out of the loop.)
736 OSStatus status
= errSecSuccess
;
737 SecKeychainItemRef item
= NULL
;
738 int count
= 0, maxUsages
= 12;
739 while (++count
<= maxUsages
&&
740 (status
= SecCertificateFindPreferenceItemWithNameAndKeyUsage(keychainOrArray
, name
, keyUsage
, &item
)) == errSecSuccess
) {
741 status
= SecKeychainItemDelete(item
);
746 // it's not an error if the item isn't found
747 return (status
== errSecItemNotFound
) ? errSecSuccess
: status
;
750 /* OS X only: __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA) */
751 OSStatus
SecCertificateSetPreference(
752 SecCertificateRef certificate
,
754 CSSM_KEYUSE keyUsage
,
761 // treat NULL certificate as a request to clear the preference
762 // (note: if keyUsage is 0, this clears all key usage prefs for name)
763 return SecCertificateDeletePreferenceItemWithNameAndKeyUsage(NULL
, name
, keyUsage
);
766 // This macro creates an ItemImpl certificate if it does not exist
769 // determine the account attribute
771 // This attribute must be synthesized from certificate label + pref item type + key usage,
772 // as only the account and service attributes can make a generic keychain item unique.
773 // For 'iprf' type items (but not 'cprf'), we append a trailing space. This insures that
774 // we can save a certificate preference if an identity preference already exists for the
775 // given service name, and vice-versa.
776 // If the key usage is 0 (i.e. the normal case), we omit the appended key usage string.
778 CFStringRef labelStr
= nil
;
779 Certificate::required(__itemImplRef
)->inferLabel(false, &labelStr
);
781 MacOSError::throwMe(errSecDataTooLarge
); // data is "in a format which cannot be displayed"
783 CFIndex accountUTF8Len
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(labelStr
), kCFStringEncodingUTF8
) + 1;
784 const char *templateStr
= "%s [key usage 0x%X]";
785 const int keyUsageMaxStrLen
= 8;
786 accountUTF8Len
+= strlen(templateStr
) + keyUsageMaxStrLen
;
787 char *accountUTF8
= (char *)malloc(accountUTF8Len
);
789 MacOSError::throwMe(errSecMemoryError
);
791 if (!CFStringGetCString(labelStr
, accountUTF8
, accountUTF8Len
-1, kCFStringEncodingUTF8
))
792 accountUTF8
[0] = (char)'\0';
794 snprintf(accountUTF8
, accountUTF8Len
-1, templateStr
, accountUTF8
, keyUsage
);
795 CssmDataContainer
account(const_cast<char *>(accountUTF8
), strlen(accountUTF8
));
799 // service attribute (name provided by the caller)
800 CFIndex serviceUTF8Len
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(name
), kCFStringEncodingUTF8
) + 1;;
801 char *serviceUTF8
= (char *)malloc(serviceUTF8Len
);
803 MacOSError::throwMe(errSecMemoryError
);
805 if (!CFStringGetCString(name
, serviceUTF8
, serviceUTF8Len
-1, kCFStringEncodingUTF8
))
806 serviceUTF8
[0] = (char)'\0';
807 CssmDataContainer
service(const_cast<char *>(serviceUTF8
), strlen(serviceUTF8
));
810 // look for existing preference item, in case this is an update
811 StorageManager::KeychainList keychains
;
812 globals().storageManager
.getSearchList(keychains
);
813 KCCursor
cursor(keychains
, kSecGenericPasswordItemClass
, NULL
);
814 FourCharCode itemType
= 'cprf';
815 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecServiceItemAttr
), service
);
816 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecTypeItemAttr
), itemType
);
818 cursor
->add(CSSM_DB_EQUAL
, Schema::attributeInfo(kSecScriptCodeItemAttr
), (sint32
)keyUsage
);
822 Item
item(kSecGenericPasswordItemClass
, 'aapl', 0, NULL
, false);
823 bool add
= (!cursor
->next(item
));
824 // at this point, we either have a new item to add or an existing item to update
826 // set item attribute values
827 item
->setAttribute(Schema::attributeInfo(kSecServiceItemAttr
), service
);
828 item
->setAttribute(Schema::attributeInfo(kSecTypeItemAttr
), itemType
);
829 item
->setAttribute(Schema::attributeInfo(kSecAccountItemAttr
), account
);
830 item
->setAttribute(Schema::attributeInfo(kSecScriptCodeItemAttr
), (sint32
)keyUsage
);
831 item
->setAttribute(Schema::attributeInfo(kSecLabelItemAttr
), service
);
837 // generic attribute (store persistent certificate reference)
838 CFDataRef pItemRef
= nil
;
839 Certificate::required(__itemImplRef
)->copyPersistentReference(pItemRef
);
841 MacOSError::throwMe(errSecInvalidItemRef
);
843 const UInt8
*dataPtr
= CFDataGetBytePtr(pItemRef
);
844 CFIndex dataLen
= CFDataGetLength(pItemRef
);
845 CssmData
pref(const_cast<void *>(reinterpret_cast<const void *>(dataPtr
)), dataLen
);
846 item
->setAttribute(Schema::attributeInfo(kSecGenericItemAttr
), pref
);
850 Keychain keychain
= nil
;
852 keychain
= globals().storageManager
.defaultKeychain();
853 if (!keychain
->exists())
854 MacOSError::throwMe(errSecNoSuchKeychain
); // Might be deleted or not available at this time.
857 keychain
= globals().storageManager
.defaultKeychainUI(item
);
863 catch (const MacOSError
&err
) {
864 if (err
.osStatus() != errSecDuplicateItem
)
865 throw; // if item already exists, fall through to update
873 /* OS X only: __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA) */
874 OSStatus
SecCertificateSetPreferred(
875 SecCertificateRef certificate
,
879 CSSM_KEYUSE keyUse
= ConvertArrayToKeyUsage(keyUsage
);
880 return SecCertificateSetPreference(certificate
, name
, keyUse
, NULL
);
883 /* OS X only: __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA) */
884 CFDictionaryRef
SecCertificateCopyValues(SecCertificateRef certificate
, CFArrayRef keys
, CFErrorRef
*error
)
886 CFDictionaryRef result
= NULL
;
887 OSStatus __secapiresult
;
888 SecCertificateRef tmpcert
= NULL
;
890 // convert input to a new-style certificate reference if necessary,
891 // since the implementation of CertificateValues calls SecCertificate API functions
892 // which now assume a unified certificate reference.
893 if (SecCertificateIsItemImplInstance(certificate
)) {
894 tmpcert
= SecCertificateCreateFromItemImplInstance(certificate
);
896 if (certificate
&& !tmpcert
) {
897 tmpcert
= (SecCertificateRef
) CFRetain(certificate
);
901 CertificateValues
cv(tmpcert
);
902 result
= cv
.copyFieldValues(keys
,error
);
905 catch (const MacOSError
&err
) { __secapiresult
=err
.osStatus(); }
906 catch (const CommonError
&err
) { __secapiresult
=SecKeychainErrFromOSStatus(err
.osStatus()); }
907 catch (const std::bad_alloc
&) { __secapiresult
=errSecAllocate
; }
908 catch (...) { __secapiresult
=errSecInternalComponent
; }
909 if (tmpcert
) { CFRelease(tmpcert
); }
913 /* OS X only: __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA) */
914 CFStringRef
SecCertificateCopyLongDescription(CFAllocatorRef alloc
, SecCertificateRef certificate
, CFErrorRef
*error
)
916 return SecCertificateCopyShortDescription(alloc
, certificate
, error
);
919 /* OS X only: __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA) */
920 CFStringRef
SecCertificateCopyShortDescription(CFAllocatorRef alloc
, SecCertificateRef certificate
, CFErrorRef
*error
)
922 CFStringRef result
= NULL
;
923 OSStatus __secapiresult
= SecCertificateInferLabel(certificate
, &result
);
924 if (error
!=NULL
&& __secapiresult
!=errSecSuccess
)
926 *error
= CFErrorCreate(kCFAllocatorDefault
, kCFErrorDomainOSStatus
,
927 __secapiresult
? __secapiresult
: CSSM_ERRCODE_INTERNAL_ERROR
, NULL
);
932 /* OS X only: __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA) */
933 CFDataRef
SecCertificateCopySerialNumber(SecCertificateRef certificate
, CFErrorRef
*error
)
935 CFDataRef result
= NULL
;
936 OSStatus __secapiresult
;
939 CertificateValues
cv(certificate
);
940 result
= cv
.copySerialNumber(error
);
943 catch (const MacOSError
&err
) { __secapiresult
=err
.osStatus(); }
944 catch (const CommonError
&err
) { __secapiresult
=SecKeychainErrFromOSStatus(err
.osStatus()); }
945 catch (const std::bad_alloc
&) { __secapiresult
=errSecAllocate
; }
946 catch (...) { __secapiresult
=errSecInternalComponent
; }
950 /* OS X only: __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_7, __MAC_10_12_4, __IPHONE_NA, __IPHONE_NA) */
951 CFDataRef
SecCertificateCopyNormalizedIssuerContent(SecCertificateRef certificate
, CFErrorRef
*error
)
953 CFDataRef result
= NULL
;
954 OSStatus __secapiresult
;
957 CertificateValues
cv(certificate
);
958 result
= cv
.copyNormalizedIssuerContent(error
);
961 catch (const MacOSError
&err
) { __secapiresult
=err
.osStatus(); }
962 catch (const CommonError
&err
) { __secapiresult
=SecKeychainErrFromOSStatus(err
.osStatus()); }
963 catch (const std::bad_alloc
&) { __secapiresult
=errSecAllocate
; }
964 catch (...) { __secapiresult
=errSecInternalComponent
; }
968 /* OS X only: __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_7, __MAC_10_12_4, __IPHONE_NA, __IPHONE_NA) */
969 CFDataRef
SecCertificateCopyNormalizedSubjectContent(SecCertificateRef certificate
, CFErrorRef
*error
)
971 CFDataRef result
= NULL
;
972 OSStatus __secapiresult
;
975 CertificateValues
cv(certificate
);
976 result
= cv
.copyNormalizedSubjectContent(error
);
979 catch (const MacOSError
&err
) { __secapiresult
=err
.osStatus(); }
980 catch (const CommonError
&err
) { __secapiresult
=SecKeychainErrFromOSStatus(err
.osStatus()); }
981 catch (const std::bad_alloc
&) { __secapiresult
=errSecAllocate
; }
982 catch (...) { __secapiresult
=errSecInternalComponent
; }
986 /* OS X only: __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_7, __MAC_10_9, __IPHONE_NA, __IPHONE_NA) */
987 bool SecCertificateIsValidX(SecCertificateRef certificate
, CFAbsoluteTime verifyTime
)
990 * deprecated function name
992 return SecCertificateIsValid(certificate
, verifyTime
);
996 CFDataRef
SecCertificateCopyPublicKeySHA1DigestFromCertificateData(CFAllocatorRef allocator
,
997 CFDataRef der_certificate
)
999 CFDataRef result
= NULL
;
1000 SecCertificateRef iosCertRef
= SecCertificateCreateWithData(allocator
, der_certificate
);
1001 if (NULL
== iosCertRef
)
1006 result
= SecCertificateCopyPublicKeySHA1Digest(iosCertRef
);
1007 CFRelease(iosCertRef
);