diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/a_object.c')
-rw-r--r-- | src/lib/libcrypto/asn1/a_object.c | 89 |
1 files changed, 29 insertions, 60 deletions
diff --git a/src/lib/libcrypto/asn1/a_object.c b/src/lib/libcrypto/asn1/a_object.c index 7e9eeaa2e1..e73b1223de 100644 --- a/src/lib/libcrypto/asn1/a_object.c +++ b/src/lib/libcrypto/asn1/a_object.c | |||
@@ -100,55 +100,45 @@ int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num) | |||
100 | p=buf; | 100 | p=buf; |
101 | c= *(p++); | 101 | c= *(p++); |
102 | num--; | 102 | num--; |
103 | if ((c >= '0') && (c <= '2')) | 103 | if ((c >= '0') && (c <= '2')) { |
104 | { | ||
105 | first= c-'0'; | 104 | first= c-'0'; |
106 | } | 105 | } else { |
107 | else | ||
108 | { | ||
109 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE); | 106 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_FIRST_NUM_TOO_LARGE); |
110 | goto err; | 107 | goto err; |
111 | } | 108 | } |
112 | 109 | ||
113 | if (num <= 0) | 110 | if (num <= 0) { |
114 | { | ||
115 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER); | 111 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_MISSING_SECOND_NUMBER); |
116 | goto err; | 112 | goto err; |
117 | } | 113 | } |
118 | c= *(p++); | 114 | c= *(p++); |
119 | num--; | 115 | num--; |
120 | for (;;) | 116 | for (;;) { |
121 | { | ||
122 | if (num <= 0) break; | 117 | if (num <= 0) break; |
123 | if ((c != '.') && (c != ' ')) | 118 | if ((c != '.') && (c != ' ')) { |
124 | { | ||
125 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR); | 119 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_SEPARATOR); |
126 | goto err; | 120 | goto err; |
127 | } | 121 | } |
128 | l=0; | 122 | l=0; |
129 | use_bn = 0; | 123 | use_bn = 0; |
130 | for (;;) | 124 | for (;;) { |
131 | { | ||
132 | if (num <= 0) break; | 125 | if (num <= 0) break; |
133 | num--; | 126 | num--; |
134 | c= *(p++); | 127 | c= *(p++); |
135 | if ((c == ' ') || (c == '.')) | 128 | if ((c == ' ') || (c == '.')) |
136 | break; | 129 | break; |
137 | if ((c < '0') || (c > '9')) | 130 | if ((c < '0') || (c > '9')) { |
138 | { | ||
139 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT); | 131 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT); |
140 | goto err; | 132 | goto err; |
141 | } | 133 | } |
142 | if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) | 134 | if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) { |
143 | { | ||
144 | use_bn = 1; | 135 | use_bn = 1; |
145 | if (!bl) | 136 | if (!bl) |
146 | bl = BN_new(); | 137 | bl = BN_new(); |
147 | if (!bl || !BN_set_word(bl, l)) | 138 | if (!bl || !BN_set_word(bl, l)) |
148 | goto err; | 139 | goto err; |
149 | } | 140 | } |
150 | if (use_bn) | 141 | if (use_bn) { |
151 | { | ||
152 | if (!BN_mul_word(bl, 10L) | 142 | if (!BN_mul_word(bl, 10L) |
153 | || !BN_add_word(bl, c-'0')) | 143 | || !BN_add_word(bl, c-'0')) |
154 | goto err; | 144 | goto err; |
@@ -156,15 +146,12 @@ int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num) | |||
156 | else | 146 | else |
157 | l=l*10L+(long)(c-'0'); | 147 | l=l*10L+(long)(c-'0'); |
158 | } | 148 | } |
159 | if (len == 0) | 149 | if (len == 0) { |
160 | { | 150 | if ((first < 2) && (l >= 40)) { |
161 | if ((first < 2) && (l >= 40)) | ||
162 | { | ||
163 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE); | 151 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_SECOND_NUMBER_TOO_LARGE); |
164 | goto err; | 152 | goto err; |
165 | } | 153 | } |
166 | if (use_bn) | 154 | if (use_bn) { |
167 | { | ||
168 | if (!BN_add_word(bl, first * 40)) | 155 | if (!BN_add_word(bl, first * 40)) |
169 | goto err; | 156 | goto err; |
170 | } | 157 | } |
@@ -172,13 +159,11 @@ int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num) | |||
172 | l+=(long)first*40; | 159 | l+=(long)first*40; |
173 | } | 160 | } |
174 | i=0; | 161 | i=0; |
175 | if (use_bn) | 162 | if (use_bn) { |
176 | { | ||
177 | int blsize; | 163 | int blsize; |
178 | blsize = BN_num_bits(bl); | 164 | blsize = BN_num_bits(bl); |
179 | blsize = (blsize + 6)/7; | 165 | blsize = (blsize + 6)/7; |
180 | if (blsize > tmpsize) | 166 | if (blsize > tmpsize) { |
181 | { | ||
182 | if (tmp != ftmp) | 167 | if (tmp != ftmp) |
183 | free(tmp); | 168 | free(tmp); |
184 | tmpsize = blsize + 32; | 169 | tmpsize = blsize + 32; |
@@ -188,22 +173,17 @@ int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num) | |||
188 | } | 173 | } |
189 | while(blsize--) | 174 | while(blsize--) |
190 | tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L); | 175 | tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L); |
191 | } | 176 | } else { |
192 | else | ||
193 | { | ||
194 | 177 | ||
195 | for (;;) | 178 | for (;;) { |
196 | { | ||
197 | tmp[i++]=(unsigned char)l&0x7f; | 179 | tmp[i++]=(unsigned char)l&0x7f; |
198 | l>>=7L; | 180 | l>>=7L; |
199 | if (l == 0L) break; | 181 | if (l == 0L) break; |
200 | } | 182 | } |
201 | 183 | ||
202 | } | 184 | } |
203 | if (out != NULL) | 185 | if (out != NULL) { |
204 | { | 186 | if (len+i > olen) { |
205 | if (len+i > olen) | ||
206 | { | ||
207 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL); | 187 | ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_BUFFER_TOO_SMALL); |
208 | goto err; | 188 | goto err; |
209 | } | 189 | } |
@@ -240,8 +220,7 @@ int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a) | |||
240 | if ((a == NULL) || (a->data == NULL)) | 220 | if ((a == NULL) || (a->data == NULL)) |
241 | return(BIO_write(bp,"NULL",4)); | 221 | return(BIO_write(bp,"NULL",4)); |
242 | i=i2t_ASN1_OBJECT(buf,sizeof buf,a); | 222 | i=i2t_ASN1_OBJECT(buf,sizeof buf,a); |
243 | if (i > (int)(sizeof(buf) - 1)) | 223 | if (i > (int)(sizeof(buf) - 1)) { |
244 | { | ||
245 | p = malloc(i + 1); | 224 | p = malloc(i + 1); |
246 | if (!p) | 225 | if (!p) |
247 | return -1; | 226 | return -1; |
@@ -265,14 +244,12 @@ ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, | |||
265 | ASN1_OBJECT *ret = NULL; | 244 | ASN1_OBJECT *ret = NULL; |
266 | p= *pp; | 245 | p= *pp; |
267 | inf=ASN1_get_object(&p,&len,&tag,&xclass,length); | 246 | inf=ASN1_get_object(&p,&len,&tag,&xclass,length); |
268 | if (inf & 0x80) | 247 | if (inf & 0x80) { |
269 | { | ||
270 | i=ASN1_R_BAD_OBJECT_HEADER; | 248 | i=ASN1_R_BAD_OBJECT_HEADER; |
271 | goto err; | 249 | goto err; |
272 | } | 250 | } |
273 | 251 | ||
274 | if (tag != V_ASN1_OBJECT) | 252 | if (tag != V_ASN1_OBJECT) { |
275 | { | ||
276 | i=ASN1_R_EXPECTING_AN_OBJECT; | 253 | i=ASN1_R_EXPECTING_AN_OBJECT; |
277 | goto err; | 254 | goto err; |
278 | } | 255 | } |
@@ -293,10 +270,8 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, | |||
293 | /* Sanity check OID encoding: can't have leading 0x80 in | 270 | /* Sanity check OID encoding: can't have leading 0x80 in |
294 | * subidentifiers, see: X.690 8.19.2 | 271 | * subidentifiers, see: X.690 8.19.2 |
295 | */ | 272 | */ |
296 | for (i = 0, p = *pp; i < len; i++, p++) | 273 | for (i = 0, p = *pp; i < len; i++, p++) { |
297 | { | 274 | if (*p == 0x80 && (!i || !(p[-1] & 0x80))) { |
298 | if (*p == 0x80 && (!i || !(p[-1] & 0x80))) | ||
299 | { | ||
300 | ASN1err(ASN1_F_C2I_ASN1_OBJECT,ASN1_R_INVALID_OBJECT_ENCODING); | 275 | ASN1err(ASN1_F_C2I_ASN1_OBJECT,ASN1_R_INVALID_OBJECT_ENCODING); |
301 | return NULL; | 276 | return NULL; |
302 | } | 277 | } |
@@ -305,8 +280,7 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, | |||
305 | /* only the ASN1_OBJECTs from the 'table' will have values | 280 | /* only the ASN1_OBJECTs from the 'table' will have values |
306 | * for ->sn or ->ln */ | 281 | * for ->sn or ->ln */ |
307 | if ((a == NULL) || ((*a) == NULL) || | 282 | if ((a == NULL) || ((*a) == NULL) || |
308 | !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) | 283 | !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) { |
309 | { | ||
310 | if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL); | 284 | if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL); |
311 | } | 285 | } |
312 | else ret=(*a); | 286 | else ret=(*a); |
@@ -316,13 +290,11 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, | |||
316 | data = (unsigned char *)ret->data; | 290 | data = (unsigned char *)ret->data; |
317 | ret->data = NULL; | 291 | ret->data = NULL; |
318 | /* once detached we can change it */ | 292 | /* once detached we can change it */ |
319 | if ((data == NULL) || (ret->length < len)) | 293 | if ((data == NULL) || (ret->length < len)) { |
320 | { | ||
321 | ret->length=0; | 294 | ret->length=0; |
322 | if (data != NULL) free(data); | 295 | if (data != NULL) free(data); |
323 | data=(unsigned char *)malloc(len ? (int)len : 1); | 296 | data=(unsigned char *)malloc(len ? (int)len : 1); |
324 | if (data == NULL) | 297 | if (data == NULL) { i=ERR_R_MALLOC_FAILURE; goto err; } |
325 | { i=ERR_R_MALLOC_FAILURE; goto err; } | ||
326 | ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA; | 298 | ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
327 | } | 299 | } |
328 | memcpy(data,p,(int)len); | 300 | memcpy(data,p,(int)len); |
@@ -349,8 +321,7 @@ ASN1_OBJECT *ASN1_OBJECT_new(void) | |||
349 | ASN1_OBJECT *ret; | 321 | ASN1_OBJECT *ret; |
350 | 322 | ||
351 | ret=(ASN1_OBJECT *)malloc(sizeof(ASN1_OBJECT)); | 323 | ret=(ASN1_OBJECT *)malloc(sizeof(ASN1_OBJECT)); |
352 | if (ret == NULL) | 324 | if (ret == NULL) { |
353 | { | ||
354 | ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE); | 325 | ASN1err(ASN1_F_ASN1_OBJECT_NEW,ERR_R_MALLOC_FAILURE); |
355 | return(NULL); | 326 | return(NULL); |
356 | } | 327 | } |
@@ -366,16 +337,14 @@ ASN1_OBJECT *ASN1_OBJECT_new(void) | |||
366 | void ASN1_OBJECT_free(ASN1_OBJECT *a) | 337 | void ASN1_OBJECT_free(ASN1_OBJECT *a) |
367 | { | 338 | { |
368 | if (a == NULL) return; | 339 | if (a == NULL) return; |
369 | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) | 340 | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) { |
370 | { | ||
371 | #ifndef CONST_STRICT /* disable purely for compile-time strict const checking. Doing this on a "real" compile will cause memory leaks */ | 341 | #ifndef CONST_STRICT /* disable purely for compile-time strict const checking. Doing this on a "real" compile will cause memory leaks */ |
372 | if (a->sn != NULL) free((void *)a->sn); | 342 | if (a->sn != NULL) free((void *)a->sn); |
373 | if (a->ln != NULL) free((void *)a->ln); | 343 | if (a->ln != NULL) free((void *)a->ln); |
374 | #endif | 344 | #endif |
375 | a->sn=a->ln=NULL; | 345 | a->sn=a->ln=NULL; |
376 | } | 346 | } |
377 | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) | 347 | if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) { |
378 | { | ||
379 | if (a->data != NULL) free((void *)a->data); | 348 | if (a->data != NULL) free((void *)a->data); |
380 | a->data=NULL; | 349 | a->data=NULL; |
381 | a->length=0; | 350 | a->length=0; |