2 * Copyright (c) 2002-2004,2008-2009,2011-2014,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@
25 @header SecCertificateRequest
26 SecCertificateRequest implements a way to issue a certificate request to a
27 certificate authority.
30 #ifndef _SECURITY_SECCERTIFICATEREQUEST_H_
31 #define _SECURITY_SECCERTIFICATEREQUEST_H_
33 #include <Security/SecBase.h>
34 #include <Security/SecKey.h>
35 #include <Security/SecCertificatePriv.h>
38 #include <Security/cssmtype.h>
45 struct SecCertificateRequestAttribute
/* for optional oids */
50 typedef struct SecCertificateRequestAttribute SecCertificateRequestAttribute
;
52 struct SecCertificateRequestAttributeList
55 SecCertificateRequestAttribute
*attr
;
57 typedef struct SecCertificateRequestAttributeList SecCertificateRequestAttributeList
;
60 @typedef SecCertificateRequestRef
61 @abstract Contains information about a certificate request.
63 typedef struct OpaqueSecCertificateRequestRef
*SecCertificateRequestRef
;
66 @function SecCertificateRequestGetTypeID
67 Returns the type identifier of all SecCertificateRequest instances.
69 CFTypeID
SecCertificateRequestGetTypeID(void);
72 @function SecCertificateRequestCreate
74 Create a certificate request operation based on a policy and certificate
75 type. If a policy is not specified, one will be chosen for the caller.
76 Once the requeste is created, a request reference is returned.
77 To submit the request call SecCertificateRequestSubmit().
79 @param policy A policy.
80 @param certificateType The certificate type (i.e. X509, PGP, etc).
81 These types are in cssmtype.h
82 @param requestType The identifier to the type of request to submit (i.e.
83 issue, verify, revoke, etc.). These are defined in cssmtype.h
84 @param privateKeyItemRef The keychain item private key to be used for this
85 certificate request. The private key item must be of class type
86 kSecAppleKeyItemClass.
87 @param attributeList An optional list of OIDs for the certificate request.
88 @param certRequest A returned reference to the certificate request. Call CFRelease when done with this certificate request.
89 @result errSecSuccess 0 No error.
91 OSStatus
SecCertificateRequestCreate(
92 const CSSM_OID
*policy
,
93 CSSM_CERT_TYPE certificateType
,
94 CSSM_TP_AUTHORITY_REQUEST_TYPE requestType
,
95 SecKeyRef privateKeyItemRef
,
96 SecKeyRef publicKeyItemRef
,
97 const SecCertificateRequestAttributeList
* attributeList
,
98 SecCertificateRequestRef
* certRequest
);
101 @function SecCertificateRequestSubmit
103 Submit a certificate request to be processed by the Security framework.
104 Once the request is submitted, an estimated time is returned indicating
105 when the request results can be retrieved. Once the estimated time has
106 elapsed, obtain the result by calling SecCertificateRequestGetResult().
108 @param certRequest A reference to the certificate request.
109 @param estimatedTime The number of estimated seconds before the result
111 @result errSecSuccess 0 No error.
113 OSStatus
SecCertificateRequestSubmit(
114 SecCertificateRequestRef certRequest
,
115 sint32
* estimatedTime
);
118 @function SecCertificateRequestGetType
119 Returns the certificate request type (i.e. issue, revoke, etc) for a given
120 certificate request item reference.
121 @param certRequestRef A reference to a submitted request.
122 @param requestType The returned request type.
123 @result errSecSuccess 0 No error.
125 OSStatus
SecCertificateRequestGetType(
126 SecCertificateRequestRef certRequestRef
,
127 CSSM_TP_AUTHORITY_REQUEST_TYPE
* requestType
);
130 @function SecCertificateRequestGetResult
131 Get the results of a certificate request. If the request is still
132 pending, the estimated time will be returned which indicates when to
133 call this function again.
134 @param certRequestRef A reference for the submitted request.
135 @param keychain The keychain in which to store the new certificate (for
136 a new cert request) and the cert request item reference. Pass NULL
137 to specify the default keychain.
138 @param estimatedTime The number of estimated seconds before the result can
140 @param certificateRef The returned certificate reference for a
141 CSSM_TP_AUTHORITY_REQUEST_CERTISSUE only. All other request types return
142 NULL here. Call CFRelease when done with this certificate reference.
143 @result errSecSuccess 0 No error.
145 OSStatus
SecCertificateRequestGetResult(
146 SecCertificateRequestRef certRequestRef
,
147 SecKeychainRef keychain
,
148 sint32
* estimatedTime
,
149 SecCertificateRef
* certificateRef
);
152 @function SecCertificateFindRequest
153 Find a pending certificate request and return a reference object
154 for it. The search criteria is based on the input parameters.
155 @param policy A policy.
156 @param certificateType The certificate type (i.e. X509, PGP, etc).
157 These types are in cssmtype.h
158 @param requestType The identifier to the type of request to find (i.e.
159 issue, verify, revoke, etc.). These are defined in cssmtype.h
160 @param privateKeyItemRef Optional private key to be used
161 for the certificate request. Matches the same argument as passed to
162 SecCertificateRequestCreate().
163 @param publicKeyItemRef Optional public key to be used
164 for the certificate request. Matches the same argument as passed to
165 SecCertificateRequestCreate().
166 @param attributeList An optional list of OID/value pairs for finding the
168 @param certRequest A returned reference to the certificate request. Call CFRelease when done with this reference.
170 OSStatus
SecCertificateFindRequest(
171 const CSSM_OID
*policy
,
172 CSSM_CERT_TYPE certificateType
,
173 CSSM_TP_AUTHORITY_REQUEST_TYPE requestType
,
174 SecKeyRef privateKeyItemRef
,
175 SecKeyRef publicKeyItemRef
,
176 const SecCertificateRequestAttributeList
* attributeList
,
177 SecCertificateRequestRef
* certRequest
);
180 @function SecCertificateRequestGetData
181 Get policy-specific data following a SecCertificateRequestSubmit.
182 @param certRequestRef A reference for the submitted request.
183 @param data Policy-specific data.
184 @result errSecSuccess 0 No error.
187 OSStatus
SecCertificateRequestGetData(
188 SecCertificateRequestRef certRequestRef
,
194 extern const void * kSecOidCommonName
;
195 extern const void * kSecOidCountryName
;
196 extern const void * kSecOidStateProvinceName
;
197 extern const void * kSecOidLocalityName
;
198 extern const void * kSecOidOrganization
;
199 extern const void * kSecOidOrganizationalUnit
;
201 extern const unsigned char SecASN1PrintableString
;
202 extern const unsigned char SecASN1UTF8String
;
205 Parameter keys for certificate request generation:
206 @param kSecCSRChallengePassword CFStringRef
207 conversion to PrintableString or UTF8String needs to be possible.
208 @param kSecCertificateKeyUsage CFNumberRef
209 with key usage mask using kSecKeyUsage constants.
210 @param kSecSubjectAltName CFArrayRef of CFStringRef or CFDataRef
211 either dnsName or emailAddress (if contains @) or
212 ipAddress, ipv4 (4) or ipv6 (16) bytes
213 @param kSecCSRBasicContraintsPathLen CFNumberRef
214 if set will include basic constraints and mark it as
215 a CA cert. If 0 <= number < 256, specifies path length, otherwise
216 path length will be omitted. Basic contraints will always be
218 @param kSecCertificateExtensions CFDictionaryRef
219 if set all keys (strings with oids in dotted notation) will be added
220 as extensions with accompanying value in binary (CFDataRef) or
221 appropriate string (CFStringRef) type (based on used character set).
222 @param kSecCertificateExtensionsEncoded CFDictionaryRef
223 if set all keys (strings with oids in dotted notation) will be added
224 as extensions with accompanying value. It is assumed that the value
225 is a CFDataRef and is already properly encoded. This value will be
226 placed straight into the extension value OCTET STRING.
228 extern const void * kSecCSRChallengePassword
;
229 extern const void * kSecSubjectAltName
;
230 extern const void * kSecCertificateKeyUsage
;
231 extern const void * kSecCSRBasicContraintsPathLen
;
232 extern const void * kSecCertificateExtensions
;
233 extern const void * kSecCertificateExtensionsEncoded
;
236 const void *oid
; /* kSecOid constant or CFDataRef with oid */
237 unsigned char type
; /* currently only SecASN1PrintableString */
238 CFTypeRef value
; /* CFStringRef -> ASCII, UTF8, CFDataRef -> binary */
241 typedef SecATV
*SecRDN
;
244 @function SecGenerateCertificateRequest
245 @abstract Return a newly generated CSR for subject and keypair.
246 @param subject RDNs in the subject
247 @param num Number of RDNs
248 @param publicKey Public key
249 @param privateKey Private key
250 @discussion only handles RSA keypairs and uses a SHA-1 PKCS1 signature
251 @result On success, a newly allocated CSR, otherwise NULL
254 SecATV cn[] = { { kSecOidCommonName, SecASN1PrintableString, CFSTR("test") }, {} };
255 SecATV c[] = { { kSecOidCountryName, SecASN1PrintableString, CFSTR("US") }, {} };
256 SecATV o[] = { { kSecOidOrganization, SecASN1PrintableString, CFSTR("Apple Inc.") }, {} };
257 SecRDN atvs[] = { cn, c, o, NULL };
259 CFDataRef
SecGenerateCertificateRequestWithParameters(SecRDN
*subject
,
260 CFDictionaryRef parameters
, SecKeyRef publicKey
, SecKeyRef privateKey
) CF_RETURNS_RETAINED
;
262 CFDataRef
SecGenerateCertificateRequest(CFArrayRef subject
,
263 CFDictionaryRef parameters
, SecKeyRef publicKey
, SecKeyRef privateKey
) CF_RETURNS_RETAINED
;
266 @function SecVerifyCertificateRequest
267 @abstract validate a CSR and return contained information to certify
268 @param publicKey (optional/out) SecKeyRef public key to certify
269 @param challenge (optional/out) CFStringRef enclosed challenge
270 @param subject (optional/out) encoded subject RDNs
271 @param extensions (optional/out) encoded extensions
273 bool SecVerifyCertificateRequest(CFDataRef csr
, SecKeyRef
*publicKey
,
274 CFStringRef
*challenge
, CFDataRef
*subject
, CFDataRef
*extensions
);
277 SecGenerateSelfSignedCertificate(CFArrayRef subject
, CFDictionaryRef parameters
,
278 SecKeyRef publicKey
, SecKeyRef privateKey
);
281 SecIdentitySignCertificate(SecIdentityRef issuer
, CFDataRef serialno
,
282 SecKeyRef publicKey
, CFTypeRef subject
, CFTypeRef extensions
);
288 SecGenerateCertificateRequestSubject(SecCertificateRef ca_certificate
, CFArrayRef subject
);
292 #endif /* _SECURITY_SECCERTIFICATEREQUEST_H_ */