bio_ndef.c 5.1 KB
Newer Older
1
/*
R
Rich Salz 已提交
2
 * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
D
Dr. Stephen Henson 已提交
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
D
Dr. Stephen Henson 已提交
8 9 10 11 12 13 14 15 16 17 18
 */

#include <openssl/asn1.h>
#include <openssl/asn1t.h>
#include <openssl/bio.h>
#include <openssl/err.h>

#include <stdio.h>

/* Experimental NDEF ASN1 BIO support routines */

19 20 21
/*
 * The usage is quite simple, initialize an ASN1 structure, get a BIO from it
 * then any data written through the BIO will end up translated to
F
FdaSilvaYY 已提交
22
 * appropriate format on the fly. The data is streamed out and does *not*
23 24 25 26
 * need to be all held in memory at once. When the BIO is flushed the output
 * is finalized and any signatures etc written out. The BIO is a 'proper'
 * BIO and can handle non blocking I/O correctly. The usage is simple. The
 * implementation is *not*...
D
Dr. Stephen Henson 已提交
27 28 29 30
 */

/* BIO support data stored in the ASN1 BIO ex_arg */

31 32 33 34 35 36 37 38 39 40 41 42 43
typedef struct ndef_aux_st {
    /* ASN1 structure this BIO refers to */
    ASN1_VALUE *val;
    const ASN1_ITEM *it;
    /* Top of the BIO chain */
    BIO *ndef_bio;
    /* Output BIO */
    BIO *out;
    /* Boundary where content is inserted */
    unsigned char **boundary;
    /* DER buffer start */
    unsigned char *derbuf;
} NDEF_SUPPORT;
D
Dr. Stephen Henson 已提交
44 45

static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
46 47
static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
                            void *parg);
D
Dr. Stephen Henson 已提交
48
static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
49 50
static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
                            void *parg);
D
Dr. Stephen Henson 已提交
51 52

BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
53 54 55 56 57
{
    NDEF_SUPPORT *ndef_aux = NULL;
    BIO *asn_bio = NULL;
    const ASN1_AUX *aux = it->funcs;
    ASN1_STREAM_ARG sarg;
D
Dr. Stephen Henson 已提交
58

59 60 61 62
    if (!aux || !aux->asn1_cb) {
        ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED);
        return NULL;
    }
F
FdaSilvaYY 已提交
63
    ndef_aux = OPENSSL_zalloc(sizeof(*ndef_aux));
64
    asn_bio = BIO_new(BIO_f_asn1());
F
FdaSilvaYY 已提交
65 66
    if (ndef_aux == NULL || asn_bio == NULL)
        goto err;
D
Dr. Stephen Henson 已提交
67

68 69
    /* ASN1 bio needs to be next to output BIO */
    out = BIO_push(asn_bio, out);
F
FdaSilvaYY 已提交
70
    if (out == NULL)
71
        goto err;
D
Dr. Stephen Henson 已提交
72

73 74
    BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free);
    BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free);
D
Dr. Stephen Henson 已提交
75

76
    /*
F
FdaSilvaYY 已提交
77
     * Now let callback prepends any digest, cipher etc BIOs ASN1 structure
78 79
     * needs.
     */
D
Dr. Stephen Henson 已提交
80

81 82 83
    sarg.out = out;
    sarg.ndef_bio = NULL;
    sarg.boundary = NULL;
D
Dr. Stephen Henson 已提交
84

85 86
    if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0)
        goto err;
D
Dr. Stephen Henson 已提交
87

88 89 90 91 92
    ndef_aux->val = val;
    ndef_aux->it = it;
    ndef_aux->ndef_bio = sarg.ndef_bio;
    ndef_aux->boundary = sarg.boundary;
    ndef_aux->out = out;
D
Dr. Stephen Henson 已提交
93

94
    BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
D
Dr. Stephen Henson 已提交
95

96
    return sarg.ndef_bio;
D
Dr. Stephen Henson 已提交
97

98
 err:
R
Rich Salz 已提交
99
    BIO_free(asn_bio);
R
Rich Salz 已提交
100
    OPENSSL_free(ndef_aux);
101 102
    return NULL;
}
D
Dr. Stephen Henson 已提交
103 104

static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
105 106 107 108
{
    NDEF_SUPPORT *ndef_aux;
    unsigned char *p;
    int derlen;
D
Dr. Stephen Henson 已提交
109

110 111
    if (!parg)
        return 0;
D
Dr. Stephen Henson 已提交
112

113
    ndef_aux = *(NDEF_SUPPORT **)parg;
D
Dr. Stephen Henson 已提交
114

115 116
    derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
    p = OPENSSL_malloc(derlen);
117
    if (p == NULL)
M
Matt Caswell 已提交
118 119
        return 0;

120 121 122
    ndef_aux->derbuf = p;
    *pbuf = p;
    derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
D
Dr. Stephen Henson 已提交
123

124 125
    if (!*ndef_aux->boundary)
        return 0;
D
Dr. Stephen Henson 已提交
126

127
    *plen = *ndef_aux->boundary - *pbuf;
D
Dr. Stephen Henson 已提交
128

129 130
    return 1;
}
D
Dr. Stephen Henson 已提交
131

132 133 134 135
static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
                            void *parg)
{
    NDEF_SUPPORT *ndef_aux;
D
Dr. Stephen Henson 已提交
136

137 138
    if (!parg)
        return 0;
D
Dr. Stephen Henson 已提交
139

140
    ndef_aux = *(NDEF_SUPPORT **)parg;
D
Dr. Stephen Henson 已提交
141

R
Rich Salz 已提交
142
    OPENSSL_free(ndef_aux->derbuf);
D
Dr. Stephen Henson 已提交
143

144 145 146 147 148
    ndef_aux->derbuf = NULL;
    *pbuf = NULL;
    *plen = 0;
    return 1;
}
D
Dr. Stephen Henson 已提交
149

150 151 152 153 154 155 156 157 158 159
static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
                            void *parg)
{
    NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
    if (!ndef_prefix_free(b, pbuf, plen, parg))
        return 0;
    OPENSSL_free(*pndef_aux);
    *pndef_aux = NULL;
    return 1;
}
D
Dr. Stephen Henson 已提交
160 161

static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
{
    NDEF_SUPPORT *ndef_aux;
    unsigned char *p;
    int derlen;
    const ASN1_AUX *aux;
    ASN1_STREAM_ARG sarg;

    if (!parg)
        return 0;

    ndef_aux = *(NDEF_SUPPORT **)parg;

    aux = ndef_aux->it->funcs;

    /* Finalize structures */
    sarg.ndef_bio = ndef_aux->ndef_bio;
    sarg.out = ndef_aux->out;
    sarg.boundary = ndef_aux->boundary;
    if (aux->asn1_cb(ASN1_OP_STREAM_POST,
                     &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
        return 0;

    derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
    p = OPENSSL_malloc(derlen);
186
    if (p == NULL)
M
Matt Caswell 已提交
187 188
        return 0;

189 190 191 192 193 194 195 196 197 198 199
    ndef_aux->derbuf = p;
    *pbuf = p;
    derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);

    if (!*ndef_aux->boundary)
        return 0;
    *pbuf = *ndef_aux->boundary;
    *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);

    return 1;
}