Skip to content

Commit 4066e98

Browse files
committed
Validate the hash input of the signature hash APIs
wc_SignatureGenerateHash_ex() signed whatever it was given. For WC_SIGNATURE_TYPE_RSA_W_ENC a bare digest produced PKCS#1 v1.5 padding without a DigestInfo, which no other implementation accepts, and the default self-verify then failed with ASN_PARSE_E after the signature buffer was already written. For ECC and plain RSA a hash of the wrong length for hash_type was signed as-is. wc_SignatureVerifyHash() parsed the DigestInfo but skipped the AlgorithmIdentifier OID and ignored trailing data, so a DigestInfo for another hash, or one followed by extra bytes, passed validation. Both functions now apply the same checks. For RSA_W_ENC, hash_data must be exactly one DER DigestInfo whose AlgorithmIdentifier holds the OID of hash_type with absent or NULL (05 00) parameters, and whose digest has the hash_type size. The parameters are checked directly because the template GetAlgoId() reads an indefinite-length NULL (05 80) as an empty one. For the other types, hash_len must be the hash_type digest size. Without ASN support, the RSA_W_ENC hash APIs return SIG_TYPE_E, as the data APIs already do. Fixes #12231. Fixes F-16574. Fixes F-16575.
1 parent 833186a commit 4066e98

4 files changed

Lines changed: 254 additions & 29 deletions

File tree

‎doc/dox_comments/header_files/signature.h‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,20 @@ int wc_SignatureGenerate(
168168
\brief This function verifies a signature using a pre-computed hash.
169169
Unlike wc_SignatureVerify which hashes the data first, this function
170170
takes the hash directly and verifies the signature against it.
171-
If sig_type is WC_SIGNATURE_TYPE_RSA_W_ENC, hash data must be encoded
172-
with wc_EncodeSignature prior to calling.
171+
If sig_type is WC_SIGNATURE_TYPE_RSA_W_ENC, hash data must be the
172+
DigestInfo for hash_type, as output by wc_EncodeSignature.
173173
174174
\return 0 Success
175175
\return SIG_TYPE_E Signature type not enabled/available
176176
\return BAD_FUNC_ARG Bad function argument provided
177177
\return BUFFER_E Output buffer too small or input too large
178+
\return ASN_PARSE_E RSA_W_ENC hash data is not a DigestInfo or its OID
179+
is invalid
180+
\return BAD_LENGTH_E Hash, digest or DigestInfo length does not match
181+
hash_type
182+
\return ASN_SIG_OID_E DigestInfo OID does not match hash_type
183+
\return HASH_TYPE_E Hash type not available or has no DigestInfo OID
184+
\return MEMORY_E Memory allocation failed
178185
179186
\param hash_type A hash type from enum wc_HashType
180187
\param sig_type A signature type such as WC_SIGNATURE_TYPE_ECC or
@@ -221,13 +228,20 @@ int wc_SignatureVerifyHash(enum wc_HashType hash_type,
221228
\brief This function generates a signature from a pre-computed hash.
222229
Unlike wc_SignatureGenerate which hashes the data first, this
223230
function takes the hash directly and signs it.
224-
If sig_type is WC_SIGNATURE_TYPE_RSA_W_ENC, hash data must be encoded
225-
with wc_EncodeSignature prior to calling.
231+
If sig_type is WC_SIGNATURE_TYPE_RSA_W_ENC, hash data must be the
232+
DigestInfo for hash_type, as output by wc_EncodeSignature.
226233
227234
\return 0 Success
228235
\return SIG_TYPE_E Signature type not enabled/available
229236
\return BAD_FUNC_ARG Bad function argument provided
230237
\return BUFFER_E Output buffer too small or input too large
238+
\return ASN_PARSE_E RSA_W_ENC hash data is not a DigestInfo or its OID
239+
is invalid
240+
\return BAD_LENGTH_E Hash, digest or DigestInfo length does not match
241+
hash_type
242+
\return ASN_SIG_OID_E DigestInfo OID does not match hash_type
243+
\return HASH_TYPE_E Hash type not available or has no DigestInfo OID
244+
\return MEMORY_E Memory allocation failed
231245
232246
\param hash_type A hash type from enum wc_HashType
233247
\param sig_type A signature type such as WC_SIGNATURE_TYPE_ECC or
@@ -276,11 +290,20 @@ int wc_SignatureGenerateHash(enum wc_HashType hash_type,
276290
\brief This function generates a signature from a pre-computed hash
277291
with extended options. This is similar to wc_SignatureGenerateHash
278292
but allows optional verification of the signature after generation.
293+
If sig_type is WC_SIGNATURE_TYPE_RSA_W_ENC, hash data must be the
294+
DigestInfo for hash_type, as output by wc_EncodeSignature.
279295
280296
\return 0 Success
281297
\return SIG_TYPE_E Signature type not enabled/available
282298
\return BAD_FUNC_ARG Bad function argument provided
283299
\return BUFFER_E Output buffer too small or input too large
300+
\return ASN_PARSE_E RSA_W_ENC hash data is not a DigestInfo or its OID
301+
is invalid
302+
\return BAD_LENGTH_E Hash, digest or DigestInfo length does not match
303+
hash_type
304+
\return ASN_SIG_OID_E DigestInfo OID does not match hash_type
305+
\return HASH_TYPE_E Hash type not available or has no DigestInfo OID
306+
\return MEMORY_E Memory allocation failed
284307
285308
\param hash_type A hash type from enum wc_HashType
286309
\param sig_type A signature type such as WC_SIGNATURE_TYPE_ECC or

‎tests/api/test_signature.c‎

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,12 @@ int test_wc_SignatureFeatureCoverage(void)
737737
ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, sig_type,
738738
hash, (word32)sizeof(hash), sig2, sig2_len, &ecc, key_len),
739739
0);
740+
741+
/* Hash length that does not match hash_type */
742+
sig2_len = (word32)sizeof(sig2);
743+
ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_SHA256,
744+
sig_type, hash, (word32)sizeof(hash) - 1, sig2, &sig2_len,
745+
&ecc, key_len, &rng, 0), WC_NO_ERR_TRACE(BAD_LENGTH_E));
740746
}
741747

