2 * Copyright (c) 2007-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@
25 SecCertificateInternal
28 #ifndef _SECURITY_SECCERTIFICATEINTERNAL_H_
29 #define _SECURITY_SECCERTIFICATEINTERNAL_H_
31 #include <Security/SecCertificatePriv.h>
32 #include <Security/certextensions.h>
33 #include <libDER/DER_Keys.h>
37 CFDataRef
SecCertificateGetAuthorityKeyID(SecCertificateRef certificate
);
38 CFDataRef
SecCertificateGetSubjectKeyID(SecCertificateRef certificate
);
40 /* Return an array of CFURLRefs each of which is an crl distribution point for
42 CFArrayRef
SecCertificateGetCRLDistributionPoints(SecCertificateRef certificate
);
44 /* Return an array of CFURLRefs each of which is an ocspResponder for this
46 CFArrayRef
SecCertificateGetOCSPResponders(SecCertificateRef certificate
);
48 /* Return an array of CFURLRefs each of which is an caIssuer for this
50 CFArrayRef
SecCertificateGetCAIssuers(SecCertificateRef certificate
);
52 /* Dump certificate for debugging. */
53 void SecCertificateShow(SecCertificateRef certificate
);
55 /* Return the DER encoded issuer sequence for the receiving certificates issuer. */
56 CFDataRef
SecCertificateCopyIssuerSequence(SecCertificateRef certificate
);
58 /* Return the DER encoded subject sequence for the receiving certificates subject. */
59 CFDataRef
SecCertificateCopySubjectSequence(SecCertificateRef certificate
);
61 /* Return the content of a DER encoded X.501 name (without the tag and length
62 fields) for the receiving certificates issuer. */
63 CFDataRef
SecCertificateGetNormalizedIssuerContent(SecCertificateRef certificate
);
65 /* Return the content of a DER encoded X.501 name (without the tag and length
66 fields) for the receiving certificates subject. */
67 CFDataRef
SecCertificateGetNormalizedSubjectContent(SecCertificateRef certificate
);
69 /* Return the normalized name or NULL if it fails to parse */
70 CFDataRef
SecDistinguishedNameCopyNormalizedContent(CFDataRef distinguished_name
);
72 /* Return true iff the certificate has a subject. */
73 bool SecCertificateHasSubject(SecCertificateRef certificate
);
74 /* Return true iff the certificate has a critical subject alt name. */
75 bool SecCertificateHasCriticalSubjectAltName(SecCertificateRef certificate
);
77 /* Return the contents of the SubjectAltName extension. */
78 const DERItem
* SecCertificateGetSubjectAltName(SecCertificateRef certificate
);
80 /* Return true if certificate contains one or more critical extensions we
81 are unable to parse. */
82 bool SecCertificateHasUnknownCriticalExtension(SecCertificateRef certificate
);
84 /* Return true iff certificate is valid as of verifyTime. */
85 bool SecCertificateIsValid(SecCertificateRef certificate
,
86 CFAbsoluteTime verifyTime
);
88 /* Return an attribute dictionary used to store this item in a keychain. */
89 CFDictionaryRef
SecCertificateCopyAttributeDictionary(
90 SecCertificateRef certificate
);
92 /* Return a certificate from the attribute dictionary that was used to store
93 this item in a keychain. */
94 SecCertificateRef
SecCertificateCreateFromAttributeDictionary(
95 CFDictionaryRef refAttributes
);
97 /* Return a SecKeyRef for the public key embedded in the cert. */
99 SecKeyRef
SecCertificateCopyPublicKey_ios(SecCertificateRef certificate
);
101 SecKeyRef
SecCertificateCopyPublicKey(SecCertificateRef certificate
);
104 /* Return the SecCEBasicConstraints extension for this certificate if it
106 const SecCEBasicConstraints
*
107 SecCertificateGetBasicConstraints(SecCertificateRef certificate
);
109 /* Returns array of CFDataRefs containing the generalNames that are
110 Permitted Subtree Name Constraints for this certificate if it has
112 CFArrayRef
SecCertificateGetPermittedSubtrees(SecCertificateRef certificate
);
115 /* Returns array of CFDataRefs containing the generalNames that are
116 Excluded Subtree Name Constraints for this certificate if it has
118 CFArrayRef
SecCertificateGetExcludedSubtrees(SecCertificateRef certificate
);
120 /* Return the SecCEPolicyConstraints extension for this certificate if it
122 const SecCEPolicyConstraints
*
123 SecCertificateGetPolicyConstraints(SecCertificateRef certificate
);
125 /* Return a dictionary from CFDataRef to CFArrayRef of CFDataRef
126 representing the policyMapping extension of this certificate. */
128 SecCertificateGetPolicyMappings(SecCertificateRef certificate
);
130 /* Return the SecCECertificatePolicies extension for this certificate if it
132 const SecCECertificatePolicies
*
133 SecCertificateGetCertificatePolicies(SecCertificateRef certificate
);
135 /* Returns UINT32_MAX if InhibitAnyPolicy extension is not present or invalid,
136 returns the value of the SkipCerts field of the InhibitAnyPolicy extension
139 SecCertificateGetInhibitAnyPolicySkipCerts(SecCertificateRef certificate
);
141 /* Return the public key algorithm and parameters for certificate. */
142 const DERAlgorithmId
*SecCertificateGetPublicKeyAlgorithm(
143 SecCertificateRef certificate
);
145 /* Return the raw public key data for certificate. */
146 const DERItem
*SecCertificateGetPublicKeyData(SecCertificateRef certificate
);
149 // MARK: Certificate Operations
151 OSStatus
SecCertificateIsSignedBy(SecCertificateRef certificate
,
152 SecKeyRef issuerKey
);
155 // MARK: Certificate Creation
157 #ifdef OPTIONAL_METHODS
158 /* Return a certificate for the PEM representation of this certificate.
159 Return NULL the passed in der_certificate is not a valid DER encoded X.509
160 certificate, and return a CFError by reference. It is the
161 responsibility of the caller to release the CFError. */
162 SecCertificateRef
SecCertificateCreateWithPEM(CFAllocatorRef allocator
,
163 CFStringRef pem_certificate
);
165 /* Return a CFStringRef containing the the pem representation of this
167 CFStringRef
SecCertificateGetPEM(SecCertificateRef der_certificate
);
169 #endif /* OPTIONAL_METHODS */
172 /* Complete the certificate chain of this certificate, setting the parent
173 certificate for each certificate along they way. Return 0 if the
174 system is able to find all the certificates to complete the certificate
175 chain either in the passed in other_certificates array or in the user or
176 the systems keychain(s).
177 If the certifcates issuer chain can not be completed, this function
178 will return an error status code.
179 NOTE: This function does not verify whether the certificate is trusted it's
180 main use is just to ensure that anyone using this certificate upstream will
181 have access to a complete (or as complete as possible in the case of
182 something going wrong) certificate chain. */
183 OSStatus
SecCertificateCompleteChain(SecCertificateRef certificate
,
184 CFArrayRef other_certificates
);
190 @function SecCertificateGetVersionNumber
191 @abstract Retrieves the version of a given certificate as a CFNumberRef.
192 @param certificate A reference to the certificate from which to obtain the certificate version.
193 @result A CFNumberRef representing the certificate version. The following values are currently known to be returned, but more may be added in the future:
198 CFNumberRef
SecCertificateGetVersionNumber(SecCertificateRef certificate
);
201 @function SecCertificateGetSerialDER
202 @abstract Retrieves the serial number of a given certificate in DER encoding.
203 @param certificate A reference to the certificate from which to obtain the serial number.
204 @result A CFDataRef containing the DER encoded serial number of the certificate, minus the tag and length fields.
206 CFDataRef
SecCertificateGetSerialDER(SecCertificateRef certificate
);
210 @function SecCertificateGetSerialString
211 @abstract Retrieves the serial number of a given certificate in human readable form.
212 @param certificate A reference to the certificate from which to obtain the serial number.
213 @result A CFStringRef containing the human readable serial number of the certificate in decimal form.
215 CFStringRef
SecCertificateGetSerialString(SecCertificateRef certificate
);
219 CFDataRef
SecCertificateGetPublicKeyDER(SecCertificateRef certificate
);
220 CFDataRef
SecCertificateGetPublicKeySHA1FingerPrint(SecCertificateRef certificate
);
221 CFDataRef
SecCertificateGetPublicKeyMD5FingerPrint(SecCertificateRef certificate
);
222 CFDataRef
SecCertificateGetSignatureAlgorithmDER(SecCertificateRef certificate
);
223 CFDataRef
SecCertificateGetSignatureAlgorithmName(SecCertificateRef certificate
);
224 CFStringRef
SecCertificateGetSignatureAlgorithmOID(SecCertificateRef certificate
);
225 CFDataRef
SecCertificateGetSignatureDER(SecCertificateRef certificate
);
226 CFDataRef
SecCertificateGetSignatureAlgorithmParametersDER(SecCertificateRef certificate
);
228 /* plist top level array is ordered list of key/value pairs */
229 CFArrayRef
SecCertificateGetSignatureAlgorithmParametersArray(SecCertificateRef certificate
);
232 /* This cert is signed by its parent? */
233 bool SecCertificateIsSignatureValid(SecCertificateRef certificate
);
235 /* This cert is signed by its parent and so on until no parent certificate can be found? */
236 bool SecCertificateIsIssuerChainValid(SecCertificateRef certificate
, CFArrayRef additionalCertificatesToSearch
);
238 /* This cert is signed by its parent and so on until no parent certificate can be found? */
239 bool SecCertificateIsSignatureChainValid(SecCertificateRef certificate
);
241 /* This cert is signed by its parent and so on until a certificate in anchors can be found. */
242 bool SecCertificateIssuerChainHasAnchorIn(SecCertificateRef certificate
, CFArrayRef anchors
);
244 /* This cert is signed by its parent and so on until a certificate in anchors can be found. */
245 bool SecCertificateSignatureChainHasAnchorIn(SecCertificateRef certificate
, CFArrayRef anchors
);
249 /* The entire certificate in DER encoding including the outer tag and length fields. */
250 CFDataRef
SecCertificateGetDER(SecCertificateRef certificate
);
252 /* Returns the status code of the last failed call for this certificate on this thread. */
253 OSStatus
SecCertificateGetStatus(SecCertificateRef certificate
);
255 CFDataRef
SecCertificateGetIssuerDER(SecCertificateRef certificate
);
256 CFDataRef
SecCertificateGetNormalizedIssuerDER(SecCertificateRef certificate
);
258 /* Return the issuer as an X509 name encoded in an array. Each element in this array is an array. Each inner array has en even number of elements. Each pair of elements in the inner array represents a key and a value. The key is a string and the value is also a string. Elements in the outer array should be considered ordered while pairs in the inner array should not. */
259 CFArrayRef
SecCertificateGetIssuerArray(SecCertificateRef certificate
);
262 CFDataRef
SecCertificateGetSubjectDER(SecCertificateRef certificate
);
263 CFDataRef
SecCertificateGetNormalizedSubjectDER(SecCertificateRef certificate
);
264 /* See SecCertificateGetIssuerArray for a description of the returned array. */
265 CFArrayRef
SecCertificateGetSubjectArray(SecCertificateRef certificate
);
267 CFDateRef
SecCertificateGetNotValidBeforeDate(SecCertificateRef certificate
);
268 CFDateRef
SecCertificateGetNotValidDateDate(SecCertificateRef certificate
);
273 CFIndex
SecCertificateGetExtensionCount(SecCertificateRef certificate
, index
);
274 CFDataRef
SecCertificateGetExtensionAtIndexDER(SecCertificateRef certificate
, CFIndex index
);
275 bool SecCertificateIsExtensionAtIndexCritical(SecCertificateRef certificate
, CFIndex index
);
277 /* array see email example. */
278 CFArrayRef
SecCertificateGetExtensionAtIndexParamsArray(SecCertificateRef certificate
, CFIndex index
);
280 CFStringRef
SecCertificateGetExtensionAtIndexName(SecCertificateRef certificate
, CFIndex index
);
281 CFStringRef
SecCertificateGetExtensionAtIndexOID(SecCertificateRef certificate
, CFIndex index
);
285 /* Return an array with all of this certificates SecCertificateExtensionRefs. */
286 CFArrayRef
SecCertificateGetExtensions(SecCertificateRef certificate
);
288 /* Return the SecCertificateExtensionRef for the extension with the given oid. Return NULL if it does not exist or if an error occours call SecCertificateGetStatus() to see if an error occured or not. */
289 SecCertificateExtensionRef
SecCertificateGetExtensionWithOID(SecCertificateRef certificate
, CFDataRef oid
);
291 CFDataRef
SecCertificateExtensionGetDER(SecCertificateExtensionRef extension
, CFDataRef oid
);
292 CFStringRef
SecCertificateExtensionName(SecCertificateExtensionRef extension
);
293 CFDataRef
SecCertificateExtensionGetOIDDER(SecCertificateExtensionRef extension
, CFDataRef oid
);
294 CFStringRef
SecCertificateExtensionGetOIDString(SecCertificateExtensionRef extension
, CFDataRef oid
);
295 bool SecCertificateExtensionIsCritical(SecCertificateExtensionRef extension
);
296 CFArrayRef
SecCertificateExtensionGetContentDER(SecCertificateExtensionRef extension
);
298 /* Return the content of extension as an array. The array has en even number of elements. Each pair of elements in the array represents a key and a value. The key is a string and the value is either a string, or dictionary or an array of key value pairs like the outer array. */
299 CFArrayRef
SecCertificateExtensionGetContentArray(SecCertificateExtensionRef extension
);
306 void appendProperty(CFMutableArrayRef properties
, CFStringRef propertyType
,
307 CFStringRef label
, CFStringRef localizedLabel
, CFTypeRef value
);
309 /* Utility functions. */
310 CFStringRef
SecDERItemCopyOIDDecimalRepresentation(CFAllocatorRef allocator
,
312 CFDataRef
createNormalizedX501Name(CFAllocatorRef allocator
,
313 const DERItem
*x501name
);
315 /* Decode a choice of UTCTime or GeneralizedTime to a CFAbsoluteTime. Return
316 an absoluteTime if the date was valid and properly decoded. Return
317 NULL_TIME otherwise. */
318 CFAbsoluteTime
SecAbsoluteTimeFromDateContent(DERTag tag
, const uint8_t *bytes
,
321 bool SecCertificateHasMarkerExtension(SecCertificateRef certificate
, CFTypeRef oid
);
323 typedef OSStatus (*parseGeneralNameCallback
)(void *context
,
324 SecCEGeneralNameType type
, const DERItem
*value
);
325 OSStatus
SecCertificateParseGeneralNameContentProperty(DERTag tag
,
326 const DERItem
*generalNameContent
,
327 void *context
, parseGeneralNameCallback callback
);
329 OSStatus
SecCertificateParseGeneralNames(const DERItem
*generalNames
, void *context
,
330 parseGeneralNameCallback callback
);
334 #endif /* !_SECURITY_SECCERTIFICATEINTERNAL_H_ */