Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/asn1/bio_ndef.c
Line
Count
Source
1
/*
2
 * Copyright 2008-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
10
#include <openssl/asn1.h>
11
#include <openssl/asn1t.h>
12
#include <openssl/bio.h>
13
#include <openssl/err.h>
14
#include "crypto/asn1.h"
15
16
#include <stdio.h>
17
18
/* Experimental NDEF ASN1 BIO support routines */
19
20
/*
21
 * The usage is quite simple, initialize an ASN1 structure, get a BIO from it
22
 * then any data written through the BIO will end up translated to
23
 * appropriate format on the fly. The data is streamed out and does *not*
24
 * need to be all held in memory at once. When the BIO is flushed the output
25
 * is finalized and any signatures etc written out. The BIO is a 'proper'
26
 * BIO and can handle non blocking I/O correctly. The usage is simple. The
27
 * implementation is *not*...
28
 */
29
30
/* BIO support data stored in the ASN1 BIO ex_arg */
31
32
typedef struct ndef_aux_st {
33
    /* ASN1 structure this BIO refers to */
34
    ASN1_VALUE *val;
35
    const ASN1_ITEM *it;
36
    /* Top of the BIO chain */
37
    BIO *ndef_bio;
38
    /* Output BIO */
39
    BIO *out;
40
    /*
41
     * Content octet string in which the encoder records where the content
42
     * belongs.  Borrowed from the caller.
43
     */
44
    ASN1_STRING *content;
45
    /* DER buffer start */
46
    unsigned char *derbuf;
47
} NDEF_SUPPORT;
48
49
static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
50
static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
51
    void *parg);
52
static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
53
static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
54
    void *parg);
55
56
/*
57
 * On success, the returned BIO owns the input BIO as part of its BIO chain.
58
 * On failure, NULL is returned and the input BIO is owned by the caller.
59
 */
60
BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
61
0
{
62
0
    NDEF_SUPPORT *ndef_aux = NULL;
63
0
    BIO *asn_bio = NULL;
64
0
    const ASN1_AUX *aux = it->funcs;
65
0
    ASN1_STREAM_ARG sarg;
66
0
    BIO *pop_bio = NULL;
67
0
    ASN1_STRING *content = NULL;
68
69
0
    if (!aux || !aux->asn1_cb) {
70
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_STREAMING_NOT_SUPPORTED);
71
0
        return NULL;
72
0
    }
73
0
    ndef_aux = OPENSSL_zalloc(sizeof(*ndef_aux));
74
0
    asn_bio = BIO_new(BIO_f_asn1());
75
0
    if (ndef_aux == NULL || asn_bio == NULL)
76
0
        goto err;
77
78
    /* ASN1 bio needs to be next to output BIO */
79
0
    out = BIO_push(asn_bio, out);
80
0
    if (out == NULL)
81
0
        goto err;
82
0
    pop_bio = asn_bio;
83
84
0
    if (BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free) <= 0
85
0
        || BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free) <= 0
86
0
        || BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux) <= 0)
87
0
        goto err;
88
89
    /*
90
     * Now let the callback prepend any digest, cipher, etc., that the BIO's
91
     * ASN1 structure needs.
92
     */
93
94
0
    sarg.out = out;
95
0
    sarg.ndef_bio = NULL;
96
0
    sarg.boundary = NULL;
97
98
    /*
99
     * The asn1_cb(), must not have mutated asn_bio on error, leaving it in the
100
     * middle of some partially built, but not returned BIO chain.
101
     */
102
0
    if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0) {
103
        /*
104
         * ndef_aux is now owned by asn_bio so we must not free it in the err
105
         * clean up block
106
         */
107
0
        ndef_aux = NULL;
108
0
        goto err;
109
0
    }
110
111
0
    if (aux->asn1_cb(ASN1_OP_GET0_STREAM_CONTENT, &val, it, &content) > 0
112
0
        && content != NULL)
113
0
        content->flags |= ASN1_STRING_FLAG_NDEF;
114
0
    else
115
0
        content = NULL; /* Don't leave a junk borrowed pointer to play with */
