]>
git.saurik.com Git - apple/security.git/blob - libsecurity_cdsa_utils/lib/cuOidParser.cpp
2 * Copyright (c) 2002-2003 Apple Computer, 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.
53 unsigned char **bytes
, // mallocd and returned
54 CSSM_SIZE
*numBytes
) // returned
64 fd
= open(fileName
, O_RDONLY
, 0);
73 buf
= (unsigned char *)malloc(size
+ extraBytes
);
78 rtn
= lseek(fd
, 0, SEEK_SET
);
82 rtn
= read(fd
, buf
, (size_t)size
);
83 if(rtn
!= (int)size
) {
85 printf("readFile: short read\n");
100 * Attempt to read dumpasn1.cfg from various places. If we can't find it,
101 * printOid() function will just print raw bytes as it
102 * would if the .cfg file did not contain the desired OID.
104 static CSSM_DATA_PTR
readConfig()
106 CSSM_DATA_PTR configData
= NULL
;
109 configData
= (CSSM_DATA_PTR
)malloc(sizeof(CSSM_DATA
));
110 if(configData
== NULL
) {
113 /* malloc one extra byte, we'll null it later */
114 rtn
= readFileExtra(CONFIG_FILE1
, 1, &configData
->Data
,
115 &configData
->Length
);
117 rtn
= readFileExtra(CONFIG_FILE2
, 1, &configData
->Data
,
118 &configData
->Length
);
122 char *localBuildDir
= getenv(CONFIG_FILE_ENV
);
123 if(localBuildDir
== NULL
) {
127 sprintf(fileName
, "%s/%s", localBuildDir
, CONFIG_FILE_NAME
);
128 rtn
= readFileExtra(fileName
, 1, &configData
->Data
,
129 &configData
->Length
);
133 /* make the whole shebang one long C string */
134 configData
->Data
[configData
->Length
++] = '\0';
144 * The heart of this module.
146 * -- Convert Intel-style OID to a string which might be found
148 * -- search config file for that string
149 * -- if found, use that entry in config file to output meaningful
150 * string and return CSSM_TRUE. Else return CSSM_FALSE.
152 static CSSM_BOOL
parseOidWithConfig(
153 const CSSM_DATA_PTR configData
,
154 const CSSM_OID_PTR oid
,
157 char *fullOidStr
= NULL
;
158 char *ourEntry
= NULL
;
159 char *nextEntry
= NULL
;
160 char *descStart
= NULL
;
164 char *nextCr
; // next CR if any
165 char *nextNl
; // next NL if any
166 char *eol
; // end of line
169 if(configData
== NULL
) {
173 /* cook up a full OID string, with tag and length */
174 fullOidStr
= (char *)malloc((3 * oid
->Length
) +
175 // 2 chars plus space per byte
176 strlen(OID_ENTRY_START
) + // "OID = "
177 6 + // 06 xx - tag and length
179 if(fullOidStr
== NULL
) {
182 /* subsequent errors to errOut: */
184 sprintf(fullOidStr
, "OID = 06 %02X", (unsigned)oid
->Length
);
185 cp
= fullOidStr
+ strlen(fullOidStr
);
186 for(i
=0; i
<oid
->Length
; i
++) {
187 /* move cp to current end of string */
190 sprintf(cp
, " %02X", oid
->Data
[i
]);
194 * Let's play it loose and assume that there are no embedded NULLs
195 * in the config file. Thus we can use the spiffy string functions
198 ourEntry
= strstr((char *)configData
->Data
, fullOidStr
);
199 if(ourEntry
== NULL
) {
204 /* get position of NEXT full entry - may be NULL (end of file) */
205 nextEntry
= strstr(ourEntry
+1, OID_ENTRY_START
);
207 /* get position of our entry's description line */
208 descStart
= strstr(ourEntry
+1, OID_DESCR_START
);
210 /* handle not found/overflow */
211 if( (descStart
== NULL
) || // no more description lines
212 ( (descStart
> nextEntry
) && // no description in THIS entry
213 (nextEntry
!= NULL
) ) ) { // make sure this is valid
218 /* set descStart to after the leader */
219 descStart
+= strlen(OID_DESCR_START
);
222 * descStart points to the text we're interested in.
223 * First find end of line, any style.
225 nextNl
= strchr(descStart
, '\n');
226 nextCr
= strchr(descStart
, '\r');
227 if((nextNl
== NULL
) && (nextCr
== NULL
)) {
228 /* no line terminator, go to eof */
229 eol
= (char *)configData
->Data
+ configData
->Length
;
231 else if(nextCr
== NULL
) {
234 else if(nextNl
== NULL
) {
237 else if(nextNl
< nextCr
) {
238 /* both present, take first one */
245 /* caller's string buf = remainder of description line */
246 len
= eol
- descStart
;
247 if(len
> (OID_PARSER_STRING_SIZE
- 1)) {
248 /* fixed-length output buf, avoid overflow */
249 len
= OID_PARSER_STRING_SIZE
- 1;
251 memcpy(strBuf
, descStart
, len
);
255 if(fullOidStr
!= NULL
) {
261 /*** OidParser class ***/
262 OidParser::OidParser(bool noConfig
)
268 configData
= readConfig();
272 OidParser::~OidParser()
274 if(configData
== NULL
) {
277 if(configData
->Data
!= NULL
) {
278 free(configData
->Data
);
284 * Parse an Intel-style OID, generating a C string in caller-supplied buffer.
286 void OidParser::oidParse(
287 const unsigned char *oidp
,
294 oid
.Data
= (uint8
*)oidp
;
297 if((oidLen
== 0) || (oidp
== NULL
)) {
298 strcpy(strBuf
, "EMPTY");
301 if(parseOidWithConfig(configData
, &oid
, strBuf
) == CSSM_FALSE
) {
302 /* no config file, just dump the bytes */
305 sprintf(strBuf
, "OID : < 06 %02X ", (unsigned)oid
.Length
);
306 for(i
=0; i
<oid
.Length
; i
++) {
307 sprintf(cbuf
, "%02X ", oid
.Data
[i
]);
308 strcat(strBuf
, cbuf
);