]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/a_enum.c
Change functions to ANSI C.
[thirdparty/openssl.git] / crypto / asn1 / a_enum.c
CommitLineData
119f6288
DSH
1/* crypto/asn1/a_enum.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 "asn1.h"
62
63/* Support for ASN1 ENUMERATED type: based on a_int.c */
64
65/* ASN1err(ASN1_F_D2I_ASN1_ENUMERATED,ASN1_R_EXPECTING_AN_ENUMERATED);
66 */
67
6b691a5c 68int i2d_ASN1_ENUMERATED(ASN1_ENUMERATED *a, unsigned char **pp)
119f6288
DSH
69 {
70 int pad=0,ret,r,i,t;
71 unsigned char *p,*pt,*n,pb=0;
72
73 if ((a == NULL) || (a->data == NULL)) return(0);
74 t=a->type;
75 if (a->length == 0)
76 ret=1;
77 else
78 {
79 ret=a->length;
80 i=a->data[0];
81 if ((t == V_ASN1_ENUMERATED) && (i > 127))
82 {
83 pad=1;
84 pb=0;
85 }
86 else if ((t == V_ASN1_NEG_ENUMERATED) && (i>128))
87 {
88 pad=1;
89 pb=0xFF;
90 }
91 ret+=pad;
92 }
93 r=ASN1_object_size(0,ret,V_ASN1_ENUMERATED);
94 if (pp == NULL) return(r);
95 p= *pp;
96
97 ASN1_put_object(&p,0,ret,V_ASN1_ENUMERATED,V_ASN1_UNIVERSAL);
98 if (pad) *(p++)=pb;
99 if (a->length == 0)
100 *(p++)=0;
101 else if (t == V_ASN1_ENUMERATED)
102 {
103 memcpy(p,a->data,(unsigned int)a->length);
104 p+=a->length;
105 }
106 else
107 {
108 n=a->data;
109 pt=p;
110 for (i=a->length; i>0; i--)
111 *(p++)= (*(n++)^0xFF)+1;
112 if (!pad) *pt|=0x80;
113 }
114
115 *pp=p;
116 return(r);
117 }
118
6b691a5c
UM
119ASN1_ENUMERATED *d2i_ASN1_ENUMERATED(ASN1_ENUMERATED **a, unsigned char **pp,
120 long length)
119f6288
DSH
121 {
122 ASN1_ENUMERATED *ret=NULL;
123 unsigned char *p,*to,*s;
124 long len;
125 int inf,tag,xclass;
126 int i;
127
128 if ((a == NULL) || ((*a) == NULL))
129 {
130 if ((ret=ASN1_ENUMERATED_new()) == NULL) return(NULL);
131 ret->type=V_ASN1_ENUMERATED;
132 }
133 else
134 ret=(*a);
135
136 p= *pp;
137 inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
138 if (inf & 0x80)
139 {
140 i=ASN1_R_BAD_OBJECT_HEADER;
141 goto err;
142 }
143
144 if (tag != V_ASN1_ENUMERATED)
145 {
146 i=ASN1_R_EXPECTING_AN_ENUMERATED;
147 goto err;
148 }
149
150 /* We must Malloc stuff, even for 0 bytes otherwise it
151 * signifies a missing NULL parameter. */
152 s=(unsigned char *)Malloc((int)len+1);
153 if (s == NULL)
154 {
155 i=ERR_R_MALLOC_FAILURE;
156 goto err;
157 }
158 to=s;
159 if (*p & 0x80) /* a negative number */
160 {
161 ret->type=V_ASN1_NEG_ENUMERATED;
162 if (*p == 0xff)
163 {
164 p++;
165 len--;
166 }
167 for (i=(int)len; i>0; i--)
168 *(to++)= (*(p++)^0xFF)+1;
169 }
170 else
171 {
172 ret->type=V_ASN1_ENUMERATED;
173 if ((*p == 0) && (len != 1))
174 {
175 p++;
176 len--;
177 }
178 memcpy(s,p,(int)len);
179 p+=len;
180 }
181
182 if (ret->data != NULL) Free((char *)ret->data);
183 ret->data=s;
184 ret->length=(int)len;
185 if (a != NULL) (*a)=ret;
186 *pp=p;
187 return(ret);
188err:
189 ASN1err(ASN1_F_D2I_ASN1_ENUMERATED,i);
190 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
191 ASN1_ENUMERATED_free(ret);
192 return(NULL);
193 }
194
6b691a5c 195int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
119f6288
DSH
196 {
197 int i,j,k;
198 unsigned char buf[sizeof(long)+1];
199 long d;
200
201 a->type=V_ASN1_ENUMERATED;
202 if (a->length < (sizeof(long)+1))
203 {
204 if (a->data != NULL)
205 Free((char *)a->data);
206 if ((a->data=(unsigned char *)Malloc(sizeof(long)+1)) != NULL)
207 memset((char *)a->data,0,sizeof(long)+1);
208 }
209 if (a->data == NULL)
210 {
211 ASN1err(ASN1_F_ASN1_ENUMERATED_SET,ERR_R_MALLOC_FAILURE);
212 return(0);
213 }
214 d=v;
215 if (d < 0)
216 {
217 d= -d;
218 a->type=V_ASN1_NEG_ENUMERATED;
219 }
220
221 for (i=0; i<sizeof(long); i++)
222 {
223 if (d == 0) break;
224 buf[i]=(int)d&0xff;
225 d>>=8;
226 }
227 j=0;
228 if (v < 0) a->data[j++]=0;
229 for (k=i-1; k >=0; k--)
230 a->data[j++]=buf[k];
231 a->length=j;
232 return(1);
233 }
234
6b691a5c 235long ASN1_ENUMERATED_get(ASN1_ENUMERATED *a)
119f6288
DSH
236 {
237 int neg=0,i;
238 long r=0;
239
240 if (a == NULL) return(0L);
241 i=a->type;
242 if (i == V_ASN1_NEG_ENUMERATED)
243 neg=1;
244 else if (i != V_ASN1_ENUMERATED)
245 return(0);
246
247 if (a->length > sizeof(long))
248 {
249 /* hmm... a bit ugly */
250 return(0xffffffffL);
251 }
252 if (a->data == NULL)
253 return(0);
254
255 for (i=0; i<a->length; i++)
256 {
257 r<<=8;
258 r|=(unsigned char)a->data[i];
259 }
260 if (neg) r= -r;
261 return(r);
262 }
263
6b691a5c 264ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(BIGNUM *bn, ASN1_ENUMERATED *ai)
119f6288
DSH
265 {
266 ASN1_ENUMERATED *ret;
267 int len,j;
268
269 if (ai == NULL)
270 ret=ASN1_ENUMERATED_new();
271 else
272 ret=ai;
273 if (ret == NULL)
274 {
275 ASN1err(ASN1_F_BN_TO_ASN1_ENUMERATED,ERR_R_NESTED_ASN1_ERROR);
276 goto err;
277 }
278 ret->type=V_ASN1_ENUMERATED;
279 j=BN_num_bits(bn);
280 len=((j == 0)?0:((j/8)+1));
281 ret->data=(unsigned char *)Malloc(len+4);
282 ret->length=BN_bn2bin(bn,ret->data);
283 return(ret);
284err:
285 if (ret != ai) ASN1_ENUMERATED_free(ret);
286 return(NULL);
287 }
288
6b691a5c 289BIGNUM *ASN1_ENUMERATED_to_BN(ASN1_ENUMERATED *ai, BIGNUM *bn)
119f6288
DSH
290 {
291 BIGNUM *ret;
292
293 if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
294 ASN1err(ASN1_F_ASN1_ENUMERATED_TO_BN,ASN1_R_BN_LIB);
295 return(ret);
296 }