116
117
    /*
118
     * We must not fail now because the callback has prepended additional
119
     * BIOs to the chain
120
     */
121
122
0
    ndef_aux->val = val;
123
0
    ndef_aux->it = it;
124
0
    ndef_aux->ndef_bio = sarg.ndef_bio;
125
0
    ndef_aux->content = content;
126
0
    ndef_aux->out = out;
127
128
0
    return sarg.ndef_bio;
129
130
0
err:
131
    /* BIO_pop() is NULL safe */
132
0
    (void)BIO_pop(pop_bio);
133
0
    BIO_free(asn_bio);
134
0
    OPENSSL_free(ndef_aux);
135
0
    return NULL;
136
0
}
137
138
static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
139
0
{
140
0
    NDEF_SUPPORT *ndef_aux;
141
0
    unsigned char *p;
142
0
    int derlen;
143
144
0
    if (parg == NULL)
145
0
        return 0;
146
147
0
    ndef_aux = *(NDEF_SUPPORT **)parg;
148
149
0
    derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
150
0
    if (derlen < 0)
151
0
        return 0;
152
0
    if ((p = OPENSSL_malloc(derlen)) == NULL)
153
0
        return 0;
154
155
0
    ndef_aux->derbuf = p;
156
0
    *pbuf = p;
157
0
    ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
158
159
0
    if (ndef_aux->content == NULL || ndef_aux->content->data == NULL)
160
0
        return 0;
161
162
0
    *plen = (int)(ndef_aux->content->data - *pbuf);
163
164
0
    return 1;
165
0
}
166
167
static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
168
    void *parg)
169
0
{
170
0
    NDEF_SUPPORT *ndef_aux;
171
172
0
    if (parg == NULL)
173
0
        return 0;
174
175
0
    ndef_aux = *(NDEF_SUPPORT **)parg;
176
177
0
    if (ndef_aux == NULL)
178
0
        return 0;
179
180
0
    OPENSSL_free(ndef_aux->derbuf);
181
182
0
    ndef_aux->derbuf = NULL;
183
0
    *pbuf = NULL;
184
0
    *plen = 0;
185
0
    return 1;
186
0
}
187
188
static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
189
    void *parg)
190
0
{
191
0
    NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
192
0
    if (!ndef_prefix_free(b, pbuf, plen, parg))
193
0
        return 0;
194
0
    OPENSSL_free(*pndef_aux);
195
0
    *pndef_aux = NULL;
196
0
    return 1;
197
0
}
198
199
static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
200
0
{
201
0
    NDEF_SUPPORT *ndef_aux;
202
0
    unsigned char *p;
203
0
    int derlen;
204
0
    const ASN1_AUX *aux;
205
0
    ASN1_STREAM_ARG sarg;
206
207
0
    if (parg == NULL)
208
0
        return 0;
209
210
0
    ndef_aux = *(NDEF_SUPPORT **)parg;
211
212
0
    aux = ndef_aux->it->funcs;
213
214
    /* Finalize structures */
215
0
    sarg.ndef_bio = ndef_aux->ndef_bio;
216
0
    sarg.out = ndef_aux->out;
217
0
    sarg.boundary = NULL;
218
0
    if (aux->asn1_cb(ASN1_OP_STREAM_POST,
219
0
            &ndef_aux->val, ndef_aux->it, &sarg)
220
0
        <= 0)
221
0
        return 0;
222
223
0
    derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
224
0
    if (derlen < 0)
225
0
        return 0;
226
0
    if ((p = OPENSSL_malloc(derlen)) == NULL)
227
0
        return 0;
228
229
0
    ndef_aux->derbuf = p;
230
0
    *pbuf = p;
231
0
    derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
232
233
0
    if (ndef_aux->content == NULL || ndef_aux->content->data == NULL)
234
0
        return 0;
235
0
    *pbuf = ndef_aux->content->data;
236
0
    *plen = derlen - (int)(ndef_aux->content->data - ndef_aux->derbuf);
237
238
0
    return 1;
239
0
}