Skip to content

Commit 75c3030

Browse files
committed
Add option for absent hash params in PKCS7
1 parent a30d9c9 commit 75c3030

5 files changed

Lines changed: 258 additions & 32 deletions

File tree

tests/api.c

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38789,6 +38789,167 @@ static int test_wc_PKCS7_EncodeSignedData(void)
3878938789
return EXPECT_RESULT();
3879038790
} /* END test_wc_PKCS7_EncodeSignedData */
3879138791

38792+
static int test_wc_PKCS7_EncodeSignedData_absent(void)
38793+
{
38794+
EXPECT_DECLS;
38795+
#if defined(HAVE_PKCS7)
38796+
PKCS7* pkcs7 = NULL;
38797+
WC_RNG rng;
38798+
byte output[FOURK_BUF];
38799+
word32 outputSz = (word32)sizeof(output);
38800+
int withParamsLen = 0;
38801+
int withoutParamsLen = 0;
38802+
byte data[] = "Test data to encode.";
38803+
#ifndef NO_RSA
38804+
#if defined(USE_CERT_BUFFERS_2048)
38805+
byte key[sizeof(client_key_der_2048)];
38806+
byte cert[sizeof(client_cert_der_2048)];
38807+
word32 keySz = (word32)sizeof(key);
38808+
word32 certSz = (word32)sizeof(cert);
38809+
XMEMSET(key, 0, keySz);
38810+
XMEMSET(cert, 0, certSz);
38811+
XMEMCPY(key, client_key_der_2048, keySz);
38812+
XMEMCPY(cert, client_cert_der_2048, certSz);
38813+
#elif defined(USE_CERT_BUFFERS_1024)
38814+
byte key[sizeof_client_key_der_1024];
38815+
byte cert[sizeof(sizeof_client_cert_der_1024)];
38816+
word32 keySz = (word32)sizeof(key);
38817+
word32 certSz = (word32)sizeof(cert);
38818+
XMEMSET(key, 0, keySz);
38819+
XMEMSET(cert, 0, certSz);
38820+
XMEMCPY(key, client_key_der_1024, keySz);
38821+
XMEMCPY(cert, client_cert_der_1024, certSz);
38822+
#else
38823+
unsigned char cert[ONEK_BUF];
38824+
unsigned char key[ONEK_BUF];
38825+
XFILE fp = XBADFILE;
38826+
int certSz;
38827+
int keySz;
38828+
38829+
ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
38830+
XBADFILE);
38831+
ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
38832+
fp), 0);
38833+
if (fp != XBADFILE) {
38834+
XFCLOSE(fp);
38835+
fp = XBADFILE;
38836+
}
38837+
38838+
ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
38839+
XBADFILE);
38840+
ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
38841+
0);
38842+
if (fp != XBADFILE)
38843+
XFCLOSE(fp);
38844+
#endif
38845+
#elif defined(HAVE_ECC)
38846+
#if defined(USE_CERT_BUFFERS_256)
38847+
unsigned char cert[sizeof(cliecc_cert_der_256)];
38848+
unsigned char key[sizeof(ecc_clikey_der_256)];
38849+
int certSz = (int)sizeof(cert);
38850+
int keySz = (int)sizeof(key);
38851+
XMEMSET(cert, 0, certSz);
38852+
XMEMSET(key, 0, keySz);
38853+
XMEMCPY(cert, cliecc_cert_der_256, certSz);
38854+
XMEMCPY(key, ecc_clikey_der_256, keySz);
38855+
#else
38856+
unsigned char cert[ONEK_BUF];
38857+
unsigned char key[ONEK_BUF];
38858+
XFILE fp = XBADFILE;
38859+
int certSz;
38860+
int keySz;
38861+
38862+
ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
38863+
XBADFILE);
38864+
ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
38865+
if (fp != XBADFILE) {
38866+
XFCLOSE(fp);
38867+
fp = XBADFILE;
38868+
}
38869+
38870+
ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
38871+
XBADFILE);
38872+
ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
38873+
if (fp != XBADFILE)
38874+
XFCLOSE(fp);
38875+
#endif
38876+
#endif
38877+
38878+
XMEMSET(&rng, 0, sizeof(WC_RNG));
38879+
38880+
XMEMSET(output, 0, outputSz);
38881+
ExpectIntEQ(wc_InitRng(&rng), 0);
38882+
38883+
/* First generate and verify with NULL params */
38884+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
38885+
ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
38886+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
38887+
38888+
if (pkcs7 != NULL) {
38889+
pkcs7->content = data;
38890+
pkcs7->contentSz = (word32)sizeof(data);
38891+
pkcs7->privateKey = key;
38892+
pkcs7->privateKeySz = (word32)sizeof(key);
38893+
pkcs7->encryptOID = RSAk;
38894+
#ifdef NO_SHA
38895+
pkcs7->hashOID = SHA256h;
38896+
#else
38897+
pkcs7->hashOID = SHAh;
38898+
#endif
38899+
pkcs7->rng = &rng;
38900+
}
38901+
38902+
withParamsLen = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
38903+
ExpectIntGT(withParamsLen, 0);
38904+
wc_PKCS7_Free(pkcs7);
38905+
pkcs7 = NULL;
38906+
38907+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
38908+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
38909+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, withParamsLen), 0);
38910+
wc_PKCS7_Free(pkcs7);
38911+
pkcs7 = NULL;
38912+
38913+
XMEMSET(output, 0, outputSz);
38914+
38915+
/* Now generate again without params */
38916+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
38917+
ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
38918+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
38919+
38920+
if (pkcs7 != NULL) {
38921+
pkcs7->content = data;
38922+
pkcs7->contentSz = (word32)sizeof(data);
38923+
pkcs7->privateKey = key;
38924+
pkcs7->privateKeySz = (word32)sizeof(key);
38925+
pkcs7->encryptOID = RSAk;
38926+
#ifdef NO_SHA
38927+
pkcs7->hashOID = SHA256h;
38928+
#else
38929+
pkcs7->hashOID = SHAh;
38930+
#endif
38931+
pkcs7->rng = &rng;
38932+
pkcs7->hashParamsAbsent = TRUE;
38933+
}
38934+
38935+
withoutParamsLen = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
38936+
ExpectIntGT(withoutParamsLen, 0);
38937+
wc_PKCS7_Free(pkcs7);
38938+
pkcs7 = NULL;
38939+
38940+
ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
38941+
ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
38942+
ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, withoutParamsLen), 0);
38943+
38944+
/* Both are valid PKCS7 with non-zero len, ensure without is shorter */
38945+
ExpectIntLT(withoutParamsLen, withParamsLen);
38946+
38947+
wc_PKCS7_Free(pkcs7);
38948+
DoExpectIntEQ(wc_FreeRng(&rng), 0);
38949+
38950+
#endif
38951+
return EXPECT_RESULT();
38952+
}
3879238953

