]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | |
8931b30d DSH |
3 | * project. |
4 | */ | |
5 | /* ==================================================================== | |
6 | * Copyright (c) 2008 The OpenSSL Project. All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
8931b30d DSH |
14 | * |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * licensing@OpenSSL.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | */ | |
54 | ||
55 | #include <openssl/asn1.h> | |
56 | #include <openssl/asn1t.h> | |
57 | #include <openssl/bio.h> | |
58 | #include <openssl/err.h> | |
59 | ||
8931b30d DSH |
60 | #include <stdio.h> |
61 | ||
62 | /* Experimental NDEF ASN1 BIO support routines */ | |
63 | ||
0f113f3e MC |
64 | /* |
65 | * The usage is quite simple, initialize an ASN1 structure, get a BIO from it | |
66 | * then any data written through the BIO will end up translated to | |
b6453a68 | 67 | * appropriate format on the fly. The data is streamed out and does *not* |
0f113f3e MC |
68 | * need to be all held in memory at once. When the BIO is flushed the output |
69 | * is finalized and any signatures etc written out. The BIO is a 'proper' | |
70 | * BIO and can handle non blocking I/O correctly. The usage is simple. The | |
71 | * implementation is *not*... | |
8931b30d DSH |
72 | */ |
73 | ||
74 | /* BIO support data stored in the ASN1 BIO ex_arg */ | |
75 | ||
0f113f3e MC |
76 | typedef struct ndef_aux_st { |
77 | /* ASN1 structure this BIO refers to */ | |
78 | ASN1_VALUE *val; | |
79 | const ASN1_ITEM *it; | |
80 | /* Top of the BIO chain */ | |
81 | BIO *ndef_bio; | |
82 | /* Output BIO */ | |
83 | BIO *out; | |
84 | /* Boundary where content is inserted */ | |
85 | unsigned char **boundary; | |
86 | /* DER buffer start */ | |
87 | unsigned char *derbuf; | |
88 | } NDEF_SUPPORT; | |
8931b30d DSH |
89 | |
90 | static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg); | |
0f113f3e MC |
91 | static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, |
92 | void *parg); | |
8931b30d | 93 | static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg); |
0f113f3e MC |
94 | static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, |
95 | void *parg); | |
8931b30d DSH |
96 | |
97 | BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it) | |
0f113f3e MC |
98 | { |
99 | NDEF_SUPPORT *ndef_aux = NULL; | |
100 | BIO *asn_bio = NULL; | |
101 | const ASN1_AUX *aux = it->funcs; | |
102 | ASN1_STREAM_ARG sarg; | |
8931b30d | 103 | |
0f113f3e MC |
104 | if (!aux || !aux->asn1_cb) { |
105 | ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED); | |
106 | return NULL; | |
107 | } | |
b4faea50 | 108 | ndef_aux = OPENSSL_malloc(sizeof(*ndef_aux)); |
0f113f3e | 109 | asn_bio = BIO_new(BIO_f_asn1()); |
8931b30d | 110 | |
0f113f3e | 111 | /* ASN1 bio needs to be next to output BIO */ |
8931b30d | 112 | |
0f113f3e | 113 | out = BIO_push(asn_bio, out); |
8931b30d | 114 | |
90945fa3 | 115 | if (ndef_aux == NULL || asn_bio == NULL || !out) |
0f113f3e | 116 | goto err; |
8931b30d | 117 | |
0f113f3e MC |
118 | BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free); |
119 | BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free); | |
8931b30d | 120 | |
0f113f3e | 121 | /* |
8483a003 | 122 | * Now let callback prepends any digest, cipher etc BIOs ASN1 structure |
0f113f3e MC |
123 | * needs. |
124 | */ | |
8931b30d | 125 | |
0f113f3e MC |
126 | sarg.out = out; |
127 | sarg.ndef_bio = NULL; | |
128 | sarg.boundary = NULL; | |
8931b30d | 129 | |
0f113f3e MC |
130 | if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0) |
131 | goto err; | |
8931b30d | 132 | |
0f113f3e MC |
133 | ndef_aux->val = val; |
134 | ndef_aux->it = it; | |
135 | ndef_aux->ndef_bio = sarg.ndef_bio; | |
136 | ndef_aux->boundary = sarg.boundary; | |
137 | ndef_aux->out = out; | |
8931b30d | 138 | |
0f113f3e | 139 | BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux); |
8931b30d | 140 | |
0f113f3e | 141 | return sarg.ndef_bio; |
8931b30d | 142 | |
0f113f3e | 143 | err: |
ca3a82c3 | 144 | BIO_free(asn_bio); |
b548a1f1 | 145 | OPENSSL_free(ndef_aux); |
0f113f3e MC |
146 | return NULL; |
147 | } | |
8931b30d DSH |
148 | |
149 | static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg) | |
0f113f3e MC |
150 | { |
151 | NDEF_SUPPORT *ndef_aux; | |
152 | unsigned char *p; | |
153 | int derlen; | |
8931b30d | 154 | |
0f113f3e MC |
155 | if (!parg) |
156 | return 0; | |
8931b30d | 157 | |
0f113f3e | 158 | ndef_aux = *(NDEF_SUPPORT **)parg; |
8931b30d | 159 | |
0f113f3e MC |
160 | derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it); |
161 | p = OPENSSL_malloc(derlen); | |
90945fa3 | 162 | if (p == NULL) |
918bb865 MC |
163 | return 0; |
164 | ||
0f113f3e MC |
165 | ndef_aux->derbuf = p; |
166 | *pbuf = p; | |
167 | derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it); | |
8931b30d | 168 | |
0f113f3e MC |
169 | if (!*ndef_aux->boundary) |
170 | return 0; | |
8931b30d | 171 | |
0f113f3e | 172 | *plen = *ndef_aux->boundary - *pbuf; |
8931b30d | 173 | |
0f113f3e MC |
174 | return 1; |
175 | } | |
8931b30d | 176 | |
0f113f3e MC |
177 | static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, |
178 | void *parg) | |
179 | { | |
180 | NDEF_SUPPORT *ndef_aux; | |
8931b30d | 181 | |
0f113f3e MC |
182 | if (!parg) |
183 | return 0; | |
8931b30d | 184 | |
0f113f3e | 185 | ndef_aux = *(NDEF_SUPPORT **)parg; |
8931b30d | 186 | |
b548a1f1 | 187 | OPENSSL_free(ndef_aux->derbuf); |
8931b30d | 188 | |
0f113f3e MC |
189 | ndef_aux->derbuf = NULL; |
190 | *pbuf = NULL; | |
191 | *plen = 0; | |
192 | return 1; | |
193 | } | |
8931b30d | 194 | |
0f113f3e MC |
195 | static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, |
196 | void *parg) | |
197 | { | |
198 | NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg; | |
199 | if (!ndef_prefix_free(b, pbuf, plen, parg)) | |
200 | return 0; | |
201 | OPENSSL_free(*pndef_aux); | |
202 | *pndef_aux = NULL; | |
203 | return 1; | |
204 | } | |
8931b30d DSH |
205 | |
206 | static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg) | |
0f113f3e MC |
207 | { |
208 | NDEF_SUPPORT *ndef_aux; | |
209 | unsigned char *p; | |
210 | int derlen; | |
211 | const ASN1_AUX *aux; | |
212 | ASN1_STREAM_ARG sarg; | |
213 | ||
214 | if (!parg) | |
215 | return 0; | |
216 | ||
217 | ndef_aux = *(NDEF_SUPPORT **)parg; | |
218 | ||
219 | aux = ndef_aux->it->funcs; | |
220 | ||
221 | /* Finalize structures */ | |
222 | sarg.ndef_bio = ndef_aux->ndef_bio; | |
223 | sarg.out = ndef_aux->out; | |
224 | sarg.boundary = ndef_aux->boundary; | |
225 | if (aux->asn1_cb(ASN1_OP_STREAM_POST, | |
226 | &ndef_aux->val, ndef_aux->it, &sarg) <= 0) | |
227 | return 0; | |
228 | ||
229 | derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it); | |
230 | p = OPENSSL_malloc(derlen); | |
90945fa3 | 231 | if (p == NULL) |
918bb865 MC |
232 | return 0; |
233 | ||
0f113f3e MC |
234 | ndef_aux->derbuf = p; |
235 | *pbuf = p; | |
236 | derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it); | |
237 | ||
238 | if (!*ndef_aux->boundary) | |
239 | return 0; | |
240 | *pbuf = *ndef_aux->boundary; | |
241 | *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf); | |
242 | ||
243 | return 1; | |
244 | } |