summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/bio_asn1.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/bio_asn1.c')
-rw-r--r--src/lib/libcrypto/asn1/bio_asn1.c152
1 files changed, 80 insertions, 72 deletions
diff --git a/src/lib/libcrypto/asn1/bio_asn1.c b/src/lib/libcrypto/asn1/bio_asn1.c
index 8ebac02a00..36b82758ed 100644
--- a/src/lib/libcrypto/asn1/bio_asn1.c
+++ b/src/lib/libcrypto/asn1/bio_asn1.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -68,8 +68,7 @@
68/* Must be large enough for biggest tag+length */ 68/* Must be large enough for biggest tag+length */
69#define DEFAULT_ASN1_BUF_SIZE 20 69#define DEFAULT_ASN1_BUF_SIZE 20
70 70
71typedef enum 71typedef enum {
72{
73 ASN1_STATE_START, 72 ASN1_STATE_START,
74 ASN1_STATE_PRE_COPY, 73 ASN1_STATE_PRE_COPY,
75 ASN1_STATE_HEADER, 74 ASN1_STATE_HEADER,
@@ -79,14 +78,12 @@ typedef enum
79 ASN1_STATE_DONE 78 ASN1_STATE_DONE
80} asn1_bio_state_t; 79} asn1_bio_state_t;
81 80
82typedef struct BIO_ASN1_EX_FUNCS_st 81typedef struct BIO_ASN1_EX_FUNCS_st {
83{
84 asn1_ps_func *ex_func; 82 asn1_ps_func *ex_func;
85 asn1_ps_func *ex_free_func; 83 asn1_ps_func *ex_free_func;
86} BIO_ASN1_EX_FUNCS; 84} BIO_ASN1_EX_FUNCS;
87 85
88typedef struct BIO_ASN1_BUF_CTX_t 86typedef struct BIO_ASN1_BUF_CTX_t {
89{
90 /* Internal state */ 87 /* Internal state */
91 asn1_bio_state_t state; 88 asn1_bio_state_t state;
92 /* Internal buffer */ 89 /* Internal buffer */
@@ -110,7 +107,7 @@ typedef struct BIO_ASN1_BUF_CTX_t
110} BIO_ASN1_BUF_CTX; 107} BIO_ASN1_BUF_CTX;
111 108
112 109
113static int asn1_bio_write(BIO *h, const char *buf,int num); 110static int asn1_bio_write(BIO *h, const char *buf, int num);
114static int asn1_bio_read(BIO *h, char *buf, int size); 111static int asn1_bio_read(BIO *h, char *buf, int size);
115static int asn1_bio_puts(BIO *h, const char *str); 112static int asn1_bio_puts(BIO *h, const char *str);
116static int asn1_bio_gets(BIO *h, char *str, int size); 113static int asn1_bio_gets(BIO *h, char *str, int size);
@@ -121,14 +118,12 @@ static long asn1_bio_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
121 118
122static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size); 119static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
123static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 120static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
124 asn1_ps_func *cleanup, asn1_bio_state_t next); 121 asn1_ps_func *cleanup, asn1_bio_state_t next);
125static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 122static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
126 asn1_ps_func *setup, 123 asn1_ps_func *setup, asn1_bio_state_t ex_state,
127 asn1_bio_state_t ex_state, 124 asn1_bio_state_t other_state);
128 asn1_bio_state_t other_state);
129 125
130static BIO_METHOD methods_asn1= 126static BIO_METHOD methods_asn1 = {
131{
132 BIO_TYPE_ASN1, 127 BIO_TYPE_ASN1,
133 "asn1", 128 "asn1",
134 asn1_bio_write, 129 asn1_bio_write,
@@ -141,13 +136,14 @@ static BIO_METHOD methods_asn1=
141 asn1_bio_callback_ctrl, 136 asn1_bio_callback_ctrl,
142}; 137};
143 138
144BIO_METHOD *BIO_f_asn1(void) 139BIO_METHOD *
140BIO_f_asn1(void)
145{ 141{
146 return(&methods_asn1); 142 return (&methods_asn1);
147} 143}
148 144
149 145static int
150static int asn1_bio_new(BIO *b) 146asn1_bio_new(BIO *b)
151{ 147{
152 BIO_ASN1_BUF_CTX *ctx; 148 BIO_ASN1_BUF_CTX *ctx;
153 ctx = malloc(sizeof(BIO_ASN1_BUF_CTX)); 149 ctx = malloc(sizeof(BIO_ASN1_BUF_CTX));
@@ -156,14 +152,15 @@ static int asn1_bio_new(BIO *b)
156 if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) { 152 if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
157 free(ctx); 153 free(ctx);
158 return 0; 154 return 0;
159} 155 }
160 b->init = 1; 156 b->init = 1;
161 b->ptr = (char *)ctx; 157 b->ptr = (char *)ctx;
162 b->flags = 0; 158 b->flags = 0;
163 return 1; 159 return 1;
164} 160}
165 161
166static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size) 162static int
163asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
167{ 164{
168 ctx->buf = malloc(size); 165 ctx->buf = malloc(size);
169 if (!ctx->buf) 166 if (!ctx->buf)
@@ -181,9 +178,11 @@ static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
181 return 1; 178 return 1;
182} 179}
183 180
184static int asn1_bio_free(BIO *b) 181static int
182asn1_bio_free(BIO *b)
185{ 183{
186 BIO_ASN1_BUF_CTX *ctx; 184 BIO_ASN1_BUF_CTX *ctx;
185
187 ctx = (BIO_ASN1_BUF_CTX *) b->ptr; 186 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
188 if (ctx == NULL) 187 if (ctx == NULL)
189 return 0; 188 return 0;
@@ -196,11 +195,13 @@ static int asn1_bio_free(BIO *b)
196 return 1; 195 return 1;
197} 196}
198 197
199static int asn1_bio_write(BIO *b, const char *in , int inl) 198static int
199asn1_bio_write(BIO *b, const char *in , int inl)
200{ 200{
201 BIO_ASN1_BUF_CTX *ctx; 201 BIO_ASN1_BUF_CTX *ctx;
202 int wrmax, wrlen, ret; 202 int wrmax, wrlen, ret;
203 unsigned char *p; 203 unsigned char *p;
204
204 if (!in || (inl < 0) || (b->next_bio == NULL)) 205 if (!in || (inl < 0) || (b->next_bio == NULL))
205 return 0; 206 return 0;
206 ctx = (BIO_ASN1_BUF_CTX *) b->ptr; 207 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
@@ -210,42 +211,38 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
210 wrlen = 0; 211 wrlen = 0;
211 ret = -1; 212 ret = -1;
212 213
213 for(;;) { 214 for (;;) {
214 switch (ctx->state) { 215 switch (ctx->state) {
215 216
216 /* Setup prefix data, call it */ 217 /* Setup prefix data, call it */
217 case ASN1_STATE_START: 218 case ASN1_STATE_START:
218 if (!asn1_bio_setup_ex(b, ctx, ctx->prefix, 219 if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
219 ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER)) 220 ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
220 return 0; 221 return 0;
221 break; 222 break;
222 223
223 /* Copy any pre data first */ 224 /* Copy any pre data first */
224 case ASN1_STATE_PRE_COPY: 225 case ASN1_STATE_PRE_COPY:
225
226 ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free, 226 ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
227 ASN1_STATE_HEADER); 227 ASN1_STATE_HEADER);
228
229 if (ret <= 0) 228 if (ret <= 0)
230 goto done; 229 goto done;
231
232 break; 230 break;
233 231
234 case ASN1_STATE_HEADER: 232 case ASN1_STATE_HEADER:
235 ctx->buflen = 233 ctx->buflen =
236 ASN1_object_size(0, inl, ctx->asn1_tag) - inl; 234 ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
237 OPENSSL_assert(ctx->buflen <= ctx->bufsize); 235 OPENSSL_assert(ctx->buflen <= ctx->bufsize);
238 p = ctx->buf; 236 p = ctx->buf;
239 ASN1_put_object(&p, 0, inl, 237 ASN1_put_object(&p, 0, inl,
240 ctx->asn1_tag, ctx->asn1_class); 238 ctx->asn1_tag, ctx->asn1_class);
241 ctx->copylen = inl; 239 ctx->copylen = inl;
242 ctx->state = ASN1_STATE_HEADER_COPY; 240 ctx->state = ASN1_STATE_HEADER_COPY;
243
244 break; 241 break;
245 242
246 case ASN1_STATE_HEADER_COPY: 243 case ASN1_STATE_HEADER_COPY:
247 ret = BIO_write(b->next_bio, 244 ret = BIO_write(b->next_bio,
248 ctx->buf + ctx->bufpos, ctx->buflen); 245 ctx->buf + ctx->bufpos, ctx->buflen);
249 if (ret <= 0) 246 if (ret <= 0)
250 goto done; 247 goto done;
251 248
@@ -256,7 +253,6 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
256 ctx->bufpos = 0; 253 ctx->bufpos = 0;
257 ctx->state = ASN1_STATE_DATA_COPY; 254 ctx->state = ASN1_STATE_DATA_COPY;
258 } 255 }
259
260 break; 256 break;
261 257
262 case ASN1_STATE_DATA_COPY: 258 case ASN1_STATE_DATA_COPY:
@@ -275,46 +271,44 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
275 271
276 if (ctx->copylen == 0) 272 if (ctx->copylen == 0)
277 ctx->state = ASN1_STATE_HEADER; 273 ctx->state = ASN1_STATE_HEADER;
278
279 if (inl == 0) 274 if (inl == 0)
280 goto done; 275 goto done;
281
282 break; 276 break;
283 277
284 default: 278 default:
285 BIO_clear_retry_flags(b); 279 BIO_clear_retry_flags(b);
286 return 0; 280 return 0;
287
288 } 281 }
289 282
290 } 283 }
291 284
292 done: 285done:
293 BIO_clear_retry_flags(b); 286 BIO_clear_retry_flags(b);
294 BIO_copy_next_retry(b); 287 BIO_copy_next_retry(b);
295 288
296 return (wrlen > 0) ? wrlen : ret; 289 return (wrlen > 0) ? wrlen : ret;
297
298} 290}
299 291
300static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 292static int
301 asn1_ps_func *cleanup, asn1_bio_state_t next) 293asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, asn1_ps_func *cleanup,
294 asn1_bio_state_t next)
302{ 295{
303 int ret; 296 int ret;
297
304 if (ctx->ex_len <= 0) 298 if (ctx->ex_len <= 0)
305 return 1; 299 return 1;
306 for(;;) { 300 for (;;) {
307 ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos, 301 ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos,
308 ctx->ex_len); 302 ctx->ex_len);
309 if (ret <= 0) 303 if (ret <= 0)
310 break; 304 break;
311 ctx->ex_len -= ret; 305 ctx->ex_len -= ret;
312 if (ctx->ex_len > 0) 306 if (ctx->ex_len > 0)
313 ctx->ex_pos += ret; 307 ctx->ex_pos += ret;
314 else { 308 else {
315 if(cleanup) 309 if (cleanup)
316 cleanup(b, &ctx->ex_buf, &ctx->ex_len, 310 cleanup(b, &ctx->ex_buf, &ctx->ex_len,
317 &ctx->ex_arg); 311 &ctx->ex_arg);
318 ctx->state = next; 312 ctx->state = next;
319 ctx->ex_pos = 0; 313 ctx->ex_pos = 0;
320 break; 314 break;
@@ -323,10 +317,9 @@ static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
323 return ret; 317 return ret;
324} 318}
325 319
326static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 320static int
327 asn1_ps_func *setup, 321asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, asn1_ps_func *setup,
328 asn1_bio_state_t ex_state, 322 asn1_bio_state_t ex_state, asn1_bio_state_t other_state)
329 asn1_bio_state_t other_state)
330{ 323{
331 if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) { 324 if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
332 BIO_clear_retry_flags(b); 325 BIO_clear_retry_flags(b);
@@ -339,45 +332,52 @@ static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
339 return 1; 332 return 1;
340} 333}
341 334
342static int asn1_bio_read(BIO *b, char *in , int inl) 335static int
336asn1_bio_read(BIO *b, char *in , int inl)
343{ 337{
344 if (!b->next_bio) 338 if (!b->next_bio)
345 return 0; 339 return 0;
346 return BIO_read(b->next_bio, in , inl); 340 return BIO_read(b->next_bio, in , inl);
347} 341}
348 342
349static int asn1_bio_puts(BIO *b, const char *str) 343static int
344asn1_bio_puts(BIO *b, const char *str)
350{ 345{
351 return asn1_bio_write(b, str, strlen(str)); 346 return asn1_bio_write(b, str, strlen(str));
352} 347}
353 348
354static int asn1_bio_gets(BIO *b, char *str, int size) 349static int
350asn1_bio_gets(BIO *b, char *str, int size)
355{ 351{
356 if (!b->next_bio) 352 if (!b->next_bio)
357 return 0; 353 return 0;
358 return BIO_gets(b->next_bio, str , size); 354 return BIO_gets(b->next_bio, str , size);
359} 355}
360 356
361static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 357static long
358asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
362{ 359{
363 if (b->next_bio == NULL) return(0); 360 if (b->next_bio == NULL)
364 return BIO_callback_ctrl(b->next_bio,cmd,fp); 361 return (0);
362 return BIO_callback_ctrl(b->next_bio, cmd, fp);
365} 363}
366 364
367static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2) 365static long
366asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
368{ 367{
369 BIO_ASN1_BUF_CTX *ctx; 368 BIO_ASN1_BUF_CTX *ctx;
370 BIO_ASN1_EX_FUNCS *ex_func; 369 BIO_ASN1_EX_FUNCS *ex_func;
371 long ret = 1; 370 long ret = 1;
371
372 ctx = (BIO_ASN1_BUF_CTX *) b->ptr; 372 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
373 if (ctx == NULL) 373 if (ctx == NULL)
374 return 0; 374 return 0;
375 switch(cmd) { 375 switch (cmd) {
376 376
377 case BIO_C_SET_PREFIX: 377 case BIO_C_SET_PREFIX:
378 ex_func = arg2; 378 ex_func = arg2;
379 ctx->prefix = ex_func->ex_func; 379 ctx->prefix = ex_func->ex_func;
380 ctx->prefix_free = ex_func->ex_free_func; 380 ctx->prefix_free = ex_func->ex_free_func;
381 break; 381 break;
382 382
383 case BIO_C_GET_PREFIX: 383 case BIO_C_GET_PREFIX:
@@ -388,8 +388,8 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
388 388
389 case BIO_C_SET_SUFFIX: 389 case BIO_C_SET_SUFFIX:
390 ex_func = arg2; 390 ex_func = arg2;
391 ctx->suffix = ex_func->ex_func; 391 ctx->suffix = ex_func->ex_func;
392 ctx->suffix_free = ex_func->ex_free_func; 392 ctx->suffix_free = ex_func->ex_free_func;
393 break; 393 break;
394 394
395 case BIO_C_GET_SUFFIX: 395 case BIO_C_GET_SUFFIX:
@@ -413,13 +413,13 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
413 /* Call post function if possible */ 413 /* Call post function if possible */
414 if (ctx->state == ASN1_STATE_HEADER) { 414 if (ctx->state == ASN1_STATE_HEADER) {
415 if (!asn1_bio_setup_ex(b, ctx, ctx->suffix, 415 if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
416 ASN1_STATE_POST_COPY, ASN1_STATE_DONE)) 416 ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
417 return 0; 417 return 0;
418 } 418 }
419 419
420 if (ctx->state == ASN1_STATE_POST_COPY) { 420 if (ctx->state == ASN1_STATE_POST_COPY) {
421 ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free, 421 ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
422 ASN1_STATE_DONE); 422 ASN1_STATE_DONE);
423 if (ret <= 0) 423 if (ret <= 0)
424 return ret; 424 return ret;
425 } 425 }
@@ -443,20 +443,24 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
443 return ret; 443 return ret;
444} 444}
445 445
446static int asn1_bio_set_ex(BIO *b, int cmd, 446static int
447 asn1_ps_func *ex_func, asn1_ps_func *ex_free_func) 447asn1_bio_set_ex(BIO *b, int cmd, asn1_ps_func *ex_func, asn1_ps_func
448 *ex_free_func)
448{ 449{
449 BIO_ASN1_EX_FUNCS extmp; 450 BIO_ASN1_EX_FUNCS extmp;
451
450 extmp.ex_func = ex_func; 452 extmp.ex_func = ex_func;
451 extmp.ex_free_func = ex_free_func; 453 extmp.ex_free_func = ex_free_func;
452 return BIO_ctrl(b, cmd, 0, &extmp); 454 return BIO_ctrl(b, cmd, 0, &extmp);
453} 455}
454 456
455static int asn1_bio_get_ex(BIO *b, int cmd, 457static int
456 asn1_ps_func **ex_func, asn1_ps_func **ex_free_func) 458asn1_bio_get_ex(BIO *b, int cmd, asn1_ps_func **ex_func,
459 asn1_ps_func **ex_free_func)
457{ 460{
458 BIO_ASN1_EX_FUNCS extmp; 461 BIO_ASN1_EX_FUNCS extmp;
459 int ret; 462 int ret;
463
460 ret = BIO_ctrl(b, cmd, 0, &extmp); 464 ret = BIO_ctrl(b, cmd, 0, &extmp);
461 if (ret > 0) { 465 if (ret > 0) {
462 *ex_func = extmp.ex_func; 466 *ex_func = extmp.ex_func;
@@ -465,22 +469,26 @@ static int asn1_bio_get_ex(BIO *b, int cmd,
465 return ret; 469 return ret;
466} 470}
467 471
468int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free) 472int
473BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free)
469{ 474{
470 return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free); 475 return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
471} 476}
472 477
473int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free) 478int
479BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free)
474{ 480{
475 return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free); 481 return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
476} 482}
477 483
478int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free) 484int
485BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free)
479{ 486{
480 return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free); 487 return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
481} 488}
482 489
483int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free) 490int
491BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free)
484{ 492{
485 return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free); 493 return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
486} 494}