]> git.saurik.com Git - apple/security.git/blobdiff - OSX/libsecurity_cdsa_utils/lib/cuOidParser.cpp
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / OSX / libsecurity_cdsa_utils / lib / cuOidParser.cpp
index 7b2b7deffdd4d8385d27127f6bb9f4793ff17963..d490d0f8de5722e69df3733f245c2208d872637d 100644 (file)
@@ -1,26 +1,26 @@
 /*
- * Copyright (c) 2002-2003,2011-2012,2014 Apple Inc. All Rights Reserved.
- * 
+ * Copyright (c) 2002-2003,2011-2012,2014-2016 Apple Inc. All Rights Reserved.
+ *
  * The contents of this file constitute Original Code as defined in and are
  * subject to the Apple Public Source License Version 1.2 (the 'License').
- * You may not use this file except in compliance with the License. 
+ * You may not use this file except in compliance with the License.
  * Please obtain a copy of the License at http://www.apple.com/publicsource
  * and read it before using this file.
- * 
+ *
  * This 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, 
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights 
+ * Please see the License for the specific language governing rights
  * and limitations under the License.
  */
+
 /*
- * cuOidParser.cpp - parse an Intel-style OID, with the assistance 
+ * cuOidParser.cpp - parse an Intel-style OID, with the assistance
  * of dumpasn1.cfg
  */
+
 #include <Security/cssmtype.h>
 #include <string.h>
 #include <stdlib.h>
@@ -37,7 +37,7 @@
 
 /* get config file from .. or from . */
 #define                CONFIG_FILE_NAME        "dumpasn1.cfg"
-static const char      *CONFIG_FILE1 =         "../"CONFIG_FILE_NAME;
+static const char      *CONFIG_FILE1 =         "../" CONFIG_FILE_NAME;
 static const char      *CONFIG_FILE2 =         CONFIG_FILE_NAME;
 /* or from here via getenv */
 #define                CONFIG_FILE_ENV         "LOCAL_BUILD_DIR"
