Don't return stuff from void functions.
[openssl.git] / crypto / asn1 / a_int.c
1 /* crypto/asn1/a_int.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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"
61 #include <openssl/asn1.h>
62
63 ASN1_INTEGER *ASN1_INTEGER_new(void)
64 { return M_ASN1_INTEGER_new();}
65
66 void ASN1_INTEGER_free(ASN1_INTEGER *x)
67 { M_ASN1_INTEGER_free(x);}
68
69 ASN1_INTEGER *ASN1_INTEGER_dup(ASN1_INTEGER *x)
70 { return M_ASN1_INTEGER_dup(x);}
71
72 int ASN1_INTEGER_cmp(ASN1_INTEGER *x, ASN1_INTEGER *y)
73 { return M_ASN1_INTEGER_cmp(x,y);}
74
75 /* 
76  * This converts an ASN1 INTEGER into its DER encoding.
77  * The internal representation is an ASN1_STRING whose data is a big endian
78  * representation of the value, ignoring the sign. The sign is determined by
79  * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative. 
80  *
81  * Positive integers are no problem: they are almost the same as the DER
82  * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
83  *
84  * Negative integers are a bit trickier...
85  * The DER representation of negative integers is in 2s complement form.
86  * The internal form is converted by complementing each octet and finally 
87  * adding one to the result. This can be done less messily with a little trick.
88  * If the internal form has trailing zeroes then they will become FF by the
89  * complement and 0 by the add one (due to carry) so just copy as many trailing 
90  * zeros to the destination as there are in the source. The carry will add one
91  * to the last none zero octet: so complement this octet and add one and finally
92  * complement any left over until you get to the start of the string.
93  *
94  * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
95  * with 0xff. However if the first byte is 0x80 and one of the following bytes
96  * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
97  * followed by optional zeros isn't padded.
98  */
99
100 int i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
101         {
102         int pad=0,ret,r,i,t;
103         unsigned char *p,*n,pb=0;
104
105         if ((a == NULL) || (a->data == NULL)) return(0);
106         t=a->type;
107         if (a->length == 0)
108                 ret=1;
109         else
110                 {
111                 ret=a->length;
112                 i=a->data[0];
113                 if ((t == V_ASN1_INTEGER) && (i > 127)) {
114                         pad=1;
115                         pb=0;
116                 } else if(t == V_ASN1_NEG_INTEGER) {
117                         if(i>128) {
118                                 pad=1;
119                                 pb=0xFF;
120                         } else if(i == 128) {
121                         /*
122                          * Special case: if any other bytes non zero we pad:
123                          * otherwise we don't.
124                          */
125                                 for(i = 1; i < a->length; i++) if(a->data[i]) {
126                                                 pad=1;
127                                                 pb=0xFF;
128                                                 break;
129                                 }
130                         }
131                 }
132                 ret+=pad;
133                 }
134         r=ASN1_object_size(0,ret,V_ASN1_INTEGER);
135         if (pp == NULL) return(r);
136         p= *pp;
137
138         ASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL);
139         if (pad) *(p++)=pb;
140         if (a->length == 0) *(p++)=0;
141         else if (t == V_ASN1_INTEGER) memcpy(p,a->data,(unsigned int)a->length);
142         else {
143                 /* Begin at the end of the encoding */
144                 n=a->data + a->length - 1;
145                 p += a->length - 1;
146                 i = a->length;
147                 /* Copy zeros to destination as long as source is zero */
148                 while(!*n) {
149                         *(p--) = 0;
150                         n--;
151                         i--;
152                 }
153                 /* Complement and increment next octet */
154                 *(p--) = ((*(n--)) ^ 0xff) + 1;
155                 i--;
156                 /* Complement any octets left */
157                 for(;i > 0; i--) *(p--) = *(n--) ^ 0xff;
158         }
159
160         *pp+=r;
161         return(r);
162         }
163
164 ASN1_INTEGER *d2i_ASN1_INTEGER(ASN1_INTEGER **a, unsigned char **pp,
165              long length)
166         {
167         ASN1_INTEGER *ret=NULL;
168         unsigned char *p,*to,*s, *pend;
169         long len;
170         int inf,tag,xclass;
171         int i;
172
173         if ((a == NULL) || ((*a) == NULL))
174                 {
175                 if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL);
176                 ret->type=V_ASN1_INTEGER;
177                 }
178         else
179                 ret=(*a);
180
181         p= *pp;
182         inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
183         pend = p + len;
184         if (inf & 0x80)
185                 {
186                 i=ASN1_R_BAD_OBJECT_HEADER;
187                 goto err;
188                 }
189
190         if (tag != V_ASN1_INTEGER)
191                 {
192                 i=ASN1_R_EXPECTING_AN_INTEGER;
193                 goto err;
194                 }
195
196         /* We must Malloc stuff, even for 0 bytes otherwise it
197          * signifies a missing NULL parameter. */
198         s=(unsigned char *)Malloc((int)len+1);
199         if (s == NULL)
200                 {
201                 i=ERR_R_MALLOC_FAILURE;
202                 goto err;
203                 }
204         to=s;
205         if (*p & 0x80) /* a negative number */
206                 {
207                 ret->type=V_ASN1_NEG_INTEGER;
208                 if ((*p == 0xff) && (len != 1)) {
209                         p++;
210                         len--;
211                 }
212                 i = len;
213                 p += i - 1;
214                 to += i - 1;
215                 while((!*p) && i) {
216                         *(to--) = 0;
217                         i--;
218                         p--;
219                 }
220                 /* Special case: if all zeros then the number will be of
221                  * the form FF followed by n zero bytes: this corresponds to
222                  * 1 followed by n zero bytes. We've already written n zeros
223                  * so we just append an extra one and set the first byte to
224                  * a 1. This is treated separately because it is the only case
225                  * where the number of bytes is larger than len.
226                  */
227                 if(!i) {
228                         *s = 1;
229                         s[len] = 0;
230                         len++;
231                 } else {
232                         *(to--) = (*(p--) ^ 0xff) + 1;
233                         i--;
234                         for(;i > 0; i--) *(to--) = *(p--) ^ 0xff;
235                 }
236         } else {
237                 ret->type=V_ASN1_INTEGER;
238                 if ((*p == 0) && (len != 1))
239                         {
240                         p++;
241                         len--;
242                         }
243                 memcpy(s,p,(int)len);
244         }
245
246         if (ret->data != NULL) Free((char *)ret->data);
247         ret->data=s;
248         ret->length=(int)len;
249         if (a != NULL) (*a)=ret;
250         *pp=pend;
251         return(ret);
252 err:
253         ASN1err(ASN1_F_D2I_ASN1_INTEGER,i);
254         if ((ret != NULL) && ((a == NULL) || (*a != ret)))
255                 M_ASN1_INTEGER_free(ret);
256         return(NULL);
257         }
258
259 /* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of
260  * ASN1 integers: some broken software can encode a positive INTEGER
261  * with its MSB set as negative (it doesn't add a padding zero).
262  */
263
264 ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, unsigned char **pp,
265              long length)
266         {
267         ASN1_INTEGER *ret=NULL;
268         unsigned char *p,*to,*s;
269         long len;
270         int inf,tag,xclass;
271         int i;
272
273         if ((a == NULL) || ((*a) == NULL))
274                 {
275                 if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL);
276                 ret->type=V_ASN1_INTEGER;
277                 }
278         else
279                 ret=(*a);
280
281         p= *pp;
282         inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
283         if (inf & 0x80)
284                 {
285                 i=ASN1_R_BAD_OBJECT_HEADER;
286                 goto err;
287                 }
288
289         if (tag != V_ASN1_INTEGER)
290                 {
291                 i=ASN1_R_EXPECTING_AN_INTEGER;
292                 goto err;
293                 }
294
295         /* We must Malloc stuff, even for 0 bytes otherwise it
296          * signifies a missing NULL parameter. */
297         s=(unsigned char *)Malloc((int)len+1);
298         if (s == NULL)
299                 {
300                 i=ERR_R_MALLOC_FAILURE;
301                 goto err;
302                 }
303         to=s;
304                 ret->type=V_ASN1_INTEGER;
305                 if ((*p == 0) && (len != 1))
306                         {
307                         p++;
308                         len--;
309                         }
310                 memcpy(s,p,(int)len);
311                 p+=len;
312
313         if (ret->data != NULL) Free((char *)ret->data);
314         ret->data=s;
315         ret->length=(int)len;
316         if (a != NULL) (*a)=ret;
317         *pp=p;
318         return(ret);
319 err:
320         ASN1err(ASN1_F_D2I_ASN1_UINTEGER,i);
321         if ((ret != NULL) && ((a == NULL) || (*a != ret)))
322                 M_ASN1_INTEGER_free(ret);
323         return(NULL);
324         }
325
326 int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
327         {
328         int i,j,k;
329         unsigned char buf[sizeof(long)+1];
330         long d;
331
332         a->type=V_ASN1_INTEGER;
333         if (a->length < (sizeof(long)+1))
334                 {
335                 if (a->data != NULL)
336                         Free((char *)a->data);
337                 if ((a->data=(unsigned char *)Malloc(sizeof(long)+1)) != NULL)
338                         memset((char *)a->data,0,sizeof(long)+1);
339                 }
340         if (a->data == NULL)
341                 {
342                 ASN1err(ASN1_F_ASN1_INTEGER_SET,ERR_R_MALLOC_FAILURE);
343                 return(0);
344                 }
345         d=v;
346         if (d < 0)
347                 {
348                 d= -d;
349                 a->type=V_ASN1_NEG_INTEGER;
350                 }
351
352         for (i=0; i<sizeof(long); i++)
353                 {
354                 if (d == 0) break;
355                 buf[i]=(int)d&0xff;
356                 d>>=8;
357                 }
358         j=0;
359         for (k=i-1; k >=0; k--)
360                 a->data[j++]=buf[k];
361         a->length=j;
362         return(1);
363         }
364
365 long ASN1_INTEGER_get(ASN1_INTEGER *a)
366         {
367         int neg=0,i;
368         long r=0;
369
370         if (a == NULL) return(0L);
371         i=a->type;
372         if (i == V_ASN1_NEG_INTEGER)
373                 neg=1;
374         else if (i != V_ASN1_INTEGER)
375                 return(0);
376         
377         if (a->length > sizeof(long))
378                 {
379                 /* hmm... a bit ugly */
380                 return(0xffffffffL);
381                 }
382         if (a->data == NULL)
383                 return(0);
384
385         for (i=0; i<a->length; i++)
386                 {
387                 r<<=8;
388                 r|=(unsigned char)a->data[i];
389                 }
390         if (neg) r= -r;
391         return(r);
392         }
393
394 ASN1_INTEGER *BN_to_ASN1_INTEGER(BIGNUM *bn, ASN1_INTEGER *ai)
395         {
396         ASN1_INTEGER *ret;
397         int len,j;
398
399         if (ai == NULL)
400                 ret=M_ASN1_INTEGER_new();
401         else
402                 ret=ai;
403         if (ret == NULL)
404                 {
405                 ASN1err(ASN1_F_BN_TO_ASN1_INTEGER,ERR_R_NESTED_ASN1_ERROR);
406                 goto err;
407                 }
408         if(bn->neg) ret->type = V_ASN1_NEG_INTEGER;
409         else ret->type=V_ASN1_INTEGER;
410         j=BN_num_bits(bn);
411         len=((j == 0)?0:((j/8)+1));
412         ret->data=(unsigned char *)Malloc(len+4);
413         ret->length=BN_bn2bin(bn,ret->data);
414         return(ret);
415 err:
416         if (ret != ai) M_ASN1_INTEGER_free(ret);
417         return(NULL);
418         }
419
420 BIGNUM *ASN1_INTEGER_to_BN(ASN1_INTEGER *ai, BIGNUM *bn)
421         {
422         BIGNUM *ret;
423
424         if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
425                 ASN1err(ASN1_F_ASN1_INTEGER_TO_BN,ASN1_R_BN_LIB);
426         if(ai->type == V_ASN1_NEG_INTEGER) bn->neg = 1;
427         return(ret);
428         }