PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "internal/cryptlib.h"
|
|
|
|
#include "internal/der.h"
|
|
|
|
#include "crypto/bn.h"
|
|
|
|
|
|
|
|
static int int_start_context(WPACKET *pkt, int tag)
|
|
|
|
{
|
|
|
|
if (tag < 0)
|
|
|
|
return 1;
|
|
|
|
if (!ossl_assert(tag <= 30))
|
|
|
|
return 0;
|
|
|
|
return WPACKET_start_sub_packet(pkt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int int_end_context(WPACKET *pkt, int tag)
|
|
|
|
{
|
2020-05-02 19:33:24 +08:00
|
|
|
/*
|
|
|
|
* If someone set the flag WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH on this
|
|
|
|
* sub-packet and this sub-packet has nothing written to it, the DER length
|
|
|
|
* will not be written, and the total written size will be unchanged before
|
|
|
|
* and after WPACKET_close(). We use size1 and size2 to determine if
|
|
|
|
* anything was written, and only write our tag if it has.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
size_t size1, size2;
|
|
|
|
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
if (tag < 0)
|
|
|
|
return 1;
|
|
|
|
if (!ossl_assert(tag <= 30))
|
|
|
|
return 0;
|
2020-07-28 00:39:51 +08:00
|
|
|
|
|
|
|
/* Context specific are normally (?) constructed */
|
|
|
|
tag |= DER_F_CONSTRUCTED | DER_C_CONTEXT;
|
|
|
|
|
2020-05-02 19:33:24 +08:00
|
|
|
return WPACKET_get_total_written(pkt, &size1)
|
|
|
|
&& WPACKET_close(pkt)
|
|
|
|
&& WPACKET_get_total_written(pkt, &size2)
|
2020-07-28 00:39:51 +08:00
|
|
|
&& (size1 == size2 || WPACKET_put_bytes_u8(pkt, tag));
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
}
|
|
|
|
|
2020-09-30 10:15:12 +08:00
|
|
|
int ossl_DER_w_precompiled(WPACKET *pkt, int tag,
|
|
|
|
const unsigned char *precompiled,
|
|
|
|
size_t precompiled_n)
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
{
|
|
|
|
return int_start_context(pkt, tag)
|
|
|
|
&& WPACKET_memcpy(pkt, precompiled, precompiled_n)
|
|
|
|
&& int_end_context(pkt, tag);
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:15:12 +08:00
|
|
|
int ossl_DER_w_boolean(WPACKET *pkt, int tag, int b)
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
{
|
|
|
|
return int_start_context(pkt, tag)
|
|
|
|
&& WPACKET_start_sub_packet(pkt)
|
|
|
|
&& (!b || WPACKET_put_bytes_u8(pkt, 0xFF))
|
|
|
|
&& !WPACKET_close(pkt)
|
|
|
|
&& !WPACKET_put_bytes_u8(pkt, DER_P_BOOLEAN)
|
|
|
|
&& int_end_context(pkt, tag);
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:15:12 +08:00
|
|
|
int ossl_DER_w_octet_string(WPACKET *pkt, int tag,
|
|
|
|
const unsigned char *data, size_t data_n)
|
2020-08-04 10:18:51 +08:00
|
|
|
{
|
|
|
|
return int_start_context(pkt, tag)
|
|
|
|
&& WPACKET_start_sub_packet(pkt)
|
|
|
|
&& WPACKET_memcpy(pkt, data, data_n)
|
|
|
|
&& WPACKET_close(pkt)
|
|
|
|
&& WPACKET_put_bytes_u8(pkt, DER_P_OCTET_STRING)
|
|
|
|
&& int_end_context(pkt, tag);
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:15:12 +08:00
|
|
|
int ossl_DER_w_octet_string_uint32(WPACKET *pkt, int tag, uint32_t value)
|
2020-08-04 10:18:51 +08:00
|
|
|
{
|
|
|
|
unsigned char tmp[4] = { 0, 0, 0, 0 };
|
|
|
|
unsigned char *pbuf = tmp + (sizeof(tmp) - 1);
|
|
|
|
|
|
|
|
while (value > 0) {
|
|
|
|
*pbuf-- = (value & 0xFF);
|
|
|
|
value >>= 8;
|
|
|
|
}
|
2020-09-30 10:15:12 +08:00
|
|
|
return ossl_DER_w_octet_string(pkt, tag, tmp, sizeof(tmp));
|
2020-08-04 10:18:51 +08:00
|
|
|
}
|
|
|
|
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
static int int_der_w_integer(WPACKET *pkt, int tag,
|
|
|
|
int (*put_bytes)(WPACKET *pkt, const void *v,
|
|
|
|
unsigned int *top_byte),
|
|
|
|
const void *v)
|
|
|
|
{
|
|
|
|
unsigned int top_byte = 0;
|
|
|
|
|
|
|
|
return int_start_context(pkt, tag)
|
|
|
|
&& WPACKET_start_sub_packet(pkt)
|
|
|
|
&& put_bytes(pkt, v, &top_byte)
|
|
|
|
&& ((top_byte & 0x80) == 0 || WPACKET_put_bytes_u8(pkt, 0))
|
|
|
|
&& WPACKET_close(pkt)
|
|
|
|
&& WPACKET_put_bytes_u8(pkt, DER_P_INTEGER)
|
|
|
|
&& int_end_context(pkt, tag);
|
|
|
|
}
|
|
|
|
|
2022-06-21 00:40:30 +08:00
|
|
|
static int int_put_bytes_uint32(WPACKET *pkt, const void *v,
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
unsigned int *top_byte)
|
|
|
|
{
|
2022-06-21 00:40:30 +08:00
|
|
|
const uint32_t *value = v;
|
|
|
|
uint32_t tmp = *value;
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
while (tmp != 0) {
|
|
|
|
n++;
|
|
|
|
*top_byte = (tmp & 0xFF);
|
|
|
|
tmp >>= 8;
|
|
|
|
}
|
|
|
|
if (n == 0)
|
|
|
|
n = 1;
|
|
|
|
|
|
|
|
return WPACKET_put_bytes__(pkt, *value, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For integers, we only support unsigned values for now */
|
2022-06-21 00:40:30 +08:00
|
|
|
int ossl_DER_w_uint32(WPACKET *pkt, int tag, uint32_t v)
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
{
|
2022-06-21 00:40:30 +08:00
|
|
|
return int_der_w_integer(pkt, tag, int_put_bytes_uint32, &v);
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int int_put_bytes_bn(WPACKET *pkt, const void *v,
|
|
|
|
unsigned int *top_byte)
|
|
|
|
{
|
|
|
|
unsigned char *p = NULL;
|
|
|
|
size_t n = BN_num_bytes(v);
|
|
|
|
|
|
|
|
/* The BIGNUM limbs are in LE order */
|
|
|
|
*top_byte =
|
|
|
|
((bn_get_words(v) [(n - 1) / BN_BYTES]) >> (8 * ((n - 1) % BN_BYTES)))
|
|
|
|
& 0xFF;
|
|
|
|
|
|
|
|
if (!WPACKET_allocate_bytes(pkt, n, &p))
|
|
|
|
return 0;
|
|
|
|
if (p != NULL)
|
|
|
|
BN_bn2bin(v, p);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:15:12 +08:00
|
|
|
int ossl_DER_w_bn(WPACKET *pkt, int tag, const BIGNUM *v)
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
{
|
|
|
|
if (v == NULL || BN_is_negative(v))
|
|
|
|
return 0;
|
|
|
|
if (BN_is_zero(v))
|
2022-06-21 00:40:30 +08:00
|
|
|
return ossl_DER_w_uint32(pkt, tag, 0);
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
|
|
|
|
return int_der_w_integer(pkt, tag, int_put_bytes_bn, v);
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:15:12 +08:00
|
|
|
int ossl_DER_w_null(WPACKET *pkt, int tag)
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
{
|
|
|
|
return int_start_context(pkt, tag)
|
|
|
|
&& WPACKET_start_sub_packet(pkt)
|
|
|
|
&& WPACKET_close(pkt)
|
|
|
|
&& WPACKET_put_bytes_u8(pkt, DER_P_NULL)
|
|
|
|
&& int_end_context(pkt, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Constructed things need a start and an end */
|
2020-09-30 10:15:12 +08:00
|
|
|
int ossl_DER_w_begin_sequence(WPACKET *pkt, int tag)
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
{
|
|
|
|
return int_start_context(pkt, tag)
|
|
|
|
&& WPACKET_start_sub_packet(pkt);
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:15:12 +08:00
|
|
|
int ossl_DER_w_end_sequence(WPACKET *pkt, int tag)
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
{
|
2020-05-02 19:33:24 +08:00
|
|
|
/*
|
|
|
|
* If someone set the flag WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH on this
|
|
|
|
* sub-packet and this sub-packet has nothing written to it, the DER length
|
|
|
|
* will not be written, and the total written size will be unchanged before
|
|
|
|
* and after WPACKET_close(). We use size1 and size2 to determine if
|
|
|
|
* anything was written, and only write our tag if it has.
|
|
|
|
*
|
|
|
|
* Because we know that int_end_context() needs to do the same check,
|
|
|
|
* we reproduce this flag if the written length was unchanged, or we will
|
|
|
|
* have an erroneous context tag.
|
|
|
|
*/
|
|
|
|
size_t size1, size2;
|
|
|
|
|
|
|
|
return WPACKET_get_total_written(pkt, &size1)
|
|
|
|
&& WPACKET_close(pkt)
|
|
|
|
&& WPACKET_get_total_written(pkt, &size2)
|
|
|
|
&& (size1 == size2
|
|
|
|
? WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
|
|
|
|
: WPACKET_put_bytes_u8(pkt, DER_F_CONSTRUCTED | DER_P_SEQUENCE))
|
PROV: Add the beginning of a DER writing library
This library is meant to be small and quick. It's based on WPACKET,
which was extended to support DER writing. The way it's used is a
bit unusual, as it's used to write the structures backward into a
given buffer. A typical quick call looks like this:
/*
* Fill in this structure:
*
* something ::= SEQUENCE {
* id OBJECT IDENTIFIER,
* x [0] INTEGER OPTIONAL,
* y [1] BOOLEAN OPTIONAL,
* n INTEGER
* }
*/
unsigned char buf[nnnn], *p = NULL;
size_t encoded_len = 0;
WPACKET pkt;
int ok;
ok = WPACKET_init_der(&pkt, buf, sizeof(buf)
&& DER_w_start_sequence(&pkt, -1)
&& DER_w_bn(&pkt, -1, bn)
&& DER_w_boolean(&pkt, 1, bool)
&& DER_w_precompiled(&pkt, -1, OID, sizeof(OID))
&& DER_w_end_sequence(&pkt, -1)
&& WPACKET_finish(&pkt)
&& WPACKET_get_total_written(&pkt, &encoded_len)
&& (p = WPACKET_get_curr(&pkt)) != NULL;
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11450)
2020-03-31 22:54:43 +08:00
|
|
|
&& int_end_context(pkt, tag);
|
|
|
|
}
|