]>
git.saurik.com Git - apple/security.git/blob - CertTool/cdsaUtils/oidParser.cpp
a25a9be0d5a326015e58ebb94efc8002b0ad7a7e
2 * oidParser.cpp - parse an Intel-style OID, with the assistance of dumpasn1.cfg
5 #include <Security/cssmtype.h>
15 #include <sys/types.h>
19 /* get config file from .. or from . */
20 #define CONFIG_FILE_NAME "dumpasn1.cfg"
21 static char *CONFIG_FILE1
= "../"CONFIG_FILE_NAME
;
22 static char *CONFIG_FILE2
= CONFIG_FILE_NAME
;
23 /* or from here via getenv */
24 #define CONFIG_FILE_ENV "LOCAL_BUILD_DIR"
26 static char *OID_ENTRY_START
= "OID = ";
27 static char *OID_DESCR_START
= "Description = ";
29 * Read entire file with extra bytes left over in the mallocd buffer.
34 unsigned char **bytes
, // mallocd and returned
35 unsigned *numBytes
) // returned
45 fd
= open(fileName
, O_RDONLY
, 0);
54 buf
= (unsigned char *)malloc(size
+ extraBytes
);
59 rtn
= lseek(fd
, 0, SEEK_SET
);
63 rtn
= read(fd
, buf
, (size_t)size
);
64 if(rtn
!= (int)size
) {
66 printf("readFile: short read\n");
81 * Attempt to read dumpasn1.cfg from various places. If we can't find it,
82 * printOid() function will just print raw bytes as it
83 * would if the .cfg file did not contain the desired OID.
85 static CSSM_DATA_PTR
readConfig()
87 CSSM_DATA_PTR configData
= NULL
;
90 configData
= (CSSM_DATA_PTR
)malloc(sizeof(CSSM_DATA
));
91 if(configData
== NULL
) {
94 /* malloc one extra byte, we'll null it later */
95 rtn
= readFileExtra(CONFIG_FILE1
, 1, &configData
->Data
,
96 (unsigned *)&configData
->Length
);
98 rtn
= readFileExtra(CONFIG_FILE2
, 1, &configData
->Data
,
99 (unsigned *)&configData
->Length
);
103 char *localBuildDir
= getenv(CONFIG_FILE_ENV
);
104 if(localBuildDir
== NULL
) {
108 sprintf(fileName
, "%s/%s", localBuildDir
, CONFIG_FILE_NAME
);
109 rtn
= readFileExtra(fileName
, 1, &configData
->Data
,
110 (unsigned *)&configData
->Length
);
114 /* make the whole shebang one long C string */
115 configData
->Data
[configData
->Length
++] = '\0';
119 printf("""warning: no OID parser config file\n");
126 * The heart of this module.
128 * -- Convert Intel-style OID to a string which might be found
130 * -- search config file for that string
131 * -- if found, use that entry in config file to output meaningful
132 * string and return CSSM_TRUE. Else return CSSM_FALSE.
134 static CSSM_BOOL
parseOidWithConfig(
135 const CSSM_DATA_PTR configData
,
136 const CSSM_OID_PTR oid
,
139 char *fullOidStr
= NULL
;
140 char *ourEntry
= NULL
;
141 char *nextEntry
= NULL
;
142 char *descStart
= NULL
;
146 char *nextCr
; // next CR if any
147 char *nextNl
; // next NL if any
148 char *eol
; // end of line
151 if(configData
== NULL
) {
155 /* cook up a full OID string, with tag and length */
156 fullOidStr
= (char *)malloc((3 * oid
->Length
) +
157 // 2 chars plus space per byte
158 strlen(OID_ENTRY_START
) + // "OID = "
159 6 + // 06 xx - tag and length
161 if(fullOidStr
== NULL
) {
164 /* subsequent errors to errOut: */
166 sprintf(fullOidStr
, "OID = 06 %02X", (unsigned)oid
->Length
);
167 cp
= fullOidStr
+ strlen(fullOidStr
);
168 for(i
=0; i
<oid
->Length
; i
++) {
169 /* move cp to current end of string */
172 sprintf(cp
, " %02X", oid
->Data
[i
]);
176 * Let's play it loose and assume that there are no embedded NULLs
177 * in the config file. Thus we can use the spiffy string functions
180 ourEntry
= strstr((char *)configData
->Data
, fullOidStr
);
181 if(ourEntry
== NULL
) {
186 /* get position of NEXT full entry - may be NULL (end of file) */
187 nextEntry
= strstr(ourEntry
+1, OID_ENTRY_START
);
189 /* get position of our entry's description line */
190 descStart
= strstr(ourEntry
+1, OID_DESCR_START
);
192 /* handle not found/overflow */
193 if( (descStart
== NULL
) || // no more description lines
194 ( (descStart
> nextEntry
) && // no description in THIS entry
195 (nextEntry
!= NULL
) ) ) { // make sure this is valid
200 /* set descStart to after the leader */
201 descStart
+= strlen(OID_DESCR_START
);
204 * descStart points to the text we're interested in.
205 * First find end of line, any style.
207 nextNl
= strchr(descStart
, '\n');
208 nextCr
= strchr(descStart
, '\r');
209 if((nextNl
== NULL
) && (nextCr
== NULL
)) {
210 /* no line terminator, go to eof */
211 eol
= (char *)configData
->Data
+ configData
->Length
;
213 else if(nextCr
== NULL
) {
216 else if(nextNl
== NULL
) {
219 else if(nextNl
< nextCr
) {
220 /* both present, take first one */
227 /* caller's string buf = remainder of description line */
228 len
= eol
- descStart
;
229 if(len
> (OID_PARSER_STRING_SIZE
- 1)) {
230 /* fixed-length output buf, avoid overflow */
231 len
= OID_PARSER_STRING_SIZE
- 1;
233 memcpy(strBuf
, descStart
, len
);
237 if(fullOidStr
!= NULL
) {
243 /*** OidParser class ***/
244 OidParser::OidParser(bool noConfig
)
250 configData
= readConfig();
254 OidParser::~OidParser()
256 if(configData
== NULL
) {
259 if(configData
->Data
!= NULL
) {
260 free(configData
->Data
);
266 * Parse an Intel-style OID, generating a C string in caller-supplied buffer.
268 void OidParser::oidParse(
269 const unsigned char *oidp
,
276 oid
.Data
= (uint8
*)oidp
;
279 if((oidLen
== 0) || (oidp
== NULL
)) {
280 strcpy(strBuf
, "EMPTY");
283 if(parseOidWithConfig(configData
, &oid
, strBuf
) == CSSM_FALSE
) {
284 /* no config file, just dump the bytes */
287 sprintf(strBuf
, "OID : < 06 %02X ", (unsigned)oid
.Length
);
288 for(i
=0; i
<oid
.Length
; i
++) {
289 sprintf(cbuf
, "%02X ", oid
.Data
[i
]);
290 strcat(strBuf
, cbuf
);