Skip to content

Commit dcaff9d

Browse files
authored
Merge pull request #7944 from JacobBarthelmeh/pkcs12
add parsing over optional PKCS8 attributes
2 parents 887c5ab + 9a8573a commit dcaff9d

4 files changed

Lines changed: 23 additions & 6 deletions

File tree

certs/ca-key-pkcs8-attribute.der

1.21 KB
Binary file not shown.

certs/include.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ EXTRA_DIST += \
66
certs/ca-cert-chain.der \
77
certs/ca-cert.pem \
88
certs/ca-key.pem \
9+
certs/ca-key-pkcs8-attribute.der \
910
certs/client-cert.pem \
1011
certs/client-keyEnc.pem \
1112
certs/client-key.pem \

tests/api.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74549,13 +74549,15 @@ static int test_wc_GetPkcs8TraditionalOffset(void)
7454974549
int derSz = 0;
7455074550
word32 inOutIdx;
7455174551
const char* path = "./certs/server-keyPkcs8.der";
74552+
const char* pathAttributes = "./certs/ca-key-pkcs8-attribute.der";
7455274553
XFILE file = XBADFILE;
7455374554
byte der[2048];
7455474555

7455574556
ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
7455674557
ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
7455774558
if (file != XBADFILE)
7455874559
XFCLOSE(file);
74560+
file = XBADFILE; /* reset file to avoid warning of use after close */
7455974561

7456074562
/* valid case */
7456174563
inOutIdx = 0;
@@ -74577,6 +74579,16 @@ static int test_wc_GetPkcs8TraditionalOffset(void)
7457774579
inOutIdx = 0;
7457874580
ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
7457974581
WC_NO_ERR_TRACE(ASN_PARSE_E));
74582+
74583+
/* test parsing with attributes */
74584+
ExpectTrue((file = XFOPEN(pathAttributes, "rb")) != XBADFILE);
74585+
ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
74586+
if (file != XBADFILE)
74587+
XFCLOSE(file);
74588+
74589+
inOutIdx = 0;
74590+
ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx,
74591+
(word32)derSz), 0);
7458074592
#endif /* NO_ASN */
7458174593
return EXPECT_RESULT();
7458274594
}

wolfcrypt/src/asn.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6882,8 +6882,9 @@ static const ASNItem pkcs8KeyASN[] = {
68826882
/* PKEY_ALGO_PARAM_SEQ */ { 2, ASN_SEQUENCE, 1, 0, 1 },
68836883
#endif
68846884
/* PKEY_DATA */ { 1, ASN_OCTET_STRING, 0, 0, 0 },
6885-
/* attributes [0] Attributes OPTIONAL */
6886-
/* [[2: publicKey [1] PublicKey OPTIONAL ]] */
6885+
/* OPTIONAL Attributes IMPLICIT [0] */
6886+
{ 1, ASN_CONTEXT_SPECIFIC | 0, 1, 0, 1 },
6887+
/* [[2: publicKey [1] PublicKey OPTIONAL ]] */
68876888
};
68886889
enum {
68896890
PKCS8KEYASN_IDX_SEQ = 0,
@@ -6896,6 +6897,7 @@ enum {
68966897
PKCS8KEYASN_IDX_PKEY_ALGO_PARAM_SEQ,
68976898
#endif
68986899
PKCS8KEYASN_IDX_PKEY_DATA,
6900+
PKCS8KEYASN_IDX_PKEY_ATTRIBUTES,
68996901
WOLF_ENUM_DUMMY_LAST_ELEMENT(PKCS8KEYASN_IDX)
69006902
};
69016903

@@ -7306,7 +7308,9 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
73067308
*outSz = tmpSz + sz;
73077309
return (int)(tmpSz + sz);
73087310
#else
7309-
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);
73107314
int sz = 0;
73117315
int ret = 0;
73127316
word32 keyIdx = 0;
@@ -7327,7 +7331,7 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
73277331
ret = ASN_PARSE_E;
73287332
}
73297333

7330-
CALLOC_ASNSETDATA(dataASN, pkcs8KeyASN_Length, ret, NULL);
7334+
CALLOC_ASNSETDATA(dataASN, pkcs8KeyASN_Length-1, ret, NULL);
73317335

73327336
if (ret == 0) {
73337337
/* Only support default PKCS #8 format - v0. */
@@ -7353,7 +7357,7 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
73537357
SetASN_Buffer(&dataASN[PKCS8KEYASN_IDX_PKEY_DATA], key, keySz);
73547358

73557359
/* Get the size of the DER encoding. */
7356-
ret = SizeASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length, &sz);
7360+
ret = SizeASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length-1, &sz);
73577361
}
73587362
if (ret == 0) {
73597363
/* Always return the calculated size. */
@@ -7366,7 +7370,7 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
73667370
}
73677371
if (ret == 0) {
73687372
/* Encode PKCS #8 key into buffer. */
7369-
SetASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length, out);
7373+
SetASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length-1, out);
73707374
ret = sz;
73717375
}
73727376

0 commit comments

Comments
 (0)