You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-29 16:16:38 +00:00
mbedTLS: Update to new LTS v3.6.0
Keep module compatibility with mbedtls 2.x (old LTS branch). A patch has been added to allow compiling after removing all the `psa_*` files from the library folder (will look into upstreaming it). Note: mbedTLS 3.6 finally enabled TLSv1.3 by default, but it requires some module changes, and to enable PSA crypto (new "standard" API specification), so it might be best done in a separate commit/PR.
This commit is contained in:
committed by
Fabio Alessandrelli
parent
6c57928063
commit
40fa684c18
267
thirdparty/mbedtls/library/x509_create.c
vendored
267
thirdparty/mbedtls/library/x509_create.c
vendored
@@ -9,13 +9,17 @@
|
||||
|
||||
#if defined(MBEDTLS_X509_CREATE_C)
|
||||
|
||||
#include "mbedtls/x509.h"
|
||||
#include "x509_internal.h"
|
||||
#include "mbedtls/asn1write.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/oid.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
#include "mbedtls/asn1.h"
|
||||
|
||||
/* Structure linking OIDs for X.509 DN AttributeTypes to their
|
||||
* string representations and default string encodings used by Mbed TLS. */
|
||||
typedef struct {
|
||||
@@ -23,7 +27,8 @@ typedef struct {
|
||||
* "CN" or "emailAddress". */
|
||||
size_t name_len; /* Length of 'name', without trailing 0 byte. */
|
||||
const char *oid; /* String representation of OID of AttributeType,
|
||||
* as per RFC 5280, Appendix A.1. */
|
||||
* as per RFC 5280, Appendix A.1. encoded as per
|
||||
* X.690 */
|
||||
int default_tag; /* The default character encoding used for the
|
||||
* given attribute type, e.g.
|
||||
* MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
|
||||
@@ -111,79 +116,261 @@ static const x509_attr_descriptor_t *x509_attr_descr_from_name(const char *name,
|
||||
return cur;
|
||||
}
|
||||
|
||||
static int hex_to_int(char c)
|
||||
{
|
||||
return ('0' <= c && c <= '9') ? (c - '0') :
|
||||
('a' <= c && c <= 'f') ? (c - 'a' + 10) :
|
||||
('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1;
|
||||
}
|
||||
|
||||
static int hexpair_to_int(const char *hexpair)
|
||||
{
|
||||
int n1 = hex_to_int(*hexpair);
|
||||
int n2 = hex_to_int(*(hexpair + 1));
|
||||
|
||||
if (n1 != -1 && n2 != -1) {
|
||||
return (n1 << 4) | n2;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int parse_attribute_value_string(const char *s,
|
||||
int len,
|
||||
unsigned char *data,
|
||||
size_t *data_len)
|
||||
{
|
||||
const char *c;
|
||||
const char *end = s + len;
|
||||
unsigned char *d = data;
|
||||
int n;
|
||||
|
||||
for (c = s; c < end; c++) {
|
||||
if (*c == '\\') {
|
||||
c++;
|
||||
|
||||
/* Check for valid escaped characters as per RFC 4514 Section 3 */
|
||||
if (c + 1 < end && (n = hexpair_to_int(c)) != -1) {
|
||||
if (n == 0) {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
*(d++) = n;
|
||||
c++;
|
||||
} else if (c < end && strchr(" ,=+<>#;\"\\", *c)) {
|
||||
*(d++) = *c;
|
||||
} else {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
} else {
|
||||
*(d++) = *c;
|
||||
}
|
||||
|
||||
if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
}
|
||||
*data_len = (size_t) (d - data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Parse a hexstring containing a DER-encoded string.
|
||||
*
|
||||
* \param s A string of \p len bytes hexadecimal digits.
|
||||
* \param len Number of bytes to read from \p s.
|
||||
* \param data Output buffer of size \p data_size.
|
||||
* On success, it contains the payload that's DER-encoded
|
||||
* in the input (content without the tag and length).
|
||||
* If the DER tag is a string tag, the payload is guaranteed
|
||||
* not to contain null bytes.
|
||||
* \param data_size Length of the \p data buffer.
|
||||
* \param data_len On success, the length of the parsed string.
|
||||
* It is guaranteed to be less than
|
||||
* #MBEDTLS_X509_MAX_DN_NAME_SIZE.
|
||||
* \param tag The ASN.1 tag that the payload in \p data is encoded in.
|
||||
*
|
||||
* \retval 0 on success.
|
||||
* \retval #MBEDTLS_ERR_X509_INVALID_NAME if \p s does not contain
|
||||
* a valid hexstring,
|
||||
* or if the decoded hexstring is not valid DER,
|
||||
* or if the payload does not fit in \p data,
|
||||
* or if the payload is more than
|
||||
* #MBEDTLS_X509_MAX_DN_NAME_SIZE bytes,
|
||||
* of if \p *tag is an ASN.1 string tag and the payload
|
||||
* contains a null byte.
|
||||
* \retval #MBEDTLS_ERR_X509_ALLOC_FAILED on low memory.
|
||||
*/
|
||||
static int parse_attribute_value_hex_der_encoded(const char *s,
|
||||
size_t len,
|
||||
unsigned char *data,
|
||||
size_t data_size,
|
||||
size_t *data_len,
|
||||
int *tag)
|
||||
{
|
||||
/* Step 1: preliminary length checks. */
|
||||
/* Each byte is encoded by exactly two hexadecimal digits. */
|
||||
if (len % 2 != 0) {
|
||||
/* Odd number of hex digits */
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
size_t const der_length = len / 2;
|
||||
if (der_length > MBEDTLS_X509_MAX_DN_NAME_SIZE + 4) {
|
||||
/* The payload would be more than MBEDTLS_X509_MAX_DN_NAME_SIZE
|
||||
* (after subtracting the ASN.1 tag and length). Reject this early
|
||||
* to avoid allocating a large intermediate buffer. */
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
if (der_length < 1) {
|
||||
/* Avoid empty-buffer shenanigans. A valid DER encoding is never
|
||||
* empty. */
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
|
||||
/* Step 2: Decode the hex string into an intermediate buffer. */
|
||||
unsigned char *der = mbedtls_calloc(1, der_length);
|
||||
if (der == NULL) {
|
||||
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
||||
}
|
||||
/* Beyond this point, der needs to be freed on exit. */
|
||||
for (size_t i = 0; i < der_length; i++) {
|
||||
int c = hexpair_to_int(s + 2 * i);
|
||||
if (c < 0) {
|
||||
goto error;
|
||||
}
|
||||
der[i] = c;
|
||||
}
|
||||
|
||||
/* Step 3: decode the DER. */
|
||||
/* We've checked that der_length >= 1 above. */
|
||||
*tag = der[0];
|
||||
{
|
||||
unsigned char *p = der + 1;
|
||||
if (mbedtls_asn1_get_len(&p, der + der_length, data_len) != 0) {
|
||||
goto error;
|
||||
}
|
||||
/* Now p points to the first byte of the payload inside der,
|
||||
* and *data_len is the length of the payload. */
|
||||
|
||||
/* Step 4: payload validation */
|
||||
if (*data_len > MBEDTLS_X509_MAX_DN_NAME_SIZE) {
|
||||
goto error;
|
||||
}
|
||||
/* Strings must not contain null bytes. */
|
||||
if (MBEDTLS_ASN1_IS_STRING_TAG(*tag)) {
|
||||
for (size_t i = 0; i < *data_len; i++) {
|
||||
if (p[i] == 0) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Step 5: output the payload. */
|
||||
if (*data_len > data_size) {
|
||||
goto error;
|
||||
}
|
||||
memcpy(data, p, *data_len);
|
||||
}
|
||||
mbedtls_free(der);
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
mbedtls_free(der);
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
}
|
||||
|
||||
int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
int parse_ret = 0;
|
||||
const char *s = name, *c = s;
|
||||
const char *end = s + strlen(s);
|
||||
const char *oid = NULL;
|
||||
mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL };
|
||||
const x509_attr_descriptor_t *attr_descr = NULL;
|
||||
int in_tag = 1;
|
||||
char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
|
||||
char *d = data;
|
||||
int in_attr_type = 1;
|
||||
int tag;
|
||||
int numericoid = 0;
|
||||
unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
|
||||
size_t data_len = 0;
|
||||
|
||||
/* Clear existing chain if present */
|
||||
mbedtls_asn1_free_named_data_list(head);
|
||||
|
||||
while (c <= end) {
|
||||
if (in_tag && *c == '=') {
|
||||
if ((attr_descr = x509_attr_descr_from_name(s, c - s)) == NULL) {
|
||||
ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
|
||||
goto exit;
|
||||
if (in_attr_type && *c == '=') {
|
||||
if ((attr_descr = x509_attr_descr_from_name(s, (size_t) (c - s))) == NULL) {
|
||||
if ((mbedtls_oid_from_numeric_string(&oid, s, (size_t) (c - s))) != 0) {
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
} else {
|
||||
numericoid = 1;
|
||||
}
|
||||
} else {
|
||||
oid.len = strlen(attr_descr->oid);
|
||||
oid.p = mbedtls_calloc(1, oid.len);
|
||||
memcpy(oid.p, attr_descr->oid, oid.len);
|
||||
numericoid = 0;
|
||||
}
|
||||
|
||||
oid = attr_descr->oid;
|
||||
s = c + 1;
|
||||
in_tag = 0;
|
||||
d = data;
|
||||
in_attr_type = 0;
|
||||
}
|
||||
|
||||
if (!in_tag && *c == '\\' && c != end) {
|
||||
c++;
|
||||
|
||||
/* Check for valid escaped characters */
|
||||
if (c == end || *c != ',') {
|
||||
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
goto exit;
|
||||
if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) {
|
||||
if (s == c) {
|
||||
mbedtls_free(oid.p);
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
} else if (*s == '#') {
|
||||
/* We know that c >= s (loop invariant) and c != s (in this
|
||||
* else branch), hence c - s - 1 >= 0. */
|
||||
parse_ret = parse_attribute_value_hex_der_encoded(
|
||||
s + 1, (size_t) (c - s) - 1,
|
||||
data, sizeof(data), &data_len, &tag);
|
||||
if (parse_ret != 0) {
|
||||
mbedtls_free(oid.p);
|
||||
return parse_ret;
|
||||
}
|
||||
} else {
|
||||
if (numericoid) {
|
||||
mbedtls_free(oid.p);
|
||||
return MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
} else {
|
||||
if ((parse_ret =
|
||||
parse_attribute_value_string(s, (int) (c - s), data,
|
||||
&data_len)) != 0) {
|
||||
mbedtls_free(oid.p);
|
||||
return parse_ret;
|
||||
}
|
||||
tag = attr_descr->default_tag;
|
||||
}
|
||||
}
|
||||
} else if (!in_tag && (*c == ',' || c == end)) {
|
||||
mbedtls_asn1_named_data *cur =
|
||||
mbedtls_asn1_store_named_data(head, oid, strlen(oid),
|
||||
(unsigned char *) data,
|
||||
d - data);
|
||||
|
||||
mbedtls_asn1_named_data *cur =
|
||||
mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len,
|
||||
(unsigned char *) data,
|
||||
data_len);
|
||||
mbedtls_free(oid.p);
|
||||
oid.p = NULL;
|
||||
if (cur == NULL) {
|
||||
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
||||
}
|
||||
|
||||
// set tagType
|
||||
cur->val.tag = attr_descr->default_tag;
|
||||
cur->val.tag = tag;
|
||||
|
||||
while (c < end && *(c + 1) == ' ') {
|
||||
c++;
|
||||
}
|
||||
|
||||
s = c + 1;
|
||||
in_tag = 1;
|
||||
in_attr_type = 1;
|
||||
|
||||
/* Successfully parsed one name, update ret to success */
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
if (!in_tag && s != c + 1) {
|
||||
*(d++) = *c;
|
||||
|
||||
if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
|
||||
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
c++;
|
||||
}
|
||||
|
||||
exit:
|
||||
|
||||
if (oid.p != NULL) {
|
||||
mbedtls_free(oid.p);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user