742748
DoExpectIntEQ(wc_FreeRng(&rng), 0);
@@ -808,6 +814,12 @@ int test_wc_SignatureFeatureCoverage(void)
808814
WC_SIGNATURE_TYPE_RSA, data, data_len, sig, sig_len, &rsa_key,
809815
key_len), 0);
810816

817+
/* Plain RSA hash length that does not match hash_type */
818+
sig_len = (word32)sizeof(sig);
819+
ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_SHA256,
820+
WC_SIGNATURE_TYPE_RSA, data, data_len - 1, sig, &sig_len,
821+
&rsa_key, key_len, &rng, 0), WC_NO_ERR_TRACE(BAD_LENGTH_E));
822+
811823
#ifndef NO_ASN
812824
/* RSA_W_ENC round trip: true branch of
813825
* "hash_enc_len += MAX_DER_DIGEST_ASN_SZ", wc_SignatureDerEncode()'s
@@ -826,6 +838,151 @@ int test_wc_SignatureFeatureCoverage(void)
826838
WC_SIGNATURE_TYPE_RSA_W_ENC, data, data_len, sigEnc,
827839
sigEncLen, &rsa_key, key_len), 0);
828840
}
841+
842+
/* RSA_W_ENC hash APIs take the DigestInfo for hash_type */
843+
{
844+
byte hash[WC_SHA256_DIGEST_SIZE];
845+
byte enc[MAX_DER_DIGEST_SZ + 1];
846+
byte encNoNull[MAX_DER_DIGEST_SZ];
847+
word32 encLen = 0;
848+
byte sigData[512] = {0};
849+
word32 sigDataLen = (word32)sizeof(sigData);
850+
#ifndef NO_SHA
851+
byte encSha1[MAX_DER_DIGEST_SZ];
852+
word32 encSha1Len = 0;
853+
#endif
854+
855+
ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, data, data_len, hash,
856+
(word32)sizeof(hash)), 0);
857+
ExpectIntGT((int)(encLen = wc_EncodeSignature(enc, hash,
858+
(word32)sizeof(hash), wc_HashGetOID(WC_HASH_TYPE_SHA256))),
859+
0);
860+
861+
/* Same signature as the data API */
862+
ExpectIntEQ(wc_SignatureGenerate(WC_HASH_TYPE_SHA256,
863+
WC_SIGNATURE_TYPE_RSA_W_ENC, data, data_len, sigData,
864+
&sigDataLen, &rsa_key, key_len, &rng), 0);
865+
sig_len = (word32)sizeof(sig);
866+
ExpectIntEQ(wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256,
867+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sig, &sig_len,
868+
&rsa_key, key_len, &rng), 0);
869+
ExpectIntEQ(sig_len, sigDataLen);
870+
ExpectBufEQ(sig, sigData, sig_len);
871+
ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256,
872+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sig, sig_len,
873+
&rsa_key, key_len), 0);
874+
875+
/* Bare hash is rejected before signing */
876+
sig_len = (word32)sizeof(sig);
877+
ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_SHA256,
878+
WC_SIGNATURE_TYPE_RSA_W_ENC, hash, (word32)sizeof(hash), sig,
879+
&sig_len, &rsa_key, key_len, &rng, 0),
880+
WC_NO_ERR_TRACE(ASN_PARSE_E));
881+
ExpectIntEQ(sig_len, (word32)sizeof(sig));
882+
ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256,
883+
WC_SIGNATURE_TYPE_RSA_W_ENC, hash, (word32)sizeof(hash),
884+
sigData, sigDataLen, &rsa_key, key_len),
885+
WC_NO_ERR_TRACE(ASN_PARSE_E));
886+
887+
/* Trailing byte after the DigestInfo */
888+
enc[encLen] = 0;
889+
sig_len = (word32)sizeof(sig);
890+
ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_SHA256,
891+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen + 1, sig, &sig_len,
892+
&rsa_key, key_len, &rng, 0), WC_NO_ERR_TRACE(BAD_LENGTH_E));
893+
ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256,
894+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen + 1, sigData,
895+
sigDataLen, &rsa_key, key_len), WC_NO_ERR_TRACE(BAD_LENGTH_E));
896+
897+
/* Layout 30 31 30 0d 06 09 <oid> 05 00 04 20 <digest> */
898+
/* Digest nested inside the AlgorithmIdentifier SEQUENCE */
899+
enc[3] = (byte)(encLen - 4);
900+
sig_len = (word32)sizeof(sig);
901+
ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_SHA256,
902+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sig, &sig_len,
903+
&rsa_key, key_len, &rng, 0), WC_NO_ERR_TRACE(ASN_PARSE_E));
904+
ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256,
905+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sigData, sigDataLen,
906+
&rsa_key, key_len), WC_NO_ERR_TRACE(ASN_PARSE_E));
907+
908+
/* AlgorithmIdentifier parameters other than NULL */
909+
ExpectIntGT((int)(encLen = wc_EncodeSignature(enc, hash,
910+
(word32)sizeof(hash), wc_HashGetOID(WC_HASH_TYPE_SHA256))),
911+
0);
912+
if (EXPECT_SUCCESS())
913+
enc[encLen - sizeof(hash) - 4] = 0x04;
914+
sig_len = (word32)sizeof(sig);
915+
ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_SHA256,
916+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sig, &sig_len,
917+
&rsa_key, key_len, &rng, 0), WC_NO_ERR_TRACE(ASN_PARSE_E));
918+
ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256,
919+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sigData, sigDataLen,
920+
&rsa_key, key_len), WC_NO_ERR_TRACE(ASN_PARSE_E));
921+
922+
/* Indefinite-length NULL parameter (05 80) */
923+
if (EXPECT_SUCCESS()) {
924+
enc[encLen - sizeof(hash) - 4] = ASN_TAG_NULL;
925+
enc[encLen - sizeof(hash) - 3] = 0x80;
926+
}
927+
sig_len = (word32)sizeof(sig);
928+
ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_SHA256,
929+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sig, &sig_len,
930+
&rsa_key, key_len, &rng, 0), WC_NO_ERR_TRACE(ASN_PARSE_E));
931+
ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256,
932+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sigData, sigDataLen,
933+
&rsa_key, key_len), WC_NO_ERR_TRACE(ASN_PARSE_E));
934+
935+
/* DigestInfo without the NULL parameter is accepted */
936+
ExpectIntGT((int)(encLen = wc_EncodeSignature(enc, hash,
937+
(word32)sizeof(hash), wc_HashGetOID(WC_HASH_TYPE_SHA256))),
938+
0);
939+
if (EXPECT_SUCCESS()) {
940+
XMEMCPY(encNoNull, enc, encLen - sizeof(hash) - 4);
941+
XMEMCPY(encNoNull + encLen - sizeof(hash) - 4,
942+
enc + encLen - sizeof(hash) - 2, sizeof(hash) + 2);
943+
encNoNull[1] = (byte)(encLen - 4);
944+
encNoNull[3] = (byte)(enc[3] - 2);
945+
}
946+
sig_len = (word32)sizeof(sig);
947+
ExpectIntEQ(wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256,
948+
WC_SIGNATURE_TYPE_RSA_W_ENC, encNoNull, encLen - 2, sig,
949+
&sig_len, &rsa_key, key_len, &rng), 0);
950+
951+
#ifdef HAVE_BLAKE2B
952+
/* Hash type without a DigestInfo OID */
953+
sig_len = (word32)sizeof(sig);
954+
ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_BLAKE2B,
955+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sig, &sig_len,
956+
&rsa_key, key_len, &rng, 0), WC_NO_ERR_TRACE(HASH_TYPE_E));
957+
ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_BLAKE2B,
958+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sigData, sigDataLen,
959+
&rsa_key, key_len), WC_NO_ERR_TRACE(HASH_TYPE_E));
960+
#endif
961+
962+
#ifndef NO_SHA
963+
/* SHA-256 DigestInfo signed as SHA-1 */
964+
ExpectIntGT((int)(encLen = wc_EncodeSignature(enc, hash,
965+
(word32)sizeof(hash), wc_HashGetOID(WC_HASH_TYPE_SHA256))),
966+
0);
967+
sig_len = (word32)sizeof(sig);
968+
ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_SHA,
969+
WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sig, &sig_len,
970+
&rsa_key, key_len, &rng, 0), WC_NO_ERR_TRACE(BAD_LENGTH_E));
971+
972+
/* SHA-1 OID around a SHA-256 sized digest */
973+
ExpectIntGT((int)(encSha1Len = wc_EncodeSignature(encSha1, hash,
974+
(word32)sizeof(hash), wc_HashGetOID(WC_HASH_TYPE_SHA))), 0);
975+
sig_len = (word32)sizeof(sig);
976+
ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_SHA256,
977+
WC_SIGNATURE_TYPE_RSA_W_ENC, encSha1, encSha1Len, sig,
978+
&sig_len, &rsa_key, key_len, &rng, 0),
979+
WC_NO_ERR_TRACE(ASN_SIG_OID_E));
980+
ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256,
981+
WC_SIGNATURE_TYPE_RSA_W_ENC, encSha1, encSha1Len, sigData,
982+
sigDataLen, &rsa_key, key_len),
983+
WC_NO_ERR_TRACE(ASN_SIG_OID_E));
984+
#endif
985+
}
829986
#endif /* !NO_ASN */
830987

