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/a_int.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/a_int.c')
-rw-r--r-- | src/lib/libcrypto/asn1/a_int.c | 80 |
1 files changed, 29 insertions, 51 deletions
diff --git a/src/lib/libcrypto/asn1/a_int.c b/src/lib/libcrypto/asn1/a_int.c index f41d462ffe..cc2b985fa1 100644 --- a/src/lib/libcrypto/asn1/a_int.c +++ b/src/lib/libcrypto/asn1/a_int.c | |||
@@ -69,8 +69,7 @@ int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) | |||
69 | int neg, ret; | 69 | int neg, ret; |
70 | /* Compare signs */ | 70 | /* Compare signs */ |
71 | neg = x->type & V_ASN1_NEG; | 71 | neg = x->type & V_ASN1_NEG; |
72 | if (neg != (y->type & V_ASN1_NEG)) | 72 | if (neg != (y->type & V_ASN1_NEG)) { |
73 | { | ||
74 | if (neg) | 73 | if (neg) |
75 | return -1; | 74 | return -1; |
76 | else | 75 | else |
@@ -120,8 +119,7 @@ int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp) | |||
120 | neg=a->type & V_ASN1_NEG; | 119 | neg=a->type & V_ASN1_NEG; |
121 | if (a->length == 0) | 120 | if (a->length == 0) |
122 | ret=1; | 121 | ret=1; |
123 | else | 122 | else { |
124 | { | ||
125 | ret=a->length; | 123 | ret=a->length; |
126 | i=a->data[0]; | 124 | i=a->data[0]; |
127 | if (!neg && (i > 127)) { | 125 | if (!neg && (i > 127)) { |
@@ -183,8 +181,7 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, | |||
183 | unsigned char *to,*s; | 181 | unsigned char *to,*s; |
184 | int i; | 182 | int i; |
185 | 183 | ||
186 | if ((a == NULL) || ((*a) == NULL)) | 184 | if ((a == NULL) || ((*a) == NULL)) { |
187 | { | ||
188 | if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL); | 185 | if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL); |
189 | ret->type=V_ASN1_INTEGER; | 186 | ret->type=V_ASN1_INTEGER; |
190 | } | 187 | } |
@@ -197,8 +194,7 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, | |||
197 | /* We must malloc stuff, even for 0 bytes otherwise it | 194 | /* We must malloc stuff, even for 0 bytes otherwise it |
198 | * signifies a missing NULL parameter. */ | 195 | * signifies a missing NULL parameter. */ |
199 | s=(unsigned char *)malloc((int)len+1); | 196 | s=(unsigned char *)malloc((int)len+1); |
200 | if (s == NULL) | 197 | if (s == NULL) { |
201 | { | ||
202 | i=ERR_R_MALLOC_FAILURE; | 198 | i=ERR_R_MALLOC_FAILURE; |
203 | goto err; | 199 | goto err; |
204 | } | 200 | } |
@@ -208,13 +204,12 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, | |||
208 | * tolerate it. | 204 | * tolerate it. |
209 | */ | 205 | */ |
210 | ret->type=V_ASN1_INTEGER; | 206 | ret->type=V_ASN1_INTEGER; |
211 | } else if (*p & 0x80) /* a negative number */ | 207 | } else if (*p & 0x80) /* a negative number */ { |
212 | { | ||
213 | ret->type=V_ASN1_NEG_INTEGER; | 208 | ret->type=V_ASN1_NEG_INTEGER; |
214 | if ((*p == 0xff) && (len != 1)) { | 209 | if ((*p == 0xff) && (len != 1)) { |
215 | p++; | 210 | p++; |
216 | len--; | 211 | len--; |
217 | } | 212 | } |
218 | i = len; | 213 | i = len; |
219 | p += i - 1; | 214 | p += i - 1; |
220 | to += i - 1; | 215 | to += i - 1; |
@@ -222,7 +217,7 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, | |||
222 | *(to--) = 0; | 217 | *(to--) = 0; |
223 | i--; | 218 | i--; |
224 | p--; | 219 | p--; |
225 | } | 220 | } |
226 | /* Special case: if all zeros then the number will be of | 221 | /* Special case: if all zeros then the number will be of |
227 | * the form FF followed by n zero bytes: this corresponds to | 222 | * the form FF followed by n zero bytes: this corresponds to |
228 | * 1 followed by n zero bytes. We've already written n zeros | 223 | * 1 followed by n zero bytes. We've already written n zeros |
@@ -234,20 +229,19 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, | |||
234 | *s = 1; | 229 | *s = 1; |
235 | s[len] = 0; | 230 | s[len] = 0; |
236 | len++; | 231 | len++; |
237 | } else { | 232 | } else { |
238 | *(to--) = (*(p--) ^ 0xff) + 1; | 233 | *(to--) = (*(p--) ^ 0xff) + 1; |
239 | i--; | 234 | i--; |
240 | for(;i > 0; i--) *(to--) = *(p--) ^ 0xff; | 235 | for(;i > 0; i--) *(to--) = *(p--) ^ 0xff; |
241 | } | 236 | } |
242 | } else { | 237 | } else { |
243 | ret->type=V_ASN1_INTEGER; | 238 | ret->type=V_ASN1_INTEGER; |
244 | if ((*p == 0) && (len != 1)) | 239 | if ((*p == 0) && (len != 1)) { |
245 | { | ||
246 | p++; | 240 | p++; |
247 | len--; | 241 | len--; |
248 | } | 242 | } |
249 | memcpy(s,p,(int)len); | 243 | memcpy(s,p,(int)len); |
250 | } | 244 | } |
251 | 245 | ||
252 | if (ret->data != NULL) free(ret->data); | 246 | if (ret->data != NULL) free(ret->data); |
253 | ret->data=s; | 247 | ret->data=s; |
@@ -278,24 +272,20 @@ ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, | |||
278 | int inf,tag,xclass; | 272 | int inf,tag,xclass; |
279 | int i; | 273 | int i; |
280 | 274 | ||
281 | if ((a == NULL) || ((*a) == NULL)) | 275 | if ((a == NULL) || ((*a) == NULL)) { |
282 | { | ||
283 | if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL); | 276 | if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL); |
284 | ret->type=V_ASN1_INTEGER; | 277 | ret->type=V_ASN1_INTEGER; |
285 | } | 278 | } else |
286 | else | ||
287 | ret=(*a); | 279 | ret=(*a); |
288 | 280 | ||
289 | p= *pp; | 281 | p= *pp; |
290 | inf=ASN1_get_object(&p,&len,&tag,&xclass,length); | 282 | inf=ASN1_get_object(&p,&len,&tag,&xclass,length); |
291 | if (inf & 0x80) | 283 | if (inf & 0x80) { |
292 | { | ||
293 | i=ASN1_R_BAD_OBJECT_HEADER; | 284 | i=ASN1_R_BAD_OBJECT_HEADER; |
294 | goto err; | 285 | goto err; |
295 | } | 286 | } |
296 | 287 | ||
297 | if (tag != V_ASN1_INTEGER) | 288 | if (tag != V_ASN1_INTEGER) { |
298 | { | ||
299 | i=ASN1_R_EXPECTING_AN_INTEGER; | 289 | i=ASN1_R_EXPECTING_AN_INTEGER; |
300 | goto err; | 290 | goto err; |
301 | } | 291 | } |
@@ -303,21 +293,19 @@ ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, | |||
303 | /* We must malloc stuff, even for 0 bytes otherwise it | 293 | /* We must malloc stuff, even for 0 bytes otherwise it |
304 | * signifies a missing NULL parameter. */ | 294 | * signifies a missing NULL parameter. */ |
305 | s=(unsigned char *)malloc((int)len+1); | 295 | s=(unsigned char *)malloc((int)len+1); |
306 | if (s == NULL) | 296 | if (s == NULL) { |
307 | { | ||
308 | i=ERR_R_MALLOC_FAILURE; | 297 | i=ERR_R_MALLOC_FAILURE; |
309 | goto err; | 298 | goto err; |
310 | } | 299 | } |
311 | ret->type=V_ASN1_INTEGER; | 300 | ret->type=V_ASN1_INTEGER; |
312 | if(len) { | 301 | if(len) { |
313 | if ((*p == 0) && (len != 1)) | 302 | if ((*p == 0) && (len != 1)) { |
314 | { | ||
315 | p++; | 303 | p++; |
316 | len--; | 304 | len--; |
317 | } | 305 | } |
318 | memcpy(s,p,(int)len); | 306 | memcpy(s,p,(int)len); |
319 | p+=len; | 307 | p+=len; |
320 | } | 308 | } |
321 | 309 | ||
322 | if (ret->data != NULL) free(ret->data); | 310 | if (ret->data != NULL) free(ret->data); |
323 | ret->data=s; | 311 | ret->data=s; |
@@ -340,27 +328,23 @@ int ASN1_INTEGER_set(ASN1_INTEGER *a, long v) | |||
340 | long d; | 328 | long d; |
341 | 329 | ||
342 | a->type=V_ASN1_INTEGER; | 330 | a->type=V_ASN1_INTEGER; |
343 | if (a->length < (int)(sizeof(long)+1)) | 331 | if (a->length < (int)(sizeof(long)+1)) { |
344 | { | ||
345 | if (a->data != NULL) | 332 | if (a->data != NULL) |
346 | free(a->data); | 333 | free(a->data); |
347 | if ((a->data=(unsigned char *)malloc(sizeof(long)+1)) != NULL) | 334 | if ((a->data=(unsigned char *)malloc(sizeof(long)+1)) != NULL) |
348 | memset((char *)a->data,0,sizeof(long)+1); | 335 | memset((char *)a->data,0,sizeof(long)+1); |
349 | } | 336 | } |
350 | if (a->data == NULL) | 337 | if (a->data == NULL) { |
351 | { | ||
352 | ASN1err(ASN1_F_ASN1_INTEGER_SET,ERR_R_MALLOC_FAILURE); | 338 | ASN1err(ASN1_F_ASN1_INTEGER_SET,ERR_R_MALLOC_FAILURE); |
353 | return(0); | 339 | return(0); |
354 | } | 340 | } |
355 | d=v; | 341 | d=v; |
356 | if (d < 0) | 342 | if (d < 0) { |
357 | { | ||
358 | d= -d; | 343 | d= -d; |
359 | a->type=V_ASN1_NEG_INTEGER; | 344 | a->type=V_ASN1_NEG_INTEGER; |
360 | } | 345 | } |
361 | 346 | ||
362 | for (i=0; i<sizeof(long); i++) | 347 | for (i=0; i<sizeof(long); i++) { |
363 | { | ||
364 | if (d == 0) break; | 348 | if (d == 0) break; |
365 | buf[i]=(int)d&0xff; | 349 | buf[i]=(int)d&0xff; |
366 | d>>=8; | 350 | d>>=8; |
@@ -384,16 +368,14 @@ long ASN1_INTEGER_get(const ASN1_INTEGER *a) | |||
384 | else if (i != V_ASN1_INTEGER) | 368 | else if (i != V_ASN1_INTEGER) |
385 | return -1; | 369 | return -1; |
386 | 370 | ||
387 | if (a->length > (int)sizeof(long)) | 371 | if (a->length > (int)sizeof(long)) { |
388 | { | ||
389 | /* hmm... a bit ugly, return all ones */ | 372 | /* hmm... a bit ugly, return all ones */ |
390 | return -1; | 373 | return -1; |
391 | } | 374 | } |
392 | if (a->data == NULL) | 375 | if (a->data == NULL) |
393 | return 0; | 376 | return 0; |
394 | 377 | ||
395 | for (i=0; i<a->length; i++) | 378 | for (i=0; i<a->length; i++) { |
396 | { | ||
397 | r<<=8; | 379 | r<<=8; |
398 | r|=(unsigned char)a->data[i]; | 380 | r|=(unsigned char)a->data[i]; |
399 | } | 381 | } |
@@ -410,8 +392,7 @@ ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) | |||
410 | ret=M_ASN1_INTEGER_new(); | 392 | ret=M_ASN1_INTEGER_new(); |
411 | else | 393 | else |
412 | ret=ai; | 394 | ret=ai; |
413 | if (ret == NULL) | 395 | if (ret == NULL) { |
414 | { | ||
415 | ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR); | 396 | ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR); |
416 | goto err; | 397 | goto err; |
417 | } | 398 | } |
@@ -420,11 +401,9 @@ ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) | |||
420 | else ret->type=V_ASN1_INTEGER; | 401 | else ret->type=V_ASN1_INTEGER; |
421 | j=BN_num_bits(bn); | 402 | j=BN_num_bits(bn); |
422 | len=((j == 0)?0:((j/8)+1)); | 403 | len=((j == 0)?0:((j/8)+1)); |
423 | if (ret->length < len+4) | 404 | if (ret->length < len+4) { |
424 | { | ||
425 | unsigned char *new_data=realloc(ret->data, len+4); | 405 | unsigned char *new_data=realloc(ret->data, len+4); |
426 | if (!new_data) | 406 | if (!new_data) { |
427 | { | ||
428 | ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_MALLOC_FAILURE); | 407 | ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_MALLOC_FAILURE); |
429 | goto err; | 408 | goto err; |
430 | } | 409 | } |
@@ -432,8 +411,7 @@ ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) | |||
432 | } | 411 | } |
433 | ret->length=BN_bn2bin(bn,ret->data); | 412 | ret->length=BN_bn2bin(bn,ret->data); |
434 | /* Correct zero case */ | 413 | /* Correct zero case */ |
435 | if(!ret->length) | 414 | if(!ret->length) { |
436 | { | ||
437 | ret->data[0] = 0; | 415 | ret->data[0] = 0; |
438 | ret->length = 1; | 416 | ret->length = 1; |
439 | } | 417 | } |