summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_object.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/a_object.c')
-rw-r--r--src/lib/libcrypto/asn1/a_object.c100
1 files changed, 17 insertions, 83 deletions
diff --git a/src/lib/libcrypto/asn1/a_object.c b/src/lib/libcrypto/asn1/a_object.c
index dc980421d0..0a8e6c287c 100644
--- a/src/lib/libcrypto/asn1/a_object.c
+++ b/src/lib/libcrypto/asn1/a_object.c
@@ -57,12 +57,10 @@
57 */ 57 */
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include <limits.h>
61#include "cryptlib.h" 60#include "cryptlib.h"
62#include <openssl/buffer.h> 61#include <openssl/buffer.h>
63#include <openssl/asn1.h> 62#include <openssl/asn1.h>
64#include <openssl/objects.h> 63#include <openssl/objects.h>
65#include <openssl/bn.h>
66 64
67int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp) 65int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
68 { 66 {
@@ -85,12 +83,10 @@ int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
85 83
86int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num) 84int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
87 { 85 {
88 int i,first,len=0,c, use_bn; 86 int i,first,len=0,c;
89 char ftmp[24], *tmp = ftmp; 87 char tmp[24];
90 int tmpsize = sizeof ftmp;
91 const char *p; 88 const char *p;
92 unsigned long l; 89 unsigned long l;
93 BIGNUM *bl = NULL;
94 90
95 if (num == 0) 91 if (num == 0)
96 return(0); 92 return(0);
@@ -102,7 +98,7 @@ int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
102 num--; 98 num--;
103 if ((c >= '0') && (c <= '2')) 99 if ((c >= '0') && (c <= '2'))
104 { 100 {
105 first= c-'0'; 101 first=(c-'0')*40;
106 } 102 }
107 else 103 else
108 { 104 {
@@ -126,7 +122,6 @@ int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
126 goto err; 122 goto err;
127 } 123 }
128 l=0; 124 l=0;
129 use_bn = 0;
130 for (;;) 125 for (;;)
131 { 126 {
132 if (num <= 0) break; 127 if (num <= 0) break;
@@ -139,22 +134,7 @@ int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
139 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT); 134 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT);
140 goto err; 135 goto err;
141 } 136 }
142 if (!use_bn && l > (ULONG_MAX / 10L)) 137 l=l*10L+(long)(c-'0');
143 {
144 use_bn = 1;
145 if (!bl)
146 bl = BN_new();
147 if (!bl || !BN_set_word(bl, l))
148 goto err;
149 }
150 if (use_bn)
151 {
152 if (!BN_mul_word(bl, 10L)
153 || !BN_add_word(bl, c-'0'))
154 goto err;
155 }
156 else
157 l=l*10L+(long)(c-'0');
158 } 138 }
159 if (len == 0) 139 if (len == 0)
160 { 140 {
@@ -163,42 +143,14 @@ int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
163 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE); 143 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE);
164 goto err; 144 goto err;
165 } 145 }
166 if (use_bn) 146 l+=(long)first;
167 {
168 if (!BN_add_word(bl, first * 40))
169 goto err;
170 }
171 else
172 l+=(long)first*40;
173 } 147 }
174 i=0; 148 i=0;
175 if (use_bn) 149 for (;;)
176 {
177 int blsize;
178 blsize = BN_num_bits(bl);
179 blsize = (blsize + 6)/7;
180 if (blsize > tmpsize)
181 {
182 if (tmp != ftmp)
183 OPENSSL_free(tmp);
184 tmpsize = blsize + 32;
185 tmp = OPENSSL_malloc(tmpsize);
186 if (!tmp)
187 goto err;
188 }
189 while(blsize--)
190 tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L);
191 }
192 else
193 { 150 {
194 151 tmp[i++]=(unsigned char)l&0x7f;
195 for (;;) 152 l>>=7L;
196 { 153 if (l == 0L) break;
197 tmp[i++]=(unsigned char)l&0x7f;
198 l>>=7L;
199 if (l == 0L) break;
200 }
201
202 } 154 }
203 if (out != NULL) 155 if (out != NULL)
204 { 156 {
@@ -214,16 +166,8 @@ int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
214 else 166 else
215 len+=i; 167 len+=i;
216 } 168 }
217 if (tmp != ftmp)
218 OPENSSL_free(tmp);
219 if (bl)
220 BN_free(bl);
221 return(len); 169 return(len);
222err: 170err:
223 if (tmp != ftmp)
224 OPENSSL_free(tmp);
225 if (bl)
226 BN_free(bl);
227 return(0); 171 return(0);
228 } 172 }
229 173
@@ -234,31 +178,21 @@ int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
234 178
235int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a) 179int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
236 { 180 {
237 char buf[80], *p = buf; 181 char buf[80];
238 int i; 182 int i;
239 183
240 if ((a == NULL) || (a->data == NULL)) 184 if ((a == NULL) || (a->data == NULL))
241 return(BIO_write(bp,"NULL",4)); 185 return(BIO_write(bp,"NULL",4));
242 i=i2t_ASN1_OBJECT(buf,sizeof buf,a); 186 i=i2t_ASN1_OBJECT(buf,sizeof buf,a);
243 if (i > (int)(sizeof(buf) - 1)) 187 if (i > sizeof buf) i=sizeof buf;
244 { 188 BIO_write(bp,buf,i);
245 p = OPENSSL_malloc(i + 1);
246 if (!p)
247 return -1;
248 i2t_ASN1_OBJECT(p,i + 1,a);
249 }
250 if (i <= 0)
251 return BIO_write(bp, "<INVALID>", 9);
252 BIO_write(bp,p,i);
253 if (p != buf)
254 OPENSSL_free(p);
255 return(i); 189 return(i);
256 } 190 }
257 191
258ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, 192ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, unsigned char **pp,
259 long length) 193 long length)
260{ 194{
261 const unsigned char *p; 195 unsigned char *p;
262 long len; 196 long len;
263 int tag,xclass; 197 int tag,xclass;
264 int inf,i; 198 int inf,i;
@@ -285,11 +219,11 @@ err:
285 ASN1_OBJECT_free(ret); 219 ASN1_OBJECT_free(ret);
286 return(NULL); 220 return(NULL);
287} 221}
288ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, 222ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, unsigned char **pp,
289 long len) 223 long len)
290 { 224 {
291 ASN1_OBJECT *ret=NULL; 225 ASN1_OBJECT *ret=NULL;
292 const unsigned char *p; 226 unsigned char *p;
293 int i; 227 int i;
294 228
295 /* only the ASN1_OBJECTs from the 'table' will have values 229 /* only the ASN1_OBJECTs from the 'table' will have values
@@ -321,7 +255,7 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
321 *pp=p; 255 *pp=p;
322 return(ret); 256 return(ret);
323err: 257err:
324 ASN1err(ASN1_F_C2I_ASN1_OBJECT,i); 258 ASN1err(ASN1_F_D2I_ASN1_OBJECT,i);
325 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 259 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
326 ASN1_OBJECT_free(ret); 260 ASN1_OBJECT_free(ret);
327 return(NULL); 261 return(NULL);