diff options
author | tedu <> | 2014-04-18 00:58:49 +0000 |
---|---|---|
committer | tedu <> | 2014-04-18 00:58:49 +0000 |
commit | a1df5b66c7ad477a535e56a504d8da47e1998cfa (patch) | |
tree | b216ca6fba673789f2c494699c4cf41d23337bfa /src/lib/libcrypto/asn1/asn1_par.c | |
parent | 07f5c09b19f56c323fa22ebd5efb5a4df9f5dc4d (diff) | |
download | openbsd-a1df5b66c7ad477a535e56a504d8da47e1998cfa.tar.gz openbsd-a1df5b66c7ad477a535e56a504d8da47e1998cfa.tar.bz2 openbsd-a1df5b66c7ad477a535e56a504d8da47e1998cfa.zip |
lob a few more knf grenades in here to soften things up.
Diffstat (limited to 'src/lib/libcrypto/asn1/asn1_par.c')
-rw-r--r-- | src/lib/libcrypto/asn1/asn1_par.c | 143 |
1 files changed, 43 insertions, 100 deletions
diff --git a/src/lib/libcrypto/asn1/asn1_par.c b/src/lib/libcrypto/asn1/asn1_par.c index d68d4a8298..d3d2f52481 100644 --- a/src/lib/libcrypto/asn1/asn1_par.c +++ b/src/lib/libcrypto/asn1/asn1_par.c | |||
@@ -129,15 +129,13 @@ static int asn1_parse2(BIO *bp, const unsigned char **pp, long length, int offse | |||
129 | p= *pp; | 129 | p= *pp; |
130 | tot=p+length; | 130 | tot=p+length; |
131 | op=p-1; | 131 | op=p-1; |
132 | while ((p < tot) && (op < p)) | 132 | while ((p < tot) && (op < p)) { |
133 | { | ||
134 | op=p; | 133 | op=p; |
135 | j=ASN1_get_object(&p,&len,&tag,&xclass,length); | 134 | j=ASN1_get_object(&p,&len,&tag,&xclass,length); |
136 | #ifdef LINT | 135 | #ifdef LINT |
137 | j=j; | 136 | j=j; |
138 | #endif | 137 | #endif |
139 | if (j & 0x80) | 138 | if (j & 0x80) { |
140 | { | ||
141 | if (BIO_write(bp,"Error in encoding\n",18) <= 0) | 139 | if (BIO_write(bp,"Error in encoding\n",18) <= 0) |
142 | goto end; | 140 | goto end; |
143 | ret=0; | 141 | ret=0; |
@@ -149,35 +147,28 @@ static int asn1_parse2(BIO *bp, const unsigned char **pp, long length, int offse | |||
149 | if (BIO_printf(bp,"%5ld:",(long)offset+(long)(op- *pp)) | 147 | if (BIO_printf(bp,"%5ld:",(long)offset+(long)(op- *pp)) |
150 | <= 0) goto end; | 148 | <= 0) goto end; |
151 | 149 | ||
152 | if (j != (V_ASN1_CONSTRUCTED | 1)) | 150 | if (j != (V_ASN1_CONSTRUCTED | 1)) { |
153 | { | ||
154 | if (BIO_printf(bp,"d=%-2d hl=%ld l=%4ld ", | 151 | if (BIO_printf(bp,"d=%-2d hl=%ld l=%4ld ", |
155 | depth,(long)hl,len) <= 0) | 152 | depth,(long)hl,len) <= 0) |
156 | goto end; | 153 | goto end; |
157 | } | 154 | } else { |
158 | else | ||
159 | { | ||
160 | if (BIO_printf(bp,"d=%-2d hl=%ld l=inf ", | 155 | if (BIO_printf(bp,"d=%-2d hl=%ld l=inf ", |
161 | depth,(long)hl) <= 0) | 156 | depth,(long)hl) <= 0) |
162 | goto end; | 157 | goto end; |
163 | } | 158 | } |
164 | if (!asn1_print_info(bp,tag,xclass,j,(indent)?depth:0)) | 159 | if (!asn1_print_info(bp,tag,xclass,j,(indent)?depth:0)) |
165 | goto end; | 160 | goto end; |
166 | if (j & V_ASN1_CONSTRUCTED) | 161 | if (j & V_ASN1_CONSTRUCTED) { |
167 | { | ||
168 | ep=p+len; | 162 | ep=p+len; |
169 | if (BIO_write(bp,"\n",1) <= 0) goto end; | 163 | if (BIO_write(bp,"\n",1) <= 0) goto end; |
170 | if (len > length) | 164 | if (len > length) { |
171 | { | ||
172 | BIO_printf(bp, | 165 | BIO_printf(bp, |
173 | "length is greater than %ld\n",length); | 166 | "length is greater than %ld\n",length); |
174 | ret=0; | 167 | ret=0; |
175 | goto end; | 168 | goto end; |
176 | } | 169 | } |
177 | if ((j == 0x21) && (len == 0)) | 170 | if ((j == 0x21) && (len == 0)) { |
178 | { | 171 | for (;;) { |
179 | for (;;) | ||
180 | { | ||
181 | r=asn1_parse2(bp,&p,(long)(tot-p), | 172 | r=asn1_parse2(bp,&p,(long)(tot-p), |
182 | offset+(p - *pp),depth+1, | 173 | offset+(p - *pp),depth+1, |
183 | indent,dump); | 174 | indent,dump); |
@@ -186,21 +177,16 @@ static int asn1_parse2(BIO *bp, const unsigned char **pp, long length, int offse | |||
186 | } | 177 | } |
187 | } | 178 | } |
188 | else | 179 | else |
189 | while (p < ep) | 180 | while (p < ep) { |
190 | { | ||
191 | r=asn1_parse2(bp,&p,(long)len, | 181 | r=asn1_parse2(bp,&p,(long)len, |
192 | offset+(p - *pp),depth+1, | 182 | offset+(p - *pp),depth+1, |
193 | indent,dump); | 183 | indent,dump); |
194 | if (r == 0) { ret=0; goto end; } | 184 | if (r == 0) { ret=0; goto end; } |
195 | } | 185 | } |
196 | } | 186 | } else if (xclass != 0) { |
197 | else if (xclass != 0) | ||
198 | { | ||
199 | p+=len; | 187 | p+=len; |
200 | if (BIO_write(bp,"\n",1) <= 0) goto end; | 188 | if (BIO_write(bp,"\n",1) <= 0) goto end; |
201 | } | 189 | } else { |
202 | else | ||
203 | { | ||
204 | nl=0; | 190 | nl=0; |
205 | if ( (tag == V_ASN1_PRINTABLESTRING) || | 191 | if ( (tag == V_ASN1_PRINTABLESTRING) || |
206 | (tag == V_ASN1_T61STRING) || | 192 | (tag == V_ASN1_T61STRING) || |
@@ -209,95 +195,72 @@ static int asn1_parse2(BIO *bp, const unsigned char **pp, long length, int offse | |||
209 | (tag == V_ASN1_NUMERICSTRING) || | 195 | (tag == V_ASN1_NUMERICSTRING) || |
210 | (tag == V_ASN1_UTF8STRING) || | 196 | (tag == V_ASN1_UTF8STRING) || |
211 | (tag == V_ASN1_UTCTIME) || | 197 | (tag == V_ASN1_UTCTIME) || |
212 | (tag == V_ASN1_GENERALIZEDTIME)) | 198 | (tag == V_ASN1_GENERALIZEDTIME)) { |
213 | { | ||
214 | if (BIO_write(bp,":",1) <= 0) goto end; | 199 | if (BIO_write(bp,":",1) <= 0) goto end; |
215 | if ((len > 0) && | 200 | if ((len > 0) && |
216 | BIO_write(bp,(const char *)p,(int)len) | 201 | BIO_write(bp,(const char *)p,(int)len) |
217 | != (int)len) | 202 | != (int)len) |
218 | goto end; | 203 | goto end; |
219 | } | 204 | } else if (tag == V_ASN1_OBJECT) { |
220 | else if (tag == V_ASN1_OBJECT) | ||
221 | { | ||
222 | opp=op; | 205 | opp=op; |
223 | if (d2i_ASN1_OBJECT(&o,&opp,len+hl) != NULL) | 206 | if (d2i_ASN1_OBJECT(&o,&opp,len+hl) != NULL) { |
224 | { | ||
225 | if (BIO_write(bp,":",1) <= 0) goto end; | 207 | if (BIO_write(bp,":",1) <= 0) goto end; |
226 | i2a_ASN1_OBJECT(bp,o); | 208 | i2a_ASN1_OBJECT(bp,o); |
227 | } | 209 | } else { |
228 | else | ||
229 | { | ||
230 | if (BIO_write(bp,":BAD OBJECT",11) <= 0) | 210 | if (BIO_write(bp,":BAD OBJECT",11) <= 0) |
231 | goto end; | 211 | goto end; |
232 | } | 212 | } |
233 | } | 213 | } else if (tag == V_ASN1_BOOLEAN) { |
234 | else if (tag == V_ASN1_BOOLEAN) | ||
235 | { | ||
236 | int ii; | 214 | int ii; |
237 | 215 | ||
238 | opp=op; | 216 | opp=op; |
239 | ii=d2i_ASN1_BOOLEAN(NULL,&opp,len+hl); | 217 | ii=d2i_ASN1_BOOLEAN(NULL,&opp,len+hl); |
240 | if (ii < 0) | 218 | if (ii < 0) { |
241 | { | ||
242 | if (BIO_write(bp,"Bad boolean\n",12) <= 0) | 219 | if (BIO_write(bp,"Bad boolean\n",12) <= 0) |
243 | goto end; | 220 | goto end; |
244 | } | 221 | } |
245 | BIO_printf(bp,":%d",ii); | 222 | BIO_printf(bp,":%d",ii); |
246 | } | 223 | } else if (tag == V_ASN1_BMPSTRING) { |
247 | else if (tag == V_ASN1_BMPSTRING) | ||
248 | { | ||
249 | /* do the BMP thang */ | 224 | /* do the BMP thang */ |
250 | } | 225 | } else if (tag == V_ASN1_OCTET_STRING) { |
251 | else if (tag == V_ASN1_OCTET_STRING) | ||
252 | { | ||
253 | int i,printable=1; | 226 | int i,printable=1; |
254 | 227 | ||
255 | opp=op; | 228 | opp=op; |
256 | os=d2i_ASN1_OCTET_STRING(NULL,&opp,len+hl); | 229 | os=d2i_ASN1_OCTET_STRING(NULL,&opp,len+hl); |
257 | if (os != NULL && os->length > 0) | 230 | if (os != NULL && os->length > 0) { |
258 | { | ||
259 | opp = os->data; | 231 | opp = os->data; |
260 | /* testing whether the octet string is | 232 | /* testing whether the octet string is |
261 | * printable */ | 233 | * printable */ |
262 | for (i=0; i<os->length; i++) | 234 | for (i=0; i<os->length; i++) { |
263 | { | ||
264 | if (( (opp[i] < ' ') && | 235 | if (( (opp[i] < ' ') && |
265 | (opp[i] != '\n') && | 236 | (opp[i] != '\n') && |
266 | (opp[i] != '\r') && | 237 | (opp[i] != '\r') && |
267 | (opp[i] != '\t')) || | 238 | (opp[i] != '\t')) || |
268 | (opp[i] > '~')) | 239 | (opp[i] > '~')) { |
269 | { | ||
270 | printable=0; | 240 | printable=0; |
271 | break; | 241 | break; |
272 | } | 242 | } |
273 | } | 243 | } |
274 | if (printable) | 244 | if (printable) { |
275 | /* printable string */ | 245 | /* printable string */ |
276 | { | ||
277 | if (BIO_write(bp,":",1) <= 0) | 246 | if (BIO_write(bp,":",1) <= 0) |
278 | goto end; | 247 | goto end; |
279 | if (BIO_write(bp,(const char *)opp, | 248 | if (BIO_write(bp,(const char *)opp, |
280 | os->length) <= 0) | 249 | os->length) <= 0) |
281 | goto end; | 250 | goto end; |
282 | } | 251 | } else if (!dump) { |
283 | else if (!dump) | ||
284 | /* not printable => print octet string | 252 | /* not printable => print octet string |
285 | * as hex dump */ | 253 | * as hex dump */ |
286 | { | ||
287 | if (BIO_write(bp,"[HEX DUMP]:",11) <= 0) | 254 | if (BIO_write(bp,"[HEX DUMP]:",11) <= 0) |
288 | goto end; | 255 | goto end; |
289 | for (i=0; i<os->length; i++) | 256 | for (i=0; i<os->length; i++) { |
290 | { | ||
291 | if (BIO_printf(bp,"%02X" | 257 | if (BIO_printf(bp,"%02X" |
292 | , opp[i]) <= 0) | 258 | , opp[i]) <= 0) |
293 | goto end; | 259 | goto end; |
294 | } | 260 | } |
295 | } | 261 | } else { |
296 | else | ||
297 | /* print the normal dump */ | 262 | /* print the normal dump */ |
298 | { | 263 | if (!nl) { |
299 | if (!nl) | ||
300 | { | ||
301 | if (BIO_write(bp,"\n",1) <= 0) | 264 | if (BIO_write(bp,"\n",1) <= 0) |
302 | goto end; | 265 | goto end; |
303 | } | 266 | } |
@@ -310,80 +273,62 @@ static int asn1_parse2(BIO *bp, const unsigned char **pp, long length, int offse | |||
310 | nl=1; | 273 | nl=1; |
311 | } | 274 | } |
312 | } | 275 | } |
313 | if (os != NULL) | 276 | if (os != NULL) { |
314 | { | ||
315 | M_ASN1_OCTET_STRING_free(os); | 277 | M_ASN1_OCTET_STRING_free(os); |
316 | os=NULL; | 278 | os=NULL; |
317 | } | 279 | } |
318 | } | 280 | } else if (tag == V_ASN1_INTEGER) { |
319 | else if (tag == V_ASN1_INTEGER) | ||
320 | { | ||
321 | ASN1_INTEGER *bs; | 281 | ASN1_INTEGER *bs; |
322 | int i; | 282 | int i; |
323 | 283 | ||
324 | opp=op; | 284 | opp=op; |
325 | bs=d2i_ASN1_INTEGER(NULL,&opp,len+hl); | 285 | bs=d2i_ASN1_INTEGER(NULL,&opp,len+hl); |
326 | if (bs != NULL) | 286 | if (bs != NULL) { |
327 | { | ||
328 | if (BIO_write(bp,":",1) <= 0) goto end; | 287 | if (BIO_write(bp,":",1) <= 0) goto end; |
329 | if (bs->type == V_ASN1_NEG_INTEGER) | 288 | if (bs->type == V_ASN1_NEG_INTEGER) |
330 | if (BIO_write(bp,"-",1) <= 0) | 289 | if (BIO_write(bp,"-",1) <= 0) |
331 | goto end; | 290 | goto end; |
332 | for (i=0; i<bs->length; i++) | 291 | for (i=0; i<bs->length; i++) { |
333 | { | ||
334 | if (BIO_printf(bp,"%02X", | 292 | if (BIO_printf(bp,"%02X", |
335 | bs->data[i]) <= 0) | 293 | bs->data[i]) <= 0) |
336 | goto end; | 294 | goto end; |
337 | } | 295 | } |
338 | if (bs->length == 0) | 296 | if (bs->length == 0) { |
339 | { | ||
340 | if (BIO_write(bp,"00",2) <= 0) | 297 | if (BIO_write(bp,"00",2) <= 0) |
341 | goto end; | 298 | goto end; |
342 | } | 299 | } |
343 | } | 300 | } else { |
344 | else | ||
345 | { | ||
346 | if (BIO_write(bp,"BAD INTEGER",11) <= 0) | 301 | if (BIO_write(bp,"BAD INTEGER",11) <= 0) |
347 | goto end; | 302 | goto end; |
348 | } | 303 | } |
349 | M_ASN1_INTEGER_free(bs); | 304 | M_ASN1_INTEGER_free(bs); |
350 | } | 305 | } else if (tag == V_ASN1_ENUMERATED) { |
351 | else if (tag == V_ASN1_ENUMERATED) | ||
352 | { | ||
353 | ASN1_ENUMERATED *bs; | 306 | ASN1_ENUMERATED *bs; |
354 | int i; | 307 | int i; |
355 | 308 | ||
356 | opp=op; | 309 | opp=op; |
357 | bs=d2i_ASN1_ENUMERATED(NULL,&opp,len+hl); | 310 | bs=d2i_ASN1_ENUMERATED(NULL,&opp,len+hl); |
358 | if (bs != NULL) | 311 | if (bs != NULL) { |
359 | { | ||
360 | if (BIO_write(bp,":",1) <= 0) goto end; | 312 | if (BIO_write(bp,":",1) <= 0) goto end; |
361 | if (bs->type == V_ASN1_NEG_ENUMERATED) | 313 | if (bs->type == V_ASN1_NEG_ENUMERATED) |
362 | if (BIO_write(bp,"-",1) <= 0) | 314 | if (BIO_write(bp,"-",1) <= 0) |
363 | goto end; | 315 | goto end; |
364 | for (i=0; i<bs->length; i++) | 316 | for (i=0; i<bs->length; i++) { |
365 | { | ||
366 | if (BIO_printf(bp,"%02X", | 317 | if (BIO_printf(bp,"%02X", |
367 | bs->data[i]) <= 0) | 318 | bs->data[i]) <= 0) |
368 | goto end; | 319 | goto end; |
369 | } | 320 | } |
370 | if (bs->length == 0) | 321 | if (bs->length == 0) { |
371 | { | ||
372 | if (BIO_write(bp,"00",2) <= 0) | 322 | if (BIO_write(bp,"00",2) <= 0) |
373 | goto end; | 323 | goto end; |
374 | } | 324 | } |
375 | } | 325 | } else { |
376 | else | ||
377 | { | ||
378 | if (BIO_write(bp,"BAD ENUMERATED",14) <= 0) | 326 | if (BIO_write(bp,"BAD ENUMERATED",14) <= 0) |
379 | goto end; | 327 | goto end; |
380 | } | 328 | } |
381 | M_ASN1_ENUMERATED_free(bs); | 329 | M_ASN1_ENUMERATED_free(bs); |
382 | } | 330 | } else if (len > 0 && dump) { |
383 | else if (len > 0 && dump) | 331 | if (!nl) { |
384 | { | ||
385 | if (!nl) | ||
386 | { | ||
387 | if (BIO_write(bp,"\n",1) <= 0) | 332 | if (BIO_write(bp,"\n",1) <= 0) |
388 | goto end; | 333 | goto end; |
389 | } | 334 | } |
@@ -394,13 +339,11 @@ static int asn1_parse2(BIO *bp, const unsigned char **pp, long length, int offse | |||
394 | nl=1; | 339 | nl=1; |
395 | } | 340 | } |
396 | 341 | ||
397 | if (!nl) | 342 | if (!nl) { |
398 | { | ||
399 | if (BIO_write(bp,"\n",1) <= 0) goto end; | 343 | if (BIO_write(bp,"\n",1) <= 0) goto end; |
400 | } | 344 | } |
401 | p+=len; | 345 | p+=len; |
402 | if ((tag == V_ASN1_EOC) && (xclass == 0)) | 346 | if ((tag == V_ASN1_EOC) && (xclass == 0)) { |
403 | { | ||
404 | ret=2; /* End of sequence */ | 347 | ret=2; /* End of sequence */ |
405 | goto end; | 348 | goto end; |
406 | } | 349 | } |
@@ -429,7 +372,7 @@ const char *ASN1_tag2str(int tag) | |||
429 | }; | 372 | }; |
430 | 373 | ||
431 | if((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED)) | 374 | if((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED)) |
432 | tag &= ~0x100; | 375 | tag &= ~0x100; |
433 | 376 | ||
434 | if(tag < 0 || tag > 30) return "(unknown)"; | 377 | if(tag < 0 || tag > 30) return "(unknown)"; |
435 | return tag2str[tag]; | 378 | return tag2str[tag]; |