831988
DoExpectIntEQ(wc_FreeRng(&rng), 0);

‎wolfcrypt/src/signature.c‎

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,51 @@ static int wc_SignatureDerEncode(enum wc_HashType hash_type, byte* hash_data,
9292

9393
return ret;
9494
}
95+
96+
/* hash_data must be exactly one DigestInfo for hash_type. */
97+
static int wc_SignatureCheckDigestInfo(enum wc_HashType hash_type,
98+
const byte* hash_data, word32 hash_len, int digest_len)
99+
{
100+
int ret;
101+
int len;
102+
int hash_oid;
103+
word32 idx = 0;
104+
word32 algoIdx;
105+
word32 algoEnd;
106+
word32 oid = 0;
107+
108+
hash_oid = wc_HashGetOID(hash_type);
109+
if (hash_oid < 0)
110+
return hash_oid;
111+
if (GetSequence(hash_data, &idx, &len, hash_len) < 0)
112+
return ASN_PARSE_E;
113+
if (idx + (word32)len != hash_len)
114+
return BAD_LENGTH_E;
115+
algoIdx = idx;
116+
if (GetSequence(hash_data, &algoIdx, &len, hash_len) < 0)
117+
return ASN_PARSE_E;
118+
algoEnd = algoIdx + (word32)len;
119+
if (GetASNObjectId(hash_data, &algoIdx, &len, algoEnd) < 0)
120+
return ASN_PARSE_E;
121+
algoIdx += (word32)len;
122+
/* GetAlgoId() also takes an indefinite-length NULL (05 80). */
123+
if (algoIdx != algoEnd && (algoIdx + 2 != algoEnd ||
124+
hash_data[algoIdx] != ASN_TAG_NULL || hash_data[algoIdx + 1] != 0))
125+
return ASN_PARSE_E;
126+
ret = GetAlgoId(hash_data, &idx, &oid, oidHashType, algoEnd);
127+
if (ret == WC_NO_ERR_TRACE(MEMORY_E))
128+
return ret;
129+
if (ret < 0 || idx != algoEnd)
130+
return ASN_PARSE_E;
131+
if (GetOctetString(hash_data, &idx, &len, hash_len) < 0)
132+
return ASN_PARSE_E;
133+
if (len != digest_len || idx + (word32)len != hash_len)
134+
return BAD_LENGTH_E;
135+
if ((int)oid != hash_oid)
136+
return ASN_SIG_OID_E;
137+
138+
return 0;
139+
}
95140
#endif /* !NO_RSA && !NO_ASN */
96141

