Fix typo in OCSP nonce extension.
[openssl.git] / crypto / asn1 / a_int.c
index 82db75f5e163638d9ac69e9ec998269ce10866f3..b0fc97ea273a3e0debf1504f71a0524b87c676f1 100644 (file)
 #include "cryptlib.h"
 #include <openssl/asn1.h>
 
-ASN1_INTEGER *ASN1_INTEGER_new(void)
-{ return M_ASN1_INTEGER_new();}
-
-void ASN1_INTEGER_free(ASN1_INTEGER *x)
-{ M_ASN1_INTEGER_free(x);}
-
 ASN1_INTEGER *ASN1_INTEGER_dup(ASN1_INTEGER *x)
 { return M_ASN1_INTEGER_dup(x);}
 
@@ -73,7 +67,7 @@ int ASN1_INTEGER_cmp(ASN1_INTEGER *x, ASN1_INTEGER *y)
 { return M_ASN1_INTEGER_cmp(x,y);}
 
 /* 
- * This converts an ASN1 INTEGER into its DER encoding.
+ * This converts an ASN1 INTEGER into its content encoding.
  * The internal representation is an ASN1_STRING whose data is a big endian
  * representation of the value, ignoring the sign. The sign is determined by
  * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative. 
@@ -97,23 +91,23 @@ int ASN1_INTEGER_cmp(ASN1_INTEGER *x, ASN1_INTEGER *y)
  * followed by optional zeros isn't padded.
  */
 
-int i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
+int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
        {
-       int pad=0,ret,r,i,t;
+       int pad=0,ret,i,neg;
        unsigned char *p,*n,pb=0;
 
        if ((a == NULL) || (a->data == NULL)) return(0);
-       t=a->type;
+       neg=a->type & V_ASN1_NEG;
        if (a->length == 0)
                ret=1;
        else
                {
                ret=a->length;
                i=a->data[0];
-               if ((t == V_ASN1_INTEGER) && (i > 127)) {
+               if (!neg && (i > 127)) {
                        pad=1;
                        pb=0;
-               } else if(t == V_ASN1_NEG_INTEGER) {
+               } else if(neg) {
                        if(i>128) {
                                pad=1;
                                pb=0xFF;
@@ -131,14 +125,12 @@ int i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
                }
                ret+=pad;
                }
-       r=ASN1_object_size(0,ret,V_ASN1_INTEGER);
-       if (pp == NULL) return(r);
+       if (pp == NULL) return(ret);
        p= *pp;
 
-       ASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL);
        if (pad) *(p++)=pb;
        if (a->length == 0) *(p++)=0;
-       else if (t == V_ASN1_INTEGER) memcpy(p,a->data,(unsigned int)a->length);
+       else if (!neg) memcpy(p,a->data,(unsigned int)a->length);
        else {
                /* Begin at the end of the encoding */
                n=a->data + a->length - 1;
@@ -157,17 +149,17 @@ int i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
                for(;i > 0; i--) *(p--) = *(n--) ^ 0xff;
        }
 
-       *pp+=r;
-       return(r);
+       *pp+=ret;
+       return(ret);
        }
 
-ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **a, unsigned char **pp,
-            long length)
+/* Convert just ASN1 INTEGER content octets to ASN1_INTEGER structure */
+
+ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, unsigned char **pp,
+            long len)
        {
        ASN1_INTEGER *ret=NULL;
        unsigned char *p,*to,*s, *pend;
-       long len;
-       int inf,tag,xclass;
        int i;
 
        if ((a == NULL) || ((*a) == NULL))
@@ -179,19 +171,7 @@ ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **a, unsigned char **pp,
                ret=(*a);
 
        p= *pp;
-       inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
        pend = p + len;
-       if (inf & 0x80)
-               {
-               i=ASN1_R_BAD_OBJECT_HEADER;
-               goto err;
-               }
-
-       if (tag != V_ASN1_INTEGER)
-               {
-               i=ASN1_R_EXPECTING_AN_INTEGER;
-               goto err;
-               }
 
        /* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it
         * signifies a missing NULL parameter. */
@@ -261,6 +241,7 @@ err:
        return(NULL);
        }
 
+
 /* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of
  * ASN1 integers: some broken software can encode a positive INTEGER
  * with its MSB set as negative (it doesn't add a padding zero).
@@ -379,7 +360,7 @@ long ASN1_INTEGER_get(ASN1_INTEGER *a)
        if (i == V_ASN1_NEG_INTEGER)
                neg=1;
        else if (i != V_ASN1_INTEGER)
-               return(0);
+               return -1;
        
        if (a->length > sizeof(long))
                {
@@ -387,7 +368,7 @@ long ASN1_INTEGER_get(ASN1_INTEGER *a)
                return(0xffffffffL);
                }
        if (a->data == NULL)
-               return(0);
+               return 0;
 
        for (i=0; i<a->length; i++)
                {
@@ -430,7 +411,7 @@ BIGNUM *ASN1_INTEGER_to_BN(ASN1_INTEGER *ai, BIGNUM *bn)
 
        if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
                ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB);
-       if(ai->type == V_ASN1_NEG_INTEGER) bn->neg = 1;
+       else if(ai->type == V_ASN1_NEG_INTEGER) ret->neg = 1;
        return(ret);
        }