2018-02-08 18:04:43 +00:00
|
|
|
/*
|
|
|
|
* X.509 base functions for creating certificates / CSRs
|
|
|
|
*
|
2020-09-05 10:53:20 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2021-12-21 11:54:05 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-02-08 18:04:43 +00: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 11:54:05 +00:00
|
|
|
#include "common.h"
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CREATE_C)
|
|
|
|
|
|
|
|
#include "mbedtls/x509.h"
|
|
|
|
#include "mbedtls/asn1write.h"
|
2021-12-21 11:54:05 +00:00
|
|
|
#include "mbedtls/error.h"
|
2018-02-08 18:04:43 +00:00
|
|
|
#include "mbedtls/oid.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-02-16 16:19:46 +00:00
|
|
|
/* Structure linking OIDs for X.509 DN AttributeTypes to their
|
|
|
|
* string representations and default string encodings used by Mbed TLS. */
|
2018-02-08 18:04:43 +00:00
|
|
|
typedef struct {
|
2023-04-18 08:38:24 +00:00
|
|
|
const char *name; /* String representation of AttributeType, e.g.
|
|
|
|
* "CN" or "emailAddress". */
|
|
|
|
size_t name_len; /* Length of 'name', without trailing 0 byte. */
|
|
|
|
const char *oid; /* String representation of OID of AttributeType,
|
2019-02-16 16:19:46 +00:00
|
|
|
* as per RFC 5280, Appendix A.1. */
|
2023-04-18 08:38:24 +00:00
|
|
|
int default_tag; /* The default character encoding used for the
|
2019-02-16 16:19:46 +00:00
|
|
|
* given attribute type, e.g.
|
|
|
|
* MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
|
2018-02-08 18:04:43 +00:00
|
|
|
} x509_attr_descriptor_t;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
#define ADD_STRLEN(s) s, sizeof(s) - 1
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2019-02-16 16:19:46 +00:00
|
|
|
/* X.509 DN attributes from RFC 5280, Appendix A.1. */
|
2018-02-08 18:04:43 +00:00
|
|
|
static const x509_attr_descriptor_t x509_attrs[] =
|
|
|
|
{
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("CN"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("commonName"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("C"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("countryName"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("O"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("organizationName"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("L"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("locality"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("R"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("OU"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("organizationalUnitName"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("ST"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("stateOrProvinceName"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("emailAddress"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("serialNumber"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("postalAddress"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("postalCode"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("dnQualifier"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("title"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("surName"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("SN"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("givenName"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("GN"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("initials"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("pseudonym"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("generationQualifier"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("domainComponent"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
|
2023-04-18 08:38:24 +00:00
|
|
|
{ ADD_STRLEN("DC"),
|
2019-02-16 16:19:46 +00:00
|
|
|
MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
|
|
|
|
{ NULL, 0, NULL, MBEDTLS_ASN1_NULL }
|
2018-02-08 18:04:43 +00:00
|
|
|
};
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
static const x509_attr_descriptor_t *x509_attr_descr_from_name(const char *name, size_t name_len)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
const x509_attr_descriptor_t *cur;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
for (cur = x509_attrs; cur->name != NULL; cur++) {
|
|
|
|
if (cur->name_len == name_len &&
|
|
|
|
strncmp(cur->name, name, name_len) == 0) {
|
2018-02-08 18:04:43 +00:00
|
|
|
break;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (cur->name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-02-16 16:19:46 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return cur;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2023-08-04 16:09:03 +00:00
|
|
|
int ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
2018-02-08 18:04:43 +00:00
|
|
|
const char *s = name, *c = s;
|
2023-04-18 08:38:24 +00:00
|
|
|
const char *end = s + strlen(s);
|
2018-02-08 18:04:43 +00:00
|
|
|
const char *oid = NULL;
|
2023-04-18 08:38:24 +00:00
|
|
|
const x509_attr_descriptor_t *attr_descr = NULL;
|
2018-02-08 18:04:43 +00:00
|
|
|
int in_tag = 1;
|
|
|
|
char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
|
|
|
|
char *d = data;
|
|
|
|
|
|
|
|
/* Clear existing chain if present */
|
2023-04-18 08:38:24 +00:00
|
|
|
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) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-02-16 16:19:46 +00:00
|
|
|
oid = attr_descr->oid;
|
2018-02-08 18:04:43 +00:00
|
|
|
s = c + 1;
|
|
|
|
in_tag = 0;
|
|
|
|
d = data;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (!in_tag && *c == '\\' && c != end) {
|
2018-02-08 18:04:43 +00:00
|
|
|
c++;
|
|
|
|
|
|
|
|
/* Check for valid escaped characters */
|
2023-04-18 08:38:24 +00:00
|
|
|
if (c == end || *c != ',') {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
goto exit;
|
|
|
|
}
|
2023-04-18 08:38:24 +00:00
|
|
|
} 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);
|
|
|
|
|
|
|
|
if (cur == NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 16:19:46 +00:00
|
|
|
// set tagType
|
|
|
|
cur->val.tag = attr_descr->default_tag;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
while (c < end && *(c + 1) == ' ') {
|
2018-02-08 18:04:43 +00:00
|
|
|
c++;
|
2023-04-18 08:38:24 +00:00
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
s = c + 1;
|
|
|
|
in_tag = 1;
|
2023-08-04 16:09:03 +00:00
|
|
|
|
|
|
|
/* Successfully parsed one name, update ret to success */
|
|
|
|
ret = 0;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (!in_tag && s != c + 1) {
|
2018-02-08 18:04:43 +00:00
|
|
|
*(d++) = *c;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
|
2018-02-08 18:04:43 +00:00
|
|
|
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return ret;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
|
|
|
|
* to store the critical boolean for us
|
|
|
|
*/
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
|
|
|
|
int critical, const unsigned char *val, size_t val_len)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
|
|
|
mbedtls_asn1_named_data *cur;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
|
|
|
|
NULL, val_len + 1)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cur->val.p[0] = critical;
|
2023-04-18 08:38:24 +00:00
|
|
|
memcpy(cur->val.p + 1, val, val_len);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return 0;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RelativeDistinguishedName ::=
|
|
|
|
* SET OF AttributeTypeAndValue
|
|
|
|
*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
|
|
*/
|
2023-04-18 08:38:24 +00:00
|
|
|
static int x509_write_name(unsigned char **p,
|
|
|
|
unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *cur_name)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2021-12-21 11:54:05 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 18:04:43 +00:00
|
|
|
size_t len = 0;
|
2023-04-18 08:38:24 +00:00
|
|
|
const char *oid = (const char *) cur_name->oid.p;
|
2019-02-16 16:19:46 +00:00
|
|
|
size_t oid_len = cur_name->oid.len;
|
|
|
|
const unsigned char *name = cur_name->val.p;
|
|
|
|
size_t name_len = cur_name->val.len;
|
|
|
|
|
|
|
|
// Write correct string tag and value
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
|
|
|
|
cur_name->val.tag,
|
|
|
|
(const char *) name,
|
|
|
|
name_len));
|
2018-02-08 18:04:43 +00:00
|
|
|
// Write OID
|
|
|
|
//
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
|
|
|
|
oid_len));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00: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 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00: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_SET));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return (int) len;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *first)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2021-12-21 11:54:05 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 18:04:43 +00:00
|
|
|
size_t len = 0;
|
|
|
|
mbedtls_asn1_named_data *cur = first;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
while (cur != NULL) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
|
2018-02-08 18:04:43 +00:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00: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 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return (int) len;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
|
|
|
|
const char *oid, size_t oid_len,
|
2023-10-21 12:06:23 +00:00
|
|
|
unsigned char *sig, size_t size,
|
|
|
|
mbedtls_pk_type_t pk_alg)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2021-12-21 11:54:05 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-10-21 12:06:23 +00:00
|
|
|
int write_null_par;
|
2018-02-08 18:04:43 +00:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (*p < start || (size_t) (*p - start) < size) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
len = size;
|
|
|
|
(*p) -= len;
|
2023-04-18 08:38:24 +00:00
|
|
|
memcpy(*p, sig, len);
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (*p - start < 1) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 18:04:43 +00:00
|
|
|
|
|
|
|
*--(*p) = 0;
|
|
|
|
len += 1;
|
|
|
|
|
2023-04-18 08:38:24 +00: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 18:04:43 +00:00
|
|
|
|
|
|
|
// Write OID
|
|
|
|
//
|
2023-10-21 12:06:23 +00:00
|
|
|
if (pk_alg == MBEDTLS_PK_ECDSA) {
|
|
|
|
/*
|
|
|
|
* The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
|
|
|
|
* algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
|
|
|
|
* https://www.rfc-editor.org/rfc/rfc5758#section-3.
|
|
|
|
*/
|
|
|
|
write_null_par = 0;
|
|
|
|
} else {
|
|
|
|
write_null_par = 1;
|
|
|
|
}
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len,
|
|
|
|
0, write_null_par));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return (int) len;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
static int x509_write_extension(unsigned char **p, unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *ext)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2021-12-21 11:54:05 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 18:04:43 +00:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
|
|
|
|
ext->val.len - 1));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
if (ext->val.p[0] != 0) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
|
|
|
|
ext->oid.len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
|
2018-02-08 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00: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 18:04:43 +00:00
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return (int) len;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extension ::= SEQUENCE {
|
|
|
|
* extnID OBJECT IDENTIFIER,
|
|
|
|
* critical BOOLEAN DEFAULT FALSE,
|
|
|
|
* extnValue OCTET STRING
|
|
|
|
* -- contains the DER encoding of an ASN.1 value
|
|
|
|
* -- corresponding to the extension type identified
|
|
|
|
* -- by extnID
|
|
|
|
* }
|
|
|
|
*/
|
2023-04-18 08:38:24 +00:00
|
|
|
int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *first)
|
2018-02-08 18:04:43 +00:00
|
|
|
{
|
2021-12-21 11:54:05 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 18:04:43 +00:00
|
|
|
size_t len = 0;
|
|
|
|
mbedtls_asn1_named_data *cur_ext = first;
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
while (cur_ext != NULL) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
|
2018-02-08 18:04:43 +00:00
|
|
|
cur_ext = cur_ext->next;
|
|
|
|
}
|
|
|
|
|
2023-04-18 08:38:24 +00:00
|
|
|
return (int) len;
|
2018-02-08 18:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_X509_CREATE_C */
|