@@ -45,7 +45,7 @@ static const char     *CONFIG_FILE2 =         CONFIG_FILE_NAME;
 static const char      *OID_ENTRY_START = "OID = ";
 static const char      *OID_DESCR_START = "Description = ";
 /*
- * Read entire file with extra bytes left over in the mallocd buffer. 
+ * Read entire file with extra bytes left over in the mallocd buffer.
  */
 static
 int readFileExtra(
@@ -56,10 +56,10 @@ int readFileExtra(
 {
        int rtn;
        int fd;
-       unsigned char *buf;
+       unsigned char *buf = NULL;
        struct stat     sb;
        size_t size;
-       
+
        *numBytes = 0;
        *bytes = NULL;
        fd = open(fileName, O_RDONLY, 0);
@@ -76,12 +76,9 @@ int readFileExtra(
                rtn = ENOMEM;
                goto errOut;
        }
-       rtn = (int)lseek(fd, 0, SEEK_SET);
-       if(rtn < 0) {
-               goto errOut;
-       }
        rtn = (int)read(fd, buf, (size_t)size);
        if(rtn != (int)size) {
+        free(buf);
                if(rtn >= 0) {
                        printf("readFile: short read\n");
                }
@@ -98,7 +95,7 @@ errOut:
 }
 
 /*
- * Attempt to read dumpasn1.cfg from various places. If we can't find it, 
+ * Attempt to read dumpasn1.cfg from various places. If we can't find it,
  * printOid() function will just print raw bytes as it
  * would if the .cfg file did not contain the desired OID.
  */
@@ -106,28 +103,36 @@ static CSSM_DATA_PTR readConfig()
 {
        CSSM_DATA_PTR   configData = NULL;
        int                             rtn;
-       
+
        configData = (CSSM_DATA_PTR)malloc(sizeof(CSSM_DATA));
        if(configData == NULL) {
                return NULL;
        }
        /* malloc one extra byte, we'll null it later */
-       rtn = readFileExtra(CONFIG_FILE1, 1, &configData->Data, 
+       rtn = readFileExtra(CONFIG_FILE1, 1, &configData->Data,
                &configData->Length);
        if(rtn) {
-               rtn = readFileExtra(CONFIG_FILE2, 1, &configData->Data, 
+               rtn = readFileExtra(CONFIG_FILE2, 1, &configData->Data,
                                &configData->Length);
        }
        if(rtn) {
-               char fileName[100];
                char *localBuildDir  = getenv(CONFIG_FILE_ENV);
                if(localBuildDir == NULL) {
                        rtn = 1;
                }
                else {
-                       sprintf(fileName,  "%s/%s", localBuildDir, CONFIG_FILE_NAME);
-                       rtn = readFileExtra(fileName, 1, &configData->Data, 
-                               &configData->Length);
+                       char *pathBuf = NULL;
+                       rtn = asprintf(&pathBuf, "%s/%s", localBuildDir, CONFIG_FILE_NAME);
+                       if (rtn < 1 || !pathBuf) {
+                               rtn = 1;
+                       }
+                       else {
+                               rtn = readFileExtra(pathBuf, 1, &configData->Data,
+                                               &configData->Length);
+                       }
+                       if (pathBuf) {
+                               free(pathBuf);
+                       }
                }
        }
        if(rtn == 0) {
@@ -142,17 +147,17 @@ static CSSM_DATA_PTR readConfig()
 }
 
 /*
- * The heart of this module. 
+ * The heart of this module.
  *
- * -- Convert Intel-style OID to a string which might be found 
+ * -- Convert Intel-style OID to a string which might be found
  *    in the config file
  * -- search config file for that string
  * -- if found, use that entry in config file to output meaningful
  *    string and return CSSM_TRUE. Else return CSSM_FALSE.
  */
 static CSSM_BOOL parseOidWithConfig(
-       const CSSM_DATA_PTR configData, 
-       const CSSM_OID_PTR      oid, 
+       const CSSM_DATA_PTR configData,
+       const CSSM_OID_PTR      oid,
        char                            *strBuf)
 {
        char                            *fullOidStr = NULL;
@@ -166,13 +171,13 @@ static CSSM_BOOL parseOidWithConfig(
        char                            *nextNl;                // next NL if any
        char                            *eol;                   // end of line
        int                                     len;
-       
+
        if(configData == NULL) {
                return CSSM_FALSE;
        }
-       
+
        /* cook up a full OID string, with tag and length */
-       fullOidStr = (char *)malloc((3 * oid->Length) +         
+       fullOidStr = (char *)malloc((3 * oid->Length) +
                                                                                                // 2 chars plus space per byte
                strlen(OID_ENTRY_START) +                               // "OID = "
                6 +                                                                             // 06 xx - tag and length
@@ -181,7 +186,7 @@ static CSSM_BOOL parseOidWithConfig(
                return CSSM_FALSE;
        }
        /* subsequent errors to errOut: */
-       
+
        sprintf(fullOidStr, "OID = 06 %02X", (unsigned)oid->Length);
        cp = fullOidStr + strlen(fullOidStr);
        for(i=0; i<oid->Length; i++) {
@@ -190,24 +195,24 @@ static CSSM_BOOL parseOidWithConfig(
                /* add one byte */
                sprintf(cp, " %02X", oid->Data[i]);
        }
-       
-       /* 
+
+       /*
         * Let's play it loose and assume that there are no embedded NULLs
         * in the config file. Thus we can use the spiffy string functions
-        * in stdlib. 
+        * in stdlib.
         */
        ourEntry = strstr((char *)configData->Data, fullOidStr);
        if(ourEntry == NULL) {
                brtn = CSSM_FALSE;
                goto errOut;
        }
-       
+
        /* get position of NEXT full entry - may be NULL (end of file) */
        nextEntry = strstr(ourEntry+1, OID_ENTRY_START);
-       
+
        /* get position of our entry's description line */
        descStart = strstr(ourEntry+1, OID_DESCR_START);
-       
+
        /* handle not found/overflow */
        if( (descStart == NULL) ||                      // no more description lines
            ( (descStart > nextEntry) &&        // no description in THIS entry
@@ -215,13 +220,13 @@ static CSSM_BOOL parseOidWithConfig(
                brtn = CSSM_FALSE;
                goto errOut;
        }
-       
+
        /* set descStart to after the leader */
        descStart += strlen(OID_DESCR_START);
-       
-       /* 
+
+       /*
         * descStart points to the text we're interested in.
-        * First find end of line, any style. 
+        * First find end of line, any style.
         */
        nextNl = strchr(descStart, '\n');
        nextCr = strchr(descStart, '\r');
@@ -242,7 +247,7 @@ static CSSM_BOOL parseOidWithConfig(
        else {
                eol = nextCr;
        }
-       
+
        /* caller's string buf = remainder of description line */
        len = (int)(eol - descStart);
        if(len > (OID_PARSER_STRING_SIZE - 1)) {
@@ -251,7 +256,7 @@ static CSSM_BOOL parseOidWithConfig(
        }
        memcpy(strBuf, descStart, len);
        strBuf[len] = '\0';
-       brtn = CSSM_TRUE; 
+       brtn = CSSM_TRUE;
 errOut:
        if(fullOidStr != NULL) {
                free(fullOidStr);
@@ -291,10 +296,10 @@ void OidParser::oidParse(
 {
        unsigned i;
        CSSM_OID oid;
-       
+
        oid.Data = (uint8  *)oidp;
        oid.Length = oidLen;
-       
+
        if((oidLen == 0) || (oidp == NULL)) {
                strcpy(strBuf, "EMPTY");
                return;
@@ -302,7 +307,7 @@ void OidParser::oidParse(
        if(parseOidWithConfig(configData, &oid, strBuf) == CSSM_FALSE) {
                /* no config file, just dump the bytes */
                char cbuf[8];
-               
+
                sprintf(strBuf, "OID : < 06 %02X ", (unsigned)oid.Length);
                for(i=0; i<oid.Length; i++) {
                        sprintf(cbuf, "%02X ", oid.Data[i]);