2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2018-04-17 21:18:40 +08:00
|
|
|
* Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2008-03-13 05:14:28 +08:00
|
|
|
*
|
2018-12-06 20:17:34 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08:00
|
|
|
* 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
|
2008-03-13 05:14:28 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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 */
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* 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
|
2016-02-27 03:30:15 +08:00
|
|
|
* appropriate format on the fly. The data is streamed out and does *not*
|
2015-01-22 11:40:55 +08:00
|
|
|
* 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*...
|
2008-03-13 05:14:28 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* BIO support data stored in the ASN1 BIO ex_arg */
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
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;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
|
2015-01-22 11:40:55 +08:00
|
|
|
static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
|
|
|
|
void *parg);
|
2008-03-13 05:14:28 +08:00
|
|
|
static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
|
2015-01-22 11:40:55 +08:00
|
|
|
static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
|
|
|
|
void *parg);
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2019-01-16 04:51:25 +08:00
|
|
|
/* unfortunately cannot constify this due to CMS_stream() and PKCS7_stream() */
|
2008-03-13 05:14:28 +08:00
|
|
|
BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
NDEF_SUPPORT *ndef_aux = NULL;
|
|
|
|
BIO *asn_bio = NULL;
|
|
|
|
const ASN1_AUX *aux = it->funcs;
|
|
|
|
ASN1_STREAM_ARG sarg;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!aux || !aux->asn1_cb) {
|
|
|
|
ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-07 04:40:58 +08:00
|
|
|
ndef_aux = OPENSSL_zalloc(sizeof(*ndef_aux));
|
2015-01-22 11:40:55 +08:00
|
|
|
asn_bio = BIO_new(BIO_f_asn1());
|
2016-03-07 04:40:58 +08:00
|
|
|
if (ndef_aux == NULL || asn_bio == NULL)
|
|
|
|
goto err;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/* ASN1 bio needs to be next to output BIO */
|
|
|
|
out = BIO_push(asn_bio, out);
|
2016-03-07 04:40:58 +08:00
|
|
|
if (out == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free);
|
|
|
|
BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free);
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2016-03-11 04:34:48 +08:00
|
|
|
* Now let callback prepends any digest, cipher etc BIOs ASN1 structure
|
2015-01-22 11:40:55 +08:00
|
|
|
* needs.
|
|
|
|
*/
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
sarg.out = out;
|
|
|
|
sarg.ndef_bio = NULL;
|
|
|
|
sarg.boundary = NULL;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0)
|
|
|
|
goto err;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ndef_aux->val = val;
|
|
|
|
ndef_aux->it = it;
|
|
|
|
ndef_aux->ndef_bio = sarg.ndef_bio;
|
|
|
|
ndef_aux->boundary = sarg.boundary;
|
|
|
|
ndef_aux->out = out;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return sarg.ndef_bio;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
2015-03-25 23:31:18 +08:00
|
|
|
BIO_free(asn_bio);
|
2015-05-01 22:02:07 +08:00
|
|
|
OPENSSL_free(ndef_aux);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
NDEF_SUPPORT *ndef_aux;
|
|
|
|
unsigned char *p;
|
|
|
|
int derlen;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (parg == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ndef_aux = *(NDEF_SUPPORT **)parg;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
|
2018-04-03 23:31:16 +08:00
|
|
|
if ((p = OPENSSL_malloc(derlen)) == NULL) {
|
|
|
|
ASN1err(ASN1_F_NDEF_PREFIX, ERR_R_MALLOC_FAILURE);
|
2015-03-05 01:49:51 +08:00
|
|
|
return 0;
|
2018-04-03 23:31:16 +08:00
|
|
|
}
|
2015-03-05 01:49:51 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ndef_aux->derbuf = p;
|
|
|
|
*pbuf = p;
|
|
|
|
derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (*ndef_aux->boundary == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
*plen = *ndef_aux->boundary - *pbuf;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
|
|
|
|
void *parg)
|
|
|
|
{
|
|
|
|
NDEF_SUPPORT *ndef_aux;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (parg == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ndef_aux = *(NDEF_SUPPORT **)parg;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-05-01 22:02:07 +08:00
|
|
|
OPENSSL_free(ndef_aux->derbuf);
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ndef_aux->derbuf = NULL;
|
|
|
|
*pbuf = NULL;
|
|
|
|
*plen = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
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;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
NDEF_SUPPORT *ndef_aux;
|
|
|
|
unsigned char *p;
|
|
|
|
int derlen;
|
|
|
|
const ASN1_AUX *aux;
|
|
|
|
ASN1_STREAM_ARG sarg;
|
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (parg == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
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);
|
2019-07-14 15:55:15 +08:00
|
|
|
if (derlen < 0)
|
|
|
|
return 0;
|
2018-04-03 23:31:16 +08:00
|
|
|
if ((p = OPENSSL_malloc(derlen)) == NULL) {
|
|
|
|
ASN1err(ASN1_F_NDEF_SUFFIX, ERR_R_MALLOC_FAILURE);
|
2015-03-05 01:49:51 +08:00
|
|
|
return 0;
|
2018-04-03 23:31:16 +08:00
|
|
|
}
|
2015-03-05 01:49:51 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
ndef_aux->derbuf = p;
|
|
|
|
*pbuf = p;
|
|
|
|
derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
|
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (*ndef_aux->boundary == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
*pbuf = *ndef_aux->boundary;
|
|
|
|
*plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|