2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* ASN.1 buffer writing functionality
|
|
|
|
*
|
2020-09-05 12:53:20 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2021-12-21 12:54:05 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-02-08 19:04:43 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#include "common.h"
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ASN1_WRITE_C)
|
|
|
|
|
|
|
|
#include "mbedtls/asn1write.h"
|
2021-12-21 12:54:05 +01:00
|
|
|
#include "mbedtls/error.h"
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (len < 0x80) {
|
|
|
|
if (*p - start < 1) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
*--(*p) = (unsigned char) len;
|
2023-04-18 10:38:24 +02:00
|
|
|
return 1;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (len <= 0xFF) {
|
|
|
|
if (*p - start < 2) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
*--(*p) = (unsigned char) len;
|
|
|
|
*--(*p) = 0x81;
|
2023-04-18 10:38:24 +02:00
|
|
|
return 2;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (len <= 0xFFFF) {
|
|
|
|
if (*p - start < 3) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
*--(*p) = MBEDTLS_BYTE_0(len);
|
|
|
|
*--(*p) = MBEDTLS_BYTE_1(len);
|
2018-02-08 19:04:43 +01:00
|
|
|
*--(*p) = 0x82;
|
2023-04-18 10:38:24 +02:00
|
|
|
return 3;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (len <= 0xFFFFFF) {
|
|
|
|
if (*p - start < 4) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
*--(*p) = MBEDTLS_BYTE_0(len);
|
|
|
|
*--(*p) = MBEDTLS_BYTE_1(len);
|
|
|
|
*--(*p) = MBEDTLS_BYTE_2(len);
|
2018-02-08 19:04:43 +01:00
|
|
|
*--(*p) = 0x83;
|
2023-04-18 10:38:24 +02:00
|
|
|
return 4;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2022-12-21 12:05:54 +01:00
|
|
|
int len_is_valid = 1;
|
2018-07-28 11:16:41 +02:00
|
|
|
#if SIZE_MAX > 0xFFFFFFFF
|
2023-04-18 10:38:24 +02:00
|
|
|
len_is_valid = (len <= 0xFFFFFFFF);
|
2018-07-28 11:16:41 +02:00
|
|
|
#endif
|
2023-04-18 10:38:24 +02:00
|
|
|
if (len_is_valid) {
|
|
|
|
if (*p - start < 5) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*--(*p) = MBEDTLS_BYTE_0(len);
|
|
|
|
*--(*p) = MBEDTLS_BYTE_1(len);
|
|
|
|
*--(*p) = MBEDTLS_BYTE_2(len);
|
|
|
|
*--(*p) = MBEDTLS_BYTE_3(len);
|
2018-02-08 19:04:43 +01:00
|
|
|
*--(*p) = 0x84;
|
2023-04-18 10:38:24 +02:00
|
|
|
return 5;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (*p - start < 1) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
*--(*p) = tag;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 1;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_raw_buffer(unsigned char **p, unsigned char *start,
|
|
|
|
const unsigned char *buf, size_t size)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (*p < start || (size_t) (*p - start) < size) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
len = size;
|
|
|
|
(*p) -= len;
|
2023-04-18 10:38:24 +02:00
|
|
|
memcpy(*p, buf, len);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) len;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_mpi(unsigned char **p, unsigned char *start, const mbedtls_mpi *X)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
// Write the MPI
|
|
|
|
//
|
2023-04-18 10:38:24 +02:00
|
|
|
len = mbedtls_mpi_size(X);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2022-07-18 14:48:00 +02:00
|
|
|
/* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
|
|
|
|
* as 0 digits. We need to end up with 020100, not with 0200. */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (len == 0) {
|
2022-07-18 14:48:00 +02:00
|
|
|
len = 1;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2022-07-18 14:48:00 +02:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (*p < start || (size_t) (*p - start) < len) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
(*p) -= len;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
// DER format assumes 2s complement for numbers, so the leftmost bit
|
|
|
|
// should be 0 for positive numbers and 1 for negative numbers.
|
|
|
|
//
|
2023-04-18 10:38:24 +02:00
|
|
|
if (X->s == 1 && **p & 0x80) {
|
|
|
|
if (*p - start < 1) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
*--(*p) = 0x00;
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_INTEGER));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
ret = (int) len;
|
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_BIGNUM_C */
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_null(unsigned char **p, unsigned char *start)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
// Write NULL
|
|
|
|
//
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, 0));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_NULL));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) len;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_oid(unsigned char **p, unsigned char *start,
|
|
|
|
const char *oid, size_t oid_len)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
|
|
|
|
(const unsigned char *) oid, oid_len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) len;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, unsigned char *start,
|
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
size_t par_len)
|
2023-10-21 14:06:23 +02:00
|
|
|
{
|
|
|
|
return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, unsigned char *start,
|
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
size_t par_len, int has_par)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-10-21 14:06:23 +02:00
|
|
|
if (has_par) {
|
|
|
|
if (par_len == 0) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
|
|
|
|
} else {
|
|
|
|
len += par_len;
|
|
|
|
}
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) len;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_bool(unsigned char **p, unsigned char *start, int boolean)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (*p - start < 1) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
*--(*p) = (boolean) ? 255 : 0;
|
|
|
|
len++;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BOOLEAN));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) len;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
static int asn1_write_tagged_int(unsigned char **p, unsigned char *start, int val, int tag)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
do {
|
|
|
|
if (*p - start < 1) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
len += 1;
|
|
|
|
*--(*p) = val & 0xff;
|
|
|
|
val >>= 8;
|
2023-04-18 10:38:24 +02:00
|
|
|
} while (val > 0);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (**p & 0x80) {
|
|
|
|
if (*p - start < 1) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
*--(*p) = 0x00;
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) len;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_enum(unsigned char **p, unsigned char *start, int val)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_tagged_string(unsigned char **p, unsigned char *start, int tag,
|
|
|
|
const char *text, size_t text_len)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
|
|
|
|
(const unsigned char *) text,
|
|
|
|
text_len));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) len;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_utf8_string(unsigned char **p, unsigned char *start,
|
|
|
|
const char *text, size_t text_len)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len);
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_printable_string(unsigned char **p, unsigned char *start,
|
|
|
|
const char *text, size_t text_len)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
|
|
|
|
text_len);
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_ia5_string(unsigned char **p, unsigned char *start,
|
|
|
|
const char *text, size_t text_len)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_named_bitstring(unsigned char **p,
|
|
|
|
unsigned char *start,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t bits)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
|
|
|
size_t unused_bits, byte_len;
|
|
|
|
const unsigned char *cur_byte;
|
|
|
|
unsigned char cur_byte_shifted;
|
|
|
|
unsigned char bit;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
byte_len = (bits + 7) / 8;
|
|
|
|
unused_bits = (byte_len * 8) - bits;
|
2021-12-21 12:54:05 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Named bitstrings require that trailing 0s are excluded in the encoding
|
|
|
|
* of the bitstring. Trailing 0s are considered part of the 'unused' bits
|
|
|
|
* when encoding this value in the first content octet
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (bits != 0) {
|
2021-12-21 12:54:05 +01:00
|
|
|
cur_byte = buf + byte_len - 1;
|
|
|
|
cur_byte_shifted = *cur_byte >> unused_bits;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (;;) {
|
2021-12-21 12:54:05 +01:00
|
|
|
bit = cur_byte_shifted & 0x1;
|
|
|
|
cur_byte_shifted >>= 1;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (bit != 0) {
|
2021-12-21 12:54:05 +01:00
|
|
|
break;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
|
|
|
|
bits--;
|
2023-04-18 10:38:24 +02:00
|
|
|
if (bits == 0) {
|
2021-12-21 12:54:05 +01:00
|
|
|
break;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (bits % 8 == 0) {
|
2021-12-21 12:54:05 +01:00
|
|
|
cur_byte_shifted = *--cur_byte;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_asn1_write_bitstring(p, start, buf, bits);
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_bitstring(unsigned char **p, unsigned char *start,
|
|
|
|
const unsigned char *buf, size_t bits)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-07-10 16:15:01 +02:00
|
|
|
size_t len = 0;
|
|
|
|
size_t unused_bits, byte_len;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
byte_len = (bits + 7) / 8;
|
|
|
|
unused_bits = (byte_len * 8) - bits;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (*p < start || (size_t) (*p - start) < byte_len + 1) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-07-10 16:15:01 +02:00
|
|
|
len = byte_len + 1;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-07-10 16:15:01 +02:00
|
|
|
/* Write the bitstring. Ensure the unused bits are zeroed */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (byte_len > 0) {
|
2019-07-10 16:15:01 +02:00
|
|
|
byte_len--;
|
2023-04-18 10:38:24 +02:00
|
|
|
*--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
|
|
|
|
(*p) -= byte_len;
|
|
|
|
memcpy(*p, buf, byte_len);
|
2019-07-10 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write unused bits */
|
2023-04-18 10:38:24 +02:00
|
|
|
*--(*p) = (unsigned char) unused_bits;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) len;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_asn1_write_octet_string(unsigned char **p, unsigned char *start,
|
|
|
|
const unsigned char *buf, size_t size)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) len;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
/* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
|
|
|
|
* which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
|
|
|
|
static mbedtls_asn1_named_data *asn1_find_named_data(
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_asn1_named_data *list,
|
|
|
|
const char *oid, size_t len)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
while (list != NULL) {
|
|
|
|
if (list->oid.len == len &&
|
|
|
|
memcmp(list->oid.p, oid, len) == 0) {
|
2019-02-16 17:19:46 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return list;
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_asn1_named_data **head,
|
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
const unsigned char *val,
|
|
|
|
size_t val_len)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
mbedtls_asn1_named_data *cur;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
|
2018-02-08 19:04:43 +01:00
|
|
|
// Add new entry if not present yet based on OID
|
|
|
|
//
|
2023-04-18 10:38:24 +02:00
|
|
|
cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
|
|
|
|
sizeof(mbedtls_asn1_named_data));
|
|
|
|
if (cur == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cur->oid.len = oid_len;
|
2023-04-18 10:38:24 +02:00
|
|
|
cur->oid.p = mbedtls_calloc(1, oid_len);
|
|
|
|
if (cur->oid.p == NULL) {
|
|
|
|
mbedtls_free(cur);
|
|
|
|
return NULL;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
memcpy(cur->oid.p, oid, oid_len);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cur->val.len = val_len;
|
2023-04-18 10:38:24 +02:00
|
|
|
if (val_len != 0) {
|
|
|
|
cur->val.p = mbedtls_calloc(1, val_len);
|
|
|
|
if (cur->val.p == NULL) {
|
|
|
|
mbedtls_free(cur->oid.p);
|
|
|
|
mbedtls_free(cur);
|
|
|
|
return NULL;
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cur->next = *head;
|
|
|
|
*head = cur;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else if (val_len == 0) {
|
|
|
|
mbedtls_free(cur->val.p);
|
2021-12-21 12:54:05 +01:00
|
|
|
cur->val.p = NULL;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else if (cur->val.len != val_len) {
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Enlarge existing value buffer if needed
|
|
|
|
* Preserve old data until the allocation succeeded, to leave list in
|
|
|
|
* a consistent state in case allocation fails.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
void *p = mbedtls_calloc(1, val_len);
|
|
|
|
if (p == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_free(cur->val.p);
|
2018-02-08 19:04:43 +01:00
|
|
|
cur->val.p = p;
|
|
|
|
cur->val.len = val_len;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (val != NULL && val_len != 0) {
|
|
|
|
memcpy(cur->val.p, val, val_len);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return cur;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ASN1_WRITE_C */
|