]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/a_int.c
Remove NOPROTO definitions and error code comments.
[thirdparty/openssl.git] / crypto / asn1 / a_int.c
CommitLineData
d02b48c6 1/* crypto/asn1/a_int.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
ec577822 61#include <openssl/asn1.h>
d02b48c6 62
6b691a5c 63int i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
d02b48c6
RE
64 {
65 int pad=0,ret,r,i,t;
66 unsigned char *p,*pt,*n,pb=0;
67
68 if ((a == NULL) || (a->data == NULL)) return(0);
69 t=a->type;
70 if (a->length == 0)
71 ret=1;
72 else
73 {
74 ret=a->length;
75 i=a->data[0];
76 if ((t == V_ASN1_INTEGER) && (i > 127))
77 {
78 pad=1;
79 pb=0;
80 }
81 else if ((t == V_ASN1_NEG_INTEGER) && (i>128))
82 {
83 pad=1;
84 pb=0xFF;
85 }
86 ret+=pad;
87 }
88 r=ASN1_object_size(0,ret,V_ASN1_INTEGER);
89 if (pp == NULL) return(r);
90 p= *pp;
91
92 ASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL);
93 if (pad) *(p++)=pb;
94 if (a->length == 0)
95 *(p++)=0;
96 else if (t == V_ASN1_INTEGER)
97 {
98 memcpy(p,a->data,(unsigned int)a->length);
99 p+=a->length;
100 }
101 else
102 {
103 n=a->data;
104 pt=p;
105 for (i=a->length; i>0; i--)
106 *(p++)= (*(n++)^0xFF)+1;
107 if (!pad) *pt|=0x80;
108 }
109
110 *pp=p;
111 return(r);
112 }
113
6b691a5c
UM
114ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **a, unsigned char **pp,
115 long length)
d02b48c6
RE
116 {
117 ASN1_INTEGER *ret=NULL;
118 unsigned char *p,*to,*s;
119 long len;
120 int inf,tag,xclass;
121 int i;
122
123 if ((a == NULL) || ((*a) == NULL))
124 {
125 if ((ret=ASN1_INTEGER_new()) == NULL) return(NULL);
126 ret->type=V_ASN1_INTEGER;
127 }
128 else
129 ret=(*a);
130
131 p= *pp;
132 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
133 if (inf & 0x80)
134 {
135 i=ASN1_R_BAD_OBJECT_HEADER;
136 goto err;
137 }
138
139 if (tag != V_ASN1_INTEGER)
140 {
141 i=ASN1_R_EXPECTING_AN_INTEGER;
142 goto err;
143 }
144
145 /* We must Malloc stuff, even for 0 bytes otherwise it
146 * signifies a missing NULL parameter. */
147 s=(unsigned char *)Malloc((int)len+1);
148 if (s == NULL)
149 {
150 i=ERR_R_MALLOC_FAILURE;
151 goto err;
152 }
153 to=s;
154 if (*p & 0x80) /* a negative number */
155 {
156 ret->type=V_ASN1_NEG_INTEGER;
157 if (*p == 0xff)
158 {
159 p++;
160 len--;
161 }
162 for (i=(int)len; i>0; i--)
163 *(to++)= (*(p++)^0xFF)+1;
164 }
165 else
166 {
167 ret->type=V_ASN1_INTEGER;
168 if ((*p == 0) && (len != 1))
169 {
170 p++;
171 len--;
172 }
173 memcpy(s,p,(int)len);
174 p+=len;
175 }
176
177 if (ret->data != NULL) Free((char *)ret->data);
178 ret->data=s;
179 ret->length=(int)len;
180 if (a != NULL) (*a)=ret;
181 *pp=p;
182 return(ret);
183err:
184 ASN1err(ASN1_F_D2I_ASN1_INTEGER,i);
185 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
186 ASN1_INTEGER_free(ret);
187 return(NULL);
188 }
189
6b691a5c 190int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
d02b48c6
RE
191 {
192 int i,j,k;
193 unsigned char buf[sizeof(long)+1];
194 long d;
195
196 a->type=V_ASN1_INTEGER;
197 if (a->length < (sizeof(long)+1))
198 {
199 if (a->data != NULL)
200 Free((char *)a->data);
201 if ((a->data=(unsigned char *)Malloc(sizeof(long)+1)) != NULL)
202 memset((char *)a->data,0,sizeof(long)+1);
203 }
204 if (a->data == NULL)
205 {
206 ASN1err(ASN1_F_ASN1_INTEGER_SET,ERR_R_MALLOC_FAILURE);
207 return(0);
208 }
209 d=v;
210 if (d < 0)
211 {
212 d= -d;
213 a->type=V_ASN1_NEG_INTEGER;
214 }
215
216 for (i=0; i<sizeof(long); i++)
217 {
218 if (d == 0) break;
219 buf[i]=(int)d&0xff;
220 d>>=8;
221 }
222 j=0;
223 if (v < 0) a->data[j++]=0;
224 for (k=i-1; k >=0; k--)
225 a->data[j++]=buf[k];
226 a->length=j;
227 return(1);
228 }
229
6b691a5c 230long ASN1_INTEGER_get(ASN1_INTEGER *a)
d02b48c6
RE
231 {
232 int neg=0,i;
233 long r=0;
234
235 if (a == NULL) return(0L);
236 i=a->type;
237 if (i == V_ASN1_NEG_INTEGER)
238 neg=1;
239 else if (i != V_ASN1_INTEGER)
240 return(0);
241
242 if (a->length > sizeof(long))
243 {
58964a49
RE
244 /* hmm... a bit ugly */
245 return(0xffffffffL);
d02b48c6
RE
246 }
247 if (a->data == NULL)
248 return(0);
249
250 for (i=0; i<a->length; i++)
251 {
252 r<<=8;
253 r|=(unsigned char)a->data[i];
254 }
255 if (neg) r= -r;
256 return(r);
257 }
258
6b691a5c 259ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai)
d02b48c6
RE
260 {
261 ASN1_INTEGER *ret;
262 int len,j;
263
264 if (ai == NULL)
265 ret=ASN1_INTEGER_new();
266 else
267 ret=ai;
268 if (ret == NULL)
269 {
dfeab068 270 ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR);
d02b48c6
RE
271 goto err;
272 }
273 ret->type=V_ASN1_INTEGER;
274 j=BN_num_bits(bn);
275 len=((j == 0)?0:((j/8)+1));
276 ret->data=(unsigned char *)Malloc(len+4);
277 ret->length=BN_bn2bin(bn,ret->data);
278 return(ret);
279err:
280 if (ret != ai) ASN1_INTEGER_free(ret);
281 return(NULL);
282 }
283
6b691a5c 284BIGNUM *ASN1_INTEGER_to_BN(ASN1_INTEGER *ai, BIGNUM *bn)
d02b48c6
RE
285 {
286 BIGNUM *ret;
287
288 if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
289 ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB);
290 return(ret);
291 }