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