From 2a1165460ec9214e0d2a3a06511c5139b94e4e95 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Wed, 4 Sep 2024 15:11:10 -0600 Subject: [PATCH 1/2] add parsing over optional PKCS8 attributes --- certs/ca-key-pkcs8-attribute.der | Bin 0 -> 1239 bytes certs/include.am | 1 + tests/api.c | 11 +++++++++++ wolfcrypt/src/asn.c | 5 +++-- 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 certs/ca-key-pkcs8-attribute.der diff --git a/certs/ca-key-pkcs8-attribute.der b/certs/ca-key-pkcs8-attribute.der new file mode 100644 index 0000000000000000000000000000000000000000..692a8cccf4d425060acb57059ebcac704e4a8263 GIT binary patch literal 1239 zcmV;|1StD3f&|k70RS)!1_>&LNQUrs4#*Aqyhl|0)hbn0KW{%EflgI zghE@*I3G&#O?43Qwl$yM%6$Wv)2!YDX6`ES*sy(W2UH6Xm?2Go%Mu{x%un7$eaUin zGV02ZyJ=Do123ramBsOkVpbdPXK$nsL6xlvo8zUxu+ngLP%vAl=m;!)?Wdx8jW{VB ztjF#0e3h`3f_+I`IL&t@@4bL^Q)6g009Dm0RTO2 zPGA~@f1QPV;Y$a6;MSQc-kte2G!AzTJ_zbhO5cpzbM=>ze!({Nr!rYTb4^r z%3L^4x8btW$06E05{C!m8{9+e=uZ9&e z2%=gGCSm(<49c81eY_&>>!YX^0Qri59sq=>N5nITPxrK|2@xyRtUM~A^_&vWxIzRo!WoaF+6fdJul zMSxkh(@Eb7?-JGz@VCnOGER)&5EVj;nX)<=0hw_T*hI@hsuAofs~K?GCnKPJ|1k@{#R>?+)v@#oz8 zbMSdX8zG}v1Y=SM$=Bx58(m(O`cnddfdJU;PGn(gkqlu0wGX>QXHy>B2gD?Po@|68 zkqqP#1Qtzrn9P8!7T8dC0}mcz#}U^4D--yFkui1^#Isf+^+{ zxS{8k&?7$ulK}GJO@8gSmTdR%LC-RQPMqs)GW7 zfFaWaibU&Bw|r*5hb!hNPU?f=={4c<5l8=__57bLEymo<_PWKD9CJW4ol)4Uclo(H>%JRu{hK+ohqn{;>Vrj-n7WVT zO3buOsDB8$K}o1dC_P?1!Ok}T%lRzbQ38R10HIf(KDpMfgKdL9kNeQvsLlfkBe9_y zW6H!yRy8ZpE@fbq1yz|5HT~7!M8~Cwa$mB7QK)f6lQz#>A7N1p1%T+U#e2G5BI7)D z!-NG@ZPq$1go%?g%Q)JqF$K-eY%SeY(^m`dr8R1>t}(hpmdXIfOXC?5L(@X~Z|Z=+ z(E@>i0J+2gij-Q&z@?>xVV=|^x$mi+ z$#)CKaArNa$W-6bGvW1r`6pq%S4G{oMHeLXBjh*^ktgosAlMYhlt*_!cSRVU4bFK# zS20!zNoT&4SWouEtKx~{pfFflMNFfe}s BOlJT9 literal 0 HcmV?d00001 diff --git a/certs/include.am b/certs/include.am index dd87e3265..d4417fe8e 100644 --- a/certs/include.am +++ b/certs/include.am @@ -6,6 +6,7 @@ EXTRA_DIST += \ certs/ca-cert-chain.der \ certs/ca-cert.pem \ certs/ca-key.pem \ + certs/ca-key-pkcs8-attribute.der \ certs/client-cert.pem \ certs/client-keyEnc.pem \ certs/client-key.pem \ diff --git a/tests/api.c b/tests/api.c index 65b6d2e7a..b1afc1b0a 100644 --- a/tests/api.c +++ b/tests/api.c @@ -74549,6 +74549,7 @@ static int test_wc_GetPkcs8TraditionalOffset(void) int derSz = 0; word32 inOutIdx; const char* path = "./certs/server-keyPkcs8.der"; + const char* pathAttributes = "./certs/ca-key-pkcs8-attribute.der"; XFILE file = XBADFILE; byte der[2048]; @@ -74577,6 +74578,16 @@ static int test_wc_GetPkcs8TraditionalOffset(void) inOutIdx = 0; ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz), WC_NO_ERR_TRACE(ASN_PARSE_E)); + + /* test parsing with attributes */ + ExpectTrue((file = XFOPEN(pathAttributes, "rb")) != XBADFILE); + ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0); + if (file != XBADFILE) + XFCLOSE(file); + + inOutIdx = 0; + ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, + (word32)derSz), 0); #endif /* NO_ASN */ return EXPECT_RESULT(); } diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index d41f8cbe4..1510dbec8 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -6882,8 +6882,9 @@ static const ASNItem pkcs8KeyASN[] = { /* PKEY_ALGO_PARAM_SEQ */ { 2, ASN_SEQUENCE, 1, 0, 1 }, #endif /* PKEY_DATA */ { 1, ASN_OCTET_STRING, 0, 0, 0 }, - /* attributes [0] Attributes OPTIONAL */ - /* [[2: publicKey [1] PublicKey OPTIONAL ]] */ +/* OPTIONAL Attributes IMPLICIT [0] */ + { 1, ASN_CONTEXT_SPECIFIC | 0, 1, 0, 1 }, +/* [[2: publicKey [1] PublicKey OPTIONAL ]] */ }; enum { PKCS8KEYASN_IDX_SEQ = 0, From 9a8573afc9f2175dff3be100057ed3d3841b59eb Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Wed, 4 Sep 2024 15:48:44 -0600 Subject: [PATCH 2/2] touch up pkcs8 create function and test case warning --- tests/api.c | 1 + wolfcrypt/src/asn.c | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/api.c b/tests/api.c index b1afc1b0a..880e4199e 100644 --- a/tests/api.c +++ b/tests/api.c @@ -74557,6 +74557,7 @@ static int test_wc_GetPkcs8TraditionalOffset(void) ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0); if (file != XBADFILE) XFCLOSE(file); + file = XBADFILE; /* reset file to avoid warning of use after close */ /* valid case */ inOutIdx = 0; diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 1510dbec8..54f257753 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -6897,6 +6897,7 @@ enum { PKCS8KEYASN_IDX_PKEY_ALGO_PARAM_SEQ, #endif PKCS8KEYASN_IDX_PKEY_DATA, + PKCS8KEYASN_IDX_PKEY_ATTRIBUTES, WOLF_ENUM_DUMMY_LAST_ELEMENT(PKCS8KEYASN_IDX) }; @@ -7307,7 +7308,9 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz, *outSz = tmpSz + sz; return (int)(tmpSz + sz); #else - DECL_ASNSETDATA(dataASN, pkcs8KeyASN_Length); + /* pkcs8KeyASN_Length-1, the -1 is because we are not adding the optional + * set of attributes */ + DECL_ASNSETDATA(dataASN, pkcs8KeyASN_Length-1); int sz = 0; int ret = 0; word32 keyIdx = 0; @@ -7328,7 +7331,7 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz, ret = ASN_PARSE_E; } - CALLOC_ASNSETDATA(dataASN, pkcs8KeyASN_Length, ret, NULL); + CALLOC_ASNSETDATA(dataASN, pkcs8KeyASN_Length-1, ret, NULL); if (ret == 0) { /* Only support default PKCS #8 format - v0. */ @@ -7354,7 +7357,7 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz, SetASN_Buffer(&dataASN[PKCS8KEYASN_IDX_PKEY_DATA], key, keySz); /* Get the size of the DER encoding. */ - ret = SizeASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length, &sz); + ret = SizeASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length-1, &sz); } if (ret == 0) { /* Always return the calculated size. */ @@ -7367,7 +7370,7 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz, } if (ret == 0) { /* Encode PKCS #8 key into buffer. */ - SetASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length, out); + SetASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length-1, out); ret = sz; }