first parameter is difference in days, not years
[openssl.git] / crypto / asn1 / a_int.c
index 5d76de79ba89d3e8ab3b8b3b8b8c4476ea5e1354..ad0d2506f63203679814cd36018f1dc9c4221b91 100644 (file)
 #include <stdio.h>
 #include "cryptlib.h"
 #include <openssl/asn1.h>
+#include <openssl/bn.h>
 
-ASN1_INTEGER *ASN1_INTEGER_dup(ASN1_INTEGER *x)
+ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
 { return M_ASN1_INTEGER_dup(x);}
 
-int ASN1_INTEGER_cmp(ASN1_INTEGER *x, ASN1_INTEGER *y)
-{ return M_ASN1_INTEGER_cmp(x,y);}
+int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
+       { 
+       int neg, ret;
+       /* Compare signs */
+       neg = x->type & V_ASN1_NEG;
+       if (neg != (y->type & V_ASN1_NEG))
+               {
+               if (neg)
+                       return -1;
+               else
+                       return 1;
+               }
+
+       ret = ASN1_STRING_cmp(x, y);
+
+       if (neg)
+               return -ret;
+       else
+               return ret;
+       }
+       
 
 /* 
  * This converts an ASN1 INTEGER into its content encoding.
@@ -236,7 +256,7 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
        *pp=pend;
        return(ret);
 err:
-       ASN1err(ASN1_F_D2I_ASN1_INTEGER,i);
+       ASN1err(ASN1_F_C2I_ASN1_INTEGER,i);
        if ((ret != NULL) && ((a == NULL) || (*a != ret)))
                M_ASN1_INTEGER_free(ret);
        return(NULL);
@@ -253,7 +273,7 @@ ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
        {
        ASN1_INTEGER *ret=NULL;
        const unsigned char *p;
-       unsigned char *to,*s;
+       unsigned char *s;
        long len;
        int inf,tag,xclass;
        int i;
@@ -288,7 +308,6 @@ ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
                i=ERR_R_MALLOC_FAILURE;
                goto err;
                }
-       to=s;
        ret->type=V_ASN1_INTEGER;
        if(len) {
                if ((*p == 0) && (len != 1))
@@ -353,7 +372,7 @@ int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
        return(1);
        }
 
-long ASN1_INTEGER_get(ASN1_INTEGER *a)
+long ASN1_INTEGER_get(const ASN1_INTEGER *a)
        {
        int neg=0,i;
        long r=0;
@@ -367,8 +386,8 @@ long ASN1_INTEGER_get(ASN1_INTEGER *a)
        
        if (a->length > (int)sizeof(long))
                {
-               /* hmm... a bit ugly */
-               return(0xffffffffL);
+               /* hmm... a bit ugly, return all ones */
+               return -1;
                }
        if (a->data == NULL)
                return 0;
@@ -382,7 +401,7 @@ long ASN1_INTEGER_get(ASN1_INTEGER *a)
        return(r);
        }
 
-ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai)
+ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
        {
        ASN1_INTEGER *ret;
        int len,j;
@@ -396,7 +415,7 @@ ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai)
                ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR);
                goto err;
                }
-       if (BN_get_sign(bn))
+       if (BN_is_negative(bn))
                ret->type = V_ASN1_NEG_INTEGER;
        else ret->type=V_ASN1_INTEGER;
        j=BN_num_bits(bn);
@@ -424,14 +443,14 @@ err:
        return(NULL);
        }
 
-BIGNUM *ASN1_INTEGER_to_BN(ASN1_INTEGER *ai, BIGNUM *bn)
+BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
        {
        BIGNUM *ret;
 
        if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
                ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB);
        else if(ai->type == V_ASN1_NEG_INTEGER)
-               BN_set_sign(ret, 1);
+               BN_set_negative(ret, 1);
        return(ret);
        }