]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cdsa_utils/lib/cuOidParser.cpp
Security-57740.31.2.tar.gz
[apple/security.git] / OSX / libsecurity_cdsa_utils / lib / cuOidParser.cpp
1 /*
2 * Copyright (c) 2002-2003,2011-2012,2014-2016 Apple Inc. All Rights Reserved.
3 *
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.
9 *
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.
17 */
18
19 /*
20 * cuOidParser.cpp - parse an Intel-style OID, with the assistance
21 * of dumpasn1.cfg
22 */
23
24 #include <Security/cssmtype.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include "cuOidParser.h"
29 #include "cuFileIo.h"
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37
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"
44
45 static const char *OID_ENTRY_START = "OID = ";
46 static const char *OID_DESCR_START = "Description = ";
47 /*
48 * Read entire file with extra bytes left over in the mallocd buffer.
49 */
50 static
51 int readFileExtra(
52 const char *fileName,
53 unsigned extraBytes,
54 unsigned char **bytes, // mallocd and returned
55 CSSM_SIZE *numBytes) // returned
56 {
57 int rtn;
58 int fd;
59 unsigned char *buf;
60 struct stat sb;
61 size_t size;
62
63 *numBytes = 0;
64 *bytes = NULL;
65 fd = open(fileName, O_RDONLY, 0);
66 if(fd < 0) {
67 return 1;
68 }
69 rtn = fstat(fd, &sb);
70 if(rtn) {
71 goto errOut;
72 }
73 size = (size_t)sb.st_size;
74 buf = (unsigned char *)malloc(size + extraBytes);
75 if(buf == NULL) {
76 rtn = ENOMEM;
77 goto errOut;
78 }
79 rtn = (int)lseek(fd, 0, SEEK_SET);
80 if(rtn < 0) {
81 goto errOut;
82 }
83 rtn = (int)read(fd, buf, (size_t)size);
84 if(rtn != (int)size) {
85 if(rtn >= 0) {
86 printf("readFile: short read\n");
87 }
88 rtn = EIO;
89 }
90 else {
91 rtn = 0;
92 *bytes = buf;
93 *numBytes = size;
94 }
95 errOut:
96 close(fd);
97 return rtn;
98 }
99
100 /*
101 * Attempt to read dumpasn1.cfg from various places. If we can't find it,
102 * printOid() function will just print raw bytes as it
103 * would if the .cfg file did not contain the desired OID.
104 */
105 static CSSM_DATA_PTR readConfig()
106 {
107 CSSM_DATA_PTR configData = NULL;
108 int rtn;
109
110 configData = (CSSM_DATA_PTR)malloc(sizeof(CSSM_DATA));
111 if(configData == NULL) {
112 return NULL;
113 }
114 /* malloc one extra byte, we'll null it later */
115 rtn = readFileExtra(CONFIG_FILE1, 1, &configData->Data,
116 &configData->Length);
117 if(rtn) {
118 rtn = readFileExtra(CONFIG_FILE2, 1, &configData->Data,
119 &configData->Length);
120 }
121 if(rtn) {
122 char *localBuildDir = getenv(CONFIG_FILE_ENV);
123 if(localBuildDir == NULL) {
124 rtn = 1;
125 }
126 else {
127 char *pathBuf = NULL;
128 rtn = asprintf(&pathBuf, "%s/%s", localBuildDir, CONFIG_FILE_NAME);
129 if (rtn < 1 || !pathBuf) {
130 rtn = 1;
131 }
132 else {
133 rtn = readFileExtra(pathBuf, 1, &configData->Data,
134 &configData->Length);
135 }
136 if (pathBuf) {
137 free(pathBuf);
138 }
139 }
140 }
141 if(rtn == 0) {
142 /* make the whole shebang one long C string */
143 configData->Data[configData->Length++] = '\0';
144 return configData;
145 }
146 else {
147 free(configData);
148 return NULL;
149 }
150 }
151
152 /*
153 * The heart of this module.
154 *
155 * -- Convert Intel-style OID to a string which might be found
156 * in the config file
157 * -- search config file for that string
158 * -- if found, use that entry in config file to output meaningful
159 * string and return CSSM_TRUE. Else return CSSM_FALSE.
160 */
161 static CSSM_BOOL parseOidWithConfig(
162 const CSSM_DATA_PTR configData,
163 const CSSM_OID_PTR oid,
164 char *strBuf)
165 {
166 char *fullOidStr = NULL;
167 char *ourEntry = NULL;
168 char *nextEntry = NULL;
169 char *descStart = NULL;
170 char *cp;
171 unsigned i;
172 CSSM_BOOL brtn;
173 char *nextCr; // next CR if any
174 char *nextNl; // next NL if any
175 char *eol; // end of line
176 int len;
177
178 if(configData == NULL) {
179 return CSSM_FALSE;
180 }
181
182 /* cook up a full OID string, with tag and length */
183 fullOidStr = (char *)malloc((3 * oid->Length) +
184 // 2 chars plus space per byte
185 strlen(OID_ENTRY_START) + // "OID = "
186 6 + // 06 xx - tag and length
187 1); // NULL
188 if(fullOidStr == NULL) {
189 return CSSM_FALSE;
190 }
191 /* subsequent errors to errOut: */
192
193 sprintf(fullOidStr, "OID = 06 %02X", (unsigned)oid->Length);
194 cp = fullOidStr + strlen(fullOidStr);
195 for(i=0; i<oid->Length; i++) {
196 /* move cp to current end of string */
197 cp += strlen(cp);
198 /* add one byte */
199 sprintf(cp, " %02X", oid->Data[i]);
200 }
201
202 /*
203 * Let's play it loose and assume that there are no embedded NULLs
204 * in the config file. Thus we can use the spiffy string functions
205 * in stdlib.
206 */
207 ourEntry = strstr((char *)configData->Data, fullOidStr);
208 if(ourEntry == NULL) {
209 brtn = CSSM_FALSE;
210 goto errOut;
211 }
212
213 /* get position of NEXT full entry - may be NULL (end of file) */
214 nextEntry = strstr(ourEntry+1, OID_ENTRY_START);
215
216 /* get position of our entry's description line */
217 descStart = strstr(ourEntry+1, OID_DESCR_START);
218
219 /* handle not found/overflow */
220 if( (descStart == NULL) || // no more description lines
221 ( (descStart > nextEntry) && // no description in THIS entry
222 (nextEntry != NULL) ) ) { // make sure this is valid
223 brtn = CSSM_FALSE;
224 goto errOut;
225 }
226
227 /* set descStart to after the leader */
228 descStart += strlen(OID_DESCR_START);
229
230 /*
231 * descStart points to the text we're interested in.
232 * First find end of line, any style.
233 */
234 nextNl = strchr(descStart, '\n');
235 nextCr = strchr(descStart, '\r');
236 if((nextNl == NULL) && (nextCr == NULL)) {
237 /* no line terminator, go to eof */
238 eol = (char *)configData->Data + configData->Length;
239 }
240 else if(nextCr == NULL) {
241 eol = nextNl;
242 }
243 else if(nextNl == NULL) {
244 eol = nextCr;
245 }
246 else if(nextNl < nextCr) {
247 /* both present, take first one */
248 eol = nextNl;
249 }
250 else {
251 eol = nextCr;
252 }
253
254 /* caller's string buf = remainder of description line */
255 len = (int)(eol - descStart);
256 if(len > (OID_PARSER_STRING_SIZE - 1)) {
257 /* fixed-length output buf, avoid overflow */
258 len = OID_PARSER_STRING_SIZE - 1;
259 }
260 memcpy(strBuf, descStart, len);
261 strBuf[len] = '\0';
262 brtn = CSSM_TRUE;
263 errOut:
264 if(fullOidStr != NULL) {
265 free(fullOidStr);
266 }
267 return brtn;
268 }
269
270 /*** OidParser class ***/
271 OidParser::OidParser(bool noConfig)
272 {
273 if(noConfig) {
274 configData = NULL;
275 }
276 else {
277 configData = readConfig();
278 }
279 }
280
281 OidParser::~OidParser()
282 {
283 if(configData == NULL) {
284 return;
285 }
286 if(configData->Data != NULL) {
287 free(configData->Data);
288 }
289 free(configData);
290 }
291
292 /*
293 * Parse an Intel-style OID, generating a C string in caller-supplied buffer.
294 */
295 void OidParser::oidParse(
296 const unsigned char *oidp,
297 unsigned oidLen,
298 char *strBuf)
299 {
300 unsigned i;
301 CSSM_OID oid;
302
303 oid.Data = (uint8 *)oidp;
304 oid.Length = oidLen;
305
306 if((oidLen == 0) || (oidp == NULL)) {
307 strcpy(strBuf, "EMPTY");
308 return;
309 }
310 if(parseOidWithConfig(configData, &oid, strBuf) == CSSM_FALSE) {
311 /* no config file, just dump the bytes */
312 char cbuf[8];
313
314 sprintf(strBuf, "OID : < 06 %02X ", (unsigned)oid.Length);
315 for(i=0; i<oid.Length; i++) {
316 sprintf(cbuf, "%02X ", oid.Data[i]);
317 strcat(strBuf, cbuf);
318 }
319 strcat(strBuf, ">");
320 }
321 }
322
323