2 * Copyright (c) 2002-2010 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@
26 The functions and data types in SecTrust implement trust computation
27 and allow the caller to apply trust decisions to the evaluation.
30 #ifndef _SECURITY_SECTRUST_H_
31 #define _SECURITY_SECTRUST_H_
33 #include <Security/SecBase.h>
34 #include <Security/cssmtype.h>
35 #include <Security/cssmapple.h>
36 #include <CoreFoundation/CoreFoundation.h>
37 #include <AvailabilityMacros.h>
40 #if defined(__cplusplus)
45 @typedef SecTrustResultType
46 @abstract Specifies the trust result type.
47 @constant kSecTrustResultInvalid Indicates an invalid setting or result.
48 @constant kSecTrustResultProceed Indicates you may proceed. This value
49 may be returned by the SecTrustEvaluate function or stored as part of
50 the user trust settings.
51 @constant kSecTrustResultConfirm Indicates confirmation with the user
52 is required before proceeding. This value may be returned by the
53 SecTrustEvaluate function or stored as part of the user trust settings.
54 @constant kSecTrustResultDeny Indicates a user-configured deny; do not
55 proceed. This value may be returned by the SecTrustEvaluate function
56 or stored as part of the user trust settings.
57 @constant kSecTrustResultUnspecified Indicates user intent is unknown.
58 This value may be returned by the SecTrustEvaluate function or stored
59 as part of the user trust settings.
60 @constant kSecTrustResultRecoverableTrustFailure Indicates a trust
61 framework failure; retry after fixing inputs. This value may be returned
62 by the SecTrustEvaluate function but not stored as part of the user
64 @constant kSecTrustResultFatalTrustFailure Indicates a trust framework
65 failure; no "easy" fix. This value may be returned by the
66 SecTrustEvaluate function but not stored as part of the user trust
68 @constant kSecTrustResultOtherError Indicates a failure other than that
69 of trust evaluation. This value may be returned by the SecTrustEvaluate
70 function but not stored as part of the user trust settings.
72 typedef uint32_t SecTrustResultType
;
74 kSecTrustResultInvalid
,
75 kSecTrustResultProceed
,
76 kSecTrustResultConfirm
,
78 kSecTrustResultUnspecified
,
79 kSecTrustResultRecoverableTrustFailure
,
80 kSecTrustResultFatalTrustFailure
,
81 kSecTrustResultOtherError
85 @typedef SecTrustUserSetting
86 @abstract Specifies user-specified trust settings.
88 typedef SecTrustResultType SecTrustUserSetting
;
91 @typedef SecTrustOptionFlags
92 @abstract Options for customizing trust evaluation.
93 @constant kSecTrustOptionAllowExpired Allow expired certs.
94 @constant kSecTrustOptionLeafIsCA Allow CA as leaf certificate.
95 @constant kSecTrustOptionFetchIssuerFromNet Allow network fetch of CA cert.
96 @constant kSecTrustOptionAllowExpiredRoot Allow expired roots.
97 @constant kSecTrustOptionRequireRevPerCert Require positive revocation
98 check per certificate.
99 @constant kSecTrustOptionUseTrustSettings Use TrustSettings instead of
101 @constant kSecTrustOptionImplicitAnchors Properly self-signed certs are
102 treated as anchors implicitly.
104 typedef uint32_t SecTrustOptionFlags
;
106 kSecTrustOptionAllowExpired
= 0x00000001,
107 kSecTrustOptionLeafIsCA
= 0x00000002,
108 kSecTrustOptionFetchIssuerFromNet
= 0x00000004,
109 kSecTrustOptionAllowExpiredRoot
= 0x00000008,
110 kSecTrustOptionRequireRevPerCert
= 0x00000010,
111 kSecTrustOptionUseTrustSettings
= 0x00000020,
112 kSecTrustOptionImplicitAnchors
= 0x00000040
116 @enum Trust Property Constants
117 @discussion Predefined property key constants used to obtain values in
118 a dictionary of trust evaluation results.
119 @constant kSecPropertyTypeTitle Specifies a key whose value is a
120 CFStringRef containing the title (display name) of this certificate.
121 @constant kSecPropertyTypeError Specifies a key whose value is a
122 CFStringRef containing the reason for a trust evaluation failure.
124 extern CFTypeRef kSecPropertyTypeTitle
125 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
);
126 extern CFTypeRef kSecPropertyTypeError
127 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
);
132 @abstract A pointer to an opaque trust management structure.
134 typedef struct OpaqueSecTrustRef
*SecTrustRef
;
138 @typedef SecTrustCallback
139 @abstract Delivers the result from an asynchronous trust evaluation.
140 @param trustRef A reference to the trust object which has been evaluated.
141 @param trustResult The trust result of the evaluation. Additional status
142 information can be obtained by calling SecTrustCopyProperties().
144 typedef void (^SecTrustCallback
)(SecTrustRef trustRef
, SecTrustResultType trustResult
);
148 @function SecTrustGetTypeID
149 @abstract Returns the type identifier of SecTrust instances.
150 @result The CFTypeID of SecTrust instances.
152 CFTypeID
SecTrustGetTypeID(void)
153 __OSX_AVAILABLE_STARTING(__MAC_10_3
, __IPHONE_2_0
);
156 @function SecTrustCreateWithCertificates
157 @abstract Creates a trust object based on the given certificates and
159 @param certificates The group of certificates to verify.
160 @param policies An array of one or more policies. You may pass a
161 SecPolicyRef to represent a single policy.
162 @param trustRef On return, a pointer to the trust management reference.
163 @result A result code. See "Security Error Codes" (SecBase.h).
164 @discussion If multiple policies are passed in, all policies must verify
165 for the chain to be considered valid.
167 OSStatus
SecTrustCreateWithCertificates(CFArrayRef certificates
,
168 CFTypeRef policies
, SecTrustRef
*trustRef
)
169 __OSX_AVAILABLE_STARTING(__MAC_10_3
, __IPHONE_2_0
);
173 @function SecTrustSetPolicies
174 @abstract Set (replace) the set of policies to evaluate.
175 @param trust The reference to the trust to change.
176 @param policies An array of one or more policies. You may pass a
177 SecPolicyRef to represent a single policy.
178 @result A result code. See "Security Error Codes" (SecBase.h).
180 OSStatus
SecTrustSetPolicies(SecTrustRef trust
, CFTypeRef policies
)
181 __OSX_AVAILABLE_STARTING(__MAC_10_3
, __IPHONE_NA
);
184 @function SecTrustSetOptions
185 @abstract Sets optional flags and data for customizing a trust evaluation.
186 @param trustRef The reference to the trust to change.
187 @param options Flags to change evaluation behavior for this trust.
188 @result A result code. See "Security Error Codes" (SecBase.h).
190 OSStatus
SecTrustSetOptions(SecTrustRef trustRef
, SecTrustOptionFlags options
)
191 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
);
194 @function SecTrustSetParameters
195 @abstract Sets the action and action data for a trust object.
196 @param trustRef The reference to the trust to change.
197 @param action A trust action.
198 @param actionData A reference to data associated with this action.
199 @result A result code. See "Security Error Codes" (SecBase.h).
200 @discussion This function is deprecated in Mac OS X 10.7 and later;
201 use SecTrustSetOptions instead.
203 OSStatus
SecTrustSetParameters(SecTrustRef trustRef
,
204 CSSM_TP_ACTION action
, CFDataRef actionData
)
205 DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
;
208 @function SecTrustSetAnchorCertificates
209 @abstract Sets the anchor certificates for a given trust.
210 @param trust A reference to a trust object.
211 @param anchorCertificates An array of anchor certificates.
212 @result A result code. See "Security Error Codes" (SecBase.h).
213 @discussion Calling this function without also calling
214 SecTrustSetAnchorCertificatesOnly() will disable trusting any
215 anchors other than the ones in anchorCertificates.
217 OSStatus
SecTrustSetAnchorCertificates(SecTrustRef trust
,
218 CFArrayRef anchorCertificates
)
219 __OSX_AVAILABLE_STARTING(__MAC_10_3
, __IPHONE_2_0
);
222 @function SecTrustSetAnchorCertificatesOnly
223 @abstract Reenables trusting anchor certificates in addition to those
224 passed in via the SecTrustSetAnchorCertificates API.
225 @param trust A reference to a trust object.
226 @param anchorCertificatesOnly If true, disables trusting any anchors other
227 than the ones passed in via SecTrustSetAnchorCertificates(). If false,
228 the built in anchor certificates are also trusted.
229 @result A result code. See "Security Error Codes" (SecBase.h).
231 OSStatus
SecTrustSetAnchorCertificatesOnly(SecTrustRef trust
,
232 Boolean anchorCertificatesOnly
)
233 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_2_0
);
236 @function SecTrustSetKeychains
237 @abstract Sets the keychains for a given trust object.
238 @param trust A reference to a trust object.
239 @param keychainOrArray A reference to an array of keychains to search, a
240 single keychain, or NULL to use the default keychain search list.
241 @result A result code. See "Security Error Codes" (SecBase.h).
242 @discussion By default, the user's keychain search list and the system
243 anchors keychain are searched for certificates to complete the chain. You
244 can specify a zero-element array if you do not want any keychains searched.
246 OSStatus
SecTrustSetKeychains(SecTrustRef trust
, CFTypeRef keychainOrArray
)
247 __OSX_AVAILABLE_STARTING(__MAC_10_3
, __IPHONE_NA
);
250 @function SecTrustSetVerifyDate
251 @abstract Set the date for which the trust should be verified.
252 @param trust A reference to a trust object.
253 @param verifyDate The date for which to verify trust.
254 @result A result code. See "Security Error Codes" (SecBase.h).
255 @discussion This function lets you evaluate certificate validity for a
256 given date (for example, to determine if a signature was valid on the date
257 it was signed, even if the certificate has since expired.) If this function
258 is not called, the time at which SecTrustEvaluate() is called is used
259 implicitly as the verification time.
261 OSStatus
SecTrustSetVerifyDate(SecTrustRef trust
, CFDateRef verifyDate
)
262 __OSX_AVAILABLE_STARTING(__MAC_10_3
, __IPHONE_2_0
);
265 @function SecTrustGetVerifyTime
266 @abstract Returns the verify time.
267 @param trust A reference to the trust object being verified.
268 @result A CFAbsoluteTime value representing the time at which certificates
269 should be checked for validity.
270 @discussion This function retrieves the verification time for the given
271 trust reference, as set by a prior call to SecTrustSetVerifyDate(). If the
272 verification time has not been set, this function returns a value of 0,
273 indicating that the current date/time is implicitly used for verification.
275 CFAbsoluteTime
SecTrustGetVerifyTime(SecTrustRef trust
)
276 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_2_0
);
279 @function SecTrustEvaluate
280 @abstract Evaluates a trust reference synchronously.
281 @param trust A reference to the trust object to evaluate.
282 @param result A pointer to a result type.
283 @result A result code. See "Security Error Codes" (SecBase.h).
284 @discussion This function will completely evaluate trust before returning,
285 possibly including network access to fetch intermediate certificates or to
286 perform revocation checking. Since this function can block during those
287 operations, you should call it from within a function that is placed on a
288 dispatch queue, or in a separate thread from your application's main
289 run loop. Alternatively, you can use the SecTrustEvaluateAsync() function
290 in Mac OS X 10.7 and later.
292 OSStatus
SecTrustEvaluate(SecTrustRef trust
, SecTrustResultType
*result
)
293 __OSX_AVAILABLE_STARTING(__MAC_10_3
, __IPHONE_2_0
);
297 @function SecTrustEvaluateAsync
298 @abstract Evaluates a trust reference asynchronously.
299 @param trust A reference to the trust object to evaluate.
300 @param queue A dispatch queue on which the result callback should be
301 executed. Pass NULL to use the current dispatch queue.
302 @param result A SecTrustCallback block which will be executed when the
303 trust evaluation is complete.
304 @result A result code. See "Security Error Codes" (SecBase.h).
306 OSStatus
SecTrustEvaluateAsync(SecTrustRef trust
,
307 dispatch_queue_t queue
, SecTrustCallback result
)
308 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
);
312 @function SecTrustGetResult
313 @abstract Returns detailed information on the outcome of an evaluation.
314 @param trustRef A reference to a trust object.
315 @param result A pointer to the result from the call to SecTrustEvaluate.
316 @param certChain On return, a pointer to the certificate chain used to
317 validate the input certificate. Call the CFRelease function to release
319 @param statusChain On return, a pointer to the status of the certificate
320 chain. Do not attempt to free this pointer; it remains valid until the
321 trust is destroyed or the next call to SecTrustEvaluate.
322 @result A result code. See "Security Error Codes" (SecBase.h).
323 @discussion This function is deprecated in Mac OS X 10.7 and later.
324 To get the complete certificate chain, use SecTrustGetCertificateCount and
325 SecTrustGetCertificateAtIndex. To get detailed status information for each
326 certificate, use SecTrustCopyProperties. To get the overall trust result
327 for the evaluation, use SecTrustGetTrustResult.
329 OSStatus
SecTrustGetResult(SecTrustRef trustRef
, SecTrustResultType
*result
,
330 CFArrayRef
*certChain
, CSSM_TP_APPLE_EVIDENCE_INFO
**statusChain
)
331 DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
;
334 @function SecTrustGetCssmResult
335 @abstract Gets the CSSM trust result.
336 @param trust A reference to a trust.
337 @param result On return, a pointer to the CSSM trust result.
338 @result A result code. See "Security Error Codes" (SecBase.h).
339 @discussion This function is deprecated in Mac OS X 10.7 and later.
340 To get detailed status information for each certificate, use
341 SecTrustCopyProperties. To get the overall trust result for the evaluation,
342 use SecTrustGetTrustResult.
344 OSStatus
SecTrustGetCssmResult(SecTrustRef trust
,
345 CSSM_TP_VERIFY_CONTEXT_RESULT_PTR
*result
)
346 DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
;
349 @function SecTrustGetCssmResultCode
350 @abstract Gets the result code from the most recent call to SecTrustEvaluate
351 for the specified trust.
352 @param trust A reference to a trust.
353 @param resultCode On return, the result code produced by the most recent
354 evaluation of the given trust (cssmerr.h). The value of resultCode is
355 undefined if SecTrustEvaluate has not been called.
356 @result A result code. See "Security Error Codes" (SecBase.h). Returns
357 errSecTrustNotAvailable if SecTrustEvaluate has not been called for the
359 @discussion This function is deprecated in Mac OS X 10.7 and later.
360 To get detailed status information for each certificate, use
361 SecTrustCopyProperties. To get the overall trust result for the evaluation,
362 use SecTrustGetTrustResult.
364 OSStatus
SecTrustGetCssmResultCode(SecTrustRef trust
, OSStatus
*resultCode
)
365 DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
;
368 @function SecTrustGetTrustResult
369 @param trustRef A reference to a trust object.
370 @param result A pointer to the result from the most recent call to
371 SecTrustEvaluate for this trust reference. If SecTrustEvaluate has not been
372 called, the result is kSecTrustResultInvalid.
373 @result A result code. See "Security Error Codes" (SecBase.h).
374 @discussion This function replaces SecTrustGetResult for the purpose of
375 obtaining the current evaluation result of a given trust reference.
377 OSStatus
SecTrustGetTrustResult(SecTrustRef trustRef
,
378 SecTrustResultType
*result
)
379 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
);
382 @function SecTrustGetTPHandle
383 @abstract Gets the CSSM trust handle
384 @param trust A reference to a trust.
385 @param handle On return, a CSSM trust handle.
386 @result A result code. See "Security Error Codes" (SecBase.h).
387 @discussion This function is deprecated in Mac OS X 10.7 and later.
389 OSStatus
SecTrustGetTPHandle(SecTrustRef trust
, CSSM_TP_HANDLE
*handle
)
390 DEPRECATED_IN_MAC_OS_X_VERSION_10_7_AND_LATER
;
393 @function SecTrustCopyPolicies
394 @abstract Returns an array of policies used by a given trust.
395 @param trust A reference to a trust object.
396 @param policies On return, an array of policies used by this trust.
397 Call the CFRelease function to release this reference.
398 @result A result code. See "Security Error Codes" (SecBase.h).
400 OSStatus
SecTrustCopyPolicies(SecTrustRef trust
, CFArrayRef
*policies
)
401 __OSX_AVAILABLE_STARTING(__MAC_10_3
, __IPHONE_NA
);
404 @function SecTrustCopyCustomAnchorCertificates
405 @abstract Returns an array of custom anchor certificates used by a given
406 trust, as set by a prior call to SecTrustSetAnchorCertificates, or NULL if
407 no custom anchors have been specified.
408 @param trust A reference to a trust.
409 @param anchors On return, an array of custom anchor certificates (roots)
410 used by this trust, or NULL if no custom anchors have been specified. Call
411 the CFRelease function to release this reference.
412 @result A result code. See "Security Error Codes" (SecBase.h).
414 OSStatus
SecTrustCopyCustomAnchorCertificates(SecTrustRef trust
,
416 __OSX_AVAILABLE_STARTING(__MAC_10_5
, __IPHONE_NA
);
419 @function SecTrustCopyAnchorCertificates
420 @abstract Returns an array of default anchor (root) certificates used by
422 @param anchors On return, an array containing the system's default anchors
423 (roots). Call the CFRelease function to release this pointer.
424 @result A result code. See "Security Error Codes" (SecBase.h).
426 OSStatus
SecTrustCopyAnchorCertificates(CFArrayRef
*anchors
)
427 __OSX_AVAILABLE_STARTING(__MAC_10_3
, __IPHONE_NA
);
430 @function SecTrustCopyPublicKey
431 @abstract Return the public key for the leaf certificate after trust has
433 @param trust A reference to the trust object which has been evaluated.
434 @result The certificate's public key, or NULL if it the public key could
435 not be extracted (this can happen with DSA certificate chains if the
436 parameters in the chain cannot be found). The caller is responsible
437 for calling CFRelease on the returned key when it is no longer needed.
439 SecKeyRef
SecTrustCopyPublicKey(SecTrustRef trust
)
440 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_2_0
);
443 @function SecTrustGetCertificateCount
444 @abstract Returns the number of certificates in an evaluated certificate
446 @param trust Reference to a trust object.
447 @result The number of certificates in the trust chain. This function will
448 return 1 if the trust has not been evaluated, and the number of
449 certificates in the chain including the anchor if it has.
451 CFIndex
SecTrustGetCertificateCount(SecTrustRef trust
)
452 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_2_0
);
455 @function SecTrustGetCertificateAtIndex
456 @abstract Returns a certificate from the trust chain.
457 @param trust Reference to a trust object.
458 @param ix The index of the requested certificate. Indices run from 0
459 (leaf) to the anchor (or last certificate found if no anchor was found).
460 The leaf cert (index 0) is always present regardless of whether the trust
461 reference has been evaluated or not.
462 @result A SecCertificateRef for the requested certificate.
464 SecCertificateRef
SecTrustGetCertificateAtIndex(SecTrustRef trust
, CFIndex ix
)
465 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_2_0
);
468 @function SecTrustCopyProperties
469 @abstract Return a property array for this trust evaluation.
470 @param trust A reference to the trust object. If the trust has not been
471 evaluated, the returned property array will be empty.
472 @result A property array. It is the caller's responsibility to CFRelease
473 the returned array when it is no longer needed.
474 @discussion This function returns an ordered array of CFDictionaryRef
475 instances for each certificate in the chain. Indices run from 0 (leaf) to
476 the anchor (or last certificate found if no anchor was found). The property
477 at index 0 of the array may also include general information about the
478 entire chain's validity in the context of this trust evaluation. See the
479 "Trust Property Constants" section for a list of currently defined keys.
481 CFArrayRef
SecTrustCopyProperties(SecTrustRef trust
)
482 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_2_0
);
485 #if defined(__cplusplus)
489 #endif /* !_SECURITY_SECTRUST_H_ */