2016-05-17 18:51:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2018-12-06 12:17:34 +00:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-17 18:51:34 +00: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 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/buffer.h>
|
2000-12-08 19:09:35 +00:00
|
|
|
#include <openssl/asn1.h>
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-12-30 02:40:26 +00:00
|
|
|
#ifndef NO_OLD_ASN1
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
# ifndef OPENSSL_NO_STDIO
|
2019-01-15 20:51:25 +00:00
|
|
|
int ASN1_i2d_fp(i2d_of_void *i2d, FILE *out, const void *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
int ret;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
|
|
ASN1err(ASN1_F_ASN1_I2D_FP, ERR_R_BUF_LIB);
|
2017-10-17 14:04:09 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
BIO_set_fp(b, out, BIO_NOCLOSE);
|
|
|
|
ret = ASN1_i2d_bio(i2d, b, x);
|
|
|
|
BIO_free(b);
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
# endif
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2019-01-15 20:51:25 +00:00
|
|
|
int ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, const void *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
char *b;
|
|
|
|
unsigned char *p;
|
|
|
|
int i, j = 0, n, ret = 1;
|
|
|
|
|
|
|
|
n = i2d(x, NULL);
|
2017-10-27 13:13:11 +00:00
|
|
|
if (n <= 0)
|
|
|
|
return 0;
|
|
|
|
|
2015-04-28 19:28:14 +00:00
|
|
|
b = OPENSSL_malloc(n);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (b == NULL) {
|
|
|
|
ASN1err(ASN1_F_ASN1_I2D_BIO, ERR_R_MALLOC_FAILURE);
|
2017-10-17 14:04:09 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
p = (unsigned char *)b;
|
|
|
|
i2d(x, &p);
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00: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 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-12-30 02:40:26 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-01-14 20:57:28 +00:00
|
|
|
#ifndef OPENSSL_NO_STDIO
|
2019-01-15 20:51:25 +00:00
|
|
|
int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, const void *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
int ret;
|
2000-12-30 02:40:26 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_I2D_FP, ERR_R_BUF_LIB);
|
2017-10-17 14:04:09 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
BIO_set_fp(b, out, BIO_NOCLOSE);
|
|
|
|
ret = ASN1_item_i2d_bio(it, b, x);
|
|
|
|
BIO_free(b);
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-12-30 02:40:26 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-15 20:51:25 +00:00
|
|
|
int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, const void *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
unsigned char *b = NULL;
|
|
|
|
int i, j = 0, n, ret = 1;
|
2000-12-30 02:40:26 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
n = ASN1_item_i2d(x, &b, it);
|
|
|
|
if (b == NULL) {
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_I2D_BIO, ERR_R_MALLOC_FAILURE);
|
2017-10-17 14:04:09 +00:00
|
|
|
return 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-12-30 02:40:26 +00:00
|
|
|
|
2015-01-22 03:40:55 +00: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 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|