[331] | 1 | /*
|
---|
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
| 5 | * this file except in compliance with the License. You can obtain a copy
|
---|
| 6 | * in the file LICENSE in the source distribution or at
|
---|
| 7 | * https://www.openssl.org/source/license.html
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include <stdio.h>
|
---|
| 11 | #include "internal/cryptlib.h"
|
---|
| 12 | #include <openssl/buffer.h>
|
---|
| 13 | #include <openssl/asn1.h>
|
---|
| 14 |
|
---|
| 15 | #ifndef NO_OLD_ASN1
|
---|
| 16 |
|
---|
| 17 | # ifndef OPENSSL_NO_STDIO
|
---|
| 18 | int ASN1_i2d_fp(i2d_of_void *i2d, FILE *out, void *x)
|
---|
| 19 | {
|
---|
| 20 | BIO *b;
|
---|
| 21 | int ret;
|
---|
| 22 |
|
---|
| 23 | if ((b = BIO_new(BIO_s_file())) == NULL) {
|
---|
| 24 | ASN1err(ASN1_F_ASN1_I2D_FP, ERR_R_BUF_LIB);
|
---|
| 25 | return (0);
|
---|
| 26 | }
|
---|
| 27 | BIO_set_fp(b, out, BIO_NOCLOSE);
|
---|
| 28 | ret = ASN1_i2d_bio(i2d, b, x);
|
---|
| 29 | BIO_free(b);
|
---|
| 30 | return (ret);
|
---|
| 31 | }
|
---|
| 32 | # endif
|
---|
| 33 |
|
---|
| 34 | int ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, unsigned char *x)
|
---|
| 35 | {
|
---|
| 36 | char *b;
|
---|
| 37 | unsigned char *p;
|
---|
| 38 | int i, j = 0, n, ret = 1;
|
---|
| 39 |
|
---|
| 40 | n = i2d(x, NULL);
|
---|
| 41 | b = OPENSSL_malloc(n);
|
---|
| 42 | if (b == NULL) {
|
---|
| 43 | ASN1err(ASN1_F_ASN1_I2D_BIO, ERR_R_MALLOC_FAILURE);
|
---|
| 44 | return (0);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | p = (unsigned char *)b;
|
---|
| 48 | i2d(x, &p);
|
---|
| 49 |
|
---|
| 50 | for (;;) {
|
---|
| 51 | i = BIO_write(out, &(b[j]), n);
|
---|
| 52 | if (i == n)
|
---|
| 53 | break;
|
---|
| 54 | if (i <= 0) {
|
---|
| 55 | ret = 0;
|
---|
| 56 | break;
|
---|
| 57 | }
|
---|
| 58 | j += i;
|
---|
| 59 | n -= i;
|
---|
| 60 | }
|
---|
| 61 | OPENSSL_free(b);
|
---|
| 62 | return (ret);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | #endif
|
---|
| 66 |
|
---|
| 67 | #ifndef OPENSSL_NO_STDIO
|
---|
| 68 | int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *x)
|
---|
| 69 | {
|
---|
| 70 | BIO *b;
|
---|
| 71 | int ret;
|
---|
| 72 |
|
---|
| 73 | if ((b = BIO_new(BIO_s_file())) == NULL) {
|
---|
| 74 | ASN1err(ASN1_F_ASN1_ITEM_I2D_FP, ERR_R_BUF_LIB);
|
---|
| 75 | return (0);
|
---|
| 76 | }
|
---|
| 77 | BIO_set_fp(b, out, BIO_NOCLOSE);
|
---|
| 78 | ret = ASN1_item_i2d_bio(it, b, x);
|
---|
| 79 | BIO_free(b);
|
---|
| 80 | return (ret);
|
---|
| 81 | }
|
---|
| 82 | #endif
|
---|
| 83 |
|
---|
| 84 | int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x)
|
---|
| 85 | {
|
---|
| 86 | unsigned char *b = NULL;
|
---|
| 87 | int i, j = 0, n, ret = 1;
|
---|
| 88 |
|
---|
| 89 | n = ASN1_item_i2d(x, &b, it);
|
---|
| 90 | if (b == NULL) {
|
---|
| 91 | ASN1err(ASN1_F_ASN1_ITEM_I2D_BIO, ERR_R_MALLOC_FAILURE);
|
---|
| 92 | return (0);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | for (;;) {
|
---|
| 96 | i = BIO_write(out, &(b[j]), n);
|
---|
| 97 | if (i == n)
|
---|
| 98 | break;
|
---|
| 99 | if (i <= 0) {
|
---|
| 100 | ret = 0;
|
---|
| 101 | break;
|
---|
| 102 | }
|
---|
| 103 | j += i;
|
---|
| 104 | n -= i;
|
---|
| 105 | }
|
---|
| 106 | OPENSSL_free(b);
|
---|
| 107 | return (ret);
|
---|
| 108 | }
|
---|