Imported Upstream version 1.1.1l
[platform/upstream/openssl1.1.git] / crypto / asn1 / bio_ndef.c
1 /*
2  * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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
8  */
9
10 #include <openssl/asn1.h>
11 #include <openssl/asn1t.h>
12 #include <openssl/bio.h>
13 #include <openssl/err.h>
14
15 #include <stdio.h>
16
17 /* Experimental NDEF ASN1 BIO support routines */
18
19 /*
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*...
27  */
28
29 /* BIO support data stored in the ASN1 BIO ex_arg */
30
31 typedef struct ndef_aux_st {
32     /* ASN1 structure this BIO refers to */
33     ASN1_VALUE *val;
34     const ASN1_ITEM *it;
35     /* Top of the BIO chain */
36     BIO *ndef_bio;
37     /* Output BIO */
38     BIO *out;
39     /* Boundary where content is inserted */
40     unsigned char **boundary;
41     /* DER buffer start */
42     unsigned char *derbuf;
43 } NDEF_SUPPORT;
44
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,
47                             void *parg);
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,
50                             void *parg);
51
52 BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
53 {
54     NDEF_SUPPORT *ndef_aux = NULL;
55     BIO *asn_bio = NULL;
56     const ASN1_AUX *aux = it->funcs;
57     ASN1_STREAM_ARG sarg;
58
59     if (!aux || !aux->asn1_cb) {
60         ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED);
61         return NULL;
62     }
63     ndef_aux = OPENSSL_zalloc(sizeof(*ndef_aux));
64     asn_bio = BIO_new(BIO_f_asn1());
65     if (ndef_aux == NULL || asn_bio == NULL)
66         goto err;
67
68     /* ASN1 bio needs to be next to output BIO */
69     out = BIO_push(asn_bio, out);
70     if (out == NULL)
71         goto err;
72
73     BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free);
74     BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free);
75
76     /*
77      * Now let callback prepends any digest, cipher etc BIOs ASN1 structure
78      * needs.
79      */
80
81     sarg.out = out;
82     sarg.ndef_bio = NULL;
83     sarg.boundary = NULL;
84
85     if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0)
86         goto err;
87
88     ndef_aux->val = val;
89     ndef_aux->it = it;
90     ndef_aux->ndef_bio = sarg.ndef_bio;
91     ndef_aux->boundary = sarg.boundary;
92     ndef_aux->out = out;
93
94     BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
95
96     return sarg.ndef_bio;
97
98  err:
99     BIO_free(asn_bio);
100     OPENSSL_free(ndef_aux);
101     return NULL;
102 }
103
104 static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
105 {
106     NDEF_SUPPORT *ndef_aux;
107     unsigned char *p;
108     int derlen;
109
110     if (!parg)
111         return 0;
112
113     ndef_aux = *(NDEF_SUPPORT **)parg;
114
115     derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
116     if (derlen < 0)
117         return 0;
118     if ((p = OPENSSL_malloc(derlen)) == NULL) {
119         ASN1err(ASN1_F_NDEF_PREFIX, ERR_R_MALLOC_FAILURE);
120         return 0;
121     }
122
123     ndef_aux->derbuf = p;
124     *pbuf = p;
125     derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
126
127     if (!*ndef_aux->boundary)
128         return 0;
129
130     *plen = *ndef_aux->boundary - *pbuf;
131
132     return 1;
133 }
134
135 static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
136                             void *parg)
137 {
138     NDEF_SUPPORT *ndef_aux;
139
140     if (!parg)
141         return 0;
142
143     ndef_aux = *(NDEF_SUPPORT **)parg;
144
145     if (ndef_aux == NULL)
146         return 0;
147
148     OPENSSL_free(ndef_aux->derbuf);
149
150     ndef_aux->derbuf = NULL;
151     *pbuf = NULL;
152     *plen = 0;
153     return 1;
154 }
155
156 static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
157                             void *parg)
158 {
159     NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
160     if (!ndef_prefix_free(b, pbuf, plen, parg))
161         return 0;
162     OPENSSL_free(*pndef_aux);
163     *pndef_aux = NULL;
164     return 1;
165 }
166
167 static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
168 {
169     NDEF_SUPPORT *ndef_aux;
170     unsigned char *p;
171     int derlen;
172     const ASN1_AUX *aux;
173     ASN1_STREAM_ARG sarg;
174
175     if (!parg)
176         return 0;
177
178     ndef_aux = *(NDEF_SUPPORT **)parg;
179
180     aux = ndef_aux->it->funcs;
181
182     /* Finalize structures */
183     sarg.ndef_bio = ndef_aux->ndef_bio;
184     sarg.out = ndef_aux->out;
185     sarg.boundary = ndef_aux->boundary;
186     if (aux->asn1_cb(ASN1_OP_STREAM_POST,
187                      &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
188         return 0;
189
190     derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
191     if ((p = OPENSSL_malloc(derlen)) == NULL) {
192         ASN1err(ASN1_F_NDEF_SUFFIX, ERR_R_MALLOC_FAILURE);
193         return 0;
194     }
195
196     ndef_aux->derbuf = p;
197     *pbuf = p;
198     derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
199
200     if (!*ndef_aux->boundary)
201         return 0;
202     *pbuf = *ndef_aux->boundary;
203     *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
204
205     return 1;
206 }