2016-05-18 02:51:34 +08:00
|
|
|
/*
|
2021-05-20 21:22:33 +08:00
|
|
|
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:17:34 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:34 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/buffer.h>
|
2000-12-09 03:09:35 +08:00
|
|
|
#include <openssl/asn1.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2000-12-30 10:40:26 +08:00
|
|
|
#ifndef NO_OLD_ASN1
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
# ifndef OPENSSL_NO_STDIO
|
2019-01-16 04:51:25 +08:00
|
|
|
int ASN1_i2d_fp(i2d_of_void *i2d, FILE *out, const void *x)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
int ret;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
BIO_set_fp(b, out, BIO_NOCLOSE);
|
|
|
|
ret = ASN1_i2d_bio(i2d, b, x);
|
|
|
|
BIO_free(b);
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
# endif
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2019-01-16 04:51:25 +08:00
|
|
|
int ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, const void *x)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
char *b;
|
|
|
|
unsigned char *p;
|
|
|
|
int i, j = 0, n, ret = 1;
|
|
|
|
|
|
|
|
n = i2d(x, NULL);
|
2017-10-27 21:13:11 +08:00
|
|
|
if (n <= 0)
|
|
|
|
return 0;
|
|
|
|
|
2015-04-29 03:28:14 +08:00
|
|
|
b = OPENSSL_malloc(n);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (b == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
p = (unsigned char *)b;
|
|
|
|
i2d(x, &p);
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
for (;;) {
|
|
|
|
i = BIO_write(out, &(b[j]), n);
|
|
|
|
if (i == n)
|
|
|
|
break;
|
|
|
|
if (i <= 0) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
j += i;
|
|
|
|
n -= i;
|
|
|
|
}
|
|
|
|
OPENSSL_free(b);
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2000-12-30 10:40:26 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-01-15 04:57:28 +08:00
|
|
|
#ifndef OPENSSL_NO_STDIO
|
2019-01-16 04:51:25 +08:00
|
|
|
int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, const void *x)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
int ret;
|
2000-12-30 10:40:26 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
BIO_set_fp(b, out, BIO_NOCLOSE);
|
|
|
|
ret = ASN1_item_i2d_bio(it, b, x);
|
|
|
|
BIO_free(b);
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2000-12-30 10:40:26 +08:00
|
|
|
#endif
|
|
|
|
|
2019-01-16 04:51:25 +08:00
|
|
|
int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, const void *x)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
unsigned char *b = NULL;
|
|
|
|
int i, j = 0, n, ret = 1;
|
2000-12-30 10:40:26 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
n = ASN1_item_i2d(x, &b, it);
|
|
|
|
if (b == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2000-12-30 10:40:26 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
for (;;) {
|
|
|
|
i = BIO_write(out, &(b[j]), n);
|
|
|
|
if (i == n)
|
|
|
|
break;
|
|
|
|
if (i <= 0) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
j += i;
|
|
|
|
n -= i;
|
|
|
|
}
|
|
|
|
OPENSSL_free(b);
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2021-05-07 23:16:48 +08:00
|
|
|
|
|
|
|
BIO *ASN1_item_i2d_mem_bio(const ASN1_ITEM *it, const ASN1_VALUE *val)
|
|
|
|
{
|
|
|
|
BIO *res;
|
|
|
|
|
|
|
|
if (it == NULL || val == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((res = BIO_new(BIO_s_mem())) == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (ASN1_item_i2d_bio(it, res, val) <= 0) {
|
|
|
|
BIO_free(res);
|
|
|
|
res = NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|