]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cdsa_utils/lib/cuOidParser.cpp
e9bc4e3bcea45c06736bed61f3e5d57d36043043
   2  * Copyright (c) 2002-2003,2011-2012,2014-2016 Apple Inc. All Rights Reserved. 
   4  * The contents of this file constitute Original Code as defined in and are 
   5  * subject to the Apple Public Source License Version 1.2 (the 'License'). 
   6  * You may not use this file except in compliance with the License. 
   7  * Please obtain a copy of the License at http://www.apple.com/publicsource 
   8  * and read it before using this file. 
  10  * This Original Code and all software distributed under the License are 
  11  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  12  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  13  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  14  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  15  * Please see the License for the specific language governing rights 
  16  * and limitations under the License. 
  20  * cuOidParser.cpp - parse an Intel-style OID, with the assistance 
  24 #include <Security/cssmtype.h> 
  28 #include "cuOidParser.h" 
  34 #include <sys/types.h> 
  38 /* get config file from .. or from . */ 
  39 #define                 CONFIG_FILE_NAME        "dumpasn1.cfg" 
  40 static const char       *CONFIG_FILE1 
=         "../" CONFIG_FILE_NAME
; 
  41 static const char       *CONFIG_FILE2 
=         CONFIG_FILE_NAME
; 
  42 /* or from here via getenv */ 
  43 #define                 CONFIG_FILE_ENV         "LOCAL_BUILD_DIR" 
  45 static const char       *OID_ENTRY_START 
= "OID = "; 
  46 static const char       *OID_DESCR_START 
= "Description = "; 
  48  * Read entire file with extra bytes left over in the mallocd buffer. 
  54         unsigned char   **bytes
,                // mallocd and returned 
  55         CSSM_SIZE               
*numBytes
)              // returned 
  59         unsigned char *buf 
= NULL
; 
  65         fd 
= open(fileName
, O_RDONLY
, 0); 
  73         size 
= (size_t)sb
.st_size
; 
  74         buf 
= (unsigned char *)malloc(size 
+ extraBytes
); 
  79         rtn 
= (int)lseek(fd
, 0, SEEK_SET
); 
  83         rtn 