97142
int wc_SignatureGetSize(enum wc_SignatureType sig_type,
@@ -194,32 +239,18 @@ int wc_SignatureVerifyHash(
194239
return ret;
195240
}
196241

197-
#if !defined(NO_RSA) && !defined(NO_ASN)
198-
/* For WC_SIGNATURE_TYPE_RSA_W_ENC, we need to extract the actual size of
199-
* the ASN.1-encoded hash.
200-
*/
201242
if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) {
202-
int hash_dec_len;
203-
word32 idx = 0;
204-
if (GetSequence(hash_data, &idx, &hash_dec_len, hash_len) < 0)
205-
return ASN_PARSE_E;
206-
/* skip the AlgorithmIdentifier */
207-
if (GetSequence(hash_data, &idx, &hash_dec_len, hash_len) < 0)
208-
return ASN_PARSE_E;
209-
idx += (word32)hash_dec_len;
210-
/* now sitting at the OCTET STRING containing the digest */
211-
if (GetOctetString(hash_data, &idx, &hash_dec_len, hash_len) < 0)
212-
return ASN_PARSE_E;
213-
if (hash_dec_len != ret)
214-
return BAD_LENGTH_E;
215-
}
216-
else
243+
#if !defined(NO_RSA) && !defined(NO_ASN)
244+
ret = wc_SignatureCheckDigestInfo(hash_type, hash_data, hash_len, ret);
245+
if (ret != 0)
246+
return ret;
247+
#else
248+
return SIG_TYPE_E;
217249
#endif
218-
{
219-
if (hash_len != (word32)ret) {
220-
WOLFSSL_MSG("wc_SignatureVerify: Invalid hash size");
221-
return BAD_LENGTH_E;
222-
}
250+
}
251+
else if (hash_len != (word32)ret) {
252+
WOLFSSL_MSG("wc_SignatureVerify: Invalid hash size");
253+
return BAD_LENGTH_E;
223254
}
224255

