]> git.saurik.com Git - apple/security.git/blobdiff - libsecurity_keychain/lib/SecImportExport.c
Security-55471.14.18.tar.gz
[apple/security.git] / libsecurity_keychain / lib / SecImportExport.c
index ed7a949fddcd81eda166198188ff13ba76d53a9e..705632236ecd9f6d3bc96a46b0c116e28a04ee3c 100644 (file)
@@ -1,15 +1,15 @@
 /*
  * Copyright (c) 2007-2010 Apple Inc. All Rights Reserved.
- * 
+ *
  * @APPLE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * compliance with the License. Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this
  * file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
@@ -17,7 +17,7 @@
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_LICENSE_HEADER_END@
  */
 
@@ -56,7 +56,7 @@ static void collect_certs(const void *key, const void *value, void *context)
         CFDataRef cert_bytes = CFDictionaryGetValue(value, CFSTR("cert"));
         if (!cert_bytes)
             return;
-        SecCertificateRef cert = 
+        SecCertificateRef cert =
             SecCertificateCreateWithData(kCFAllocatorDefault, cert_bytes);
         if (!cert)
             return;
@@ -71,10 +71,10 @@ typedef struct {
     CFArrayRef certs;
 } build_trust_chains_context;
 
-static void build_trust_chains(const void *key, const void *value, 
+static void build_trust_chains(const void *key, const void *value,
     void *context)
 {
-    CFMutableDictionaryRef identity_dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 
+    CFMutableDictionaryRef identity_dict = CFDictionaryCreateMutable(kCFAllocatorDefault,
         0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
     SecKeyRef private_key = NULL;
     SecCertificateRef cert = NULL;
@@ -92,7 +92,7 @@ static void build_trust_chains(const void *key, const void *value,
     /* p12import only passes up rsa keys */
 //FIXME: needs SecKeyCreateRSAPrivateKey implementation
 //#if 0
-//     private_key = SecKeyCreateRSAPrivateKey(kCFAllocatorDefault, 
+//     private_key = SecKeyCreateRSAPrivateKey(kCFAllocatorDefault,
 //        CFDataGetBytePtr(key_bytes), CFDataGetLength(key_bytes),
 //        kSecKeyEncodingPkcs1);
 //#endif
@@ -102,7 +102,7 @@ static void build_trust_chains(const void *key, const void *value,
     identity = SecIdentityCreate(kCFAllocatorDefault, cert, private_key);
        if(!identity) goto out; //require(identity, out);
     CFDictionarySetValue(identity_dict, kSecImportItemIdentity, identity);
-    
+
     eval_chain = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
        if(!eval_chain) goto out; //require(eval_chain, out);
     CFArrayAppendValue(eval_chain, cert);
@@ -115,7 +115,7 @@ static void build_trust_chains(const void *key, const void *value,
        if(!trust) goto out; //require(trust, out);
     SecTrustEvaluate(trust, &result);
     CFDictionarySetValue(identity_dict, kSecImportItemTrust, trust);
-    
+
     cert_chain = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
        if(!cert_chain) goto out; //require(cert_chain, out);
     CFIndex cert_chain_length = SecTrustGetCertificateCount(trust);
@@ -123,7 +123,7 @@ static void build_trust_chains(const void *key, const void *value,
     for (i = 0; i < cert_chain_length; i++)
         CFArrayAppendValue(cert_chain, SecTrustGetCertificateAtIndex(trust, i));
     CFDictionarySetValue(identity_dict, kSecImportItemCertChain, cert_chain);
-    
+
     CFArrayAppendValue(a_build_trust_chains_context->identities, identity_dict);
 out:
     CFReleaseSafe(identity_dict);
@@ -143,7 +143,7 @@ OSStatus SecPKCS12Import(CFDataRef pkcs12_data, CFDictionaryRef options, CFArray
        // SecKeychainItemImport API, which supports importing items into a
        // specified keychain with initial access control settings for keys.
        //
-       OSStatus status = noErr;
+       OSStatus status = errSecSuccess;
        SecExternalFormat inputFormat = kSecFormatPKCS12;
        SecExternalItemType itemType = kSecItemTypeAggregate;
        SecItemImportExportFlags flags = 0; /* don't know if it's PEM armoured */
@@ -166,12 +166,12 @@ OSStatus SecPKCS12Import(CFDataRef pkcs12_data, CFDictionaryRef options, CFArray
                if (importPassword)
                        CFRetain(importPassword);
        }
-       
+
        if (!importKeychain) {
                // SecKeychainItemImport requires a keychain, so use default
                status = SecKeychainCopyDefault(&importKeychain);
        }
-       
+
        memset(&keyParams, 0, sizeof(SecKeyImportExportParameters));
        keyParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
        keyParams.passphrase = importPassword;
@@ -181,11 +181,11 @@ OSStatus SecPKCS12Import(CFDataRef pkcs12_data, CFDictionaryRef options, CFArray
                                                                   NULL,                /* no filename */
                                                                   &inputFormat,
                                                                   &itemType,
-                                                                  flags, 
+                                                                  flags,
                                                                   &keyParams,
                                                                   importKeychain,
                                                                   &tmpItems);
-       
+
        // build an array of all non-identity certificates which were imported
        if (!status) {
                certs = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
@@ -198,7 +198,7 @@ OSStatus SecPKCS12Import(CFDataRef pkcs12_data, CFDictionaryRef options, CFArray
                        }
                }
        }
-       
+
        // now build the output items (array of dictionaries)
        if (!status) {
                identities = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
@@ -208,14 +208,14 @@ OSStatus SecPKCS12Import(CFDataRef pkcs12_data, CFDictionaryRef options, CFArray
                        CFTypeID itemID = CFGetTypeID(anItem);
                        if (itemID == SecIdentityGetTypeID()) {
                                CFMutableDictionaryRef itemDict;
-                               itemDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 
+                               itemDict = CFDictionaryCreateMutable(kCFAllocatorDefault,
                                                                                                         0,
                                                                                                         &kCFTypeDictionaryKeyCallBacks,
                                                                                                         &kCFTypeDictionaryValueCallBacks);
 
                                SecCertificateRef itemCert = NULL;
                                status = SecIdentityCopyCertificate((SecIdentityRef)anItem, &itemCert);
-                               
+
                                // label
                                if (!status) {
                                        CFStringRef label = SecCertificateCopySubjectSummary(itemCert);
@@ -234,7 +234,7 @@ OSStatus SecPKCS12Import(CFDataRef pkcs12_data, CFDictionaryRef options, CFArray
                                                status = SecKeyGetCSSMKey(itemKey, &cssmKey);
                                                if (!status) {
                                                        unsigned char hash[CC_SHA1_DIGEST_LENGTH];
-                                                       CC_SHA1(cssmKey->KeyData.Data, cssmKey->KeyData.Length, &hash[0]);
+                                                       CC_SHA1(cssmKey->KeyData.Data, (CC_LONG)cssmKey->KeyData.Length, &hash[0]);
                                                        CFDataRef digest = CFDataCreate(NULL, (const UInt8 *)hash, CC_SHA1_DIGEST_LENGTH);
                                                        if (digest) {
                                                                CFDictionaryAddValue(itemDict, kSecImportItemKeyID, digest);
@@ -267,18 +267,18 @@ OSStatus SecPKCS12Import(CFDataRef pkcs12_data, CFDictionaryRef options, CFArray
                                        CFDictionaryAddValue(itemDict, kSecImportItemCertChain, certArray);
                                        CFRelease(certArray);
                                }
-                               
+
                                // identity
                                CFDictionaryAddValue(itemDict, kSecImportItemIdentity, anItem);
-                               
+
                                if (itemCert)
-                                       CFRelease(itemCert);                            
+                                       CFRelease(itemCert);
                                CFArrayAppendValue(identities, itemDict);
                                CFRelease(itemDict);
                        }
                }
        }
-       
+
        if (items)
                *items = identities;
        else if (identities)
@@ -294,16 +294,16 @@ OSStatus SecPKCS12Import(CFDataRef pkcs12_data, CFDictionaryRef options, CFArray
                CFRelease(importAccess);
        if (importPassword)
                CFRelease(importPassword);
-       
+
        return status;
-       
+
 //FIXME: needs SecAsn1Coder implementation
 #if 0
     pkcs12_context context = {};
     SecAsn1CoderCreate(&context.coder);
     if (options)
         context.passphrase = CFDictionaryGetValue(options, kSecImportExportPassphrase);
-    context.items = CFDictionaryCreateMutable(kCFAllocatorDefault, 
+    context.items = CFDictionaryCreateMutable(kCFAllocatorDefault,
         0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
     int status = p12decode(&context, pkcs12_data);
     if (!status) {
@@ -314,22 +314,22 @@ OSStatus SecPKCS12Import(CFDataRef pkcs12_data, CFDictionaryRef options, CFArray
         build_trust_chains_context a_build_trust_chains_context = { identities, certs };
         CFDictionaryApplyFunction(context.items, build_trust_chains, &a_build_trust_chains_context);
         CFReleaseSafe(certs);
-        
+
         /* ignoring certs that weren't picked up as part of the certchain for found keys */
-        
+
         *items = identities;
     }
 
     CFReleaseSafe(context.items);
     SecAsn1CoderRelease(context.coder);
-    
+
     switch (status) {
-    case p12_noErr: return noErr;
+    case p12_noErr: return errSecSuccess;
     case p12_passwordErr: return errSecAuthFailed;
     case p12_decodeErr: return errSecDecode;
     default: return errSecInternal;
     };
-    return noErr;
+    return errSecSuccess;
 #endif
 }