2 * Copyright (c) 2007-2011,2013-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.h
28 #ifndef _SECURITY_SECCERTIFICATEINTERNAL_H_
29 #define _SECURITY_SECCERTIFICATEINTERNAL_H_
31 //#include <Security/SecCertificatePrivP.h>
32 #include "SecCertificatePrivP.h"
33 #include "certextensionsP.h"
34 #include <libDER/DER_Keys.h>
36 #if defined(__cplusplus)
40 CFDataRef
SecCertificateGetAuthorityKeyIDP(SecCertificateRefP certificate
);
41 CFDataRef
SecCertificateGetSubjectKeyIDP(SecCertificateRefP certificate
);
43 /* Return an array of CFURLRefs each of which is an crl distribution point for
45 CFArrayRef
SecCertificateGetCRLDistributionPointsP(SecCertificateRefP certificate
);
47 /* Return an array of CFURLRefs each of which is an ocspResponder for this
49 CFArrayRef
SecCertificateGetOCSPRespondersP(SecCertificateRefP certificate
);
51 /* Return an array of CFURLRefs each of which is an caIssuer for this
53 CFArrayRef
SecCertificateGetCAIssuersP(SecCertificateRefP certificate
);
55 /* Dump certificate for debugging. */
56 void SecCertificateShowP(SecCertificateRefP certificate
);
58 /* Return the DER encoded issuer sequence for the receiving certificates issuer. */
59 CFDataRef
SecCertificateCopyIssuerSequenceP(SecCertificateRefP certificate
);
61 /* Return the DER encoded subject sequence for the receiving certificates subject. */
62 CFDataRef
SecCertificateCopySubjectSequenceP(SecCertificateRefP certificate
);
64 /* Return the content of a DER encoded X.501 name (without the tag and length
65 fields) for the receiving certificates issuer. */
66 CFDataRef
SecCertificateGetNormalizedIssuerContentP(SecCertificateRefP certificate
);
68 /* Return the content of a DER encoded X.501 name (without the tag and length
69 fields) for the receiving certificates subject. */
70 CFDataRef
SecCertificateGetNormalizedSubjectContentP(SecCertificateRefP certificate
);
72 CFDataRef
SecDERItemCopySequenceP(DERItem
*content
);
74 /* Return true iff the certificate has a subject. */
75 bool SecCertificateHasSubjectP(SecCertificateRefP certificate
);
77 /* Return true iff the certificate has a critical subject alt name. */
78 bool SecCertificateHasCriticalSubjectAltNameP(SecCertificateRefP certificate
);
80 /* Return true if certificate contains one or more critical extensions we
81 are unable to parse. */
82 bool SecCertificateHasUnknownCriticalExtensionP(SecCertificateRefP certificate
);
84 /* Return true iff certificate is valid as of verifyTime. */
85 bool SecCertificateIsValidP(SecCertificateRefP certificate
,
86 CFAbsoluteTime verifyTime
);
88 /* Return an attribute dictionary used to store this item in a keychain. */
89 CFDictionaryRef
SecCertificateCopyAttributeDictionaryP(
90 SecCertificateRefP certificate
);
92 /* Return a certificate from the attribute dictionary that was used to store
93 this item in a keychain. */
94 SecCertificateRefP
SecCertificateCreateFromAttributeDictionaryP(
95 CFDictionaryRef refAttributes
);
97 /* Return a SecKeyRef for the public key embedded in the cert. */
98 SecKeyRefP
SecCertificateCopyPublicKeyP(SecCertificateRefP certificate
);
100 /* Return the SecCEBasicConstraints extension for this certificate if it
102 const SecCEBasicConstraints
*
103 SecCertificateGetBasicConstraintsP(SecCertificateRefP certificate
);
105 /* Return the SecCEPolicyConstraints extension for this certificate if it
107 const SecCEPolicyConstraints
*
108 SecCertificateGetPolicyConstraintsP(SecCertificateRefP certificate
);
110 /* Return a dictionary from CFDataRef to CFArrayRef of CFDataRef
111 representing the policyMapping extension of this certificate. */
113 SecCertificateGetPolicyMappingsP(SecCertificateRefP certificate
);
115 /* Return the SecCECertificatePolicies extension for this certificate if it
117 const SecCECertificatePolicies
*
118 SecCertificateGetCertificatePoliciesP(SecCertificateRefP certificate
);
120 /* Returns UINT32_MAX if InhibitAnyPolicy extension is not present or invalid,
121 returns the value of the SkipCerts field of the InhibitAnyPolicy extension
124 SecCertificateGetInhibitAnyPolicySkipCertsP(SecCertificateRefP certificate
);
126 /* Return the public key algorithm and parameters for certificate. */
127 const DERAlgorithmId
*SecCertificateGetPublicKeyAlgorithmP(
128 SecCertificateRefP certificate
);
130 /* Return the raw public key data for certificate. */
131 const DERItem
*SecCertificateGetPublicKeyDataP(SecCertificateRefP certificate
);
134 #pragma mark Certificate Operations
136 OSStatus
SecCertificateIsSignedByP(SecCertificateRefP certificate
,
137 SecKeyRefP issuerKey
);
140 #pragma mark Certificate Creation
142 #ifdef OPTIONAL_METHODS
143 /* Return a certificate for the PEM representation of this certificate.
144 Return NULL the passed in der_certificate is not a valid DER encoded X.509
145 certificate, and return a CFError by reference. It is the
146 responsibility of the caller to release the CFError. */
147 SecCertificateRefP
SecCertificateCreateWithPEMP(CFAllocatorRef allocator
,
148 CFStringRef pem_certificate
);
150 /* Return a CFStringRef containing the the pem representation of this
152 CFStringRef
SecCertificateGetPEMP(SecCertificateRefP der_certificate
);
154 #endif /* OPTIONAL_METHODS */
157 /* Complete the certificate chain of this certificate, setting the parent
158 certificate for each certificate along they way. Return 0 if the
159 system is able to find all the certificates to complete the certificate
160 chain either in the passed in other_certificates array or in the user or
161 the systems keychain(s).
162 If the certificate's issuer chain can not be completed, this function
163 will return an error status code.
164 NOTE: This function does not verify whether the certificate is trusted it's
165 main use is just to ensure that anyone using this certificate upstream will
166 have access to a complete (or as complete as possible in the case of
167 something going wrong) certificate chain. */
168 OSStatus
SecCertificateCompleteChainP(SecCertificateRefP certificate
,
169 CFArrayRef other_certificates
);
175 @function SecCertificateGetVersionNumberP
176 @abstract Retrieves the version of a given certificate as a CFNumberRef.
177 @param certificate A reference to the certificate from which to obtain the certificate version.
178 @result A CFNumberRef representing the certificate version. The following values are currently known to be returned, but more may be added in the future:
183 CFNumberRef
SecCertificateGetVersionNumberP(SecCertificateRefP certificate
);
186 @function SecCertificateGetSerialDERP
187 @abstract Retrieves the serial number of a given certificate in DER encoding.
188 @param certificate A reference to the certificate from which to obtain the serial number.
189 @result A CFDataRef containing the DER encoded serial number of the certificate, minus the tag and length fields.
191 CFDataRef
SecCertificateGetSerialDERP(SecCertificateRefP certificate
);
195 @function SecCertificateGetSerialStringP
196 @abstract Retrieves the serial number of a given certificate in human readable form.
197 @param certificate A reference to the certificate from which to obtain the serial number.
198 @result A CFStringRef containing the human readable serial number of the certificate in decimal form.
200 CFStringRef
SecCertificateGetSerialStringP(SecCertificateRefP certificate
);
204 CFDataRef
SecCertificateGetPublicKeyDERP(SecCertificateRefP certificate
);
205 CFDataRef
SecCertificateGetPublicKeySHA1FingerPrintP(SecCertificateRefP certificate
);
206 CFDataRef
SecCertificateGetPublicKeyMD5FingerPrintP(SecCertificateRefP certificate
);
207 CFDataRef
SecCertificateGetSignatureAlgorithmDERP(SecCertificateRefP certificate
);
208 CFDataRef
SecCertificateGetSignatureAlgorithmNameP(SecCertificateRefP certificate
);
209 CFStringRef
SecCertificateGetSignatureAlgorithmOIDP(SecCertificateRefP certificate
);
210 CFDataRef
SecCertificateGetSignatureDERP(SecCertificateRefP certificate
);
211 CFDataRef
SecCertificateGetSignatureAlgorithmParametersDERP(SecCertificateRefP certificate
);
213 /* plist top level array is orderd list of key/value pairs */
214 CFArrayRef
SecCertificateGetSignatureAlgorithmParametersArrayP(SecCertificateRefP certificate
);
217 /* This cert is signed by its parent? */
218 bool SecCertificateIsSignatureValidP(SecCertificateRefP certificate
);
220 /* This cert is signed by its parent and so on until no parent certificate can be found? */
221 bool SecCertificateIsIssuerChainValidP(SecCertificateRefP certificate
, CFArrayRef additionalCertificatesToSearch
);
223 /* This cert is signed by its parent and so on until no parent certificate can be found? */
224 bool SecCertificateIsSignatureChainValidP(SecCertificateRefP certificate
);
226 /* This cert is signed by its parent and so on until a certiicate in anchors can be found. */
227 bool SecCertificateIssuerChainHasAnchorInP(SecCertificateRefP certificate
, CFArrayRef anchors
);
229 /* This cert is signed by its parent and so on until a certiicate in anchors can be found. */
230 bool SecCertificateSignatureChainHasAnchorInP(SecCertificateRefP certificate
, CFArrayRef anchors
);
232 bool SecCertificateIsSelfSignedP(SecCertificateRefP certificate
);
236 /* The entire certificate in DER encoding including the outer tag and length fields. */
237 CFDataRef
SecCertificateGetDERP(SecCertificateRefP certificate
);
239 /* Returns the status code of the last failed call for this certificate on this thread. */
240 OSStatus
SecCertificateGetStatusP(SecCertificateRefP certificate
);
242 CFDataRef
SecCertificateGetIssuerDERP(SecCertificateRefP certificate
);
243 CFDataRef
SecCertificateGetNormalizedIssuerDERP(SecCertificateRefP certificate
);
245 /* 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. */
246 CFArrayRef
SecCertificateGetIssuerArrayP(SecCertificateRefP certificate
);
249 CFDataRef
SecCertificateGetSubjectDERP(SecCertificateRefP certificate
);
250 CFDataRef
SecCertificateGetNormalizedSubjectDERP(SecCertificateRefP certificate
);
251 /* See SecCertificateGetIssuerArray for a description of the returned array. */
252 CFArrayRef
SecCertificateGetSubjectArrayP(SecCertificateRefP certificate
);
254 CFDateRef
SecCertificateGetNotValidBeforeDateP(SecCertificateRefP certificate
);
255 CFDateRef
SecCertificateGetNotValidDateP(SecCertificateRefP certificate
);
260 CFIndex
SecCertificateGetExtensionCountP(SecCertificateRefP certificate
, index
);
261 CFDataRef
SecCertificateGetExtensionAtIndexDERP(SecCertificateRefP certificate
, CFIndex index
);
262 bool SecCertificateIsExtensionAtIndexCriticalP(SecCertificateRefP certificate
, CFIndex index
);
264 /* array see email example. */
265 CFArrayRef
SecCertificateGetExtensionAtIndexParamsArrayP(SecCertificateRefP certificate
, CFIndex index
);
267 CFStringRef
SecCertificateGetExtensionAtIndexNameP(SecCertificateRefP certificate
, CFIndex index
);
268 CFStringRef
SecCertificateGetExtensionAtIndexOIDP(SecCertificateRefP certificate
, CFIndex index
);
272 /* Return an array with all of this certificates SecCertificateExtensionRefs. */
273 CFArrayRef
SecCertificateGetExtensionsP(SecCertificateRefP certificate
);
275 /* 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. */
276 SecCertificateExtensionRef
SecCertificateGetExtensionWithOIDP(SecCertificateRefP certificate
, CFDataRef oid
);
278 CFDataRef
SecCertificateExtensionGetDERP(SecCertificateExtensionRef extension
, CFDataRef oid
);
279 CFStringRef
SecCertificateExtensionNameP(SecCertificateExtensionRef extension
);
280 CFDataRef
SecCertificateExtensionGetOIDDERP(SecCertificateExtensionRef extension
, CFDataRef oid
);
281 CFStringRef
SecCertificateExtensionGetOIDStringP(SecCertificateExtensionRef extension
, CFDataRef oid
);
282 bool SecCertificateExtensionIsCriticalP(SecCertificateExtensionRef extension
);
283 CFArrayRef
SecCertificateExtensionGetContentDERP(SecCertificateExtensionRef extension
);
285 /* 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. */
286 CFArrayRef
SecCertificateExtensionGetContentArrayP(SecCertificateExtensionRef extension
);
293 void appendPropertyP(CFMutableArrayRef properties
,
294 CFStringRef propertyType
, CFStringRef label
, CFTypeRef value
);
296 /* Utility functions. */
297 CFStringRef
SecDERItemCopyOIDDecimalRepresentation(CFAllocatorRef allocator
,
299 CFDataRef
createNormalizedX501Name(CFAllocatorRef allocator
,
300 const DERItem
*x501name
);
302 /* Decode a choice of UTCTime or GeneralizedTime to a CFAbsoluteTime. Return
303 an absoluteTime if the date was valid and properly decoded. Return
304 NULL_TIME otherwise. */
305 CFAbsoluteTime
SecAbsoluteTimeFromDateContent(DERTag tag
, const uint8_t *bytes
,
308 #if defined(__cplusplus)
312 #endif /* !_SECURITY_SECCERTIFICATEINTERNAL_H_ */