225256
ret = 0;
@@ -443,6 +474,19 @@ int wc_SignatureGenerateHash_ex(
443474
WOLFSSL_MSG("wc_SignatureGenerate: Invalid hash type/len");
444475
return ret;
445476
}
477+
if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) {
478+
#if !defined(NO_RSA) && !defined(NO_ASN)
479+
ret = wc_SignatureCheckDigestInfo(hash_type, hash_data, hash_len, ret);
480+
if (ret != 0)
481+
return ret;
482+
#else
483+
return SIG_TYPE_E;
484+
#endif
485+
}
486+
else if (hash_len != (word32)ret) {
487+
WOLFSSL_MSG("wc_SignatureGenerate: Invalid hash size");
488+
return BAD_LENGTH_E;
489+
}
446490
ret = 0;
447491

448492
/* Create signature using hash as data */

‎wolfssl/wolfcrypt/signature.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ enum wc_SignatureType {
5656
WC_SIGNATURE_TYPE_NONE = 0,
5757
WC_SIGNATURE_TYPE_ECC = 1,
5858
WC_SIGNATURE_TYPE_RSA = 2,
59-
WC_SIGNATURE_TYPE_RSA_W_ENC = 3 /* Adds DER header via wc_EncodeSignature */
59+
WC_SIGNATURE_TYPE_RSA_W_ENC = 3 /* Data APIs add the DigestInfo, *Hash
60+
* APIs take wc_EncodeSignature output */
6061
};
6162

6263
/* Signature wrapper disabled check */

0 commit comments

Comments
 (0)