1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <openssl/asn1.h>
16 
17 #include <openssl/bio.h>
18 #include <openssl/err.h>
19 #include <openssl/mem.h>
20 
21 
ASN1_item_i2d_fp(const ASN1_ITEM * it,FILE * out,void * x)22 int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, void *x) {
23   BIO *b = BIO_new_fp(out, BIO_NOCLOSE);
24   if (b == NULL) {
25     OPENSSL_PUT_ERROR(ASN1, ERR_R_BUF_LIB);
26     return 0;
27   }
28   int ret = ASN1_item_i2d_bio(it, b, x);
29   BIO_free(b);
30   return ret;
31 }
32 
ASN1_item_i2d_bio(const ASN1_ITEM * it,BIO * out,void * x)33 int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x) {
34   unsigned char *b = NULL;
35   int n = ASN1_item_i2d(reinterpret_cast<ASN1_VALUE *>(x), &b, it);
36   if (b == NULL) {
37     return 0;
38   }
39 
40   int ret = BIO_write_all(out, b, n);
41   OPENSSL_free(b);
42   return ret;
43 }
44