3879338954
/*
3879438955
* Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
@@ -84809,6 +84970,7 @@ TEST_CASE testCases[] = {
8480984970
TEST_DECL(test_wc_PKCS7_EncodeData),
8481084971
TEST_DECL(test_wc_PKCS7_EncodeSignedData),
8481184972
TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
84973+
TEST_DECL(test_wc_PKCS7_EncodeSignedData_absent),
8481284974
TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
8481384975
TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
8481484976
TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),

wolfcrypt/src/asn.c

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6063,22 +6063,8 @@ enum {
60636063
#define algoIdASN_Length (sizeof(algoIdASN) / sizeof(ASNItem))
60646064
#endif
60656065

6066-
/* Get the OID id/sum from the BER encoding of an algorithm identifier.
6067-
*
6068-
* NULL tag is skipped if present.
6069-
*
6070-
* @param [in] input Buffer holding BER encoded data.
6071-
* @param [in, out] inOutIdx On in, start of algorithm identifier.
6072-
* On out, start of ASN.1 item after algorithm id.
6073-
* @param [out] oid Id of OID in algorithm identifier data.
6074-
* @param [in] oidType Type of OID to expect.
6075-
* @param [in] maxIdx Maximum index of data in buffer.
6076-
* @return 0 on success.
6077-
* @return ASN_PARSE_E when encoding is invalid.
6078-
* @return ASN_UNKNOWN_OID_E when the OID cannot be verified.
6079-
*/
6080-
int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid,
6081-
word32 oidType, word32 maxIdx)
6066+
static int GetAlgoIdImpl(const byte* input, word32* inOutIdx, word32* oid,
6067+
word32 oidType, word32 maxIdx, byte *absentParams)
60826068
{
60836069
#ifndef WOLFSSL_ASN_TEMPLATE
60846070
int length;
@@ -6104,6 +6090,10 @@ int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid,
61046090
ret = GetASNNull(input, &idx, maxIdx);
61056091
if (ret != 0)
61066092
return ret;
6093+
6094+
if (absentParams != NULL) {
6095+
*absentParams = FALSE;
6096+
}
61076097
}
61086098
}
61096099
}
@@ -6128,13 +6118,49 @@ int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid,
61286118
if (ret == 0) {
61296119
/* Return the OID id/sum. */
61306120
*oid = dataASN[ALGOIDASN_IDX_OID].data.oid.sum;
6121+
6122+
if ((absentParams != NULL) &&
6123+
(dataASN[ALGOIDASN_IDX_NULL].tag == ASN_TAG_NULL)) {
6124+
*absentParams = FALSE;
6125+
}
61316126
}
61326127

61336128
FREE_ASNGETDATA(dataASN, NULL);
61346129
return ret;
61356130
#endif /* WOLFSSL_ASN_TEMPLATE */
61366131
}
61376132

