proxmark3/common/mbedtls/asn1write.c

368 lines
10 KiB
C
Raw Normal View History

2018-11-08 00:05:02 +08:00
/*
* ASN.1 buffer writing functionality
*
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: GPL-2.0
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_ASN1_WRITE_C)
#include "mbedtls/asn1write.h"
#include <string.h>
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#define mbedtls_calloc calloc
#define mbedtls_free free
#endif
int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len) {
2019-03-10 07:00:59 +08:00
if (len < 0x80) {
if (*p - start < 1)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
*--(*p) = (unsigned char) len;
2019-03-10 07:00:59 +08:00
return (1);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
if (len <= 0xFF) {
if (*p - start < 2)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
*--(*p) = (unsigned char) len;
*--(*p) = 0x81;
2019-03-10 07:00:59 +08:00
return (2);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
if (len <= 0xFFFF) {
if (*p - start < 3)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
*--(*p) = (len) & 0xFF;
*--(*p) = (len >> 8) & 0xFF;
2018-11-08 00:05:02 +08:00
*--(*p) = 0x82;
2019-03-10 07:00:59 +08:00
return (3);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
if (len <= 0xFFFFFF) {
if (*p - start < 4)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
*--(*p) = (len) & 0xFF;
*--(*p) = (len >> 8) & 0xFF;
*--(*p) = (len >> 16) & 0xFF;
2018-11-08 00:05:02 +08:00
*--(*p) = 0x83;
2019-03-10 07:00:59 +08:00
return (4);
2018-11-08 00:05:02 +08:00
}
#if SIZE_MAX > 0xFFFFFFFF
2019-03-10 07:00:59 +08:00
if (len <= 0xFFFFFFFF)
2018-11-08 00:05:02 +08:00
#endif
{
2019-03-10 07:00:59 +08:00
if (*p - start < 5)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
*--(*p) = (len) & 0xFF;
*--(*p) = (len >> 8) & 0xFF;
*--(*p) = (len >> 16) & 0xFF;
*--(*p) = (len >> 24) & 0xFF;
2018-11-08 00:05:02 +08:00
*--(*p) = 0x84;
2019-03-10 07:00:59 +08:00
return (5);
2018-11-08 00:05:02 +08:00
}
#if SIZE_MAX > 0xFFFFFFFF
2019-03-10 07:00:59 +08:00
return (MBEDTLS_ERR_ASN1_INVALID_LENGTH);
2018-11-08 00:05:02 +08:00
#endif
}
int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag) {
2019-03-10 07:00:59 +08:00
if (*p - start < 1)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
*--(*p) = tag;
2019-03-10 07:00:59 +08:00
return (1);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
int mbedtls_asn1_write_raw_buffer(unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t size) {
2018-11-08 00:05:02 +08:00
size_t len = 0;
2019-03-10 07:00:59 +08:00
if (*p < start || (size_t)(*p - start) < size)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
len = size;
(*p) -= len;
2019-03-10 07:00:59 +08:00
memcpy(*p, buf, len);
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-08 00:05:02 +08:00
}
#if defined(MBEDTLS_BIGNUM_C)
int mbedtls_asn1_write_mpi(unsigned char **p, unsigned char *start, const mbedtls_mpi *X) {
2018-11-08 00:05:02 +08:00
int ret;
size_t len = 0;
// Write the MPI
//
2019-03-10 07:00:59 +08:00
len = mbedtls_mpi_size(X);
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
if (*p < start || (size_t)(*p - start) < len)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
(*p) -= len;
2019-03-10 07:00:59 +08:00
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
2018-11-08 00:05:02 +08:00
// DER format assumes 2s complement for numbers, so the leftmost bit
// should be 0 for positive numbers and 1 for negative numbers.
//
2019-03-10 07:00:59 +08:00
if (X->s == 1 && **p & 0x80) {
if (*p - start < 1)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
*--(*p) = 0x00;
len += 1;
}
2019-03-10 07:00:59 +08: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-11-08 00:05:02 +08:00
ret = (int) len;
cleanup:
2019-03-10 07:00:59 +08:00
return (ret);
2018-11-08 00:05:02 +08:00
}
#endif /* MBEDTLS_BIGNUM_C */
int mbedtls_asn1_write_null(unsigned char **p, unsigned char *start) {
2018-11-08 00:05:02 +08:00
int ret;
size_t len = 0;
// Write NULL
//
2019-03-10 07:00:59 +08: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-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
int mbedtls_asn1_write_oid(unsigned char **p, unsigned char *start,
const char *oid, size_t oid_len) {
2018-11-08 00:05:02 +08:00
int ret;
size_t len = 0;
2019-03-10 07:00:59 +08: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-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, unsigned char *start,
const char *oid, size_t oid_len,
size_t par_len) {
2018-11-08 00:05:02 +08:00
int ret;
size_t len = 0;
2019-03-10 07:00:59 +08:00
if (par_len == 0)
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
2018-11-08 00:05:02 +08:00
else
len += par_len;
2019-03-10 07:00:59 +08:00
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08: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-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-08 00:05:02 +08:00
}
int mbedtls_asn1_write_bool(unsigned char **p, unsigned char *start, int boolean) {
2018-11-08 00:05:02 +08:00
int ret;
size_t len = 0;
2019-03-10 07:00:59 +08:00
if (*p - start < 1)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
*--(*p) = (boolean) ? 255 : 0;
len++;
2019-03-10 07:00:59 +08: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-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-08 00:05:02 +08:00
}
int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val) {
2018-11-08 00:05:02 +08:00
int ret;
size_t len = 0;
2019-03-10 07:00:59 +08:00
if (*p - start < 1)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
len += 1;
*--(*p) = val;
2019-03-10 07:00:59 +08:00
if (val > 0 && **p & 0x80) {
if (*p - start < 1)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
*--(*p) = 0x00;
len += 1;
}
2019-03-10 07:00:59 +08: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-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
int mbedtls_asn1_write_printable_string(unsigned char **p, unsigned char *start,
const char *text, size_t text_len) {
2018-11-08 00:05:02 +08:00
int ret;
size_t len = 0;
2019-03-10 07:00:59 +08:00
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
(const unsigned char *) text, text_len));
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08: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_PRINTABLE_STRING));
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
int mbedtls_asn1_write_ia5_string(unsigned char **p, unsigned char *start,
const char *text, size_t text_len) {
2018-11-08 00:05:02 +08:00
int ret;
size_t len = 0;
2019-03-10 07:00:59 +08:00
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
(const unsigned char *) text, text_len));
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08: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_IA5_STRING));
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
int mbedtls_asn1_write_bitstring(unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t bits) {
2018-11-08 00:05:02 +08:00
int ret;
size_t len = 0, size;
2019-03-10 07:00:59 +08:00
size = (bits / 8) + ((bits % 8) ? 1 : 0);
2018-11-08 00:05:02 +08:00
// Calculate byte length
//
2019-03-10 07:00:59 +08:00
if (*p < start || (size_t)(*p - start) < size + 1)
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
2018-11-08 00:05:02 +08:00
len = size + 1;
(*p) -= size;
2019-03-10 07:00:59 +08:00
memcpy(*p, buf, size);
2018-11-08 00:05:02 +08:00
// Write unused bits
//
2019-03-10 07:00:59 +08:00
*--(*p) = (unsigned char)(size * 8 - bits);
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08: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-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
int mbedtls_asn1_write_octet_string(unsigned char **p, unsigned char *start,
const unsigned char *buf, size_t size) {
2018-11-08 00:05:02 +08:00
int ret;
size_t len = 0;
2019-03-10 07:00:59 +08:00
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08: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-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return ((int) len);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **head,
const char *oid, size_t oid_len,
const unsigned char *val,
size_t val_len) {
2018-11-08 00:05:02 +08:00
mbedtls_asn1_named_data *cur;
2019-03-10 07:00:59 +08:00
if ((cur = mbedtls_asn1_find_named_data(*head, oid, oid_len)) == NULL) {
2018-11-08 00:05:02 +08:00
// Add new entry if not present yet based on OID
//
2019-03-10 07:00:59 +08:00
cur = (mbedtls_asn1_named_data *)mbedtls_calloc(1,
sizeof(mbedtls_asn1_named_data));
if (cur == NULL)
return (NULL);
2018-11-08 00:05:02 +08:00
cur->oid.len = oid_len;
2019-03-10 07:00:59 +08:00
cur->oid.p = mbedtls_calloc(1, oid_len);
if (cur->oid.p == NULL) {
mbedtls_free(cur);
return (NULL);
2018-11-08 00:05:02 +08:00
}
2019-03-10 07:00:59 +08:00
memcpy(cur->oid.p, oid, oid_len);
2018-11-08 00:05:02 +08:00
cur->val.len = val_len;
2019-03-10 07:00:59 +08:00
cur->val.p = mbedtls_calloc(1, val_len);
if (cur->val.p == NULL) {
mbedtls_free(cur->oid.p);
mbedtls_free(cur);
return (NULL);
2018-11-08 00:05:02 +08:00
}
cur->next = *head;
*head = cur;
2019-03-10 07:00:59 +08:00
} else if (cur->val.len < val_len) {
2018-11-08 00:05:02 +08: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.
*/
2019-03-10 07:00:59 +08:00
void *p = mbedtls_calloc(1, val_len);
if (p == NULL)
return (NULL);
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
mbedtls_free(cur->val.p);
2018-11-08 00:05:02 +08:00
cur->val.p = p;
cur->val.len = val_len;
}
2019-03-10 07:00:59 +08:00
if (val != NULL)
memcpy(cur->val.p, val, val_len);
2018-11-08 00:05:02 +08:00
2019-03-10 07:00:59 +08:00
return (cur);
2018-11-08 00:05:02 +08:00
}
#endif /* MBEDTLS_ASN1_WRITE_C */