summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/asn1_lib.c
diff options
context:
space:
mode:
authortedu <>2014-04-18 00:58:49 +0000
committertedu <>2014-04-18 00:58:49 +0000
commita1df5b66c7ad477a535e56a504d8da47e1998cfa (patch)
treeb216ca6fba673789f2c494699c4cf41d23337bfa /src/lib/libcrypto/asn1/asn1_lib.c
parent07f5c09b19f56c323fa22ebd5efb5a4df9f5dc4d (diff)
downloadopenbsd-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_lib.c')
-rw-r--r--src/lib/libcrypto/asn1/asn1_lib.c92
1 files changed, 30 insertions, 62 deletions
diff --git a/src/lib/libcrypto/asn1/asn1_lib.c b/src/lib/libcrypto/asn1/asn1_lib.c
index 6ec383859f..7693556281 100644
--- a/src/lib/libcrypto/asn1/asn1_lib.c
+++ b/src/lib/libcrypto/asn1/asn1_lib.c
@@ -72,8 +72,7 @@ static int _asn1_check_infinite_end(const unsigned char **p, long len)
72 * things up */ 72 * things up */
73 if (len <= 0) 73 if (len <= 0)
74 return(1); 74 return(1);
75 else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) 75 else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) {
76 {
77 (*p)+=2; 76 (*p)+=2;
78 return(1); 77 return(1);
79 } 78 }
@@ -104,13 +103,11 @@ int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
104 ret=(*p&V_ASN1_CONSTRUCTED); 103 ret=(*p&V_ASN1_CONSTRUCTED);
105 xclass=(*p&V_ASN1_PRIVATE); 104 xclass=(*p&V_ASN1_PRIVATE);
106 i= *p&V_ASN1_PRIMITIVE_TAG; 105 i= *p&V_ASN1_PRIMITIVE_TAG;
107 if (i == V_ASN1_PRIMITIVE_TAG) 106 if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */
108 { /* high-tag */
109 p++; 107 p++;
110 if (--max == 0) goto err; 108 if (--max == 0) goto err;
111 l=0; 109 l=0;
112 while (*p&0x80) 110 while (*p&0x80) {
113 {
114 l<<=7L; 111 l<<=7L;
115 l|= *(p++)&0x7f; 112 l|= *(p++)&0x7f;
116 if (--max == 0) goto err; 113 if (--max == 0) goto err;
@@ -120,9 +117,7 @@ int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
120 l|= *(p++)&0x7f; 117 l|= *(p++)&0x7f;
121 tag=(int)l; 118 tag=(int)l;
122 if (--max == 0) goto err; 119 if (--max == 0) goto err;
123 } 120 } else {
124 else
125 {
126 tag=i; 121 tag=i;
127 p++; 122 p++;
128 if (--max == 0) goto err; 123 if (--max == 0) goto err;
@@ -137,8 +132,7 @@ int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
137 (int)(omax+ *pp)); 132 (int)(omax+ *pp));
138 133
139#endif 134#endif
140 if (*plength > (omax - (p - *pp))) 135 if (*plength > (omax - (p - *pp))) {
141 {
142 ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_TOO_LONG); 136 ASN1err(ASN1_F_ASN1_GET_OBJECT,ASN1_R_TOO_LONG);
143 /* Set this so that even if things are not long enough 137 /* Set this so that even if things are not long enough
144 * the values are set correctly */ 138 * the values are set correctly */
@@ -158,23 +152,18 @@ static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, int max
158 unsigned int i; 152 unsigned int i;
159 153
160 if (max-- < 1) return(0); 154 if (max-- < 1) return(0);
161 if (*p == 0x80) 155 if (*p == 0x80) {
162 {
163 *inf=1; 156 *inf=1;
164 ret=0; 157 ret=0;
165 p++; 158 p++;
166 } 159 } else {
167 else
168 {
169 *inf=0; 160 *inf=0;
170 i= *p&0x7f; 161 i= *p&0x7f;
171 if (*(p++) & 0x80) 162 if (*(p++) & 0x80) {
172 {
173 if (i > sizeof(long)) 163 if (i > sizeof(long))
174 return 0; 164 return 0;
175 if (max-- == 0) return(0); 165 if (max-- == 0) return(0);
176 while (i-- > 0) 166 while (i-- > 0) {
177 {
178 ret<<=8L; 167 ret<<=8L;
179 ret|= *(p++); 168 ret|= *(p++);
180 if (max-- == 0) return(0); 169 if (max-- == 0) return(0);
@@ -202,13 +191,11 @@ void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
202 i|=(xclass&V_ASN1_PRIVATE); 191 i|=(xclass&V_ASN1_PRIVATE);
203 if (tag < 31) 192 if (tag < 31)
204 *(p++)=i|(tag&V_ASN1_PRIMITIVE_TAG); 193 *(p++)=i|(tag&V_ASN1_PRIMITIVE_TAG);
205 else 194 else {
206 {
207 *(p++)=i|V_ASN1_PRIMITIVE_TAG; 195 *(p++)=i|V_ASN1_PRIMITIVE_TAG;
208 for(i = 0, ttag = tag; ttag > 0; i++) ttag >>=7; 196 for(i = 0, ttag = tag; ttag > 0; i++) ttag >>=7;
209 ttag = i; 197 ttag = i;
210 while(i-- > 0) 198 while(i-- > 0) {
211 {
212 p[i] = tag & 0x7f; 199 p[i] = tag & 0x7f;
213 if(i != (ttag - 1)) p[i] |= 0x80; 200 if(i != (ttag - 1)) p[i] |= 0x80;
214 tag >>= 7; 201 tag >>= 7;
@@ -237,15 +224,13 @@ static void asn1_put_length(unsigned char **pp, int length)
237 int i,l; 224 int i,l;
238 if (length <= 127) 225 if (length <= 127)
239 *(p++)=(unsigned char)length; 226 *(p++)=(unsigned char)length;
240 else 227 else {
241 {
242 l=length; 228 l=length;
243 for (i=0; l > 0; i++) 229 for (i=0; l > 0; i++)
244 l>>=8; 230 l>>=8;
245 *(p++)=i|0x80; 231 *(p++)=i|0x80;
246 l=i; 232 l=i;
247 while (i-- > 0) 233 while (i-- > 0) {
248 {
249 p[i]=length&0xff; 234 p[i]=length&0xff;
250 length>>=8; 235 length>>=8;
251 } 236 }
@@ -260,10 +245,8 @@ int ASN1_object_size(int constructed, int length, int tag)
260 245
261 ret=length; 246 ret=length;
262 ret++; 247 ret++;
263 if (tag >= 31) 248 if (tag >= 31) {
264 { 249 while (tag > 0) {
265 while (tag > 0)
266 {
267 tag>>=7; 250 tag>>=7;
268 ret++; 251 ret++;
269 } 252 }
@@ -271,10 +254,8 @@ int ASN1_object_size(int constructed, int length, int tag)
271 if (constructed == 2) 254 if (constructed == 2)
272 return ret + 3; 255 return ret + 3;
273 ret++; 256 ret++;
274 if (length > 127) 257 if (length > 127) {
275 { 258 while (length > 0) {
276 while (length > 0)
277 {
278 length>>=8; 259 length>>=8;
279 ret++; 260 ret++;
280 } 261 }
@@ -284,17 +265,14 @@ int ASN1_object_size(int constructed, int length, int tag)
284 265
285static int _asn1_Finish(ASN1_const_CTX *c) 266static int _asn1_Finish(ASN1_const_CTX *c)
286{ 267{
287 if ((c->inf == (1|V_ASN1_CONSTRUCTED)) && (!c->eos)) 268 if ((c->inf == (1|V_ASN1_CONSTRUCTED)) && (!c->eos)) {
288 { 269 if (!ASN1_const_check_infinite_end(&c->p,c->slen)) {
289 if (!ASN1_const_check_infinite_end(&c->p,c->slen))
290 {
291 c->error=ERR_R_MISSING_ASN1_EOS; 270 c->error=ERR_R_MISSING_ASN1_EOS;
292 return(0); 271 return(0);
293 } 272 }
294 } 273 }
295 if ( ((c->slen != 0) && !(c->inf & 1)) || 274 if ( ((c->slen != 0) && !(c->inf & 1)) ||
296 ((c->slen < 0) && (c->inf & 1))) 275 ((c->slen < 0) && (c->inf & 1))) {
297 {
298 c->error=ERR_R_ASN1_LENGTH_MISMATCH; 276 c->error=ERR_R_ASN1_LENGTH_MISMATCH;
299 return(0); 277 return(0);
300 } 278 }
@@ -318,19 +296,16 @@ int asn1_GetSequence(ASN1_const_CTX *c, long *length)
318 q=c->p; 296 q=c->p;
319 c->inf=ASN1_get_object(&(c->p),&(c->slen),&(c->tag),&(c->xclass), 297 c->inf=ASN1_get_object(&(c->p),&(c->slen),&(c->tag),&(c->xclass),
320 *length); 298 *length);
321 if (c->inf & 0x80) 299 if (c->inf & 0x80) {
322 {
323 c->error=ERR_R_BAD_GET_ASN1_OBJECT_CALL; 300 c->error=ERR_R_BAD_GET_ASN1_OBJECT_CALL;
324 return(0); 301 return(0);
325 } 302 }
326 if (c->tag != V_ASN1_SEQUENCE) 303 if (c->tag != V_ASN1_SEQUENCE) {
327 {
328 c->error=ERR_R_EXPECTING_AN_ASN1_SEQUENCE; 304 c->error=ERR_R_EXPECTING_AN_ASN1_SEQUENCE;
329 return(0); 305 return(0);
330 } 306 }
331 (*length)-=(c->p-q); 307 (*length)-=(c->p-q);
332 if (c->max && (*length < 0)) 308 if (c->max && (*length < 0)) {
333 {
334 c->error=ERR_R_ASN1_LENGTH_MISMATCH; 309 c->error=ERR_R_ASN1_LENGTH_MISMATCH;
335 return(0); 310 return(0);
336 } 311 }
@@ -359,8 +334,7 @@ ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
359 ret=ASN1_STRING_new(); 334 ret=ASN1_STRING_new();
360 if (!ret) 335 if (!ret)
361 return NULL; 336 return NULL;
362 if (!ASN1_STRING_copy(ret,str)) 337 if (!ASN1_STRING_copy(ret,str)) {
363 {
364 ASN1_STRING_free(ret); 338 ASN1_STRING_free(ret);
365 return NULL; 339 return NULL;
366 } 340 }
@@ -372,31 +346,27 @@ int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
372 unsigned char *c; 346 unsigned char *c;
373 const char *data=_data; 347 const char *data=_data;
374 348
375 if (len < 0) 349 if (len < 0) {
376 {
377 if (data == NULL) 350 if (data == NULL)
378 return(0); 351 return(0);
379 else 352 else
380 len=strlen(data); 353 len=strlen(data);
381 } 354 }
382 if ((str->length < len) || (str->data == NULL)) 355 if ((str->length < len) || (str->data == NULL)) {
383 {
384 c=str->data; 356 c=str->data;
385 if (c == NULL) 357 if (c == NULL)
386 str->data=malloc(len+1); 358 str->data=malloc(len+1);
387 else 359 else
388 str->data=realloc(c,len+1); 360 str->data=realloc(c,len+1);
389 361
390 if (str->data == NULL) 362 if (str->data == NULL) {
391 {
392 ASN1err(ASN1_F_ASN1_STRING_SET,ERR_R_MALLOC_FAILURE); 363 ASN1err(ASN1_F_ASN1_STRING_SET,ERR_R_MALLOC_FAILURE);
393 str->data=c; 364 str->data=c;
394 return(0); 365 return(0);
395 } 366 }
396 } 367 }
397 str->length=len; 368 str->length=len;
398 if (data != NULL) 369 if (data != NULL) {
399 {
400 memcpy(str->data,data,len); 370 memcpy(str->data,data,len);
401 /* an allowance for strings :-) */ 371 /* an allowance for strings :-) */
402 str->data[len]='\0'; 372 str->data[len]='\0';
@@ -423,8 +393,7 @@ ASN1_STRING *ASN1_STRING_type_new(int type)
423 ASN1_STRING *ret; 393 ASN1_STRING *ret;
424 394
425 ret=(ASN1_STRING *)malloc(sizeof(ASN1_STRING)); 395 ret=(ASN1_STRING *)malloc(sizeof(ASN1_STRING));
426 if (ret == NULL) 396 if (ret == NULL) {
427 {
428 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE); 397 ASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE);
429 return(NULL); 398 return(NULL);
430 } 399 }
@@ -448,8 +417,7 @@ int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
448 int i; 417 int i;
449 418
450 i=(a->length-b->length); 419 i=(a->length-b->length);
451 if (i == 0) 420 if (i == 0) {
452 {
453 i=memcmp(a->data,b->data,a->length); 421 i=memcmp(a->data,b->data,a->length);
454 if (i == 0) 422 if (i == 0)
455 return(a->type-b->type); 423 return(a->type-b->type);