= (int)read(fd
, buf
, (size_t)size
); 
  84         if(rtn 
!= (int)size
) { 
  86                         printf("readFile: short read\n"); 
  97     if(buf
) { free(buf
); buf 
= NULL
; } 
 104  * Attempt to read dumpasn1.cfg from various places. If we can't find it, 
 105  * printOid() function will just print raw bytes as it 
 106  * would if the .cfg file did not contain the desired OID. 
 108 static CSSM_DATA_PTR 
readConfig() 
 110         CSSM_DATA_PTR   configData 
= NULL
; 
 113         configData 
= (CSSM_DATA_PTR
)malloc(sizeof(CSSM_DATA
)); 
 114         if(configData 
== NULL
) { 
 117         /* malloc one extra byte, we'll null it later */ 
 118         rtn 
= readFileExtra(CONFIG_FILE1
, 1, &configData
->Data
, 
 119                 &configData
->Length
); 
 121                 rtn 
= readFileExtra(CONFIG_FILE2
, 1, &configData
->Data
, 
 122                                 &configData
->Length
); 
 125                 char *localBuildDir  
= getenv(CONFIG_FILE_ENV
); 
 126                 if(localBuildDir 
== NULL
) { 
 130                         char *pathBuf 
= NULL
; 
 131                         rtn 
= asprintf(&pathBuf
, "%s/%s", localBuildDir
, CONFIG_FILE_NAME
); 
 132                         if (rtn 
< 1 || !pathBuf
) { 
 136                                 rtn 
= readFileExtra(pathBuf
, 1, &configData
->Data
, 
 137                                                 &configData
->Length
); 
 145                 /* make the whole shebang one long C string */ 
 146                 configData
->Data
[configData
->Length
++] = '\0'; 
 156  * The heart of this module. 
 158  * -- Convert Intel-style OID to a string which might be found 
 160  * -- search config file for that string 
 161  * -- if found, use that entry in config file to output meaningful 
 162  *    string and return CSSM_TRUE. Else return CSSM_FALSE. 
 164 static CSSM_BOOL 
parseOidWithConfig( 
 165         const CSSM_DATA_PTR configData
, 
 166         const CSSM_OID_PTR      oid
, 
 169         char                            *fullOidStr 
= NULL
; 
 170         char                            *ourEntry 
= NULL
; 
 171         char                            *nextEntry 
= NULL
; 
 172         char                            *descStart 
= NULL
; 
 176         char                            *nextCr
;                // next CR if any 
 177         char                            *nextNl
;                // next NL if any 
 178         char                            *eol
;                   // end of line 
 181         if(configData 
== NULL
) { 
 185         /* cook up a full OID string, with tag and length */ 
 186         fullOidStr 
= (char *)malloc((3 * oid
->Length
) + 
 187                                                                                                 // 2 chars plus space per byte 
 188                 strlen(OID_ENTRY_START
) +                               // "OID = " 
 189                 6 +                                                                             // 06 xx - tag and length 
 191         if(fullOidStr 
== NULL
) { 
 194         /* subsequent errors to errOut: */ 
 196         sprintf(fullOidStr
, "OID = 06 %02X", (unsigned)oid
->Length
); 
 197         cp 
= fullOidStr 
+ strlen(fullOidStr
); 
 198         for(i
=0; i
<oid
->Length
; i
++) { 
 199                 /* move cp to current end of string */ 
 202                 sprintf(cp
, " %02X", oid
->Data
[i
]); 
 206          * Let's play it loose and assume that there are no embedded NULLs 
 207          * in the config file. Thus we can use the spiffy string functions 
 210         ourEntry 
= strstr((char *)configData
->Data
, fullOidStr
); 
 211         if(ourEntry 
== NULL
) { 
 216         /* get position of NEXT full entry - may be NULL (end of file) */ 
 217         nextEntry 
= strstr(ourEntry
+1, OID_ENTRY_START
); 
 219         /* get position of our entry's description line */ 
 220         descStart 
= strstr(ourEntry
+1, OID_DESCR_START
); 
 222         /* handle not found/overflow */ 
 223         if( (descStart 
== NULL
) ||                      // no more description lines 
 224             ( (descStart 
> nextEntry
) &&        // no description in THIS entry 
 225               (nextEntry 
!= NULL
) ) ) {         // make sure this is valid 
 230         /* set descStart to after the leader */ 
 231         descStart 
+= strlen(OID_DESCR_START
); 
 234          * descStart points to the text we're interested in. 
 235          * First find end of line, any style. 
 237         nextNl 
= strchr(descStart
, '\n'); 
 238         nextCr 
= strchr(descStart
, '\r'); 
 239         if((nextNl 
== NULL
) && (nextCr 
== NULL
)) { 
 240                 /* no line terminator, go to eof */ 
 241                 eol 
= (char *)configData
->Data 
+ configData
->Length
; 
 243         else if(nextCr 
== NULL
) { 
 246         else if(nextNl 
== NULL
) { 
 249         else if(nextNl 
< nextCr
) { 
 250                 /* both present, take first one */ 
 257         /* caller's string buf = remainder of description line */ 
 258         len 
= (int)(eol 
- descStart
); 
 259         if(len 
> (OID_PARSER_STRING_SIZE 
- 1)) { 
 260                 /* fixed-length output buf, avoid overflow */ 
 261                 len 
= OID_PARSER_STRING_SIZE 
- 1; 
 263         memcpy(strBuf
, descStart
, len
); 
 267         if(fullOidStr 
!= NULL
) { 
 273 /*** OidParser class ***/ 
 274 OidParser::OidParser(bool noConfig
) 
 280                 configData 
= readConfig(); 
 284 OidParser::~OidParser() 
 286         if(configData 
== NULL
) { 
 289         if(configData
->Data 
!= NULL
) { 
 290                 free(configData
->Data
); 
 296  * Parse an Intel-style OID, generating a C string in caller-supplied buffer. 
 298 void OidParser::oidParse( 
 299         const unsigned char     *oidp
, 
 306         oid
.Data 
= (uint8  
*)oidp
; 
 309         if((oidLen 
== 0) || (oidp 
== NULL
)) { 
 310                 strcpy(strBuf
, "EMPTY"); 
 313         if(parseOidWithConfig(configData
, &oid
, strBuf
) == CSSM_FALSE
) { 
 314                 /* no config file, just dump the bytes */ 
 317                 sprintf(strBuf
, "OID : < 06 %02X ", (unsigned)oid
.Length
); 
 318                 for(i
=0; i
<oid
.Length
; i
++) { 
 319                         sprintf(cbuf
, "%02X ", oid
.Data
[i
]); 
 320                         strcat(strBuf
, cbuf
);