2 * Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/asn1.h>
11 #include <openssl/asn1t.h>
12 #include <openssl/bio.h>
13 #include <openssl/err.h>
17 /* Experimental NDEF ASN1 BIO support routines */
20 * The usage is quite simple, initialize an ASN1 structure, get a BIO from it
21 * then any data written through the BIO will end up translated to
22 * appropriate format on the fly. The data is streamed out and does *not*
23 * need to be all held in memory at once. When the BIO is flushed the output
24 * is finalized and any signatures etc written out. The BIO is a 'proper'
25 * BIO and can handle non blocking I/O correctly. The usage is simple. The
26 * implementation is *not*...
29 /* BIO support data stored in the ASN1 BIO ex_arg */
31 typedef struct ndef_aux_st {
32 /* ASN1 structure this BIO refers to */
35 /* Top of the BIO chain */
39 /* Boundary where content is inserted */
40 unsigned char **boundary;
41 /* DER buffer start */
42 unsigned char *derbuf;
45 static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
46 static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
48 static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
49 static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
52 /* unfortunately cannot constify this due to CMS_stream() and PKCS7_stream() */
53 BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
55 NDEF_SUPPORT *ndef_aux = NULL;
57 const ASN1_AUX *aux = it->funcs;
60 if (!aux || !aux->asn1_cb) {
61 ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED);
64 ndef_aux = OPENSSL_zalloc(sizeof(*ndef_aux));
65 asn_bio = BIO_new(BIO_f_asn1());
66 if (ndef_aux == NULL || asn_bio == NULL)
69 /* ASN1 bio needs to be next to output BIO */
70 out = BIO_push(asn_bio, out);
74 BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free);
75 BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free);
78 * Now let callback prepends any digest, cipher etc BIOs ASN1 structure
86 if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0)
91 ndef_aux->ndef_bio = sarg.ndef_bio;
92 ndef_aux->boundary = sarg.boundary;
95 BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
101 OPENSSL_free(ndef_aux);
105 static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
107 NDEF_SUPPORT *ndef_aux;
114 ndef_aux = *(NDEF_SUPPORT **)parg;
116 derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
117 if ((p = OPENSSL_malloc(derlen)) == NULL) {
118 ASN1err(ASN1_F_NDEF_PREFIX, ERR_R_MALLOC_FAILURE);
122 ndef_aux->derbuf = p;
124 derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
126 if (!*ndef_aux->boundary)
129 *plen = *ndef_aux->boundary - *pbuf;
134 static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
137 NDEF_SUPPORT *ndef_aux;
142 ndef_aux = *(NDEF_SUPPORT **)parg;
144 OPENSSL_free(ndef_aux->derbuf);
146 ndef_aux->derbuf = NULL;
152 static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
155 NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
156 if (!ndef_prefix_free(b, pbuf, plen, parg))
158 OPENSSL_free(*pndef_aux);
163 static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
165 NDEF_SUPPORT *ndef_aux;
169 ASN1_STREAM_ARG sarg;
174 ndef_aux = *(NDEF_SUPPORT **)parg;
176 aux = ndef_aux->it->funcs;
178 /* Finalize structures */
179 sarg.ndef_bio = ndef_aux->ndef_bio;
180 sarg.out = ndef_aux->out;
181 sarg.boundary = ndef_aux->boundary;
182 if (aux->asn1_cb(ASN1_OP_STREAM_POST,
183 &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
186 derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
187 if ((p = OPENSSL_malloc(derlen)) == NULL) {
188 ASN1err(ASN1_F_NDEF_SUFFIX, ERR_R_MALLOC_FAILURE);
192 ndef_aux->derbuf = p;
194 derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
196 if (!*ndef_aux->boundary)
198 *pbuf = *ndef_aux->boundary;
199 *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);