]> git.saurik.com Git - apple/security.git/blobdiff - OSX/sec/Security/Tool/verify_cert.c
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / sec / Security / Tool / verify_cert.c
index a1b8ebfd31d97f3c8046e0fb945ee765afd82803..3db25f965e3a7c43f31537820fc81c56c4ff99e7 100644 (file)
 #include <Security/SecCertificate.h>
 #include <Security/SecTrust.h>
 #include <Security/SecPolicy.h>
+#include <utilities/fileIo.h>
 
 #include <sys/stat.h>
 #include <stdio.h>
 #include <time.h>
 
-int readFile(const char        *fileName, unsigned char **bytes, unsigned *numBytes);
 CFStringRef policyToConstant(const char *policy);
 int verify_cert(int argc, char * const *argv);
 
-/* Read an entire file. Copied from cuFileIo.c */
-int readFile(
-             const char                *fileName,
-             unsigned char     **bytes,                /* malloc'd and returned */
-             unsigned          *numBytes)              /* returned */
-{
-    int rtn;
-    int fd;
-    unsigned char *buf;
-    struct stat        sb;
-    unsigned size;
-    
-    *numBytes = 0;
-    *bytes = NULL;
-    fd = open(fileName, O_RDONLY, 0);
-    if (fd < 0) {
-        return errno;
-    }
-    
-    rtn = fstat(fd, &sb);
-    if (rtn) {
-        goto errOut;
-    }
-    size = (unsigned)sb.st_size;
-    buf = malloc(size);
-    if (buf == NULL) {
-        rtn = ENOMEM;
-        goto errOut;
-    }
-    
-    rtn = (int)lseek(fd, 0, SEEK_SET);
-    if (rtn < 0) {
-        free(buf);
-        goto errOut;
-    }
-    
-    rtn = (int)read(fd, buf, (size_t)size);
-    if (rtn != (int)size) {
-        if (rtn >= 0) {
-            printf("readFile: short read\n");
-        }
-        free(buf);
-        rtn = EIO;
-    }
-    else {
-        rtn = 0;
-        *bytes = buf;
-        *numBytes = size;
-    }
-errOut:
-    close(fd);
-    return rtn;
-}
-
 static int addCertFile(const char *fileName, CFMutableArrayRef *array) {
     SecCertificateRef certRef = NULL;
     CFDataRef dataRef = NULL;
     unsigned char *buf = NULL;
-    unsigned int numBytes;
+    size_t numBytes;
     int rtn = 0;
     
-    if (readFile(fileName, &buf, &numBytes)) {
+    if (readFileSizet(fileName, &buf, &numBytes)) {
         rtn = -1;
         goto errOut;
     }
@@ -169,8 +115,8 @@ int verify_cert(int argc, char * const *argv) {
        CFMutableArrayRef roots = NULL;
 
     CFMutableDictionaryRef dict = NULL;
-    const char *name = NULL;
-    bool client = false;
+    CFStringRef name = NULL;
+    CFBooleanRef client = kCFBooleanFalse;
     
     OSStatus ortn;
        int     ourRtn = 0;
@@ -224,8 +170,8 @@ int verify_cert(int argc, char * const *argv) {
                 fetch = false;
                                break;
                        case 'n':
-                if (name != NULL) {
-                    name = optarg;
+                if (name == NULL) {
+                    name = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingUTF8);
                 }
                                break;
                        case 'q':
@@ -233,7 +179,7 @@ int verify_cert(int argc, char * const *argv) {
                                break;
             case 'C':
                 /* Set to client */
-                client = true;
+                client = kCFBooleanTrue;
                 break;
             case 'd':
                 memset(&time, 0, sizeof(struct tm));
@@ -294,21 +240,23 @@ int verify_cert(int argc, char * const *argv) {
         dict = CFDictionaryCreateMutable(NULL, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
         
         if (name == NULL) {
+            fprintf(stderr, "Name not specified for IPsec or SSL policy. '-n' is a required option for these policies.");
             ourRtn = 2;
             goto errOut;
         }
         CFDictionaryAddValue(dict, kSecPolicyName, name);
-        CFDictionaryAddValue(dict, kSecPolicyClient, &client);
+        CFDictionaryAddValue(dict, kSecPolicyClient, client);
     }
     else if (!CFStringCompare(policy, kSecPolicyAppleEAP, 0)) {
         dict = CFDictionaryCreateMutable(NULL, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
         
-        CFDictionaryAddValue(dict, kSecPolicyClient, &client);
+        CFDictionaryAddValue(dict, kSecPolicyClient, client);
     }
     else if (!CFStringCompare(policy, kSecPolicyAppleSMIME, 0)) {
         dict = CFDictionaryCreateMutable(NULL, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
         
         if (name == NULL) {
+            fprintf(stderr, "Name not specified for SMIME policy. '-n' is a required option for this policy.");
             ourRtn = 2;
             goto errOut;
         }
@@ -430,5 +378,6 @@ errOut:
     CFRELEASE(dict);
        CFRELEASE(policyRef);
        CFRELEASE(trustRef);
+    CFRELEASE(name);
        return ourRtn;
 }