@@ -96,6 +96,9 @@ ASN Options:
9696 cost of taking up more memory. Adds initials, givenname, dnQualifer for
9797 example.
9898 * WC_ASN_HASH_SHA256: Force use of SHA2-256 for the internal hash ID calcs.
99+ * WOLFSSL_ALLOW_ENCODING_CA_FALSE: Allow encoding BasicConstraints CA:FALSE
100+ * which is discouraged by X.690 specification - default values shall not
101+ * be encoded.
99102*/
100103
101104#include <wolfssl/wolfcrypt/error-crypt.h>
@@ -18622,7 +18625,8 @@ static int DecodeBasicCaConstraint(const byte* input, int sz, DecodedCert* cert)
1862218625 if ((ret == 0) && (dataASN[BASICCONSASN_IDX_SEQ].length != 0)) {
1862318626 /* Bad encoding when CA Boolean is false
1862418627 * (default when not present). */
18625- #ifndef ASN_TEMPLATE_SKIP_ISCA_CHECK
18628+ #if !defined(ASN_TEMPLATE_SKIP_ISCA_CHECK) && \
18629+ !defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE)
1862618630 if ((dataASN[BASICCONSASN_IDX_CA].length != 0) && (!isCA)) {
1862718631 WOLFSSL_ERROR_VERBOSE(ASN_PARSE_E);
1862818632 ret = ASN_PARSE_E;
@@ -26055,10 +26059,9 @@ static int SetCaWithPathLen(byte* out, word32 outSz, byte pathLen)
2605526059 return (int)sizeof(caPathLenBasicConstASN1);
2605626060}
2605726061
26058-
26059- /* encode CA basic constraints true
26062+ /* encode CA basic constraints
2606026063 * return total bytes written */
26061- static int SetCa (byte* out, word32 outSz)
26064+ static int SetCaEx (byte* out, word32 outSz, byte isCa )
2606226065{
2606326066 /* ASN1->DER sequence for Basic Constraints True */
2606426067 const byte caBasicConstASN1[] = {
@@ -26074,9 +26077,20 @@ static int SetCa(byte* out, word32 outSz)
2607426077
2607526078 XMEMCPY(out, caBasicConstASN1, sizeof(caBasicConstASN1));
2607626079
26080+ if (!isCa) {
26081+ out[sizeof(caBasicConstASN1)-1] = isCa;
26082+ }
26083+
2607726084 return (int)sizeof(caBasicConstASN1);
2607826085}
2607926086
26087+ /* encode CA basic constraints true
26088+ * return total bytes written */
26089+ static int SetCa(byte* out, word32 outSz)
26090+ {
26091+ return SetCaEx(out, outSz, 1);
26092+ }
26093+
2608026094/* encode basic constraints without CA Boolean
2608126095 * return total bytes written */
2608226096static int SetBC(byte* out, word32 outSz)
@@ -27827,6 +27841,13 @@ static int EncodeExtensions(Cert* cert, byte* output, word32 maxSz,
2782727841 dataASN[CERTEXTSASN_IDX_BC_PATHLEN].noOut = 1;
2782827842 }
2782927843 }
27844+ #ifdef WOLFSSL_ALLOW_ENCODING_CA_FALSE
27845+ else if (cert->isCaSet) {
27846+ SetASN_Boolean(&dataASN[CERTEXTSASN_IDX_BC_CA], 0);
27847+ SetASN_Buffer(&dataASN[CERTEXTSASN_IDX_BC_OID], bcOID, sizeof(bcOID));
27848+ dataASN[CERTEXTSASN_IDX_BC_PATHLEN].noOut = 1;
27849+ }
27850+ #endif
2783027851 else if (cert->basicConstSet) {
2783127852 /* Set Basic Constraints to be a non Certificate Authority. */
2783227853 SetASN_Buffer(&dataASN[CERTEXTSASN_IDX_BC_OID], bcOID, sizeof(bcOID));
@@ -28475,7 +28496,17 @@ static int EncodeCert(Cert* cert, DerCert* der, RsaKey* rsaKey, ecc_key* eccKey,
2847528496
2847628497 der->extensionsSz += der->caSz;
2847728498 }
28499+ #ifdef WOLFSSL_ALLOW_ENCODING_CA_FALSE
2847828500 /* Set CA */
28501+ else if (cert->isCaSet) {
28502+ der->caSz = SetCaEx(der->ca, sizeof(der->ca), cert->isCA);
28503+ if (der->caSz <= 0)
28504+ return EXTENSIONS_E;
28505+
28506+ der->extensionsSz += der->caSz;
28507+ }
28508+ #endif
28509+ /* Set CA true */
2847928510 else if (cert->isCA) {
2848028511 der->caSz = SetCa(der->ca, sizeof(der->ca));
2848128512 if (der->caSz <= 0)
@@ -29873,7 +29904,17 @@ static int EncodeCertReq(Cert* cert, DerCert* der, RsaKey* rsaKey,
2987329904
2987429905 der->extensionsSz += der->caSz;
2987529906 }
29907+ #ifdef WOLFSSL_ALLOW_ENCODING_CA_FALSE
2987629908 /* Set CA */
29909+ else if (cert->isCaSet) {
29910+ der->caSz = SetCaEx(der->ca, sizeof(der->ca), cert->isCA);
29911+ if (der->caSz <= 0)
29912+ return EXTENSIONS_E;
29913+
29914+ der->extensionsSz += der->caSz;
29915+ }
29916+ #endif
29917+ /* Set CA true */
2987729918 else if (cert->isCA) {
2987829919 der->caSz = SetCa(der->ca, sizeof(der->ca));
2987929920 if (der->caSz <= 0)
0 commit comments