2  * Copyright (c) 2002-2004,2011,2013-2014 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 <Security/SecCertificateRequest.h> 
  26 #include "SecBridge.h" 
  27 #include "CertificateRequest.h" 
  28 #include "SecImportExport.h" 
  29 #include "SecCertificate.h" 
  32 SecCertificateRequestGetTypeID(void) 
  36         return gTypes().CertificateRequest
.typeID
; 
  38         END_SECAPI1(_kCFRuntimeNotATypeID
) 
  42 OSStatus 
SecCertificateRequestCreate( 
  43         const CSSM_OID 
*policy
, 
  44         CSSM_CERT_TYPE certificateType
, 
  45         CSSM_TP_AUTHORITY_REQUEST_TYPE requestType
, 
  46             SecKeyRef privateKeyItemRef
, 
  47             SecKeyRef publicKeyItemRef
, 
  48             const SecCertificateRequestAttributeList
* attributeList
, 
  49         SecCertificateRequestRef
* certRequest
) 
  52         Required(certRequest
); 
  54         *certRequest 
= (new CertificateRequest(*policy
, certificateType
, requestType
, 
  55                 privateKeyItemRef
, publicKeyItemRef
, attributeList
))->handle(); 
  60 OSStatus 
SecCertificateRequestSubmit( 
  61         SecCertificateRequestRef certRequest
, 
  62         sint32
* estimatedTime
) 
  66         CertificateRequest::required(certRequest
)->submit(estimatedTime
); 
  72 OSStatus 
SecCertificateRequestGetType( 
  73         SecCertificateRequestRef certRequestRef
, 
  74         CSSM_TP_AUTHORITY_REQUEST_TYPE 
*requestType
) 
  78         Required(requestType
); 
  79         *requestType 
= CertificateRequest::required(certRequestRef
)->reqType(); 
  84 OSStatus 
SecCertificateRequestGetResult( 
  85         SecCertificateRequestRef certRequestRef
, 
  86         SecKeychainRef keychain
, 
  87         sint32 
*estimatedTime
, 
  88         SecCertificateRef 
*certificateRef
) 
  93         *certificateRef 
= NULL
; 
  94         CertificateRequest::required(certRequestRef
)->getResult(estimatedTime
, certData
); 
  95         if(certData
.data() != NULL
) { 
  97                  * Convert to SecCertifcateRef, optionally import.  
  99                 CFDataRef cfCert 
= CFDataCreate(NULL
, (UInt8 
*)certData
.data(), certData
.Length
); 
 100                 SecExternalItemType itemType 
= kSecItemTypeCertificate
; 
 101                 CFArrayRef outItems 
= NULL
; 
 102                 bool freeKcRef 
= false; 
 105                 if(keychain 
== NULL
) { 
 107                          * Unlike most Sec* calls, if the keychain argument to SecKeychainItemImport() 
 108                          * is NULL, the item is not imported to the default keychain. At our 
 109                          * interface, however, a NULL keychain means "import to the default 
 112                         ortn 
= SecKeychainCopyDefault(&keychain
); 
 114                                 certReqDbg("GetResult: SecKeychainCopyDefault failure"); 
 115                                 /* oh well, there's nothing we can do about this */ 
 121                 ortn 
= SecKeychainItemImport(cfCert
, NULL
, 
 122                         NULL
,                   // format, don't care 
 126                         keychain
,               // optional, like ours 
 133                         certReqDbg("SecCertificateRequestGetResult: SecKeychainItemImport failure"); 
 134                         MacOSError::throwMe(ortn
); 
 136                 CFIndex numItems 
= CFArrayGetCount(outItems
); 
 139                                 certReqDbg("SecCertificateRequestGetResult: import zero items"); 
 140                                 MacOSError::throwMe(errSecInternalComponent
); 
 142                                 certReqDbg("SecCertificateRequestGetResult: import %d items",  
 144                                 /* but drop thru anyway, take the first one */ 
 146                                 SecCertificateRef certRef 
=  
 147                                         (SecCertificateRef
)(CFArrayGetValueAtIndex(outItems
, 0)); 
 148                                 if(CFGetTypeID(certRef
) != SecCertificateGetTypeID()) { 
 149                                         certReqDbg("SecCertificateRequestGetResult: bad type"); 
 153                                         *certificateRef 
= certRef
; 
 161 OSStatus 
SecCertificateFindRequest( 
 162         const CSSM_OID 
*policy
, 
 163         CSSM_CERT_TYPE certificateType
, 
 164         CSSM_TP_AUTHORITY_REQUEST_TYPE requestType
, 
 165                 SecKeyRef publicKeyItemRef
,                              
 166                 SecKeyRef privateKeyItemRef
,                             
 167                 const SecCertificateRequestAttributeList
* attributeList
, 
 168         SecCertificateRequestRef
* certRequest
) 
 172         Required(certRequest
); 
 174         *certRequest 
= (new CertificateRequest(*policy
, certificateType
, requestType
, 
 175                 privateKeyItemRef
, publicKeyItemRef
, attributeList
, false))->handle(); 
 180 OSStatus 
SecCertificateRequestGetData( 
 181         SecCertificateRequestRef        certRequestRef
, 
 187         CertificateRequest::required(certRequestRef
)->getReturnData(CssmData::overlay(*data
));