diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/bio_asn1.c')
-rw-r--r-- | src/lib/libcrypto/asn1/bio_asn1.c | 132 |
1 files changed, 66 insertions, 66 deletions
diff --git a/src/lib/libcrypto/asn1/bio_asn1.c b/src/lib/libcrypto/asn1/bio_asn1.c index b59645909c..a6ad850e46 100644 --- a/src/lib/libcrypto/asn1/bio_asn1.c +++ b/src/lib/libcrypto/asn1/bio_asn1.c | |||
@@ -69,7 +69,7 @@ | |||
69 | #define DEFAULT_ASN1_BUF_SIZE 20 | 69 | #define DEFAULT_ASN1_BUF_SIZE 20 |
70 | 70 | ||
71 | typedef enum | 71 | typedef enum |
72 | { | 72 | { |
73 | ASN1_STATE_START, | 73 | ASN1_STATE_START, |
74 | ASN1_STATE_PRE_COPY, | 74 | ASN1_STATE_PRE_COPY, |
75 | ASN1_STATE_HEADER, | 75 | ASN1_STATE_HEADER, |
@@ -77,16 +77,16 @@ typedef enum | |||
77 | ASN1_STATE_DATA_COPY, | 77 | ASN1_STATE_DATA_COPY, |
78 | ASN1_STATE_POST_COPY, | 78 | ASN1_STATE_POST_COPY, |
79 | ASN1_STATE_DONE | 79 | ASN1_STATE_DONE |
80 | } asn1_bio_state_t; | 80 | } asn1_bio_state_t; |
81 | 81 | ||
82 | typedef struct BIO_ASN1_EX_FUNCS_st | 82 | typedef struct BIO_ASN1_EX_FUNCS_st |
83 | { | 83 | { |
84 | asn1_ps_func *ex_func; | 84 | asn1_ps_func *ex_func; |
85 | asn1_ps_func *ex_free_func; | 85 | asn1_ps_func *ex_free_func; |
86 | } BIO_ASN1_EX_FUNCS; | 86 | } BIO_ASN1_EX_FUNCS; |
87 | 87 | ||
88 | typedef struct BIO_ASN1_BUF_CTX_t | 88 | typedef struct BIO_ASN1_BUF_CTX_t |
89 | { | 89 | { |
90 | /* Internal state */ | 90 | /* Internal state */ |
91 | asn1_bio_state_t state; | 91 | asn1_bio_state_t state; |
92 | /* Internal buffer */ | 92 | /* Internal buffer */ |
@@ -107,7 +107,7 @@ typedef struct BIO_ASN1_BUF_CTX_t | |||
107 | int ex_len; | 107 | int ex_len; |
108 | int ex_pos; | 108 | int ex_pos; |
109 | void *ex_arg; | 109 | void *ex_arg; |
110 | } BIO_ASN1_BUF_CTX; | 110 | } BIO_ASN1_BUF_CTX; |
111 | 111 | ||
112 | 112 | ||
113 | static int asn1_bio_write(BIO *h, const char *buf,int num); | 113 | static int asn1_bio_write(BIO *h, const char *buf,int num); |
@@ -128,7 +128,7 @@ static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, | |||
128 | asn1_bio_state_t other_state); | 128 | asn1_bio_state_t other_state); |
129 | 129 | ||
130 | static BIO_METHOD methods_asn1= | 130 | static BIO_METHOD methods_asn1= |
131 | { | 131 | { |
132 | BIO_TYPE_ASN1, | 132 | BIO_TYPE_ASN1, |
133 | "asn1", | 133 | "asn1", |
134 | asn1_bio_write, | 134 | asn1_bio_write, |
@@ -139,16 +139,16 @@ static BIO_METHOD methods_asn1= | |||
139 | asn1_bio_new, | 139 | asn1_bio_new, |
140 | asn1_bio_free, | 140 | asn1_bio_free, |
141 | asn1_bio_callback_ctrl, | 141 | asn1_bio_callback_ctrl, |
142 | }; | 142 | }; |
143 | 143 | ||
144 | BIO_METHOD *BIO_f_asn1(void) | 144 | BIO_METHOD *BIO_f_asn1(void) |
145 | { | 145 | { |
146 | return(&methods_asn1); | 146 | return(&methods_asn1); |
147 | } | 147 | } |
148 | 148 | ||
149 | 149 | ||
150 | static int asn1_bio_new(BIO *b) | 150 | static int asn1_bio_new(BIO *b) |
151 | { | 151 | { |
152 | BIO_ASN1_BUF_CTX *ctx; | 152 | BIO_ASN1_BUF_CTX *ctx; |
153 | ctx = malloc(sizeof(BIO_ASN1_BUF_CTX)); | 153 | ctx = malloc(sizeof(BIO_ASN1_BUF_CTX)); |
154 | if (!ctx) | 154 | if (!ctx) |
@@ -156,15 +156,15 @@ static int asn1_bio_new(BIO *b) | |||
156 | if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) { | 156 | if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) { |
157 | free(ctx); | 157 | free(ctx); |
158 | return 0; | 158 | return 0; |
159 | } | 159 | } |
160 | b->init = 1; | 160 | b->init = 1; |
161 | b->ptr = (char *)ctx; | 161 | b->ptr = (char *)ctx; |
162 | b->flags = 0; | 162 | b->flags = 0; |
163 | return 1; | 163 | return 1; |
164 | } | 164 | } |
165 | 165 | ||
166 | static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size) | 166 | static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size) |
167 | { | 167 | { |
168 | ctx->buf = malloc(size); | 168 | ctx->buf = malloc(size); |
169 | if (!ctx->buf) | 169 | if (!ctx->buf) |
170 | return 0; | 170 | return 0; |
@@ -179,10 +179,10 @@ static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size) | |||
179 | ctx->ex_len = 0; | 179 | ctx->ex_len = 0; |
180 | ctx->state = ASN1_STATE_START; | 180 | ctx->state = ASN1_STATE_START; |
181 | return 1; | 181 | return 1; |
182 | } | 182 | } |
183 | 183 | ||
184 | static int asn1_bio_free(BIO *b) | 184 | static int asn1_bio_free(BIO *b) |
185 | { | 185 | { |
186 | BIO_ASN1_BUF_CTX *ctx; | 186 | BIO_ASN1_BUF_CTX *ctx; |
187 | ctx = (BIO_ASN1_BUF_CTX *) b->ptr; | 187 | ctx = (BIO_ASN1_BUF_CTX *) b->ptr; |
188 | if (ctx == NULL) | 188 | if (ctx == NULL) |
@@ -194,10 +194,10 @@ static int asn1_bio_free(BIO *b) | |||
194 | b->ptr = NULL; | 194 | b->ptr = NULL; |
195 | b->flags = 0; | 195 | b->flags = 0; |
196 | return 1; | 196 | return 1; |
197 | } | 197 | } |
198 | 198 | ||
199 | static int asn1_bio_write(BIO *b, const char *in , int inl) | 199 | static int asn1_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; |
@@ -211,9 +211,9 @@ static int asn1_bio_write(BIO *b, const char *in , int inl) | |||
211 | ret = -1; | 211 | ret = -1; |
212 | 212 | ||
213 | for(;;) | 213 | for(;;) |
214 | { | 214 | { |
215 | switch (ctx->state) | 215 | switch (ctx->state) |
216 | { | 216 | { |
217 | 217 | ||
218 | /* Setup prefix data, call it */ | 218 | /* Setup prefix data, call it */ |
219 | case ASN1_STATE_START: | 219 | case ASN1_STATE_START: |
@@ -255,10 +255,10 @@ static int asn1_bio_write(BIO *b, const char *in , int inl) | |||
255 | if (ctx->buflen) | 255 | if (ctx->buflen) |
256 | ctx->bufpos += ret; | 256 | ctx->bufpos += ret; |
257 | else | 257 | else |
258 | { | 258 | { |
259 | ctx->bufpos = 0; | 259 | ctx->bufpos = 0; |
260 | ctx->state = ASN1_STATE_DATA_COPY; | 260 | ctx->state = ASN1_STATE_DATA_COPY; |
261 | } | 261 | } |
262 | 262 | ||
263 | break; | 263 | break; |
264 | 264 | ||
@@ -288,26 +288,26 @@ static int asn1_bio_write(BIO *b, const char *in , int inl) | |||
288 | BIO_clear_retry_flags(b); | 288 | BIO_clear_retry_flags(b); |
289 | return 0; | 289 | return 0; |
290 | 290 | ||
291 | } | ||
292 | |||
293 | } | 291 | } |
294 | 292 | ||
293 | } | ||
294 | |||
295 | done: | 295 | done: |
296 | BIO_clear_retry_flags(b); | 296 | BIO_clear_retry_flags(b); |
297 | BIO_copy_next_retry(b); | 297 | BIO_copy_next_retry(b); |
298 | 298 | ||
299 | return (wrlen > 0) ? wrlen : ret; | 299 | return (wrlen > 0) ? wrlen : ret; |
300 | 300 | ||
301 | } | 301 | } |
302 | 302 | ||
303 | static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, | 303 | static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, |
304 | asn1_ps_func *cleanup, asn1_bio_state_t next) | 304 | asn1_ps_func *cleanup, asn1_bio_state_t next) |
305 | { | 305 | { |
306 | int ret; | 306 | int ret; |
307 | if (ctx->ex_len <= 0) | 307 | if (ctx->ex_len <= 0) |
308 | return 1; | 308 | return 1; |
309 | for(;;) | 309 | for(;;) |
310 | { | 310 | { |
311 | ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos, | 311 | ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos, |
312 | ctx->ex_len); | 312 | ctx->ex_len); |
313 | if (ret <= 0) | 313 | if (ret <= 0) |
@@ -316,62 +316,62 @@ static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, | |||
316 | if (ctx->ex_len > 0) | 316 | if (ctx->ex_len > 0) |
317 | ctx->ex_pos += ret; | 317 | ctx->ex_pos += ret; |
318 | else | 318 | else |
319 | { | 319 | { |
320 | if(cleanup) | 320 | if(cleanup) |
321 | cleanup(b, &ctx->ex_buf, &ctx->ex_len, | 321 | cleanup(b, &ctx->ex_buf, &ctx->ex_len, |
322 | &ctx->ex_arg); | 322 | &ctx->ex_arg); |
323 | ctx->state = next; | 323 | ctx->state = next; |
324 | ctx->ex_pos = 0; | 324 | ctx->ex_pos = 0; |
325 | break; | 325 | break; |
326 | } | ||
327 | } | 326 | } |
328 | return ret; | ||
329 | } | 327 | } |
328 | return ret; | ||
329 | } | ||
330 | 330 | ||
331 | static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, | 331 | static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, |
332 | asn1_ps_func *setup, | 332 | asn1_ps_func *setup, |
333 | asn1_bio_state_t ex_state, | 333 | asn1_bio_state_t ex_state, |
334 | asn1_bio_state_t other_state) | 334 | asn1_bio_state_t other_state) |
335 | { | 335 | { |
336 | if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) | 336 | if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) |
337 | { | 337 | { |
338 | BIO_clear_retry_flags(b); | 338 | BIO_clear_retry_flags(b); |
339 | return 0; | 339 | return 0; |
340 | } | 340 | } |
341 | if (ctx->ex_len > 0) | 341 | if (ctx->ex_len > 0) |
342 | ctx->state = ex_state; | 342 | ctx->state = ex_state; |
343 | else | 343 | else |
344 | ctx->state = other_state; | 344 | ctx->state = other_state; |
345 | return 1; | 345 | return 1; |
346 | } | 346 | } |
347 | 347 | ||
348 | static int asn1_bio_read(BIO *b, char *in , int inl) | 348 | static int asn1_bio_read(BIO *b, char *in , int inl) |
349 | { | 349 | { |
350 | if (!b->next_bio) | 350 | if (!b->next_bio) |
351 | return 0; | 351 | return 0; |
352 | return BIO_read(b->next_bio, in , inl); | 352 | return BIO_read(b->next_bio, in , inl); |
353 | } | 353 | } |
354 | 354 | ||
355 | static int asn1_bio_puts(BIO *b, const char *str) | 355 | static int asn1_bio_puts(BIO *b, const char *str) |
356 | { | 356 | { |
357 | return asn1_bio_write(b, str, strlen(str)); | 357 | return asn1_bio_write(b, str, strlen(str)); |
358 | } | 358 | } |
359 | 359 | ||
360 | static int asn1_bio_gets(BIO *b, char *str, int size) | 360 | static int asn1_bio_gets(BIO *b, char *str, int size) |
361 | { | 361 | { |
362 | if (!b->next_bio) | 362 | if (!b->next_bio) |
363 | return 0; | 363 | return 0; |
364 | return BIO_gets(b->next_bio, str , size); | 364 | return BIO_gets(b->next_bio, str , size); |
365 | } | 365 | } |
366 | 366 | ||
367 | static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | 367 | static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) |
368 | { | 368 | { |
369 | if (b->next_bio == NULL) return(0); | 369 | if (b->next_bio == NULL) return(0); |
370 | return BIO_callback_ctrl(b->next_bio,cmd,fp); | 370 | return BIO_callback_ctrl(b->next_bio,cmd,fp); |
371 | } | 371 | } |
372 | 372 | ||
373 | static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2) | 373 | static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2) |
374 | { | 374 | { |
375 | BIO_ASN1_BUF_CTX *ctx; | 375 | BIO_ASN1_BUF_CTX *ctx; |
376 | BIO_ASN1_EX_FUNCS *ex_func; | 376 | BIO_ASN1_EX_FUNCS *ex_func; |
377 | long ret = 1; | 377 | long ret = 1; |
@@ -379,7 +379,7 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2) | |||
379 | if (ctx == NULL) | 379 | if (ctx == NULL) |
380 | return 0; | 380 | return 0; |
381 | switch(cmd) | 381 | switch(cmd) |
382 | { | 382 | { |
383 | 383 | ||
384 | case BIO_C_SET_PREFIX: | 384 | case BIO_C_SET_PREFIX: |
385 | ex_func = arg2; | 385 | ex_func = arg2; |
@@ -419,27 +419,27 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2) | |||
419 | 419 | ||
420 | /* Call post function if possible */ | 420 | /* Call post function if possible */ |
421 | if (ctx->state == ASN1_STATE_HEADER) | 421 | if (ctx->state == ASN1_STATE_HEADER) |
422 | { | 422 | { |
423 | if (!asn1_bio_setup_ex(b, ctx, ctx->suffix, | 423 | if (!asn1_bio_setup_ex(b, ctx, ctx->suffix, |
424 | ASN1_STATE_POST_COPY, ASN1_STATE_DONE)) | 424 | ASN1_STATE_POST_COPY, ASN1_STATE_DONE)) |
425 | return 0; | 425 | return 0; |
426 | } | 426 | } |
427 | 427 | ||
428 | if (ctx->state == ASN1_STATE_POST_COPY) | 428 | if (ctx->state == ASN1_STATE_POST_COPY) |
429 | { | 429 | { |
430 | ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free, | 430 | ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free, |
431 | ASN1_STATE_DONE); | 431 | ASN1_STATE_DONE); |
432 | if (ret <= 0) | 432 | if (ret <= 0) |
433 | return ret; | 433 | return ret; |
434 | } | 434 | } |
435 | 435 | ||
436 | if (ctx->state == ASN1_STATE_DONE) | 436 | if (ctx->state == ASN1_STATE_DONE) |
437 | return BIO_ctrl(b->next_bio, cmd, arg1, arg2); | 437 | return BIO_ctrl(b->next_bio, cmd, arg1, arg2); |
438 | else | 438 | else |
439 | { | 439 | { |
440 | BIO_clear_retry_flags(b); | 440 | BIO_clear_retry_flags(b); |
441 | return 0; | 441 | return 0; |
442 | } | 442 | } |
443 | break; | 443 | break; |
444 | 444 | ||
445 | 445 | ||
@@ -448,50 +448,50 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2) | |||
448 | return 0; | 448 | return 0; |
449 | return BIO_ctrl(b->next_bio, cmd, arg1, arg2); | 449 | return BIO_ctrl(b->next_bio, cmd, arg1, arg2); |
450 | 450 | ||
451 | } | 451 | } |
452 | 452 | ||
453 | return ret; | 453 | return ret; |
454 | } | 454 | } |
455 | 455 | ||
456 | static int asn1_bio_set_ex(BIO *b, int cmd, | 456 | static int asn1_bio_set_ex(BIO *b, int cmd, |
457 | asn1_ps_func *ex_func, asn1_ps_func *ex_free_func) | 457 | asn1_ps_func *ex_func, asn1_ps_func *ex_free_func) |
458 | { | 458 | { |
459 | BIO_ASN1_EX_FUNCS extmp; | 459 | BIO_ASN1_EX_FUNCS extmp; |
460 | extmp.ex_func = ex_func; | 460 | extmp.ex_func = ex_func; |
461 | extmp.ex_free_func = ex_free_func; | 461 | extmp.ex_free_func = ex_free_func; |
462 | return BIO_ctrl(b, cmd, 0, &extmp); | 462 | return BIO_ctrl(b, cmd, 0, &extmp); |
463 | } | 463 | } |
464 | 464 | ||
465 | static int asn1_bio_get_ex(BIO *b, int cmd, | 465 | static int asn1_bio_get_ex(BIO *b, int cmd, |
466 | asn1_ps_func **ex_func, asn1_ps_func **ex_free_func) | 466 | asn1_ps_func **ex_func, asn1_ps_func **ex_free_func) |
467 | { | 467 | { |
468 | BIO_ASN1_EX_FUNCS extmp; | 468 | BIO_ASN1_EX_FUNCS extmp; |
469 | int ret; | 469 | int ret; |
470 | ret = BIO_ctrl(b, cmd, 0, &extmp); | 470 | ret = BIO_ctrl(b, cmd, 0, &extmp); |
471 | if (ret > 0) | 471 | if (ret > 0) |
472 | { | 472 | { |
473 | *ex_func = extmp.ex_func; | 473 | *ex_func = extmp.ex_func; |
474 | *ex_free_func = extmp.ex_free_func; | 474 | *ex_free_func = extmp.ex_free_func; |
475 | } | ||
476 | return ret; | ||
477 | } | 475 | } |
476 | return ret; | ||
477 | } | ||
478 | 478 | ||
479 | int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free) | 479 | int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free) |
480 | { | 480 | { |
481 | return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free); | 481 | return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free); |
482 | } | 482 | } |
483 | 483 | ||
484 | int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free) | 484 | int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free) |
485 | { | 485 | { |
486 | return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free); | 486 | return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free); |
487 | } | 487 | } |
488 | 488 | ||
489 | int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free) | 489 | int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free) |
490 | { | 490 | { |
491 | return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free); | 491 | return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free); |
492 | } | 492 | } |
493 | 493 | ||
494 | int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free) | 494 | int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free) |
495 | { | 495 | { |
496 | return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free); | 496 | return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free); |
497 | } | 497 | } |