6133+
/* Get the OID id/sum from the BER encoding of an algorithm identifier.
6134+
*
6135+
* NULL tag is skipped if present.
6136+
*
6137+
* @param [in] input Buffer holding BER encoded data.
6138+
* @param [in, out] inOutIdx On in, start of algorithm identifier.
6139+
* On out, start of ASN.1 item after algorithm id.
6140+
* @param [out] oid Id of OID in algorithm identifier data.
6141+
* @param [in] oidType Type of OID to expect.
6142+
* @param [in] maxIdx Maximum index of data in buffer.
6143+
* @return 0 on success.
6144+
* @return ASN_PARSE_E when encoding is invalid.
6145+
* @return ASN_UNKNOWN_OID_E when the OID cannot be verified.
6146+
*/
6147+
int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid,
6148+
word32 oidType, word32 maxIdx)
6149+
{
6150+
return GetAlgoIdImpl(input, inOutIdx, oid, oidType, maxIdx, NULL);
6151+
}
6152+
6153+
int GetAlgoIdEx(const byte* input, word32* inOutIdx, word32* oid,
6154+
word32 oidType, word32 maxIdx, byte *absentParams)
6155+
{
6156+
/* Assume absent until proven otherwise */
6157+
if (absentParams != NULL) {
6158+
*absentParams = TRUE;
6159+
}
6160+
6161+
return GetAlgoIdImpl(input, inOutIdx, oid, oidType, maxIdx, absentParams);
6162+
}
6163+
61386164
#ifndef NO_RSA
61396165

61406166
#ifdef WC_RSA_PSS
@@ -16082,7 +16108,7 @@ static WC_INLINE int IsSigAlgoECC(word32 algoOID)
1608216108
* @return Encoded data size on success.
1608316109
* @return 0 when dynamic memory allocation fails.
1608416110
*/
16085-
word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz)
16111+
static word32 SetAlgoIDImpl(int algoOID, byte* output, int type, int curveSz, byte absentParams)
1608616112
{
1608716113
#ifndef WOLFSSL_ASN_TEMPLATE
1608816114
word32 tagSz, idSz, seqSz, algoSz = 0;
@@ -16091,9 +16117,10 @@ word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz)
1609116117
byte seqArray[MAX_SEQ_SZ + 1]; /* add object_id to end */
1609216118
word32 length = 0;
1609316119

16094-
tagSz = (type == oidHashType ||
16120+
tagSz = ((type == oidHashType ||
1609516121
(type == oidSigType && !IsSigAlgoECC((word32)algoOID)) ||
16096-
(type == oidKeyType && algoOID == RSAk)) ? 2U : 0U;
16122+
(type == oidKeyType && algoOID == RSAk)) &&
16123+
(absentParams == FALSE)) ? 2U : 0U;
1609716124
algoName = OidFromId((word32)algoOID, (word32)type, &algoSz);
1609816125
if (algoName == NULL) {
1609916126
WOLFSSL_MSG("Unknown Algorithm");
@@ -16149,6 +16176,10 @@ word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz)
1614916176
/* Don't put out NULL DER item. */
1615016177
dataASN[ALGOIDASN_IDX_NULL].noOut = 1;
1615116178
}
16179+
/* Override for absent (not NULL) params */
16180+
if (TRUE == absentParams) {
16181+
dataASN[ALGOIDASN_IDX_NULL].noOut = 1;
16182+
}
1615216183
if (algoOID == DSAk) {
1615316184
/* Don't include SEQUENCE for DSA keys. */
1615416185
o = 1;
@@ -16191,6 +16222,27 @@ word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz)
1619116222
#endif /* WOLFSSL_ASN_TEMPLATE */
1619216223
}
1619316224

16225+
/* Encode an algorithm identifier.
16226+
*
16227+
* [algoOID, type] is unique.
16228+
*
16229+
* @param [in] algoOID Algorithm identifier.
16230+
* @param [out] output Buffer to hold encoding.
16231+
* @param [in] type Type of OID being encoded.
16232+
* @param [in] curveSz Add extra space for curve data.
16233+
* @return Encoded data size on success.
16234+
* @return 0 when dynamic memory allocation fails.
16235+
*/
16236+
word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz)
16237+
{
16238+
return SetAlgoIDImpl(algoOID, output, type, curveSz, FALSE);
16239+
}
16240+
16241+
word32 SetAlgoIDEx(int algoOID, byte* output, int type, int curveSz, byte absentParams)
16242+
{
16243+
return SetAlgoIDImpl(algoOID, output, type, curveSz, absentParams);
16244+
}
16245+
1619416246
#ifdef WOLFSSL_ASN_TEMPLATE
1619516247
/* Always encode PKCS#1 v1.5 RSA signature and compare to encoded data. */
1619616248
/* ASN.1 template for DigestInfo for a PKCS#1 v1.5 RSA signature.

0 commit comments

Comments
 (0)