OLD | NEW |
1 /* crypto/asn1/a_int.c */ | 1 /* crypto/asn1/a_int.c */ |
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * This package is an SSL implementation written | 5 * This package is an SSL implementation written |
6 * by Eric Young (eay@cryptsoft.com). | 6 * by Eric Young (eay@cryptsoft.com). |
7 * The implementation was written so as to conform with Netscapes SSL. | 7 * The implementation was written so as to conform with Netscapes SSL. |
8 * | 8 * |
9 * This library is free for commercial and non-commercial use as long as | 9 * This library is free for commercial and non-commercial use as long as |
10 * the following conditions are aheared to. The following conditions | 10 * the following conditions are aheared to. The following conditions |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 * copied and put under another distribution licence | 55 * copied and put under another distribution licence |
56 * [including the GNU Public Licence.] | 56 * [including the GNU Public Licence.] |
57 */ | 57 */ |
58 | 58 |
59 #include <stdio.h> | 59 #include <stdio.h> |
60 #include "cryptlib.h" | 60 #include "cryptlib.h" |
61 #include <openssl/asn1.h> | 61 #include <openssl/asn1.h> |
62 #include <openssl/bn.h> | 62 #include <openssl/bn.h> |
63 | 63 |
64 ASN1_INTEGER *ASN1_INTEGER_dup(ASN1_INTEGER *x) | 64 ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x) |
65 { return M_ASN1_INTEGER_dup(x);} | 65 { return M_ASN1_INTEGER_dup(x);} |
66 | 66 |
67 int ASN1_INTEGER_cmp(ASN1_INTEGER *x, ASN1_INTEGER *y) | 67 int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) |
68 { | 68 { |
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 { | 73 { |
74 if (neg) | 74 if (neg) |
75 return -1; | 75 return -1; |
76 else | 76 else |
77 return 1; | 77 return 1; |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 /* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of | 266 /* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of |
267 * ASN1 integers: some broken software can encode a positive INTEGER | 267 * ASN1 integers: some broken software can encode a positive INTEGER |
268 * with its MSB set as negative (it doesn't add a padding zero). | 268 * with its MSB set as negative (it doesn't add a padding zero). |
269 */ | 269 */ |
270 | 270 |
271 ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, | 271 ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, |
272 long length) | 272 long length) |
273 { | 273 { |
274 ASN1_INTEGER *ret=NULL; | 274 ASN1_INTEGER *ret=NULL; |
275 const unsigned char *p; | 275 const unsigned char *p; |
276 » unsigned char *to,*s; | 276 » unsigned char *s; |
277 long len; | 277 long len; |
278 int inf,tag,xclass; | 278 int inf,tag,xclass; |
279 int i; | 279 int i; |
280 | 280 |
281 if ((a == NULL) || ((*a) == NULL)) | 281 if ((a == NULL) || ((*a) == NULL)) |
282 { | 282 { |
283 if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL); | 283 if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL); |
284 ret->type=V_ASN1_INTEGER; | 284 ret->type=V_ASN1_INTEGER; |
285 } | 285 } |
286 else | 286 else |
(...skipping 14 matching lines...) Expand all Loading... |
301 } | 301 } |
302 | 302 |
303 /* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it | 303 /* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it |
304 * signifies a missing NULL parameter. */ | 304 * signifies a missing NULL parameter. */ |
305 s=(unsigned char *)OPENSSL_malloc((int)len+1); | 305 s=(unsigned char *)OPENSSL_malloc((int)len+1); |
306 if (s == NULL) | 306 if (s == NULL) |
307 { | 307 { |
308 i=ERR_R_MALLOC_FAILURE; | 308 i=ERR_R_MALLOC_FAILURE; |
309 goto err; | 309 goto err; |
310 } | 310 } |
311 to=s; | |
312 ret->type=V_ASN1_INTEGER; | 311 ret->type=V_ASN1_INTEGER; |
313 if(len) { | 312 if(len) { |
314 if ((*p == 0) && (len != 1)) | 313 if ((*p == 0) && (len != 1)) |
315 { | 314 { |
316 p++; | 315 p++; |
317 len--; | 316 len--; |
318 } | 317 } |
319 memcpy(s,p,(int)len); | 318 memcpy(s,p,(int)len); |
320 p+=len; | 319 p+=len; |
321 } | 320 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 buf[i]=(int)d&0xff; | 365 buf[i]=(int)d&0xff; |
367 d>>=8; | 366 d>>=8; |
368 } | 367 } |
369 j=0; | 368 j=0; |
370 for (k=i-1; k >=0; k--) | 369 for (k=i-1; k >=0; k--) |
371 a->data[j++]=buf[k]; | 370 a->data[j++]=buf[k]; |
372 a->length=j; | 371 a->length=j; |
373 return(1); | 372 return(1); |
374 } | 373 } |
375 | 374 |
376 long ASN1_INTEGER_get(ASN1_INTEGER *a) | 375 long ASN1_INTEGER_get(const ASN1_INTEGER *a) |
377 { | 376 { |
378 int neg=0,i; | 377 int neg=0,i; |
379 long r=0; | 378 long r=0; |
380 | 379 |
381 if (a == NULL) return(0L); | 380 if (a == NULL) return(0L); |
382 i=a->type; | 381 i=a->type; |
383 if (i == V_ASN1_NEG_INTEGER) | 382 if (i == V_ASN1_NEG_INTEGER) |
384 neg=1; | 383 neg=1; |
385 else if (i != V_ASN1_INTEGER) | 384 else if (i != V_ASN1_INTEGER) |
386 return -1; | 385 return -1; |
387 | 386 |
388 if (a->length > (int)sizeof(long)) | 387 if (a->length > (int)sizeof(long)) |
389 { | 388 { |
390 /* hmm... a bit ugly */ | 389 /* hmm... a bit ugly */ |
391 return(0xffffffffL); | 390 return(0xffffffffL); |
392 } | 391 } |
393 if (a->data == NULL) | 392 if (a->data == NULL) |
394 return 0; | 393 return 0; |
395 | 394 |
396 for (i=0; i<a->length; i++) | 395 for (i=0; i<a->length; i++) |
397 { | 396 { |
398 r<<=8; | 397 r<<=8; |
399 r|=(unsigned char)a->data[i]; | 398 r|=(unsigned char)a->data[i]; |
400 } | 399 } |
401 if (neg) r= -r; | 400 if (neg) r= -r; |
402 return(r); | 401 return(r); |
403 } | 402 } |
404 | 403 |
405 ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai) | 404 ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) |
406 { | 405 { |
407 ASN1_INTEGER *ret; | 406 ASN1_INTEGER *ret; |
408 int len,j; | 407 int len,j; |
409 | 408 |
410 if (ai == NULL) | 409 if (ai == NULL) |
411 ret=M_ASN1_INTEGER_new(); | 410 ret=M_ASN1_INTEGER_new(); |
412 else | 411 else |
413 ret=ai; | 412 ret=ai; |
414 if (ret == NULL) | 413 if (ret == NULL) |
415 { | 414 { |
(...skipping 21 matching lines...) Expand all Loading... |
437 { | 436 { |
438 ret->data[0] = 0; | 437 ret->data[0] = 0; |
439 ret->length = 1; | 438 ret->length = 1; |
440 } | 439 } |
441 return(ret); | 440 return(ret); |
442 err: | 441 err: |
443 if (ret != ai) M_ASN1_INTEGER_free(ret); | 442 if (ret != ai) M_ASN1_INTEGER_free(ret); |
444 return(NULL); | 443 return(NULL); |
445 } | 444 } |
446 | 445 |
447 BIGNUM *ASN1_INTEGER_to_BN(ASN1_INTEGER *ai, BIGNUM *bn) | 446 BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn) |
448 { | 447 { |
449 BIGNUM *ret; | 448 BIGNUM *ret; |
450 | 449 |
451 if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL) | 450 if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL) |
452 ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB); | 451 ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB); |
453 else if(ai->type == V_ASN1_NEG_INTEGER) | 452 else if(ai->type == V_ASN1_NEG_INTEGER) |
454 BN_set_negative(ret, 1); | 453 BN_set_negative(ret, 1); |
455 return(ret); | 454 return(ret); |
456 } | 455 } |
457 | 456 |
458 IMPLEMENT_STACK_OF(ASN1_INTEGER) | 457 IMPLEMENT_STACK_OF(ASN1_INTEGER) |
459 IMPLEMENT_ASN1_SET_OF(ASN1_INTEGER) | 458 IMPLEMENT_ASN1_SET_OF(ASN1_INTEGER) |
OLD | NEW |