diff --git a/README.md b/README.md index 7ef43ed..82b9852 100644 --- a/README.md +++ b/README.md @@ -116,10 +116,11 @@ make MBEDTLS_COMPAT=1 - With `MBEDTLS_THREADING_C=1`, TLS is thread safe too. - TLS uses wolfPSA's constant-time AES. `AES_FAST=1` applies to TLS too. - As in Mbed TLS, certificates may have NULL parameters after an ECDSA - signature OID. This needs `WOLFSSL_ECC_SIGALG_PARAMS_NULL_ALLOWED`, which - wolfPSA's `user_settings.h` defines. A build with its own - `USER_SETTINGS_PATH` defines it too, or rejects such certificates, for - example Mbed TLS's `test-ca2.crt`. + signature OID, and serial number 0. This needs + `WOLFSSL_ECC_SIGALG_PARAMS_NULL_ALLOWED` and `WOLFSSL_ASN_ALLOW_0_SERIAL`, + which wolfPSA's `user_settings.h` defines. A build with its own + `USER_SETTINGS_PATH` defines them too, or rejects such certificates, for + example Mbed TLS's `test-ca2.crt` and `test-ca-any_policy.crt`. - The X.509 API needs wolfCrypt's certificate parsing: without it (`NO_ASN` or `NO_CERTS`), `MBEDTLS_X509_CRT_PARSE_C` is not defined. - TLS needs the X.509 API and wolfSSL's TLS with `HAVE_SNI`: without them, @@ -131,8 +132,10 @@ Mapped so far: - net: init, connect, bind, accept, close, free, recv, send, usleep - pem: init, read_buffer, get_buffer, free -- x509_crt: init, free, parse, parse_der, parse_file, parse_path; the - default, next and suiteb profiles, as names only +- x509_crt: init, free, parse, parse_der, parse_file, parse_path, + get_ca_istrue, check_key_usage, check_extended_key_usage, has_ext_type; + the default, next and suiteb profiles, as names only +- x509: dn_gets, dn_get_next, serial_gets, time_is_past, time_is_future - ssl_config: init, free, defaults; ssl_conf: authmode, ca_chain, own_cert, min_tls_version, max_tls_version, ciphersuites, cert_profile, dbg, endpoint, transport, get_endpoint, set_user_data_p, set_user_data_n, @@ -209,17 +212,21 @@ Known differences from Mbed TLS: and ignored. - `mbedtls_pem_read_buffer` returns `MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE` for encrypted PEM. No `mbedtls_pem_write_buffer`. -- `mbedtls_x509_crt` has one node per certificate, with no parsed fields - apart from `raw`. Parse errors are `MBEDTLS_ERR_X509_INVALID_FORMAT` or - `MBEDTLS_ERR_X509_ALLOC_FAILED`. +- `mbedtls_x509_crt` has the parsed fields, but not `pk`. The fields Mbed + TLS makes private, such as `ca_istrue`, have their plain names. A + certificate that wolfCrypt rejects returns + `MBEDTLS_ERR_X509_INVALID_FORMAT`, where Mbed TLS often returns a more + specific error. +- With `NO_ASN_TIME`, `mbedtls_x509_time_is_past` and + `mbedtls_x509_time_is_future` return 0, as Mbed TLS does without + `MBEDTLS_HAVE_TIME_DATE`. - `mbedtls_x509_crt_parse_path` needs ``. On Windows, or with `NO_WOLFSSL_DIR`, it returns `MBEDTLS_ERR_X509_FILE_IO_ERROR`. - The profiles are not applied: wolfSSL's own limits hold. `mbedtls_x509_crt_profile` is opaque, so applications cannot define their own. -- wolfSSL parses some certificates differently. For example, it rejects - serial number 0 and a pathlen above `WOLFSSL_MAX_PATH_LEN`, and accepts - spaces in PEM. +- wolfSSL parses some certificates differently. For example, it rejects a + pathlen above `WOLFSSL_MAX_PATH_LEN` and accepts spaces in PEM. - No global mutexes such as `mbedtls_threading_gmtime_mutex`. ### Mbed TLS compatibility tests diff --git a/src/mbedtls/x509.c b/src/mbedtls/x509.c index 89dd61f..b8a4a43 100644 --- a/src/mbedtls/x509.c +++ b/src/mbedtls/x509.c @@ -60,30 +60,478 @@ static int MbedCertDecode(DecodedCert* cert, const byte* der, word32 len) return wc_ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL); } -/* Checks buf decodes as a certificate, without verifying it. Sets *len to - * the certificate's length, dropping any trailing data. */ -static int MbedCertCheck(const unsigned char* buf, size_t* len) +/* Sets *len to the certificate's length, dropping trailing data */ +static int MbedCertLen(const unsigned char* buf, size_t* len) { - DecodedCert* cert; word32 idx = 0; int seqLen; - int ret; if (GetSequence(buf, &idx, &seqLen, (word32)*len) < 0) { return MBEDTLS_ERR_X509_INVALID_FORMAT; } - *len = idx + (word32)seqLen; - cert = (DecodedCert*)XMALLOC(sizeof(*cert), NULL, DYNAMIC_TYPE_DCERT); - if (cert == NULL) { - return MBEDTLS_ERR_X509_ALLOC_FAILED; + return 0; +} + +/* Reads the header at *idx into buf and moves *idx to the content */ +static int MbedDerItem(unsigned char* der, word32* idx, word32 end, + mbedtls_x509_buf* buf) +{ + byte tag; + int len; + + XMEMSET(buf, 0, sizeof(*buf)); + if (GetASNTag(der, idx, &tag, end) < 0 || + GetLength(der, idx, &len, end) < 0) { + return MBEDTLS_ERR_X509_INVALID_FORMAT; } + buf->tag = tag; + buf->len = (size_t)len; + buf->p = der + *idx; + return 0; +} - ret = MbedCertDecode(cert, buf, (word32)*len); - wc_FreeDecodedCert(cert); - XFREE(cert, NULL, DYNAMIC_TYPE_DCERT); +/* As MbedDerItem, but raw spans the whole item and *idx moves past it */ +static int MbedDerRaw(unsigned char* der, word32* idx, word32 end, + mbedtls_x509_buf* raw) +{ + word32 start = *idx; + int ret; + + ret = MbedDerItem(der, idx, end, raw); + if (ret == 0) { + *idx += (word32)raw->len; + raw->p = der + start; + raw->len = *idx - start; + } + return ret; +} + +static void* MbedEntryNew(size_t size) +{ + void* entry = XMALLOC(size, NULL, DYNAMIC_TYPE_X509); + + if (entry != NULL) { + XMEMSET(entry, 0, size); + } + return entry; +} + +/* Frees the entries after the first, which is part of the certificate */ +static void MbedNameFree(mbedtls_x509_name* name) +{ + mbedtls_x509_name* cur = name->next; + mbedtls_x509_name* next; + + while (cur != NULL) { + next = cur->next; + XFREE(cur, NULL, DYNAMIC_TYPE_X509); + cur = next; + } +} + +static void MbedSeqFree(mbedtls_x509_sequence* seq) +{ + mbedtls_x509_sequence* cur = seq->next; + mbedtls_x509_sequence* next; + + while (cur != NULL) { + next = cur->next; + XFREE(cur, NULL, DYNAMIC_TYPE_X509); + cur = next; + } +} + +/* Frees what crt owns, but not crt */ +static void MbedCertFreeFields(mbedtls_x509_crt* crt) +{ + XFREE(crt->raw.p, NULL, DYNAMIC_TYPE_CERT); + MbedNameFree(&crt->issuer); + MbedNameFree(&crt->subject); + MbedSeqFree(&crt->subject_alt_names); + MbedSeqFree(&crt->authority_key_id.authorityCertIssuer); + MbedSeqFree(&crt->certificate_policies); + MbedSeqFree(&crt->ext_key_usage); +} + +/* Name at *idx, one entry per attribute in name */ +static int MbedNameParse(unsigned char* der, word32* idx, word32 end, + mbedtls_x509_buf* raw, mbedtls_x509_name* name) +{ + mbedtls_x509_name* cur = NULL; + mbedtls_x509_buf item; + word32 i = *idx; + word32 nameEnd = 0; + word32 setStart = 0; + word32 setEnd = 0; + word32 attrEnd = 0; + int ret; + + ret = MbedDerRaw(der, idx, end, raw); + if (ret == 0) { + ret = MbedDerItem(der, &i, end, &item); + nameEnd = *idx; + } + while (ret == 0 && i < nameEnd) { + if (i >= setEnd) { + ret = MbedDerItem(der, &i, nameEnd, &item); + setStart = i; + setEnd = i + (word32)item.len; + } + if (ret == 0 && cur == NULL) { + cur = name; + } + else if (ret == 0) { + cur->next_merged = (i != setStart); + cur->next = (mbedtls_x509_name*)MbedEntryNew(sizeof(*cur)); + cur = cur->next; + if (cur == NULL) { + ret = MBEDTLS_ERR_X509_ALLOC_FAILED; + } + } + if (ret == 0) { + ret = MbedDerItem(der, &i, setEnd, &item); + attrEnd = i + (word32)item.len; + } + if (ret == 0) { + ret = MbedDerItem(der, &i, attrEnd, &cur->oid); + i += (word32)cur->oid.len; + } + if (ret == 0) { + ret = MbedDerItem(der, &i, attrEnd, &cur->val); + i = attrEnd; + } + } + return ret; +} + +/* Items of the SEQUENCE OF at idx, or with inner, the first inside each */ +static int MbedSeqParse(unsigned char* der, word32 idx, word32 end, + mbedtls_x509_sequence* seq, int inner) +{ + mbedtls_x509_sequence* cur = NULL; + mbedtls_x509_buf item; + word32 next; + int ret; + + ret = MbedDerItem(der, &idx, end, &item); + end = idx + (word32)item.len; + while (ret == 0 && idx < end) { + ret = MbedDerItem(der, &idx, end, &item); + next = idx + (word32)item.len; + if (ret == 0 && inner) { + ret = MbedDerItem(der, &idx, next, &item); + } + if (ret == 0 && cur == NULL) { + cur = seq; + } + else if (ret == 0) { + cur->next = (mbedtls_x509_sequence*)MbedEntryNew(sizeof(*cur)); + cur = cur->next; + if (cur == NULL) { + ret = MBEDTLS_ERR_X509_ALLOC_FAILED; + } + } + if (ret == 0) { + cur->buf = item; + idx = next; + } + } + return ret; +} + +/* AuthorityKeyIdentifier at idx: the key ID, issuer and serial */ +static int MbedAuthKeyIdParse(unsigned char* der, word32 idx, word32 end, + mbedtls_x509_authority* akid) +{ + mbedtls_x509_buf item; + word32 start; + int ret; - return wolfPSA_MbedRet(ret, MBEDTLS_ERR_X509_INVALID_FORMAT); + ret = MbedDerItem(der, &idx, end, &item); + end = idx + (word32)item.len; + while (ret == 0 && idx < end) { + start = idx; + ret = MbedDerItem(der, &idx, end, &item); + if (ret == 0 && item.tag == ASN_CONTEXT_SPECIFIC) { + akid->keyIdentifier = item; + akid->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING; + } + else if (ret == 0 && + item.tag == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1)) { + /* GeneralNames, implicit tag */ + ret = MbedSeqParse(der, start, end, &akid->authorityCertIssuer, + 0); + } + else if (ret == 0 && item.tag == (ASN_CONTEXT_SPECIFIC | 2)) { + akid->authorityCertSerialNumber = item; + akid->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER; + } + idx += (word32)item.len; + } + return ret; +} + +#define MBED_OID(s) (const byte*)(s), (word32)(sizeof(s) - 1) + +/* The extensions Mbed TLS reports in ext_types */ +static const struct { + const byte* oid; + word32 len; + int type; +} mbedExts[] = { + { MBED_OID("\x55\x1D\x13"), MBEDTLS_X509_EXT_BASIC_CONSTRAINTS }, + { MBED_OID("\x55\x1D\x0F"), MBEDTLS_X509_EXT_KEY_USAGE }, + { MBED_OID("\x55\x1D\x25"), MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE }, + { MBED_OID("\x55\x1D\x11"), MBEDTLS_X509_EXT_SUBJECT_ALT_NAME }, + { MBED_OID("\x60\x86\x48\x01\x86\xF8\x42\x01\x01"), + MBEDTLS_X509_EXT_NS_CERT_TYPE }, + { MBED_OID("\x55\x1D\x20"), MBEDTLS_X509_EXT_CERTIFICATE_POLICIES }, + { MBED_OID("\x55\x1D\x0E"), MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER }, + { MBED_OID("\x55\x1D\x23"), MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER } +}; + +static int MbedOidIs(const mbedtls_x509_buf* oid, const byte* want, + word32 len) +{ + return oid->len == len && XMEMCMP(oid->p, want, len) == 0; +} + +/* Extensions at idx. Scalar values come from wolfCrypt's decode. */ +static int MbedExtParse(mbedtls_x509_crt* crt, unsigned char* der, + word32 idx, word32 end) +{ + mbedtls_x509_buf item; + mbedtls_x509_buf oid; + word32 extEnd; + size_t i; + int type; + int ret; + + ret = MbedDerItem(der, &idx, end, &item); + end = idx + (word32)item.len; + while (ret == 0 && idx < end) { + ret = MbedDerItem(der, &idx, end, &item); + extEnd = idx + (word32)item.len; + if (ret == 0) { + ret = MbedDerItem(der, &idx, extEnd, &oid); + idx += (word32)oid.len; + } + if (ret == 0 && idx < extEnd && der[idx] == ASN_BOOLEAN) { + ret = MbedDerItem(der, &idx, extEnd, &item); + idx += (word32)item.len; + } + if (ret == 0) { + ret = MbedDerItem(der, &idx, extEnd, &item); + } + type = 0; + for (i = 0; ret == 0 && i < sizeof(mbedExts) / sizeof(mbedExts[0]); + i++) { + if (MbedOidIs(&oid, mbedExts[i].oid, mbedExts[i].len)) { + type = mbedExts[i].type; + } + } + if ((crt->ext_types & type) != 0) { + ret = MBEDTLS_ERR_X509_INVALID_EXTENSIONS; + } + else if (type == MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) { + ret = MbedSeqParse(der, idx, extEnd, &crt->subject_alt_names, 0); + } + else if (type == MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) { + ret = MbedSeqParse(der, idx, extEnd, &crt->ext_key_usage, 0); + } + else if (type == MBEDTLS_X509_EXT_CERTIFICATE_POLICIES) { + ret = MbedSeqParse(der, idx, extEnd, &crt->certificate_policies, + 1); + } + else if (type == MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER) { + ret = MbedDerItem(der, &idx, extEnd, &crt->subject_key_id); + } + else if (type == MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER) { + ret = MbedAuthKeyIdParse(der, idx, extEnd, + &crt->authority_key_id); + } + crt->ext_types |= type; + idx = extEnd; + } + return ret; +} + +/* The n digits at *p as a number, or -1. *p moves past them. */ +static int MbedDigits(const unsigned char** p, size_t n) +{ + int value = 0; + + for (; n > 0; n--, (*p)++) { + if (**p < '0' || **p > '9') { + return -1; + } + value = value * 10 + (**p - '0'); + } + return value; +} + +/* UTCTime or GeneralizedTime in seconds, as Mbed TLS reads them */ +static int MbedTimeParse(const mbedtls_x509_buf* in, mbedtls_x509_time* t) +{ + const unsigned char* p = in->p; + size_t yearLen = (in->tag == ASN_UTC_TIME) ? 2 : 4; + + /* As Mbed TLS: Z is optional */ + if ((in->tag != ASN_UTC_TIME && in->tag != ASN_GENERALIZED_TIME) || + (in->len != yearLen + 10 && + (in->len != yearLen + 11 || in->p[in->len - 1] != 'Z'))) { + return MBEDTLS_ERR_X509_INVALID_DATE; + } + t->year = MbedDigits(&p, yearLen); + t->mon = MbedDigits(&p, 2); + t->day = MbedDigits(&p, 2); + t->hour = MbedDigits(&p, 2); + t->min = MbedDigits(&p, 2); + t->sec = MbedDigits(&p, 2); + if (t->year < 0 || t->mon < 0 || t->day < 0 || t->hour < 0 || + t->min < 0 || t->sec < 0) { + return MBEDTLS_ERR_X509_INVALID_DATE; + } + if (yearLen == 2) { + t->year += (t->year < 50) ? 2000 : 1900; + } + return 0; +} + +/* Validity at *idx */ +static int MbedValidityParse(unsigned char* der, word32* idx, word32 end, + mbedtls_x509_crt* crt) +{ + mbedtls_x509_buf item; + word32 i = *idx; + int ret; + + ret = MbedDerRaw(der, idx, end, &item); + if (ret == 0) { + ret = MbedDerItem(der, &i, end, &item); + } + if (ret == 0) { + ret = MbedDerItem(der, &i, *idx, &item); + } + if (ret == 0) { + ret = MbedTimeParse(&item, &crt->valid_from); + i += (word32)item.len; + } + if (ret == 0) { + ret = MbedDerItem(der, &i, *idx, &item); + } + if (ret == 0) { + ret = MbedTimeParse(&item, &crt->valid_to); + } + return ret; +} + +/* Sets the fields of crt from crt->raw, which cert decoded */ +static int MbedCertFields(mbedtls_x509_crt* crt, const DecodedCert* cert) +{ + unsigned char* der = crt->raw.p; + mbedtls_x509_buf item; + word32 idx = 0; + word32 end = (word32)crt->raw.len; + word32 next; + int ret; + + crt->version = cert->version + 1; + crt->ca_istrue = cert->isCA; + crt->max_pathlen = cert->pathLengthSet ? cert->pathLength + 1 : 0; + crt->key_usage = cert->extKeyUsage; +#ifndef IGNORE_NETSCAPE_CERT_TYPE + crt->ns_cert_type = cert->nsCertType; +#endif /* !IGNORE_NETSCAPE_CERT_TYPE */ + + ret = MbedDerItem(der, &idx, end, &item); + if (ret == 0) { + next = idx; + ret = MbedDerRaw(der, &next, end, &crt->tbs); + end = next; + } + if (ret == 0) { + ret = MbedDerItem(der, &idx, end, &item); + } + if (ret == 0 && idx < end && + der[idx] == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) { + ret = MbedDerRaw(der, &idx, end, &item); + } + if (ret == 0) { + ret = MbedDerItem(der, &idx, end, &crt->serial); + idx += (word32)crt->serial.len; + } + if (ret == 0) { + ret = MbedDerItem(der, &idx, end, &item); + next = idx + (word32)item.len; + } + if (ret == 0) { + ret = MbedDerItem(der, &idx, next, &crt->sig_oid); + idx = next; + } + if (ret == 0) { + ret = MbedNameParse(der, &idx, end, &crt->issuer_raw, &crt->issuer); + } + if (ret == 0) { + ret = MbedValidityParse(der, &idx, end, crt); + } + if (ret == 0) { + ret = MbedNameParse(der, &idx, end, &crt->subject_raw, + &crt->subject); + } + if (ret == 0) { + ret = MbedDerRaw(der, &idx, end, &crt->pk_raw); + } + while (ret == 0 && idx < end) { + ret = MbedDerItem(der, &idx, end, &item); + next = idx + (word32)item.len; + if (ret == 0 && item.tag == (ASN_CONTEXT_SPECIFIC | 1)) { + crt->issuer_id = item; + } + else if (ret == 0 && item.tag == (ASN_CONTEXT_SPECIFIC | 2)) { + crt->subject_id = item; + } + else if (ret == 0 && + item.tag == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 3)) { + crt->v3_ext = item; + ret = MbedExtParse(crt, der, idx, next); + } + idx = next; + } + return ret; +} + +/* Copies buf to crt and sets its fields, freeing them on failure */ +static int MbedCertLoad(mbedtls_x509_crt* crt, const unsigned char* buf, + size_t len) +{ + DecodedCert* cert; + int ret; + + XMEMSET(crt, 0, sizeof(*crt)); + crt->raw.p = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_CERT); + cert = (DecodedCert*)XMALLOC(sizeof(*cert), NULL, DYNAMIC_TYPE_DCERT); + if (crt->raw.p == NULL || cert == NULL) { + ret = MBEDTLS_ERR_X509_ALLOC_FAILED; + } + else { + XMEMCPY(crt->raw.p, buf, len); + crt->raw.len = len; + crt->raw.tag = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; + ret = MbedCertDecode(cert, crt->raw.p, (word32)len); + ret = wolfPSA_MbedRet(ret, MBEDTLS_ERR_X509_INVALID_FORMAT); + if (ret == 0) { + ret = MbedCertFields(crt, cert); + } + wc_FreeDecodedCert(cert); + } + XFREE(cert, NULL, DYNAMIC_TYPE_DCERT); + if (ret != 0) { + MbedCertFreeFields(crt); + XMEMSET(crt, 0, sizeof(*crt)); + } + return ret; } /* The checks common to the parse functions, before any allocation */ @@ -113,7 +561,7 @@ void mbedtls_x509_crt_free(mbedtls_x509_crt* crt) while (cur != NULL) { next = cur->next; - XFREE(cur->raw.p, NULL, DYNAMIC_TYPE_CERT); + MbedCertFreeFields(cur); if (cur != crt) { XFREE(cur, NULL, DYNAMIC_TYPE_X509); } @@ -205,7 +653,7 @@ int mbedtls_x509_crt_parse_der(mbedtls_x509_crt* chain, const unsigned char* buf, size_t buflen) { mbedtls_x509_crt* crt = chain; - unsigned char* copy; + mbedtls_x509_crt loaded; int ret; WOLFSSL_ENTER("mbedtls_x509_crt_parse_der"); @@ -215,18 +663,15 @@ int mbedtls_x509_crt_parse_der(mbedtls_x509_crt* chain, ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; } if (ret == 0) { - ret = MbedCertCheck(buf, &buflen); + ret = MbedCertLen(buf, &buflen); + } + if (ret == 0) { + ret = MbedCertLoad(&loaded, buf, buflen); } if (ret != 0) { return ret; } - copy = (unsigned char*)XMALLOC(buflen, NULL, DYNAMIC_TYPE_CERT); - if (copy == NULL) { - return MBEDTLS_ERR_X509_ALLOC_FAILED; - } - XMEMCPY(copy, buf, buflen); - while (crt->next != NULL) { crt = crt->next; } @@ -235,15 +680,14 @@ int mbedtls_x509_crt_parse_der(mbedtls_x509_crt* chain, crt->next = (mbedtls_x509_crt*)XMALLOC(sizeof(*crt), NULL, DYNAMIC_TYPE_X509); if (crt->next == NULL) { - XFREE(copy, NULL, DYNAMIC_TYPE_CERT); + MbedCertFreeFields(&loaded); return MBEDTLS_ERR_X509_ALLOC_FAILED; } crt = crt->next; - XMEMSET(crt, 0, sizeof(*crt)); } - crt->raw.p = copy; - crt->raw.len = buflen; + /* The lists start in the struct and point only forward, so it copies */ + *crt = loaded; return 0; } @@ -334,6 +778,355 @@ int mbedtls_x509_crt_parse_path(mbedtls_x509_crt* chain, const char* path) } #endif /* MBEDTLS_FS_IO */ +/* Moves p and n past what XSNPRINTF wrote, as MBEDTLS_X509_SAFE_SNPRINTF */ +static int MbedAdvance(int ret, char** p, size_t* n) +{ + if (ret < 0 || (size_t)ret >= *n) { + return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; + } + *p += ret; + *n -= (size_t)ret; + return 0; +} + +/* The attributes Mbed TLS prints by short name */ +static const struct { + const byte* oid; + word32 len; + const char* name; +} mbedAttrs[] = { + { MBED_OID("\x55\x04\x03"), "CN" }, + { MBED_OID("\x55\x04\x06"), "C" }, + { MBED_OID("\x55\x04\x07"), "L" }, + { MBED_OID("\x55\x04\x08"), "ST" }, + { MBED_OID("\x55\x04\x0A"), "O" }, + { MBED_OID("\x55\x04\x0B"), "OU" }, + { MBED_OID("\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01"), "emailAddress" }, + { MBED_OID("\x55\x04\x05"), "serialNumber" }, + { MBED_OID("\x55\x04\x10"), "postalAddress" }, + { MBED_OID("\x55\x04\x11"), "postalCode" }, + { MBED_OID("\x55\x04\x04"), "SN" }, + { MBED_OID("\x55\x04\x2A"), "GN" }, + { MBED_OID("\x55\x04\x2B"), "initials" }, + { MBED_OID("\x55\x04\x2C"), "generationQualifier" }, + { MBED_OID("\x55\x04\x0C"), "title" }, + { MBED_OID("\x55\x04\x2E"), "dnQualifier" }, + { MBED_OID("\x55\x04\x41"), "pseudonym" }, + { MBED_OID("\x09\x92\x26\x89\x93\xF2\x2C\x64\x01\x01"), "uid" }, + { MBED_OID("\x09\x92\x26\x89\x93\xF2\x2C\x64\x01\x19"), "DC" }, + { MBED_OID("\x55\x04\x2D"), "uniqueIdentifier" } +}; + +static const char* MbedAttrName(const mbedtls_x509_buf* oid) +{ + size_t i; + + for (i = 0; i < sizeof(mbedAttrs) / sizeof(mbedAttrs[0]); i++) { + if (MbedOidIs(oid, mbedAttrs[i].oid, mbedAttrs[i].len)) { + return mbedAttrs[i].name; + } + } + return NULL; +} + +/* Appends oid in dotted decimal form. Nothing is appended on error. */ +static int MbedOidPrint(char** p, size_t* n, const mbedtls_x509_buf* oid) +{ + char* q = *p; + size_t m = *n; + word32 value = 0; + word32 arc; + size_t i; + int first = 1; + int ret = 0; + + if (oid->len == 0 || (oid->p[oid->len - 1] & 0x80) != 0) { + return MBEDTLS_ERR_ASN1_INVALID_DATA; + } + for (i = 0; ret == 0 && i < oid->len; i++) { + if ((value == 0 && oid->p[i] == 0x80) || value > (0xFFFFFFFFU >> 7)) { + return MBEDTLS_ERR_ASN1_INVALID_DATA; + } + value = (value << 7) | (oid->p[i] & 0x7F); + if ((oid->p[i] & 0x80) != 0) { + continue; + } + if (first) { + arc = (value < 80) ? value / 40 : 2; + ret = MbedAdvance(XSNPRINTF(q, m, "%u.%u", arc, value - arc * 40), + &q, &m); + first = 0; + } + else { + ret = MbedAdvance(XSNPRINTF(q, m, ".%u", value), &q, &m); + } + value = 0; + } + if (ret == 0) { + *p = q; + *n = m; + } + return ret; +} + +/* Whether RFC 4514 escapes c anywhere in a value */ +static int MbedDnSpecial(byte c) +{ + static const char special[] = ",=+<>;\"\\"; + size_t i; + + for (i = 0; i < sizeof(special) - 1; i++) { + if ((byte)special[i] == c) { + return 1; + } + } + return 0; +} + +/* Room for extra characters after j and a NUL, as Mbed TLS checks */ +#define MBED_DN_ROOM(j, extra, size) ((j) + (extra) + 1 < (size)) + +/* "#" and the hex of the DER, or the text escaped as in RFC 4514 */ +static int MbedDnValue(char* s, size_t size, const mbedtls_x509_name* name, + int hex) +{ + static const char hexDigits[] = "0123456789ABCDEF"; + byte hdr[1 + MAX_LENGTH_SZ]; + word32 hdrSz; + size_t j = 0; + size_t i; + byte c; + + if (hex) { + /* Two characters a byte, so a longer value cannot fit */ + if (name->val.len >= size / 2) { + return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; + } + hdr[0] = (byte)name->val.tag; + hdrSz = 1 + SetLength((word32)name->val.len, hdr + 1); + s[j++] = '#'; + for (i = 0; i < hdrSz + name->val.len; i++) { + if (!MBED_DN_ROOM(j, 1, size)) { + return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; + } + c = (i < hdrSz) ? hdr[i] : name->val.p[i - hdrSz]; + s[j++] = hexDigits[c >> 4]; + s[j++] = hexDigits[c & 0x0F]; + } + } + for (i = 0; !hex && i < name->val.len; i++) { + c = name->val.p[i]; + if (!MBED_DN_ROOM(j, 0, size)) { + return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; + } + if (c == '\0') { + return MBEDTLS_ERR_X509_INVALID_NAME; + } + if (MbedDnSpecial(c) || (i == 0 && (c == '#' || c == ' ')) || + (i == name->val.len - 1 && c == ' ')) { + if (!MBED_DN_ROOM(j, 1, size)) { + return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; + } + s[j++] = '\\'; + s[j++] = (char)c; + } + else if (c < 32 || c >= 127) { + if (!MBED_DN_ROOM(j, 3, size)) { + return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; + } + s[j++] = '\\'; + s[j++] = hexDigits[c >> 4]; + s[j++] = hexDigits[c & 0x0F]; + } + else { + s[j++] = (char)c; + } + } + s[j] = '\0'; + return 0; +} + +int mbedtls_x509_dn_gets(char* buf, size_t size, const mbedtls_x509_name* dn) +{ + char s[MBEDTLS_X509_MAX_DN_NAME_SIZE]; + const mbedtls_x509_name* name; + const char* shortName; + char* p = buf; + size_t n = size; + int merge = 0; + int hex; + int ret = 0; + + if (size > 0) { + buf[0] = '\0'; + } + for (name = dn; ret == 0 && name != NULL; name = name->next) { + if (name->oid.p == NULL) { + continue; + } + if (name != dn) { + ret = MbedAdvance(XSNPRINTF(p, n, merge ? " + " : ", "), &p, &n); + } + hex = name->val.tag != MBEDTLS_ASN1_UTF8_STRING && + name->val.tag != MBEDTLS_ASN1_PRINTABLE_STRING && + name->val.tag != MBEDTLS_ASN1_IA5_STRING; + shortName = MbedAttrName(&name->oid); + if (ret == 0 && shortName != NULL) { + ret = MbedAdvance(XSNPRINTF(p, n, "%s=", shortName), &p, &n); + } + else if (ret == 0) { + ret = MbedOidPrint(&p, &n, &name->oid); + if (ret == 0) { + ret = MbedAdvance(XSNPRINTF(p, n, "="), &p, &n); + hex = 1; + } + else if (ret != MBEDTLS_ERR_X509_BUFFER_TOO_SMALL) { + ret = MbedAdvance(XSNPRINTF(p, n, "\?\?="), &p, &n); + } + } + if (ret == 0) { + ret = MbedDnValue(s, sizeof(s), name, hex); + } + if (ret == 0) { + ret = MbedAdvance(XSNPRINTF(p, n, "%s", s), &p, &n); + } + merge = name->next_merged; + } + return (ret == 0) ? (int)(size - n) : ret; +} + +int mbedtls_x509_serial_gets(char* buf, size_t size, + const mbedtls_x509_buf* serial) +{ + /* As Mbed TLS: up to 32 bytes, else the first 28 and "...." */ + size_t shown = (serial->len <= 32) ? serial->len : 28; + char* p = buf; + size_t n = size; + size_t i; + int ret = 0; + + if (size > 0) { + buf[0] = '\0'; + } + for (i = 0; ret == 0 && i < shown; i++) { + if (i == 0 && shown > 1 && serial->p[0] == 0) { + continue; + } + ret = MbedAdvance(XSNPRINTF(p, n, "%02X%s", serial->p[i], + (i < shown - 1) ? ":" : ""), &p, &n); + } + if (ret == 0 && shown != serial->len) { + ret = MbedAdvance(XSNPRINTF(p, n, "...."), &p, &n); + } + return (ret == 0) ? (int)(size - n) : ret; +} + +#ifndef NO_ASN_TIME +/* Negative, 0 or positive as a is before, equal to or after b */ +static int MbedTimeCmp(const mbedtls_x509_time* a, const mbedtls_x509_time* b) +{ + int diff = (a->year - b->year) * 10000 + (a->mon - b->mon) * 100 + + (a->day - b->day); + + if (diff == 0) { + diff = (a->hour - b->hour) * 10000 + (a->min - b->min) * 100 + + (a->sec - b->sec); + } + return diff; +} + +static int MbedTimeNow(mbedtls_x509_time* now) +{ + time_t t = wc_Time(NULL); + struct tm* tm; + struct tm* tmpTime = NULL; +#if defined(NEED_TMP_TIME) + struct tm tmpTimeStorage; + tmpTime = &tmpTimeStorage; +#endif /* NEED_TMP_TIME */ + (void)tmpTime; + + tm = (t == (time_t)-1) ? NULL : XGMTIME(&t, tmpTime); + if (tm == NULL) { + return MBEDTLS_ERR_X509_FATAL_ERROR; + } + now->year = tm->tm_year + 1900; + now->mon = tm->tm_mon + 1; + now->day = tm->tm_mday; + now->hour = tm->tm_hour; + now->min = tm->tm_min; + now->sec = tm->tm_sec; + return 0; +} +#endif /* !NO_ASN_TIME */ + +/* Without a clock, as Mbed TLS without MBEDTLS_HAVE_TIME_DATE: neither */ +int mbedtls_x509_time_is_past(const mbedtls_x509_time* to) +{ +#ifndef NO_ASN_TIME + mbedtls_x509_time now; + + return MbedTimeNow(&now) != 0 || MbedTimeCmp(to, &now) < 0; +#else + (void)to; + return 0; +#endif /* !NO_ASN_TIME */ +} + +int mbedtls_x509_time_is_future(const mbedtls_x509_time* from) +{ +#ifndef NO_ASN_TIME + mbedtls_x509_time now; + + return MbedTimeNow(&now) != 0 || MbedTimeCmp(from, &now) > 0; +#else + (void)from; + return 0; +#endif /* !NO_ASN_TIME */ +} + +int mbedtls_x509_crt_get_ca_istrue(const mbedtls_x509_crt* crt) +{ + if ((crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) == 0) { + return MBEDTLS_ERR_X509_INVALID_EXTENSIONS; + } + return crt->ca_istrue; +} + +/* encipherOnly and decipherOnly may be set only if asked for */ +int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt* crt, + unsigned int usage) +{ + const unsigned int may = MBEDTLS_X509_KU_ENCIPHER_ONLY | + MBEDTLS_X509_KU_DECIPHER_ONLY; + + if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) { + return 0; + } + if ((crt->key_usage & usage & ~may) != (usage & ~may) || + (crt->key_usage & may & ~usage) != 0) { + return MBEDTLS_ERR_X509_BAD_INPUT_DATA; + } + return 0; +} + +int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt* crt, + const char* usage_oid, size_t usage_len) +{ + const mbedtls_x509_sequence* cur; + + if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) { + return 0; + } + for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) { + if (MbedOidIs(&cur->buf, (const byte*)usage_oid, (word32)usage_len) || + MbedOidIs(&cur->buf, MBED_OID("\x55\x1D\x25\x00"))) { + return 0; + } + } + return MBEDTLS_ERR_X509_BAD_INPUT_DATA; +} + /* Opaque, so applications cannot define their own. C needs a member. */ struct mbedtls_x509_crt_profile { char unused; diff --git a/test/mbedtls_compat/x509_crt_test.c b/test/mbedtls_compat/x509_crt_test.c index f92e891..89b009b 100644 --- a/test/mbedtls_compat/x509_crt_test.c +++ b/test/mbedtls_compat/x509_crt_test.c @@ -29,12 +29,17 @@ #include #include #include +#include #include +#ifdef MBEDTLS_THREADING_C +#include +#endif /* MBEDTLS_THREADING_C */ #include #include #include #include +#include /* Any error code */ #define ERR -1 @@ -54,15 +59,15 @@ static const Case cases[] = { { IN "server1_pathlen_int_max.crt", ERR, 0 }, { "dir3/Readme", ERR, 0 }, { "dir3/test-ca2.crt", 0, 1 }, + { "dir3/test-ca.crt", 0, 1 }, { IN "server7_int-ca.crt", 0, 2 }, { IN "server7_trailing_space.crt", 0, 2 }, { IN "cli-rsa-sha256-badalg.crt.der", ERR, 0 }, /* wolfSSL ignores bytes after an empty basicConstraints. Mbed TLS: ERR */ { IN "server5.basic-constraints-sequence-overflow.badsign.crt", 0, 1 }, { IN "server5.basic-constraints-sequence-overflow.selfsigned.crt", 0, 1 }, - /* Over WOLFSSL_MAX_PATH_LEN, and serial number 0. Mbed TLS: 0, 1 */ + /* Over WOLFSSL_MAX_PATH_LEN. Mbed TLS: 0, 1 */ { IN "server1_pathlen_int_max-1.crt", ERR, 0 }, - { "dir3/test-ca.crt", ERR, 0 }, /* wolfSSL skips spaces in base64. Mbed TLS: 1, 1 and ERR, 0 */ { IN "server7_pem_space.crt", 0, 2 }, { IN "server7_all_space.crt", 0, 2 }, @@ -73,12 +78,10 @@ static const Case cases[] = { { "test-ca2.crt", 0, 1 } }; -/* Each test-ca.crt fails, as in the cases above. Mbed TLS: 0, 1 and 0, 2 and - * 1, 2. */ static const Case dirs[] = { - { "dir1", 1, 0 }, - { "dir2", 1, 1 }, - { "dir3", 2, 1 }, + { "dir1", 0, 1 }, + { "dir2", 0, 2 }, + { "dir3", 1, 2 }, /* Not in Mbed TLS's cases */ { "missing-dir", MBEDTLS_ERR_X509_FILE_IO_ERROR, 0 } }; @@ -345,7 +348,7 @@ typedef int (*ParseBuf)(mbedtls_x509_crt* chain, const unsigned char* buf, /* A DER buffer, as an embedded CA usually is. Mbed TLS ignores data after * the certificate, such as a NUL. */ -static int DerBuffer(void) +static int ParseDerBuffer(void) { static const struct { const char* what; @@ -389,9 +392,499 @@ static int DerBuffer(void) return fails; } +/* Parts of test certificates, with server5.crt's key and a bad signature */ +#define BUILT_START "A003020102020101300A06082A8648CE3D040302" \ + "300D310B300906035504030C024341" +#define BUILT_KEY "3059301306072A8648CE3D020106082A8648CE3D030107034200" \ + "0437CC56D976091E5A723EC7592DFF206EEE7CF9069174D0AD14B5F76822596292" \ + "4EE500D82311FFEA2FD2345D5D16BD8A88C26B770D55CD8A2A0EFA01C8B4EDFF" +#define BUILT_SIG "300A06082A8648CE3D0403020309003006020101020101" + +/* C=NL + O=PolarSSL, CN=Server, and UTCTime and GeneralizedTime without Z */ +static const char mvrdnHex[] = "3081E93081CF" BUILT_START + "301E170C313930323130313434343036180E3230323930323130313434343036" + "302F311C3009060355040613024E4C300F060355040A0C08506F6C617253534C" + "310F300D06035504030C06536572766572" BUILT_KEY BUILT_SIG; + +static int ParseHex(mbedtls_x509_crt* crt, const char* hex) +{ + unsigned char der[512]; + unsigned int b; + size_t n = 0; + + while (n < sizeof(der) && sscanf(hex + 2 * n, "%2x", &b) == 1) { + der[n++] = (unsigned char)b; + } + return mbedtls_x509_crt_parse_der(crt, der, n); +} + +/* From Mbed TLS's x509_cert_info and mbedtls_x509_dn_gets cases */ +typedef struct { + const char* file; /* or a name, with hex */ + int version; + const char* serial; + const char* issuer; + const char* subject; + const char* validity; + int ca; /* mbedtls_x509_crt_get_ca_istrue() */ + const char* hex; /* DER, in place of the file */ +} Fields; + +#define NO_BC MBEDTLS_ERR_X509_INVALID_EXTENSIONS +#define TEST_CA "C=NL, O=PolarSSL, CN=PolarSSL Test CA" +#define SERVER1_DATES "2019-02-10 14:44:06 2029-02-10 14:44:06" +#define JP_NAME "C=JP, ST=Tokyo, " \ + "O=\\C3\\A3\\C2\\83\\C2\\86\\C3\\A3\\C2\\82\\C2\\B9\\C3\\A3\\C2\\83" \ + "\\C2\\88 Ltd, " \ + "CN=\\C3\\A3\\C2\\83\\C2\\86\\C3\\A3\\C2\\82\\C2\\B9\\C3\\A3\\C2\\83" \ + "\\C2\\88 CA" + +static const Fields fields[] = { + { IN "server1.crt", 3, "01", TEST_CA, + "C=NL, O=PolarSSL, CN=PolarSSL Server 1", SERVER1_DATES, 0 }, + { IN "test-ca.crt", 3, "03", TEST_CA, TEST_CA, + "2019-02-10 14:44:00 2029-02-10 14:44:00", 1 }, + { IN "test-ca-any_policy.crt", 3, "00", TEST_CA, TEST_CA, + "2019-03-21 16:40:59 2029-03-21 16:40:59", 1 }, + { "server1-v1.crt", 1, "53:A2:B6:C7:04:CD:4D:8E:BC:80:0B:C1", + "CN=PolarSSL Test CA v1, OU=testing, O=PolarSSL, C=NL", + "CN=server1/int-ca-v1, OU=testing, O=PolarSSL, C=NL", SERVER1_DATES, + NO_BC }, + { IN "bitstring-in-dn.pem", 3, "02", + "CN=Test CA 01, ST=Ecnivorp, C=XX, emailAddress=tca@example.com, " + "O=Test CA Authority", + "C=XX, O=tca, ST=Ecnivorp, OU=TCA, CN=Client, " + "emailAddress=client@example.com, serialNumber=7101012255, " + "uniqueIdentifier=#030B0037313031303132323535", + "2015-03-11 12:06:51 2025-03-08 12:06:51", 0 }, + { IN "non-ascii-string-in-issuer.crt", 3, + "05:E6:53:E7:1B:74:F0:B5:D3:84:6D:0C:6D:DC:FA:3F:A4:5A:2B:E0", + JP_NAME, JP_NAME, "2020-05-20 16:17:23 2020-06-19 16:17:23", 1 }, + { "server1.commas.crt", 3, "01", TEST_CA, + "C=NL, O=PolarSSL\\, Commas, CN=PolarSSL Server 1", SERVER1_DATES, + 0 }, + { "server1.hashsymbol.crt", 3, "01", TEST_CA, + "C=NL, O=\\#PolarSSL, CN=PolarSSL Server 1", SERVER1_DATES, 0 }, + { "server1.spaces.crt", 3, "01", TEST_CA, + "C=NL, O=\\ PolarSSL\\ , CN=PolarSSL Server 1", SERVER1_DATES, 0 }, + { "server1.asciichars.crt", 3, "01", TEST_CA, + "C=NL, O=\\E6\\9E\\81\\E5\\9C\\B0SSL, CN=PolarSSL Server 1", + SERVER1_DATES, 0 }, + { "multi-valued RDN, dates without Z", 3, "01", "CN=CA", + "C=NL + O=PolarSSL, CN=Server", + "2019-02-10 14:44:06 2029-02-10 14:44:06", NO_BC, mvrdnHex } +}; + +/* Returns 1 when got is not want */ +static int ExpectStr(const char* what, const char* got, const char* want) +{ + if (strcmp(got, want) == 0) { + return 0; + } + printf("FAIL: %s: \"%s\", expected \"%s\"\n", what, got, want); + return 1; +} + +static int CheckFields(const Fields* f) +{ + mbedtls_x509_crt crt; + char buf[512]; + size_t hdr; + int fails = 0; + int ret; + + mbedtls_x509_crt_init(&crt); + ret = (f->hex != NULL) ? ParseHex(&crt, f->hex) : + mbedtls_x509_crt_parse_file(&crt, f->file); + if (ret != 0) { + printf("FAIL: %s: parse returned %d\n", f->file, ret); + return 1; + } + fails += Expect("version", crt.version, f->version); + if (mbedtls_x509_serial_gets(buf, sizeof(buf), &crt.serial) < 0) { + buf[0] = '\0'; + } + fails += ExpectStr("serial", buf, f->serial); + if (mbedtls_x509_dn_gets(buf, sizeof(buf), &crt.issuer) < 0) { + buf[0] = '\0'; + } + fails += ExpectStr("issuer", buf, f->issuer); + if (mbedtls_x509_dn_gets(buf, sizeof(buf), &crt.subject) < 0) { + buf[0] = '\0'; + } + fails += ExpectStr("subject", buf, f->subject); + (void)snprintf(buf, sizeof(buf), + "%04d-%02d-%02d %02d:%02d:%02d %04d-%02d-%02d %02d:%02d:%02d", + crt.valid_from.year, crt.valid_from.mon, crt.valid_from.day, + crt.valid_from.hour, crt.valid_from.min, crt.valid_from.sec, + crt.valid_to.year, crt.valid_to.mon, crt.valid_to.day, + crt.valid_to.hour, crt.valid_to.min, crt.valid_to.sec); + fails += ExpectStr("validity", buf, f->validity); + fails += Expect("ca_istrue", mbedtls_x509_crt_get_ca_istrue(&crt), f->ca); + /* The raw spans start with their own header */ + hdr = (crt.raw.p[1] & 0x80) ? 2 + (crt.raw.p[1] & 0x7F) : 2; + if (crt.tbs.p != crt.raw.p + hdr || crt.issuer_raw.p[0] != 0x30 || + crt.subject_raw.p[0] != 0x30 || crt.pk_raw.p[0] != 0x30) { + printf("FAIL: %s: raw spans\n", f->file); + fails++; + } + mbedtls_x509_crt_free(&crt); + printf("%s: %s fields\n", fails == 0 ? "PASS" : "FAIL", f->file); + return fails; +} + +/* From Mbed TLS's x509_check_key_usage and _extended_key_usage cases */ +static int Usage(void) +{ + static const struct { + const char* file; + unsigned int usage; + int ret; + } ku[] = { + { "server1.crt", MBEDTLS_X509_KU_KEY_CERT_SIGN, 0 }, + { "server1.key_usage.crt", 0, 0 }, + { "server1.key_usage.crt", MBEDTLS_X509_KU_DIGITAL_SIGNATURE | + MBEDTLS_X509_KU_KEY_ENCIPHERMENT, 0 }, + { "server1.key_usage.crt", MBEDTLS_X509_KU_KEY_CERT_SIGN, + MBEDTLS_ERR_X509_BAD_INPUT_DATA }, + { "server1.key_usage.crt", MBEDTLS_X509_KU_DIGITAL_SIGNATURE | + MBEDTLS_X509_KU_KEY_ENCIPHERMENT | + MBEDTLS_X509_KU_DECIPHER_ONLY, 0 }, + { "keyUsage.decipherOnly.crt", MBEDTLS_X509_KU_DIGITAL_SIGNATURE | + MBEDTLS_X509_KU_KEY_ENCIPHERMENT, + MBEDTLS_ERR_X509_BAD_INPUT_DATA }, + { "keyUsage.decipherOnly.crt", MBEDTLS_X509_KU_DIGITAL_SIGNATURE | + MBEDTLS_X509_KU_KEY_ENCIPHERMENT | + MBEDTLS_X509_KU_DECIPHER_ONLY, 0 } + }; + static const struct { + const char* file; + const char* oid; + int ret; + } eku[] = { + { "server5.crt", "\x2b\x06\x01\x05\x05\x07\x03\x01", 0 }, + { "server5.eku-cli.crt", "\x2b\x06\x01\x05\x05\x07\x03\x01", + MBEDTLS_ERR_X509_BAD_INPUT_DATA }, + { "server5.eku-srv_cli.crt", "\x2b\x06\x01\x05\x05\x07\x03\x02", 0 }, + { "server5.eku-srv_cli.crt", "\x2b\x06\x01\x05\x05\x07\x03\x03", + MBEDTLS_ERR_X509_BAD_INPUT_DATA }, + { "server5.eku-cs_any.crt", "\x2b\x06\x01\x05\x05\x07\x03\xff", 0 } + }; + mbedtls_x509_crt crt; + size_t i; + int fails = 0; + int ret; + + for (i = 0; i < sizeof(ku) / sizeof(ku[0]); i++) { + mbedtls_x509_crt_init(&crt); + ret = mbedtls_x509_crt_parse_file(&crt, ku[i].file); + if (ret == 0) { + ret = mbedtls_x509_crt_check_key_usage(&crt, ku[i].usage); + } + mbedtls_x509_crt_free(&crt); + fails += Expect(ku[i].file, ret, ku[i].ret); + } + for (i = 0; i < sizeof(eku) / sizeof(eku[0]); i++) { + mbedtls_x509_crt_init(&crt); + ret = mbedtls_x509_crt_parse_file(&crt, eku[i].file); + if (ret == 0) { + ret = mbedtls_x509_crt_check_extended_key_usage(&crt, + eku[i].oid, strlen(eku[i].oid)); + } + mbedtls_x509_crt_free(&crt); + fails += Expect(eku[i].file, ret, eku[i].ret); + } + return fails; +} + +/* Up to the extensions, with a subject of CN=Server */ +#define REPEATED_START "3081FA3081E0" BUILT_START \ + "301E170D3139303231303134343430365A170D3239303231303134343430365A" \ + "3011310F300D06035504030C06536572766572" BUILT_KEY "A32D302B" + +/* Key identifiers, subject alternative names and policies */ +static int Extensions(void) +{ + static const unsigned char keyId[] = { + 0xA5, 0x05, 0xE8, 0x64, 0xB8, 0xDC, 0xDF, 0x60, 0x0F, 0x50, + 0x12, 0x4D, 0x60, 0xA8, 0x64, 0xAF, 0x4D, 0x8B, 0x43, 0x93 + }; + /* 1.2.3.4, then anyPolicy */ + static const mbedtls_x509_buf policies[] = { + { MBEDTLS_ASN1_OID, 3, (unsigned char*)"\x2A\x03\x04" }, + { MBEDTLS_ASN1_OID, 4, (unsigned char*)"\x55\x1D\x20\x00" } + }; + /* certificatePolicies twice: the second list shorter, then longer */ + static const char* const repeated[] = { + REPEATED_START "30170603551D200410300E300506032A0304300506032A0305" + "30100603551D2004093007300506032A0306" BUILT_SIG, + REPEATED_START "30100603551D2004093007300506032A0304" + "30170603551D200410300E300506032A0305300506032A0306" BUILT_SIG + }; + mbedtls_x509_crt crt; + const mbedtls_x509_sequence* cur; + const mbedtls_x509_authority* akid = &crt.authority_key_id; + size_t i; + int fails = 0; + int n = 0; + + mbedtls_x509_crt_init(&crt); + if (mbedtls_x509_crt_parse_file(&crt, + "authorityKeyId_subjectKeyId.crt.der") != 0 || + crt.subject_key_id.len != sizeof(keyId) || + memcmp(crt.subject_key_id.p, keyId, sizeof(keyId)) != 0 || + akid->keyIdentifier.len != sizeof(keyId) || + memcmp(akid->keyIdentifier.p, keyId, sizeof(keyId)) != 0 || + akid->authorityCertIssuer.buf.tag != 0xA4 || + akid->authorityCertSerialNumber.len != 20) { + printf("FAIL: key identifiers\n"); + fails++; + } + mbedtls_x509_crt_free(&crt); + + mbedtls_x509_crt_init(&crt); + if (mbedtls_x509_crt_parse_file(&crt, IN "multiple_san.crt") == 0) { + for (cur = &crt.subject_alt_names; cur != NULL; cur = cur->next) { + n++; + } + } + if (!mbedtls_x509_crt_has_ext_type(&crt, + MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) || + mbedtls_x509_crt_has_ext_type(&crt, MBEDTLS_X509_EXT_KEY_USAGE)) { + n = -1; + } + mbedtls_x509_crt_free(&crt); + fails += Expect("subject alt names", n, 4); + + cur = NULL; + n = 0; + mbedtls_x509_crt_init(&crt); + if (mbedtls_x509_crt_parse_file(&crt, IN "test-ca-multi_policy.crt") == 0) { + cur = &crt.certificate_policies; + } + for (; cur != NULL && n < 2; cur = cur->next, n++) { + if (cur->buf.tag != policies[n].tag || + cur->buf.len != policies[n].len || + memcmp(cur->buf.p, policies[n].p, policies[n].len) != 0) { + break; + } + } + if (cur != NULL || !mbedtls_x509_crt_has_ext_type(&crt, + MBEDTLS_X509_EXT_CERTIFICATE_POLICIES)) { + n = -1; + } + mbedtls_x509_crt_free(&crt); + fails += Expect("certificate policies", n, 2); + + for (i = 0; i < sizeof(repeated) / sizeof(repeated[0]); i++) { + mbedtls_x509_crt_init(&crt); + fails += Expect("repeated extension", ParseHex(&crt, repeated[i]), + MBEDTLS_ERR_X509_INVALID_EXTENSIONS); + mbedtls_x509_crt_free(&crt); + } + return fails; +} + +/* A multi-valued RDN, C + O, then CN, and short buffers */ +static int Names(void) +{ + static unsigned char serial[33]; + mbedtls_x509_buf sn = { MBEDTLS_ASN1_INTEGER, sizeof(serial), serial }; + mbedtls_x509_crt crt; + mbedtls_x509_name empty; + mbedtls_x509_name* o; + mbedtls_x509_name* cn; + char buf[128]; + int fails = 0; + int i; + + mbedtls_x509_crt_init(&crt); + if (ParseHex(&crt, mvrdnHex) != 0 || crt.subject.next == NULL || + crt.subject.next->next == NULL) { + printf("FAIL: multi-valued RDN: parse\n"); + fails++; + } + else { + o = crt.subject.next; + cn = o->next; + fails += Expect("C, merged", crt.subject.next_merged, 1); + fails += Expect("O, not merged", o->next_merged, 0); + fails += Expect("dn_get_next", + mbedtls_x509_dn_get_next(&crt.subject) == cn, 1); + fails += Expect("dn_gets, short buffer", + mbedtls_x509_dn_gets(buf, 10, &crt.subject), + MBEDTLS_ERR_X509_BUFFER_TOO_SMALL); + o->val.tag = MBEDTLS_ASN1_OCTET_STRING; + cn->oid.len = 2; + if (mbedtls_x509_dn_gets(buf, sizeof(buf), &crt.subject) < 0) { + buf[0] = '\0'; + } + fails += ExpectStr("hex value, numeric OID", buf, + "C=NL + O=#0408506F6C617253534C, 2.5.4=#0C06536572766572"); + } + mbedtls_x509_crt_free(&crt); + + /* As Mbed TLS, over 32 bytes shows 28 and "....". Zero skipped. */ + for (i = 0; i < (int)sizeof(serial); i++) { + serial[i] = (unsigned char)i; + } + if (mbedtls_x509_serial_gets(buf, sizeof(buf), &sn) < 0) { + buf[0] = '\0'; + } + fails += ExpectStr("long serial", buf, + "01:02:03:04:05:06:07:08:09:0A:0B:0C:0D:0E:0F:10:11:12:13:14:15:16:" + "17:18:19:1A:1B...."); + fails += Expect("serial, short buffer", + mbedtls_x509_serial_gets(buf, 4, &sn), + MBEDTLS_ERR_X509_BUFFER_TOO_SMALL); + + memset(&empty, 0, sizeof(empty)); + strcpy(buf, "old"); + fails += Expect("empty name", + mbedtls_x509_dn_gets(buf, sizeof(buf), &empty), 0); + fails += ExpectStr("empty name", buf, ""); + sn.len = 0; + strcpy(buf, "old"); + fails += Expect("empty serial", + mbedtls_x509_serial_gets(buf, sizeof(buf), &sn), 0); + fails += ExpectStr("empty serial", buf, ""); + return fails; +} + +#ifndef NO_ASN_TIME +/* A clock that fails, as time() can */ +static time_t NoClock(time_t* t) +{ + if (t != NULL) { + *t = (time_t)-1; + } + return (time_t)-1; +} +#endif /* !NO_ASN_TIME */ + +/* From Mbed TLS's mbedtls_x509_time_is_past and _is_future cases */ +static int Times(void) +{ +#ifndef NO_ASN_TIME + const mbedtls_x509_time y1950 = { 1950, 1, 1, 0, 0, 0 }; + const mbedtls_x509_time y2049 = { 2049, 12, 31, 23, 59, 59 }; +#endif /* !NO_ASN_TIME */ + mbedtls_x509_crt crt; + int fails = 0; + + mbedtls_x509_crt_init(&crt); + if (mbedtls_x509_crt_parse_file(&crt, "server5-future.crt") == 0) { + fails += Expect("future, valid_from", + mbedtls_x509_time_is_future(&crt.valid_from), 1); + fails += Expect("past, valid_to", + mbedtls_x509_time_is_past(&crt.valid_to), 0); + } + else { + fails++; + } + mbedtls_x509_crt_free(&crt); + mbedtls_x509_crt_init(&crt); + if (mbedtls_x509_crt_parse_file(&crt, "server5.crt") == 0) { + fails += Expect("past, valid_from", + mbedtls_x509_time_is_past(&crt.valid_from), 1); + fails += Expect("future, valid_from", + mbedtls_x509_time_is_future(&crt.valid_from), 0); + } + else { + fails++; + } + mbedtls_x509_crt_free(&crt); +#ifndef NO_ASN_TIME + /* As Mbed TLS, both are true when the clock fails */ + (void)wc_SetTimeCb(NoClock); + fails += Expect("past, clock failed", + mbedtls_x509_time_is_past(&y2049), 1); + fails += Expect("future, clock failed", + mbedtls_x509_time_is_future(&y1950), 1); + (void)wc_SetTimeCb(NULL); +#endif /* !NO_ASN_TIME */ + return fails; +} + +#ifdef MBEDTLS_THREADING_C +#define RACE_LOOPS 100000 + +/* gmtime() of 1970 over and over, against the clock reads */ +static void* GmtimeLoop(void* arg) +{ + const time_t epoch = 0; + int i; + + (void)arg; + for (i = 0; i < RACE_LOOPS; i++) { + (void)gmtime(&epoch); + } + return NULL; +} + +static int TimeThreads(void) +{ + const mbedtls_x509_time y2000 = { 2000, 1, 1, 0, 0, 0 }; + pthread_t thread; + int past = 1; + int i; + + if (pthread_create(&thread, NULL, GmtimeLoop, NULL) != 0) { + printf("FAIL: pthread_create\n"); + return 1; + } + for (i = 0; i < RACE_LOOPS && past == 1; i++) { + past = mbedtls_x509_time_is_past(&y2000); + } + (void)pthread_join(thread, NULL); + return Expect("past, with gmtime() in another thread", past, 1); +} +#endif /* MBEDTLS_THREADING_C */ + +static int allocs; +static int failAt; + +static void* FailAtMalloc(size_t size) +{ + return (++allocs == failAt) ? NULL : malloc(size); +} + +/* Each allocation in turn fails, leaving the chain as it was */ +static int FailEachAlloc(void) +{ + mbedtls_x509_crt crt; + wolfSSL_Malloc_cb mf; + wolfSSL_Free_cb ff; + wolfSSL_Realloc_cb rf; + int fails = 0; + int ret; + + mbedtls_x509_crt_init(&crt); + if (mbedtls_x509_crt_parse_file(&crt, "test-ca.der") != 0) { + printf("FAIL: reading test-ca.der\n"); + return 1; + } + (void)wolfSSL_GetAllocators(&mf, &ff, &rf); + (void)wolfSSL_SetAllocators(FailAtMalloc, ff, rf); + for (failAt = 1, ret = -1; ret != 0 && failAt < 100; failAt++) { + allocs = 0; + ret = mbedtls_x509_crt_parse_file(&crt, IN "bitstring-in-dn.pem"); + if (ret != 0 && (ret != MBEDTLS_ERR_X509_ALLOC_FAILED || + CountCerts(&crt) != 1)) { + printf("FAIL: allocation %d failed: returned %d\n", failAt, ret); + fails++; + } + } + (void)wolfSSL_SetAllocators(mf, ff, rf); + fails += Expect("each allocation fails", CountCerts(&crt), 2); + mbedtls_x509_crt_free(&crt); + return fails; +} + int main(void) { mbedtls_x509_crt all; + size_t i; int total = 0; int fails = 0; int certs; @@ -416,7 +909,18 @@ int main(void) fails += BadInput(); fails += TempDirs(); fails += NoMemory(); - fails += DerBuffer(); + fails += ParseDerBuffer(); + for (i = 0; i < sizeof(fields) / sizeof(fields[0]); i++) { + fails += CheckFields(&fields[i]); + } + fails += Usage(); + fails += Extensions(); + fails += Names(); + fails += Times(); +#ifdef MBEDTLS_THREADING_C + fails += TimeThreads(); +#endif /* MBEDTLS_THREADING_C */ + fails += FailEachAlloc(); certs = CountCerts(&all); if (certs == total) { printf("PASS: one chain\n"); diff --git a/user_settings.h b/user_settings.h index a6c89d7..1810950 100644 --- a/user_settings.h +++ b/user_settings.h @@ -44,6 +44,11 @@ #ifndef WOLFPSA_THREAD_SAFE #define SINGLE_THREADED #endif /* WOLFPSA_THREAD_SAFE */ +#if defined(WOLFPSA_THREAD_SAFE) && !defined(_WIN32) && \ + !defined(HAVE_GMTIME_R) +/* Reentrant XGMTIME. On Windows, gmtime() is already per thread. */ +#define HAVE_GMTIME_R +#endif /* WOLFPSA_THREAD_SAFE && !_WIN32 && !HAVE_GMTIME_R */ #define WOLFSSL_PSA_ENGINE #define WOLFSSL_SP_MATH_ALL #define WOLFSSL_HAVE_SP_RSA @@ -142,6 +147,8 @@ #ifdef WOLFPSA_MBEDTLS_COMPAT /* Mbed TLS accepts NULL parameters after an ECDSA signature OID */ #define WOLFSSL_ECC_SIGALG_PARAMS_NULL_ALLOWED +/* Mbed TLS accepts serial number 0 */ +#define WOLFSSL_ASN_ALLOW_0_SERIAL /* wolfSSL TLS, with configure's TLS defaults. NO_DH: wolfPSA builds no * dh.c. */ #define HAVE_TLS_EXTENSIONS diff --git a/wolfpsa/mbedtls_compat/mbedtls/asn1.h b/wolfpsa/mbedtls_compat/mbedtls/asn1.h index b7aa67d..c88d101 100644 --- a/wolfpsa/mbedtls_compat/mbedtls/asn1.h +++ b/wolfpsa/mbedtls_compat/mbedtls/asn1.h @@ -26,10 +26,54 @@ #include +#define MBEDTLS_ERR_ASN1_OUT_OF_DATA -0x0060 +#define MBEDTLS_ERR_ASN1_UNEXPECTED_TAG -0x0062 +#define MBEDTLS_ERR_ASN1_INVALID_LENGTH -0x0064 +#define MBEDTLS_ERR_ASN1_LENGTH_MISMATCH -0x0066 +#define MBEDTLS_ERR_ASN1_INVALID_DATA -0x0068 + +#define MBEDTLS_ASN1_BOOLEAN 0x01 +#define MBEDTLS_ASN1_INTEGER 0x02 +#define MBEDTLS_ASN1_BIT_STRING 0x03 +#define MBEDTLS_ASN1_OCTET_STRING 0x04 +#define MBEDTLS_ASN1_NULL 0x05 +#define MBEDTLS_ASN1_OID 0x06 +#define MBEDTLS_ASN1_ENUMERATED 0x0A +#define MBEDTLS_ASN1_UTF8_STRING 0x0C +#define MBEDTLS_ASN1_SEQUENCE 0x10 +#define MBEDTLS_ASN1_SET 0x11 +#define MBEDTLS_ASN1_PRINTABLE_STRING 0x13 +#define MBEDTLS_ASN1_T61_STRING 0x14 +#define MBEDTLS_ASN1_IA5_STRING 0x16 +#define MBEDTLS_ASN1_UTC_TIME 0x17 +#define MBEDTLS_ASN1_GENERALIZED_TIME 0x18 +#define MBEDTLS_ASN1_UNIVERSAL_STRING 0x1C +#define MBEDTLS_ASN1_BMP_STRING 0x1E +#define MBEDTLS_ASN1_PRIMITIVE 0x00 +#define MBEDTLS_ASN1_CONSTRUCTED 0x20 +#define MBEDTLS_ASN1_CONTEXT_SPECIFIC 0x80 + +#define MBEDTLS_ASN1_TAG_CLASS_MASK 0xC0 +#define MBEDTLS_ASN1_TAG_PC_MASK 0x20 +#define MBEDTLS_ASN1_TAG_VALUE_MASK 0x1F + typedef struct mbedtls_asn1_buf { int tag; size_t len; unsigned char* p; } mbedtls_asn1_buf; +typedef struct mbedtls_asn1_sequence { + mbedtls_asn1_buf buf; + struct mbedtls_asn1_sequence* next; +} mbedtls_asn1_sequence; + +/* next_merged is set when next is in the same multi-valued RDN */ +typedef struct mbedtls_asn1_named_data { + mbedtls_asn1_buf oid; + mbedtls_asn1_buf val; + struct mbedtls_asn1_named_data* next; + unsigned char next_merged; +} mbedtls_asn1_named_data; + #endif /* MBEDTLS_ASN1_H */ diff --git a/wolfpsa/mbedtls_compat/mbedtls/x509.h b/wolfpsa/mbedtls_compat/mbedtls/x509.h index 798d330..41c5f04 100644 --- a/wolfpsa/mbedtls_compat/mbedtls/x509.h +++ b/wolfpsa/mbedtls_compat/mbedtls/x509.h @@ -49,12 +49,104 @@ typedef mbedtls_asn1_buf mbedtls_x509_buf; #define MBEDTLS_X509_BADCRL_BAD_PK 0x040000 #define MBEDTLS_X509_BADCRL_BAD_KEY 0x080000 +#define MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE -0x2080 +#define MBEDTLS_ERR_X509_UNKNOWN_OID -0x2100 #define MBEDTLS_ERR_X509_INVALID_FORMAT -0x2180 +#define MBEDTLS_ERR_X509_INVALID_VERSION -0x2200 +#define MBEDTLS_ERR_X509_INVALID_SERIAL -0x2280 +#define MBEDTLS_ERR_X509_INVALID_ALG -0x2300 +#define MBEDTLS_ERR_X509_INVALID_NAME -0x2380 +#define MBEDTLS_ERR_X509_INVALID_DATE -0x2400 +#define MBEDTLS_ERR_X509_INVALID_SIGNATURE -0x2480 +#define MBEDTLS_ERR_X509_INVALID_EXTENSIONS -0x2500 +#define MBEDTLS_ERR_X509_UNKNOWN_VERSION -0x2580 +#define MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG -0x2600 +#define MBEDTLS_ERR_X509_SIG_MISMATCH -0x2680 #define MBEDTLS_ERR_X509_CERT_VERIFY_FAILED -0x2700 +#define MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT -0x2780 #define MBEDTLS_ERR_X509_BAD_INPUT_DATA -0x2800 #define MBEDTLS_ERR_X509_FILE_IO_ERROR -0x2900 #define MBEDTLS_ERR_X509_FATAL_ERROR -0x3000 #define MBEDTLS_ERR_X509_ALLOC_FAILED PSA_ERROR_INSUFFICIENT_MEMORY #define MBEDTLS_ERR_X509_BUFFER_TOO_SMALL PSA_ERROR_BUFFER_TOO_SMALL +#define MBEDTLS_X509_KU_DIGITAL_SIGNATURE 0x80 +#define MBEDTLS_X509_KU_NON_REPUDIATION 0x40 +#define MBEDTLS_X509_KU_KEY_ENCIPHERMENT 0x20 +#define MBEDTLS_X509_KU_DATA_ENCIPHERMENT 0x10 +#define MBEDTLS_X509_KU_KEY_AGREEMENT 0x08 +#define MBEDTLS_X509_KU_KEY_CERT_SIGN 0x04 +#define MBEDTLS_X509_KU_CRL_SIGN 0x02 +#define MBEDTLS_X509_KU_ENCIPHER_ONLY 0x01 +#define MBEDTLS_X509_KU_DECIPHER_ONLY 0x8000 + +#define MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT 0x80 +#define MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER 0x40 +#define MBEDTLS_X509_NS_CERT_TYPE_EMAIL 0x20 +#define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING 0x10 +#define MBEDTLS_X509_NS_CERT_TYPE_RESERVED 0x08 +#define MBEDTLS_X509_NS_CERT_TYPE_SSL_CA 0x04 +#define MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA 0x02 +#define MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA 0x01 + +#define MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER (1 << 0) +#define MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER (1 << 1) +#define MBEDTLS_X509_EXT_KEY_USAGE (1 << 2) +#define MBEDTLS_X509_EXT_CERTIFICATE_POLICIES (1 << 3) +#define MBEDTLS_X509_EXT_POLICY_MAPPINGS (1 << 4) +#define MBEDTLS_X509_EXT_SUBJECT_ALT_NAME (1 << 5) +#define MBEDTLS_X509_EXT_ISSUER_ALT_NAME (1 << 6) +#define MBEDTLS_X509_EXT_SUBJECT_DIRECTORY_ATTRS (1 << 7) +#define MBEDTLS_X509_EXT_BASIC_CONSTRAINTS (1 << 8) +#define MBEDTLS_X509_EXT_NAME_CONSTRAINTS (1 << 9) +#define MBEDTLS_X509_EXT_POLICY_CONSTRAINTS (1 << 10) +#define MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE (1 << 11) +#define MBEDTLS_X509_EXT_CRL_DISTRIBUTION_POINTS (1 << 12) +#define MBEDTLS_X509_EXT_INIHIBIT_ANYPOLICY (1 << 13) +#define MBEDTLS_X509_EXT_FRESHEST_CRL (1 << 14) +#define MBEDTLS_X509_EXT_NS_CERT_TYPE (1 << 16) + +#define MBEDTLS_X509_MAX_DN_NAME_SIZE 256 + +typedef mbedtls_asn1_named_data mbedtls_x509_name; +typedef mbedtls_asn1_sequence mbedtls_x509_sequence; + +typedef struct mbedtls_x509_authority { + mbedtls_x509_buf keyIdentifier; + mbedtls_x509_sequence authorityCertIssuer; + mbedtls_x509_buf authorityCertSerialNumber; + mbedtls_x509_buf raw; +} mbedtls_x509_authority; + +typedef struct mbedtls_x509_time { + int year, mon, day; + int hour, min, sec; +} mbedtls_x509_time; + +#ifdef __cplusplus + extern "C" { +#endif /* __cplusplus */ + +#if defined(MBEDTLS_X509_CRT_PARSE_C) +int mbedtls_x509_dn_gets(char* buf, size_t size, const mbedtls_x509_name* dn); +int mbedtls_x509_serial_gets(char* buf, size_t size, + const mbedtls_x509_buf* serial); +int mbedtls_x509_time_is_past(const mbedtls_x509_time* to); +int mbedtls_x509_time_is_future(const mbedtls_x509_time* from); +#endif /* MBEDTLS_X509_CRT_PARSE_C */ + +/* The first entry of the next RDN, or NULL */ +static inline mbedtls_x509_name* mbedtls_x509_dn_get_next( + mbedtls_x509_name* dn) +{ + while (dn->next_merged && dn->next != NULL) { + dn = dn->next; + } + return dn->next; +} + +#ifdef __cplusplus + } /* extern "C" */ +#endif /* __cplusplus */ + #endif /* MBEDTLS_X509_H */ diff --git a/wolfpsa/mbedtls_compat/mbedtls/x509_crt.h b/wolfpsa/mbedtls_compat/mbedtls/x509_crt.h index f607ab5..c93e4ec 100644 --- a/wolfpsa/mbedtls_compat/mbedtls/x509_crt.h +++ b/wolfpsa/mbedtls_compat/mbedtls/x509_crt.h @@ -34,9 +34,40 @@ #define MBEDTLS_X509_MAX_FILE_PATH_LEN 512 #endif /* !MBEDTLS_X509_MAX_FILE_PATH_LEN */ -/* Each node contains one certificate, DER */ +/* One certificate per node. The buffers point into raw, the DER. */ typedef struct mbedtls_x509_crt { mbedtls_x509_buf raw; + mbedtls_x509_buf tbs; + + int version; + mbedtls_x509_buf serial; + mbedtls_x509_buf sig_oid; + + mbedtls_x509_buf issuer_raw; + mbedtls_x509_buf subject_raw; + mbedtls_x509_name issuer; + mbedtls_x509_name subject; + + mbedtls_x509_time valid_from; + mbedtls_x509_time valid_to; + + mbedtls_x509_buf pk_raw; + + mbedtls_x509_buf issuer_id; + mbedtls_x509_buf subject_id; + mbedtls_x509_buf v3_ext; + mbedtls_x509_sequence subject_alt_names; + mbedtls_x509_buf subject_key_id; + mbedtls_x509_authority authority_key_id; + mbedtls_x509_sequence certificate_policies; + + int ext_types; + int ca_istrue; + int max_pathlen; + unsigned int key_usage; + mbedtls_x509_sequence ext_key_usage; + unsigned char ns_cert_type; + struct mbedtls_x509_crt* next; } mbedtls_x509_crt; @@ -60,8 +91,19 @@ int mbedtls_x509_crt_parse_der(mbedtls_x509_crt* chain, int mbedtls_x509_crt_parse_file(mbedtls_x509_crt* chain, const char* path); int mbedtls_x509_crt_parse_path(mbedtls_x509_crt* chain, const char* path); #endif /* MBEDTLS_FS_IO */ +int mbedtls_x509_crt_get_ca_istrue(const mbedtls_x509_crt* crt); +int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt* crt, + unsigned int usage); +int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt* crt, + const char* usage_oid, size_t usage_len); #endif /* MBEDTLS_X509_CRT_PARSE_C */ +static inline int mbedtls_x509_crt_has_ext_type(const mbedtls_x509_crt* ctx, + int ext_type) +{ + return (ctx->ext_types & ext_type) != 0; +} + #ifdef __cplusplus } /* extern "C" */ #endif /* __cplusplus */