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
|
|
|
*
|
2016-05-17 18:51:34 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* 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
|
2005-03-31 10:55:55 +00:00
|
|
|
int ASN1_i2d_fp(i2d_of_void *i2d, FILE *out, 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);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
BIO_set_fp(b, out, BIO_NOCLOSE);
|
|
|
|
ret = ASN1_i2d_bio(i2d, b, x);
|
|
|
|
BIO_free(b);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
# endif
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2005-03-31 10:55:55 +00:00
|
|
|
int ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, unsigned char *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);
|
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);
|
|
|
|
return (0);
|
|
|
|
}
|
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);
|
|
|
|
return (ret);
|
|
|
|
}
|
2000-12-30 02:40:26 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-01-14 20:57:28 +00:00
|
|
|
#ifndef OPENSSL_NO_STDIO
|
2000-12-30 02:40:26 +00:00
|
|
|
int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, 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);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
BIO_set_fp(b, out, BIO_NOCLOSE);
|
|
|
|
ret = ASN1_item_i2d_bio(it, b, x);
|
|
|
|
BIO_free(b);
|
|
|
|
return (ret);
|
|
|
|
}
|
2000-12-30 02:40:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, 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);
|
|
|
|
return (0);
|
|
|
|
}
|
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);
|
|
|
|
return (ret);
|
|
|
|
}
|