@@ -101,12 +101,8 @@ int aws_import_public_and_private_keys_to_identity(
101101
102102 int result = AWS_OP_ERR ;
103103
104- CFDataRef cert_data = CFDataCreate (cf_alloc , public_cert_chain -> ptr , public_cert_chain -> len );
105- CFDataRef key_data = CFDataCreate (cf_alloc , private_key -> ptr , private_key -> len );
106-
107- if (!cert_data || !key_data ) {
108- return aws_raise_error (AWS_ERROR_SYS_CALL_FAILURE );
109- }
104+ CFDataRef cert_data = NULL ;
105+ CFDataRef key_data = NULL ;
110106
111107 CFArrayRef cert_import_output = NULL ;
112108 CFArrayRef key_import_output = NULL ;
@@ -118,9 +114,26 @@ int aws_import_public_and_private_keys_to_identity(
118114 import_params .version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION ;
119115 import_params .passphrase = CFSTR ("" );
120116
117+ struct aws_array_list cert_chain_list ;
118+ AWS_ZERO_STRUCT (cert_chain_list );
119+ CFDataRef root_cert_data = NULL ;
121120 SecCertificateRef certificate_ref = NULL ;
122121 SecKeychainRef import_keychain = NULL ;
123122
123+ cert_data = CFDataCreate (cf_alloc , public_cert_chain -> ptr , public_cert_chain -> len );
124+ if (!cert_data ) {
125+ AWS_LOGF_ERROR (AWS_LS_IO_PKI , "static: failed creating public cert chain data." );
126+ result = aws_raise_error (AWS_ERROR_SYS_CALL_FAILURE );
127+ goto done ;
128+ }
129+
130+ key_data = CFDataCreate (cf_alloc , private_key -> ptr , private_key -> len );
131+ if (!key_data ) {
132+ AWS_LOGF_ERROR (AWS_LS_IO_PKI , "static: failed creating private key data." );
133+ result = aws_raise_error (AWS_ERROR_SYS_CALL_FAILURE );
134+ goto done ;
135+ }
136+
124137# pragma clang diagnostic push
125138# pragma clang diagnostic ignored "-Wdeprecated-declarations"
126139 /* SecKeychain functions are marked as deprecated.
@@ -134,7 +147,8 @@ int aws_import_public_and_private_keys_to_identity(
134147 "static: error opening keychain \"%s\" with OSStatus %d" ,
135148 aws_string_c_str (keychain_path ),
136149 keychain_status );
137- return AWS_OP_ERR ;
150+ result = aws_raise_error (AWS_ERROR_SYS_CALL_FAILURE );
151+ goto done ;
138152 }
139153 keychain_status = SecKeychainUnlock (import_keychain , 0 , "" , true);
140154 if (keychain_status != errSecSuccess ) {
@@ -143,14 +157,16 @@ int aws_import_public_and_private_keys_to_identity(
143157 "static: error unlocking keychain \"%s\" with OSStatus %d" ,
144158 aws_string_c_str (keychain_path ),
145159 keychain_status );
146- return AWS_OP_ERR ;
160+ result = aws_raise_error (AWS_ERROR_SYS_CALL_FAILURE );
161+ goto done ;
147162 }
148163 } else {
149164 OSStatus keychain_status = SecKeychainCopyDefault (& import_keychain );
150165 if (keychain_status != errSecSuccess ) {
151166 AWS_LOGF_ERROR (
152167 AWS_LS_IO_PKI , "static: error opening the default keychain with OSStatus %d" , keychain_status );
153- return AWS_OP_ERR ;
168+ result = aws_raise_error (AWS_ERROR_SYS_CALL_FAILURE );
169+ goto done ;
154170 }
155171 }
156172
@@ -168,9 +184,6 @@ int aws_import_public_and_private_keys_to_identity(
168184 OSStatus key_status =
169185 SecItemImport (key_data , NULL , & format , & item_type , 0 , & import_params , import_keychain , & key_import_output );
170186
171- CFRelease (cert_data );
172- CFRelease (key_data );
173-
174187 if (cert_status != errSecSuccess && cert_status != errSecDuplicateItem ) {
175188 AWS_LOGF_ERROR (AWS_LS_IO_PKI , "static: error importing certificate with OSStatus %d" , (int )cert_status );
176189 result = aws_raise_error (AWS_IO_FILE_VALIDATION_FAILURE );
@@ -201,11 +214,8 @@ int aws_import_public_and_private_keys_to_identity(
201214 AWS_LS_IO_PKI ,
202215 "static: certificate has an existing certificate-key pair that was previously imported into the Keychain. "
203216 "Using key from Keychain instead of the one provided." );
204- struct aws_array_list cert_chain_list ;
205-
206217 if (aws_pem_objects_init_from_file_contents (& cert_chain_list , alloc , * public_cert_chain )) {
207218 AWS_LOGF_ERROR (AWS_LS_IO_PKI , "static: decoding certificate PEM failed." );
208- aws_pem_objects_clean_up (& cert_chain_list );
209219 result = AWS_OP_ERR ;
210220 goto done ;
211221 }
@@ -214,36 +224,46 @@ int aws_import_public_and_private_keys_to_identity(
214224 aws_array_list_get_at_ptr (& cert_chain_list , (void * * )& root_cert_ptr , 0 );
215225 AWS_ASSERT (root_cert_ptr );
216226 CFDataRef root_cert_data = CFDataCreate (cf_alloc , root_cert_ptr -> data .buffer , root_cert_ptr -> data .len );
217-
218- if ( root_cert_data ) {
219- certificate_ref = SecCertificateCreateWithData ( cf_alloc , root_cert_data );
220- CFRelease ( root_cert_data ) ;
227+ if (! root_cert_data ) {
228+ AWS_LOGF_ERROR ( AWS_LS_IO_PKI , "static: failed creating root cert data." );
229+ result = aws_raise_error ( AWS_ERROR_SYS_CALL_FAILURE );
230+ goto done ;
221231 }
222232
223- aws_pem_objects_clean_up (& cert_chain_list );
233+ certificate_ref = SecCertificateCreateWithData (cf_alloc , root_cert_data );
234+ if (!certificate_ref ) {
235+ AWS_LOGF_ERROR (AWS_LS_IO_PKI , "static: failed to create certificate." );
236+ result = aws_raise_error (AWS_IO_FILE_VALIDATION_FAILURE );
237+ goto done ;
238+ }
224239 } else {
225240 certificate_ref = (SecCertificateRef )CFArrayGetValueAtIndex (cert_import_output , 0 );
226241 /* SecCertificateCreateWithData returns an object with +1 retain, so we need to match that behavior here */
227242 CFRetain (certificate_ref );
228243 }
229244
230- /* if we got a cert one way or the other, create the identity and return it */
231- if (certificate_ref ) {
232- SecIdentityRef identity_output ;
233- OSStatus status = SecIdentityCreateWithCertificate (import_keychain , certificate_ref , & identity_output );
234- if (status == errSecSuccess ) {
235- CFTypeRef certs [] = {identity_output };
236- * identity = CFArrayCreate (cf_alloc , (const void * * )certs , 1L , & kCFTypeArrayCallBacks );
237- result = AWS_OP_SUCCESS ;
238- goto done ;
239- }
245+ /* we got a cert one way or the other, create the identity and return it */
246+ AWS_ASSERT (certificate_ref );
247+ SecIdentityRef identity_output ;
248+ OSStatus status = SecIdentityCreateWithCertificate (import_keychain , certificate_ref , & identity_output );
249+ if (status != errSecSuccess ) {
250+ AWS_LOGF_ERROR (AWS_LS_IO_PKI , "static: error creating identity with OSStatus %d" , key_status );
251+ result = aws_raise_error (AWS_ERROR_SYS_CALL_FAILURE );
252+ goto done ;
240253 }
241254
255+ CFTypeRef certs [] = {identity_output };
256+ * identity = CFArrayCreate (cf_alloc , (const void * * )certs , 1L , & kCFTypeArrayCallBacks );
257+ result = AWS_OP_SUCCESS ;
258+
242259done :
243260 aws_mutex_unlock (& s_sec_mutex );
244261 if (certificate_ref ) {
245262 CFRelease (certificate_ref );
246263 }
264+ if (root_cert_data ) {
265+ CFRelease (root_cert_data );
266+ }
247267 if (cert_import_output ) {
248268 CFRelease (cert_import_output );
249269 }
@@ -253,6 +273,13 @@ int aws_import_public_and_private_keys_to_identity(
253273 if (import_keychain ) {
254274 CFRelease (import_keychain );
255275 }
276+ if (cert_data ) {
277+ CFRelease (cert_data );
278+ }
279+ if (key_data ) {
280+ CFRelease (key_data );
281+ }
282+ aws_pem_objects_clean_up (& cert_chain_list );
256283
257284 return result ;
258285}
0 commit comments