Code optimization and style fixes

Signed-off-by: Przemek Stekiel <przemyslaw.stekiel@mobica.com>
This commit is contained in:
Przemek Stekiel 2023-03-10 12:40:41 +01:00
parent 89e268dfb9
commit 55ceff6d2f
2 changed files with 19 additions and 12 deletions

View file

@ -97,7 +97,7 @@ int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ct
size_t buflen = 0; size_t buflen = 0;
/* Determine the maximum size of the SubjectAltName list */ /* Determine the maximum size of the SubjectAltName list */
for(cur = san_list; cur != NULL; cur = cur->next) { for (cur = san_list; cur != NULL; cur = cur->next) {
/* Calculate size of the required buffer */ /* Calculate size of the required buffer */
switch (cur->node.type) { switch (cur->node.type) {
case MBEDTLS_X509_SAN_DNS_NAME: case MBEDTLS_X509_SAN_DNS_NAME:
@ -136,17 +136,23 @@ int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ct
case MBEDTLS_X509_SAN_DNS_NAME: case MBEDTLS_X509_SAN_DNS_NAME:
case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER: case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
case MBEDTLS_X509_SAN_IP_ADDRESS: case MBEDTLS_X509_SAN_IP_ADDRESS:
{
const unsigned char *unstructured_name =
(const unsigned char *) cur->node.san.unstructured_name.p;
size_t unstructured_name_len = cur->node.san.unstructured_name.len;
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
mbedtls_asn1_write_raw_buffer(&p, buf, mbedtls_asn1_write_raw_buffer(
(const unsigned char *) cur->node.san.unstructured_name.p, &p, buf,
cur->node.san.unstructured_name.len)); unstructured_name, unstructured_name_len));
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(
cur->node.san.unstructured_name.len)); &p, buf, unstructured_name_len));
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
mbedtls_asn1_write_tag(&p, buf, mbedtls_asn1_write_tag(
MBEDTLS_ASN1_CONTEXT_SPECIFIC | &p, buf,
cur->node.type)); MBEDTLS_ASN1_CONTEXT_SPECIFIC | cur->node.type));
break; }
break;
default: default:
/* Skip unsupported names. */ /* Skip unsupported names. */
break; break;
@ -170,7 +176,7 @@ int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ct
/* If we exceeded the allocated buffer it means that maximum size of the SubjectAltName list /* If we exceeded the allocated buffer it means that maximum size of the SubjectAltName list
* was incorrectly calculated and memory is corrupted. */ * was incorrectly calculated and memory is corrupted. */
if ( p < buf ) { if (p < buf) {
ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
} }

View file

@ -169,7 +169,6 @@ int main(int argc, char *argv[])
mbedtls_ctr_drbg_context ctr_drbg; mbedtls_ctr_drbg_context ctr_drbg;
const char *pers = "csr example app"; const char *pers = "csr example app";
mbedtls_x509_san_list *cur, *prev; mbedtls_x509_san_list *cur, *prev;
uint8_t ip[4];
/* /*
* Set to sane values * Set to sane values
@ -220,6 +219,8 @@ usage:
prev = NULL; prev = NULL;
while (q != NULL) { while (q != NULL) {
uint8_t ip[4] = { 0 };
if ((r = strchr(q, ';')) != NULL) { if ((r = strchr(q, ';')) != NULL) {
*r++ = '\0'; *r++ = '\0';
} }