Skip to content

Commit 9a8573a

Browse files
touch up pkcs8 create function and test case warning
1 parent 2a11654 commit 9a8573a

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

tests/api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74557,6 +74557,7 @@ static int test_wc_GetPkcs8TraditionalOffset(void)
7455774557
ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
7455874558
if (file != XBADFILE)
7455974559
XFCLOSE(file);
74560+
file = XBADFILE; /* reset file to avoid warning of use after close */
7456074561

7456174562
/* valid case */
7456274563
inOutIdx = 0;

wolfcrypt/src/asn.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6897,6 +6897,7 @@ enum {
68976897
PKCS8KEYASN_IDX_PKEY_ALGO_PARAM_SEQ,
68986898
#endif
68996899
PKCS8KEYASN_IDX_PKEY_DATA,
6900+
PKCS8KEYASN_IDX_PKEY_ATTRIBUTES,
69006901
WOLF_ENUM_DUMMY_LAST_ELEMENT(PKCS8KEYASN_IDX)
69016902
};
69026903

@@ -7307,7 +7308,9 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
73077308
*outSz = tmpSz + sz;
73087309
return (int)(tmpSz + sz);
73097310
#else
7310-
DECL_ASNSETDATA(dataASN, pkcs8KeyASN_Length);
7311+
/* pkcs8KeyASN_Length-1, the -1 is because we are not adding the optional
7312+
* set of attributes */
7313+
DECL_ASNSETDATA(dataASN, pkcs8KeyASN_Length-1);
73117314
int sz = 0;
73127315
int ret = 0;
73137316
word32 keyIdx = 0;
@@ -7328,7 +7331,7 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
73287331
ret = ASN_PARSE_E;
73297332
}
73307333

7331-
CALLOC_ASNSETDATA(dataASN, pkcs8KeyASN_Length, ret, NULL);
7334+
CALLOC_ASNSETDATA(dataASN, pkcs8KeyASN_Length-1, ret, NULL);
73327335

73337336
if (ret == 0) {
73347337
/* Only support default PKCS #8 format - v0. */
@@ -7354,7 +7357,7 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
73547357
SetASN_Buffer(&dataASN[PKCS8KEYASN_IDX_PKEY_DATA], key, keySz);
73557358

73567359
/* Get the size of the DER encoding. */
7357-
ret = SizeASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length, &sz);
7360+
ret = SizeASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length-1, &sz);
73587361
}
73597362
if (ret == 0) {
73607363
/* Always return the calculated size. */
@@ -7367,7 +7370,7 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
73677370
}
73687371
if (ret == 0) {
73697372
/* Encode PKCS #8 key into buffer. */
7370-
SetASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length, out);
7373+
SetASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length-1, out);
73717374
ret = sz;
73727375
}
73737376

0 commit comments

Comments
 (0)