2 * Copyright (c) 2002-2013 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 <CoreFoundation/CFString.h>
25 #include <CoreFoundation/CFNumber.h>
26 #include <CoreFoundation/CFArray.h>
27 #include <Security/SecItem.h>
28 #include <Security/SecPolicy.h>
29 #include <Security/SecPolicyPriv.h>
30 #include <Security/SecCertificate.h>
31 #include <Security/SecCertificatePriv.h>
32 #include <security_keychain/Policies.h>
33 #include <security_keychain/PolicyCursor.h>
34 #include "SecBridge.h"
37 // String constant declarations
39 #define SEC_CONST_DECL(k,v) CFTypeRef k = (CFTypeRef)(CFSTR(v));
41 SEC_CONST_DECL (kSecPolicyAppleX509Basic
, "1.2.840.113635.100.1.2");
42 SEC_CONST_DECL (kSecPolicyAppleSSL
, "1.2.840.113635.100.1.3");
43 SEC_CONST_DECL (kSecPolicyAppleSMIME
, "1.2.840.113635.100.1.8");
44 SEC_CONST_DECL (kSecPolicyAppleEAP
, "1.2.840.113635.100.1.9");
45 SEC_CONST_DECL (kSecPolicyAppleIPsec
, "1.2.840.113635.100.1.11");
46 SEC_CONST_DECL (kSecPolicyAppleiChat
, "1.2.840.113635.100.1.12");
47 SEC_CONST_DECL (kSecPolicyApplePKINITClient
, "1.2.840.113635.100.1.14");
48 SEC_CONST_DECL (kSecPolicyApplePKINITServer
, "1.2.840.113635.100.1.15");
49 SEC_CONST_DECL (kSecPolicyAppleCodeSigning
, "1.2.840.113635.100.1.16");
50 SEC_CONST_DECL (kSecPolicyApplePackageSigning
, "1.2.840.113635.100.1.17");
51 SEC_CONST_DECL (kSecPolicyAppleIDValidation
, "1.2.840.113635.100.1.18");
52 SEC_CONST_DECL (kSecPolicyMacAppStoreReceipt
, "1.2.840.113635.100.1.19");
53 SEC_CONST_DECL (kSecPolicyAppleTimeStamping
, "1.2.840.113635.100.1.20");
54 SEC_CONST_DECL (kSecPolicyAppleRevocation
, "1.2.840.113635.100.1.21");
55 SEC_CONST_DECL (kSecPolicyApplePassbookSigning
, "1.2.840.113635.100.1.22");
56 SEC_CONST_DECL (kSecPolicyAppleMobileStore
, "1.2.840.113635.100.1.23");
57 SEC_CONST_DECL (kSecPolicyAppleEscrowService
, "1.2.840.113635.100.1.24");
58 SEC_CONST_DECL (kSecPolicyAppleProfileSigner
, "1.2.840.113635.100.1.25");
59 SEC_CONST_DECL (kSecPolicyAppleQAProfileSigner
, "1.2.840.113635.100.1.26");
60 SEC_CONST_DECL (kSecPolicyAppleTestMobileStore
, "1.2.840.113635.100.1.27");
63 SEC_CONST_DECL (kSecPolicyOid
, "SecPolicyOid");
64 SEC_CONST_DECL (kSecPolicyName
, "SecPolicyName");
65 SEC_CONST_DECL (kSecPolicyClient
, "SecPolicyClient");
66 SEC_CONST_DECL (kSecPolicyRevocationFlags
, "SecPolicyRevocationFlags");
67 SEC_CONST_DECL (kSecPolicyTeamIdentifier
, "SecPolicyTeamIdentifier");
69 SEC_CONST_DECL (kSecPolicyKU_DigitalSignature
, "CE_KU_DigitalSignature");
70 SEC_CONST_DECL (kSecPolicyKU_NonRepudiation
, "CE_KU_NonRepudiation");
71 SEC_CONST_DECL (kSecPolicyKU_KeyEncipherment
, "CE_KU_KeyEncipherment");
72 SEC_CONST_DECL (kSecPolicyKU_DataEncipherment
, "CE_KU_DataEncipherment");
73 SEC_CONST_DECL (kSecPolicyKU_KeyAgreement
, "CE_KU_KeyAgreement");
74 SEC_CONST_DECL (kSecPolicyKU_KeyCertSign
, "CE_KU_KeyCertSign");
75 SEC_CONST_DECL (kSecPolicyKU_CRLSign
, "CE_KU_CRLSign");
76 SEC_CONST_DECL (kSecPolicyKU_EncipherOnly
, "CE_KU_EncipherOnly");
77 SEC_CONST_DECL (kSecPolicyKU_DecipherOnly
, "CE_KU_DecipherOnly");
81 SecPolicyRef
SecPolicyCreateWithSecAsn1Oid(SecAsn1Oid
*oidPtr
);
82 extern "C" { CFArrayRef
SecPolicyCopyEscrowRootCertificates(void); }
88 SecPolicyGetTypeID(void)
91 return gTypes().Policy
.typeID
;
92 END_SECAPI1(_kCFRuntimeNotATypeID
)
97 // Sec API bridge functions
100 SecPolicyGetOID(SecPolicyRef policyRef
, CSSM_OID
* oid
)
103 Required(oid
) = Policy::required(policyRef
)->oid();
108 SecPolicyGetValue(SecPolicyRef policyRef
, CSSM_DATA
* value
)
111 Required(value
) = Policy::required(policyRef
)->value();
116 SecPolicyCopyProperties(SecPolicyRef policyRef
)
118 /* can't use SECAPI macros, since this function does not return OSStatus */
119 CFDictionaryRef result
= NULL
;
121 result
= Policy::required(policyRef
)->properties();
133 SecPolicySetValue(SecPolicyRef policyRef
, const CSSM_DATA
*value
)
137 const CssmData
newValue(value
->Data
, value
->Length
);
138 Policy::required(policyRef
)->setValue(newValue
);
143 SecPolicySetProperties(SecPolicyRef policyRef
, CFDictionaryRef properties
)
146 Policy::required(policyRef
)->setProperties(properties
);
151 SecPolicyGetTPHandle(SecPolicyRef policyRef
, CSSM_TP_HANDLE
* tpHandle
)
154 Required(tpHandle
) = Policy::required(policyRef
)->tp()->handle();
159 SecPolicyCopyAll(CSSM_CERT_TYPE certificateType
, CFArrayRef
* policies
)
163 CFMutableArrayRef currPolicies
= NULL
;
164 currPolicies
= CFArrayCreateMutable(NULL
, 0, NULL
);
167 SecPointer
<PolicyCursor
> cursor(new PolicyCursor(NULL
, NULL
));
168 SecPointer
<Policy
> policy
;
169 while ( cursor
->next(policy
) ) /* copies the next policy */
171 CFArrayAppendValue(currPolicies
, policy
->handle()); /* 'SecPolicyRef' appended */
172 CFRelease(policy
->handle()); /* refcount bumped up when appended to array */
174 *policies
= CFArrayCreateCopy(NULL
, currPolicies
);
175 CFRelease(currPolicies
);
176 CFRelease(cursor
->handle());
182 SecPolicyCopy(CSSM_CERT_TYPE certificateType
, const CSSM_OID
*policyOID
, SecPolicyRef
* policy
)
187 SecPolicySearchRef srchRef
= NULL
;
190 ortn
= SecPolicySearchCreate(certificateType
, policyOID
, NULL
, &srchRef
);
194 ortn
= SecPolicySearchCopyNext(srchRef
, policy
);
201 SecPolicyCreateBasicX509(void)
203 // return a SecPolicyRef object for the X.509 Basic policy
204 SecPolicyRef policy
= nil
;
205 SecPolicySearchRef policySearch
= nil
;
206 OSStatus status
= SecPolicySearchCreate(CSSM_CERT_X_509v3
, &CSSMOID_APPLE_X509_BASIC
, NULL
, &policySearch
);
208 status
= SecPolicySearchCopyNext(policySearch
, &policy
);
211 CFRelease(policySearch
);
218 SecPolicyCreateSSL(Boolean server
, CFStringRef hostname
)
220 // return a SecPolicyRef object for the SSL policy, given hostname and client options
221 SecPolicyRef policy
= nil
;
222 SecPolicySearchRef policySearch
= nil
;
223 OSStatus status
= SecPolicySearchCreate(CSSM_CERT_X_509v3
, &CSSMOID_APPLE_TP_SSL
, NULL
, &policySearch
);
225 status
= SecPolicySearchCopyNext(policySearch
, &policy
);
227 if (!status
&& policy
) {
228 // set options for client-side or server-side policy evaluation
230 const char *hostnamestr
= NULL
;
232 hostnamestr
= CFStringGetCStringPtr(hostname
, kCFStringEncodingUTF8
);
233 if (hostnamestr
== NULL
) {
234 CFIndex maxLen
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(hostname
), kCFStringEncodingUTF8
) + 1;
235 strbuf
= (char *)malloc(maxLen
);
236 if (CFStringGetCString(hostname
, strbuf
, maxLen
, kCFStringEncodingUTF8
)) {
237 hostnamestr
= strbuf
;
241 uint32 hostnamelen
= (hostnamestr
) ? (uint32
)strlen(hostnamestr
) : 0;
242 uint32 flags
= (!server
) ? CSSM_APPLE_TP_SSL_CLIENT
: 0;
243 CSSM_APPLE_TP_SSL_OPTIONS opts
= {CSSM_APPLE_TP_SSL_OPTS_VERSION
, hostnamelen
, hostnamestr
, flags
};
244 CSSM_DATA data
= {sizeof(opts
), (uint8
*)&opts
};
245 SecPolicySetValue(policy
, &data
);
252 CFRelease(policySearch
);
258 SecPolicyCreateWithSecAsn1Oid(SecAsn1Oid
*oidPtr
)
260 SecPolicyRef policy
= NULL
;
262 SecPointer
<Policy
> policyObj
;
263 PolicyCursor::policy(oidPtr
, policyObj
);
264 policy
= policyObj
->handle();
273 SecPolicyCreateWithOID(CFTypeRef policyOID
)
275 // for now, we only accept the policy constants that are defined in SecPolicy.h
276 CFStringRef oidStr
= (CFStringRef
)policyOID
;
277 CSSM_OID
*oidPtr
= NULL
;
278 SecPolicyRef policy
= NULL
;
279 struct oidmap_entry_t
{
280 const CFTypeRef oidstr
;
281 const SecAsn1Oid
*oidptr
;
283 const oidmap_entry_t oidmap
[] = {
284 { kSecPolicyAppleX509Basic
, &CSSMOID_APPLE_X509_BASIC
},
285 { kSecPolicyAppleSSL
, &CSSMOID_APPLE_TP_SSL
},
286 { kSecPolicyAppleSMIME
, &CSSMOID_APPLE_TP_SMIME
},
287 { kSecPolicyAppleEAP
, &CSSMOID_APPLE_TP_EAP
},
288 { kSecPolicyAppleIPsec
, &CSSMOID_APPLE_TP_IP_SEC
},
289 { kSecPolicyAppleiChat
, &CSSMOID_APPLE_TP_ICHAT
},
290 { kSecPolicyApplePKINITClient
, &CSSMOID_APPLE_TP_PKINIT_CLIENT
},
291 { kSecPolicyApplePKINITServer
, &CSSMOID_APPLE_TP_PKINIT_SERVER
},
292 { kSecPolicyAppleCodeSigning
, &CSSMOID_APPLE_TP_CODE_SIGNING
},
293 { kSecPolicyMacAppStoreReceipt
, &CSSMOID_APPLE_TP_MACAPPSTORE_RECEIPT
},
294 { kSecPolicyAppleIDValidation
, &CSSMOID_APPLE_TP_APPLEID_SHARING
},
295 { kSecPolicyAppleTimeStamping
, &CSSMOID_APPLE_TP_TIMESTAMPING
},
296 { kSecPolicyAppleRevocation
, &CSSMOID_APPLE_TP_REVOCATION
},
297 { kSecPolicyApplePassbookSigning
, &CSSMOID_APPLE_TP_PASSBOOK_SIGNING
},
298 { kSecPolicyAppleMobileStore
, &CSSMOID_APPLE_TP_MOBILE_STORE
},
299 { kSecPolicyAppleEscrowService
, &CSSMOID_APPLE_TP_ESCROW_SERVICE
},
300 { kSecPolicyAppleProfileSigner
, &CSSMOID_APPLE_TP_PROFILE_SIGNING
},
301 { kSecPolicyAppleQAProfileSigner
, &CSSMOID_APPLE_TP_QA_PROFILE_SIGNING
},
302 { kSecPolicyAppleTestMobileStore
, &CSSMOID_APPLE_TP_TEST_MOBILE_STORE
},
304 unsigned int i
, oidmaplen
= sizeof(oidmap
) / sizeof(oidmap_entry_t
);
305 for (i
=0; i
<oidmaplen
; i
++) {
306 CFStringRef str
= (CFStringRef
) oidmap
[i
].oidstr
;
307 if (CFStringCompare(str
, oidStr
, 0) == kCFCompareEqualTo
) {
308 oidPtr
= (CSSM_OID
*)oidmap
[i
].oidptr
;
313 SecPolicySearchRef policySearch
= NULL
;
314 OSStatus status
= SecPolicySearchCreate(CSSM_CERT_X_509v3
, oidPtr
, NULL
, &policySearch
);
315 if (!status
&& policySearch
) {
316 status
= SecPolicySearchCopyNext(policySearch
, &policy
);
317 CFRelease(policySearch
);
319 if (!policy
&& CFEqual(policyOID
, kSecPolicyAppleRevocation
)) {
320 policy
= SecPolicyCreateRevocation(kSecRevocationUseAnyAvailableMethod
);
323 policy
= SecPolicyCreateWithSecAsn1Oid((SecAsn1Oid
*)oidPtr
);
331 SecPolicyCreateWithProperties(CFTypeRef policyIdentifier
, CFDictionaryRef properties
)
333 SecPolicyRef policy
= SecPolicyCreateWithOID(policyIdentifier
);
334 SecPolicySetProperties(policy
, properties
);
341 SecPolicyCreateRevocation(CFOptionFlags revocationFlags
)
343 // return a SecPolicyRef object for the unified revocation policy
344 SecAsn1Oid
*oidPtr
= (SecAsn1Oid
*)&CSSMOID_APPLE_TP_REVOCATION
;
345 SecPolicyRef policy
= SecPolicyCreateWithSecAsn1Oid(oidPtr
);
346 //%%% FIXME set policy value with revocationFlags
351 /* new in 10.9 ***FIXME*** TO BE REMOVED */
352 CFArrayRef
SecPolicyCopyEscrowRootCertificates(void)
354 return SecCertificateCopyEscrowRoots(kSecCertificateProductionEscrowRoot
);