xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/a_int.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/asn1/a_int.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/asn1.h>
62*2139Sjp161948 #include <openssl/bn.h>
630Sstevel@tonic-gate 
ASN1_INTEGER_dup(ASN1_INTEGER * x)640Sstevel@tonic-gate ASN1_INTEGER *ASN1_INTEGER_dup(ASN1_INTEGER *x)
650Sstevel@tonic-gate { return M_ASN1_INTEGER_dup(x);}
660Sstevel@tonic-gate 
ASN1_INTEGER_cmp(ASN1_INTEGER * x,ASN1_INTEGER * y)670Sstevel@tonic-gate int ASN1_INTEGER_cmp(ASN1_INTEGER *x, ASN1_INTEGER *y)
68*2139Sjp161948 	{
69*2139Sjp161948 	int neg, ret;
70*2139Sjp161948 	/* Compare signs */
71*2139Sjp161948 	neg = x->type & V_ASN1_NEG;
72*2139Sjp161948 	if (neg != (y->type & V_ASN1_NEG))
73*2139Sjp161948 		{
74*2139Sjp161948 		if (neg)
75*2139Sjp161948 			return -1;
76*2139Sjp161948 		else
77*2139Sjp161948 			return 1;
78*2139Sjp161948 		}
79*2139Sjp161948 
80*2139Sjp161948 	ret = ASN1_STRING_cmp(x, y);
81*2139Sjp161948 
82*2139Sjp161948 	if (neg)
83*2139Sjp161948 		return -ret;
84*2139Sjp161948 	else
85*2139Sjp161948 		return ret;
86*2139Sjp161948 	}
87*2139Sjp161948 
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * This converts an ASN1 INTEGER into its content encoding.
910Sstevel@tonic-gate  * The internal representation is an ASN1_STRING whose data is a big endian
920Sstevel@tonic-gate  * representation of the value, ignoring the sign. The sign is determined by
930Sstevel@tonic-gate  * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative.
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * Positive integers are no problem: they are almost the same as the DER
960Sstevel@tonic-gate  * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * Negative integers are a bit trickier...
990Sstevel@tonic-gate  * The DER representation of negative integers is in 2s complement form.
1000Sstevel@tonic-gate  * The internal form is converted by complementing each octet and finally
1010Sstevel@tonic-gate  * adding one to the result. This can be done less messily with a little trick.
1020Sstevel@tonic-gate  * If the internal form has trailing zeroes then they will become FF by the
1030Sstevel@tonic-gate  * complement and 0 by the add one (due to carry) so just copy as many trailing
1040Sstevel@tonic-gate  * zeros to the destination as there are in the source. The carry will add one
1050Sstevel@tonic-gate  * to the last none zero octet: so complement this octet and add one and finally
1060Sstevel@tonic-gate  * complement any left over until you get to the start of the string.
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
1090Sstevel@tonic-gate  * with 0xff. However if the first byte is 0x80 and one of the following bytes
1100Sstevel@tonic-gate  * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
1110Sstevel@tonic-gate  * followed by optional zeros isn't padded.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate 
i2c_ASN1_INTEGER(ASN1_INTEGER * a,unsigned char ** pp)1140Sstevel@tonic-gate int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
1150Sstevel@tonic-gate 	{
1160Sstevel@tonic-gate 	int pad=0,ret,i,neg;
1170Sstevel@tonic-gate 	unsigned char *p,*n,pb=0;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if ((a == NULL) || (a->data == NULL)) return(0);
1200Sstevel@tonic-gate 	neg=a->type & V_ASN1_NEG;
1210Sstevel@tonic-gate 	if (a->length == 0)
1220Sstevel@tonic-gate 		ret=1;
1230Sstevel@tonic-gate 	else
1240Sstevel@tonic-gate 		{
1250Sstevel@tonic-gate 		ret=a->length;
1260Sstevel@tonic-gate 		i=a->data[0];
1270Sstevel@tonic-gate 		if (!neg && (i > 127)) {
1280Sstevel@tonic-gate 			pad=1;
1290Sstevel@tonic-gate 			pb=0;
1300Sstevel@tonic-gate 		} else if(neg) {
1310Sstevel@tonic-gate 			if(i>128) {
1320Sstevel@tonic-gate 				pad=1;
1330Sstevel@tonic-gate 				pb=0xFF;
1340Sstevel@tonic-gate 			} else if(i == 128) {
1350Sstevel@tonic-gate 			/*
1360Sstevel@tonic-gate 			 * Special case: if any other bytes non zero we pad:
1370Sstevel@tonic-gate 			 * otherwise we don't.
1380Sstevel@tonic-gate 			 */
1390Sstevel@tonic-gate 				for(i = 1; i < a->length; i++) if(a->data[i]) {
1400Sstevel@tonic-gate 						pad=1;
1410Sstevel@tonic-gate 						pb=0xFF;
1420Sstevel@tonic-gate 						break;
1430Sstevel@tonic-gate 				}
1440Sstevel@tonic-gate 			}
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 		ret+=pad;
1470Sstevel@tonic-gate 		}
1480Sstevel@tonic-gate 	if (pp == NULL) return(ret);
1490Sstevel@tonic-gate 	p= *pp;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (pad) *(p++)=pb;
1520Sstevel@tonic-gate 	if (a->length == 0) *(p++)=0;
1530Sstevel@tonic-gate 	else if (!neg) memcpy(p,a->data,(unsigned int)a->length);
1540Sstevel@tonic-gate 	else {
1550Sstevel@tonic-gate 		/* Begin at the end of the encoding */
1560Sstevel@tonic-gate 		n=a->data + a->length - 1;
1570Sstevel@tonic-gate 		p += a->length - 1;
1580Sstevel@tonic-gate 		i = a->length;
1590Sstevel@tonic-gate 		/* Copy zeros to destination as long as source is zero */
1600Sstevel@tonic-gate 		while(!*n) {
1610Sstevel@tonic-gate 			*(p--) = 0;
1620Sstevel@tonic-gate 			n--;
1630Sstevel@tonic-gate 			i--;
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 		/* Complement and increment next octet */
1660Sstevel@tonic-gate 		*(p--) = ((*(n--)) ^ 0xff) + 1;
1670Sstevel@tonic-gate 		i--;
1680Sstevel@tonic-gate 		/* Complement any octets left */
1690Sstevel@tonic-gate 		for(;i > 0; i--) *(p--) = *(n--) ^ 0xff;
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	*pp+=ret;
1730Sstevel@tonic-gate 	return(ret);
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /* Convert just ASN1 INTEGER content octets to ASN1_INTEGER structure */
1770Sstevel@tonic-gate 
c2i_ASN1_INTEGER(ASN1_INTEGER ** a,const unsigned char ** pp,long len)178*2139Sjp161948 ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
1790Sstevel@tonic-gate 	     long len)
1800Sstevel@tonic-gate 	{
1810Sstevel@tonic-gate 	ASN1_INTEGER *ret=NULL;
182*2139Sjp161948 	const unsigned char *p, *pend;
183*2139Sjp161948 	unsigned char *to,*s;
1840Sstevel@tonic-gate 	int i;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if ((a == NULL) || ((*a) == NULL))
1870Sstevel@tonic-gate 		{
1880Sstevel@tonic-gate 		if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL);
1890Sstevel@tonic-gate 		ret->type=V_ASN1_INTEGER;
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 	else
1920Sstevel@tonic-gate 		ret=(*a);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	p= *pp;
1950Sstevel@tonic-gate 	pend = p + len;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it
1980Sstevel@tonic-gate 	 * signifies a missing NULL parameter. */
1990Sstevel@tonic-gate 	s=(unsigned char *)OPENSSL_malloc((int)len+1);
2000Sstevel@tonic-gate 	if (s == NULL)
2010Sstevel@tonic-gate 		{
2020Sstevel@tonic-gate 		i=ERR_R_MALLOC_FAILURE;
2030Sstevel@tonic-gate 		goto err;
2040Sstevel@tonic-gate 		}
2050Sstevel@tonic-gate 	to=s;
2060Sstevel@tonic-gate 	if(!len) {
2070Sstevel@tonic-gate 		/* Strictly speaking this is an illegal INTEGER but we
2080Sstevel@tonic-gate 		 * tolerate it.
2090Sstevel@tonic-gate 		 */
2100Sstevel@tonic-gate 		ret->type=V_ASN1_INTEGER;
2110Sstevel@tonic-gate 	} else if (*p & 0x80) /* a negative number */
2120Sstevel@tonic-gate 		{
2130Sstevel@tonic-gate 		ret->type=V_ASN1_NEG_INTEGER;
2140Sstevel@tonic-gate 		if ((*p == 0xff) && (len != 1)) {
2150Sstevel@tonic-gate 			p++;
2160Sstevel@tonic-gate 			len--;
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 		i = len;
2190Sstevel@tonic-gate 		p += i - 1;
2200Sstevel@tonic-gate 		to += i - 1;
2210Sstevel@tonic-gate 		while((!*p) && i) {
2220Sstevel@tonic-gate 			*(to--) = 0;
2230Sstevel@tonic-gate 			i--;
2240Sstevel@tonic-gate 			p--;
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 		/* Special case: if all zeros then the number will be of
2270Sstevel@tonic-gate 		 * the form FF followed by n zero bytes: this corresponds to
2280Sstevel@tonic-gate 		 * 1 followed by n zero bytes. We've already written n zeros
2290Sstevel@tonic-gate 		 * so we just append an extra one and set the first byte to
2300Sstevel@tonic-gate 		 * a 1. This is treated separately because it is the only case
2310Sstevel@tonic-gate 		 * where the number of bytes is larger than len.
2320Sstevel@tonic-gate 		 */
2330Sstevel@tonic-gate 		if(!i) {
2340Sstevel@tonic-gate 			*s = 1;
2350Sstevel@tonic-gate 			s[len] = 0;
2360Sstevel@tonic-gate 			len++;
2370Sstevel@tonic-gate 		} else {
2380Sstevel@tonic-gate 			*(to--) = (*(p--) ^ 0xff) + 1;
2390Sstevel@tonic-gate 			i--;
2400Sstevel@tonic-gate 			for(;i > 0; i--) *(to--) = *(p--) ^ 0xff;
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 	} else {
2430Sstevel@tonic-gate 		ret->type=V_ASN1_INTEGER;
2440Sstevel@tonic-gate 		if ((*p == 0) && (len != 1))
2450Sstevel@tonic-gate 			{
2460Sstevel@tonic-gate 			p++;
2470Sstevel@tonic-gate 			len--;
2480Sstevel@tonic-gate 			}
2490Sstevel@tonic-gate 		memcpy(s,p,(int)len);
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	if (ret->data != NULL) OPENSSL_free(ret->data);
2530Sstevel@tonic-gate 	ret->data=s;
2540Sstevel@tonic-gate 	ret->length=(int)len;
2550Sstevel@tonic-gate 	if (a != NULL) (*a)=ret;
2560Sstevel@tonic-gate 	*pp=pend;
2570Sstevel@tonic-gate 	return(ret);
2580Sstevel@tonic-gate err:
259*2139Sjp161948 	ASN1err(ASN1_F_C2I_ASN1_INTEGER,i);
2600Sstevel@tonic-gate 	if ((ret != NULL) && ((a == NULL) || (*a != ret)))
2610Sstevel@tonic-gate 		M_ASN1_INTEGER_free(ret);
2620Sstevel@tonic-gate 	return(NULL);
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate /* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of
2670Sstevel@tonic-gate  * ASN1 integers: some broken software can encode a positive INTEGER
2680Sstevel@tonic-gate  * with its MSB set as negative (it doesn't add a padding zero).
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate 
d2i_ASN1_UINTEGER(ASN1_INTEGER ** a,const unsigned char ** pp,long length)271*2139Sjp161948 ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
2720Sstevel@tonic-gate 	     long length)
2730Sstevel@tonic-gate 	{
2740Sstevel@tonic-gate 	ASN1_INTEGER *ret=NULL;
275*2139Sjp161948 	const unsigned char *p;
276*2139Sjp161948 	unsigned char *to,*s;
2770Sstevel@tonic-gate 	long len;
2780Sstevel@tonic-gate 	int inf,tag,xclass;
2790Sstevel@tonic-gate 	int i;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	if ((a == NULL) || ((*a) == NULL))
2820Sstevel@tonic-gate 		{
2830Sstevel@tonic-gate 		if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL);
2840Sstevel@tonic-gate 		ret->type=V_ASN1_INTEGER;
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 	else
2870Sstevel@tonic-gate 		ret=(*a);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	p= *pp;
2900Sstevel@tonic-gate 	inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
2910Sstevel@tonic-gate 	if (inf & 0x80)
2920Sstevel@tonic-gate 		{
2930Sstevel@tonic-gate 		i=ASN1_R_BAD_OBJECT_HEADER;
2940Sstevel@tonic-gate 		goto err;
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	if (tag != V_ASN1_INTEGER)
2980Sstevel@tonic-gate 		{
2990Sstevel@tonic-gate 		i=ASN1_R_EXPECTING_AN_INTEGER;
3000Sstevel@tonic-gate 		goto err;
3010Sstevel@tonic-gate 		}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it
3040Sstevel@tonic-gate 	 * signifies a missing NULL parameter. */
3050Sstevel@tonic-gate 	s=(unsigned char *)OPENSSL_malloc((int)len+1);
3060Sstevel@tonic-gate 	if (s == NULL)
3070Sstevel@tonic-gate 		{
3080Sstevel@tonic-gate 		i=ERR_R_MALLOC_FAILURE;
3090Sstevel@tonic-gate 		goto err;
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 	to=s;
3120Sstevel@tonic-gate 	ret->type=V_ASN1_INTEGER;
3130Sstevel@tonic-gate 	if(len) {
3140Sstevel@tonic-gate 		if ((*p == 0) && (len != 1))
3150Sstevel@tonic-gate 			{
3160Sstevel@tonic-gate 			p++;
3170Sstevel@tonic-gate 			len--;
3180Sstevel@tonic-gate 			}
3190Sstevel@tonic-gate 		memcpy(s,p,(int)len);
3200Sstevel@tonic-gate 		p+=len;
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (ret->data != NULL) OPENSSL_free(ret->data);
3240Sstevel@tonic-gate 	ret->data=s;
3250Sstevel@tonic-gate 	ret->length=(int)len;
3260Sstevel@tonic-gate 	if (a != NULL) (*a)=ret;
3270Sstevel@tonic-gate 	*pp=p;
3280Sstevel@tonic-gate 	return(ret);
3290Sstevel@tonic-gate err:
3300Sstevel@tonic-gate 	ASN1err(ASN1_F_D2I_ASN1_UINTEGER,i);
3310Sstevel@tonic-gate 	if ((ret != NULL) && ((a == NULL) || (*a != ret)))
3320Sstevel@tonic-gate 		M_ASN1_INTEGER_free(ret);
3330Sstevel@tonic-gate 	return(NULL);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
ASN1_INTEGER_set(ASN1_INTEGER * a,long v)3360Sstevel@tonic-gate int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
3370Sstevel@tonic-gate 	{
338*2139Sjp161948 	int j,k;
339*2139Sjp161948 	unsigned int i;
3400Sstevel@tonic-gate 	unsigned char buf[sizeof(long)+1];
3410Sstevel@tonic-gate 	long d;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	a->type=V_ASN1_INTEGER;
344*2139Sjp161948 	if (a->length < (int)(sizeof(long)+1))
3450Sstevel@tonic-gate 		{
3460Sstevel@tonic-gate 		if (a->data != NULL)
3470Sstevel@tonic-gate 			OPENSSL_free(a->data);
3480Sstevel@tonic-gate 		if ((a->data=(unsigned char *)OPENSSL_malloc(sizeof(long)+1)) != NULL)
3490Sstevel@tonic-gate 			memset((char *)a->data,0,sizeof(long)+1);
3500Sstevel@tonic-gate 		}
3510Sstevel@tonic-gate 	if (a->data == NULL)
3520Sstevel@tonic-gate 		{
3530Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_INTEGER_SET,ERR_R_MALLOC_FAILURE);
3540Sstevel@tonic-gate 		return(0);
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 	d=v;
3570Sstevel@tonic-gate 	if (d < 0)
3580Sstevel@tonic-gate 		{
3590Sstevel@tonic-gate 		d= -d;
3600Sstevel@tonic-gate 		a->type=V_ASN1_NEG_INTEGER;
3610Sstevel@tonic-gate 		}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	for (i=0; i<sizeof(long); i++)
3640Sstevel@tonic-gate 		{
3650Sstevel@tonic-gate 		if (d == 0) break;
3660Sstevel@tonic-gate 		buf[i]=(int)d&0xff;
3670Sstevel@tonic-gate 		d>>=8;
3680Sstevel@tonic-gate 		}
3690Sstevel@tonic-gate 	j=0;
3700Sstevel@tonic-gate 	for (k=i-1; k >=0; k--)
3710Sstevel@tonic-gate 		a->data[j++]=buf[k];
3720Sstevel@tonic-gate 	a->length=j;
3730Sstevel@tonic-gate 	return(1);
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
ASN1_INTEGER_get(ASN1_INTEGER * a)3760Sstevel@tonic-gate long ASN1_INTEGER_get(ASN1_INTEGER *a)
3770Sstevel@tonic-gate 	{
3780Sstevel@tonic-gate 	int neg=0,i;
3790Sstevel@tonic-gate 	long r=0;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	if (a == NULL) return(0L);
3820Sstevel@tonic-gate 	i=a->type;
3830Sstevel@tonic-gate 	if (i == V_ASN1_NEG_INTEGER)
3840Sstevel@tonic-gate 		neg=1;
3850Sstevel@tonic-gate 	else if (i != V_ASN1_INTEGER)
3860Sstevel@tonic-gate 		return -1;
3870Sstevel@tonic-gate 
388*2139Sjp161948 	if (a->length > (int)sizeof(long))
3890Sstevel@tonic-gate 		{
3900Sstevel@tonic-gate 		/* hmm... a bit ugly */
3910Sstevel@tonic-gate 		return(0xffffffffL);
3920Sstevel@tonic-gate 		}
3930Sstevel@tonic-gate 	if (a->data == NULL)
3940Sstevel@tonic-gate 		return 0;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	for (i=0; i<a->length; i++)
3970Sstevel@tonic-gate 		{
3980Sstevel@tonic-gate 		r<<=8;
3990Sstevel@tonic-gate 		r|=(unsigned char)a->data[i];
4000Sstevel@tonic-gate 		}
4010Sstevel@tonic-gate 	if (neg) r= -r;
4020Sstevel@tonic-gate 	return(r);
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 
BN_to_ASN1_INTEGER(BIGNUM * bn,ASN1_INTEGER * ai)4050Sstevel@tonic-gate ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai)
4060Sstevel@tonic-gate 	{
4070Sstevel@tonic-gate 	ASN1_INTEGER *ret;
4080Sstevel@tonic-gate 	int len,j;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (ai == NULL)
4110Sstevel@tonic-gate 		ret=M_ASN1_INTEGER_new();
4120Sstevel@tonic-gate 	else
4130Sstevel@tonic-gate 		ret=ai;
4140Sstevel@tonic-gate 	if (ret == NULL)
4150Sstevel@tonic-gate 		{
4160Sstevel@tonic-gate 		ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR);
4170Sstevel@tonic-gate 		goto err;
4180Sstevel@tonic-gate 		}
419*2139Sjp161948 	if (BN_is_negative(bn))
420*2139Sjp161948 		ret->type = V_ASN1_NEG_INTEGER;
4210Sstevel@tonic-gate 	else ret->type=V_ASN1_INTEGER;
4220Sstevel@tonic-gate 	j=BN_num_bits(bn);
4230Sstevel@tonic-gate 	len=((j == 0)?0:((j/8)+1));
4240Sstevel@tonic-gate 	if (ret->length < len+4)
4250Sstevel@tonic-gate 		{
4260Sstevel@tonic-gate 		unsigned char *new_data=OPENSSL_realloc(ret->data, len+4);
4270Sstevel@tonic-gate 		if (!new_data)
4280Sstevel@tonic-gate 			{
4290Sstevel@tonic-gate 			ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);
4300Sstevel@tonic-gate 			goto err;
4310Sstevel@tonic-gate 			}
4320Sstevel@tonic-gate 		ret->data=new_data;
4330Sstevel@tonic-gate 		}
4340Sstevel@tonic-gate 	ret->length=BN_bn2bin(bn,ret->data);
4350Sstevel@tonic-gate 	/* Correct zero case */
4360Sstevel@tonic-gate 	if(!ret->length)
4370Sstevel@tonic-gate 		{
4380Sstevel@tonic-gate 		ret->data[0] = 0;
4390Sstevel@tonic-gate 		ret->length = 1;
4400Sstevel@tonic-gate 		}
4410Sstevel@tonic-gate 	return(ret);
4420Sstevel@tonic-gate err:
4430Sstevel@tonic-gate 	if (ret != ai) M_ASN1_INTEGER_free(ret);
4440Sstevel@tonic-gate 	return(NULL);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 
ASN1_INTEGER_to_BN(ASN1_INTEGER * ai,BIGNUM * bn)4470Sstevel@tonic-gate BIGNUM *ASN1_INTEGER_to_BN(ASN1_INTEGER *ai, BIGNUM *bn)
4480Sstevel@tonic-gate 	{
4490Sstevel@tonic-gate 	BIGNUM *ret;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
4520Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB);
453*2139Sjp161948 	else if(ai->type == V_ASN1_NEG_INTEGER)
454*2139Sjp161948 		BN_set_negative(ret, 1);
4550Sstevel@tonic-gate 	return(ret);
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate IMPLEMENT_STACK_OF(ASN1_INTEGER)
4590Sstevel@tonic-gate IMPLEMENT_ASN1_SET_OF(ASN1_INTEGER)
460