Skip to content

Commit a645ef4

Browse files
author
Alexey Bakhtin
committed
8320597: RSA signature verification fails on signed data that does not encode params correctly
Reviewed-by: sgehwolf, mbalao, andrew Backport-of: 11e4a925bec3c1f79e03045d48def53188b655e6
1 parent 2131e40 commit a645ef4

File tree

2 files changed

+77
-19
lines changed

2 files changed

+77
-19
lines changed

jdk/src/share/classes/sun/security/rsa/RSASignature.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,17 @@ protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
219219
byte[] decrypted = RSACore.rsa(sigBytes, publicKey);
220220

221221
byte[] digest = getDigestValue();
222+
222223
byte[] encoded = encodeSignature(digestOID, digest);
223224
byte[] padded = padding.pad(encoded);
225+
if (MessageDigest.isEqual(padded, decrypted)) {
226+
return true;
227+
}
228+
229+
// Some vendors might omit the NULL params in digest algorithm
230+
// identifier. Try again.
231+
encoded = encodeSignatureWithoutNULL(digestOID, digest);
232+
padded = padding.pad(encoded);
224233
return MessageDigest.isEqual(padded, decrypted);
225234
} catch (javax.crypto.BadPaddingException e) {
226235
return false;
@@ -244,27 +253,19 @@ public static byte[] encodeSignature(ObjectIdentifier oid, byte[] digest)
244253
}
245254

246255
/**
247-
* Decode the signature data. Verify that the object identifier matches
248-
* and return the message digest.
256+
* Encode the digest without the NULL params, return the to-be-signed data.
257+
* This is only used by SunRsaSign.
249258
*/
250-
public static byte[] decodeSignature(ObjectIdentifier oid, byte[] sig)
259+
static byte[] encodeSignatureWithoutNULL(ObjectIdentifier oid, byte[] digest)
251260
throws IOException {
252-
// Enforce strict DER checking for signatures
253-
DerInputStream in = new DerInputStream(sig, 0, sig.length, false);
254-
DerValue[] values = in.getSequence(2);
255-
if ((values.length != 2) || (in.available() != 0)) {
256-
throw new IOException("SEQUENCE length error");
257-
}
258-
AlgorithmId algId = AlgorithmId.parse(values[0]);
259-
if (algId.getOID().equals((Object)oid) == false) {
260-
throw new IOException("ObjectIdentifier mismatch: "
261-
+ algId.getOID());
262-
}
263-
if (algId.getEncodedParams() != null) {
264-
throw new IOException("Unexpected AlgorithmId parameters");
265-
}
266-
byte[] digest = values[1].getOctetString();
267-
return digest;
261+
DerOutputStream out = new DerOutputStream();
262+
DerOutputStream oidout = new DerOutputStream();
263+
oidout.putOID(oid);
264+
out.write(DerValue.tag_Sequence, oidout);
265+
out.putOctetString(digest);
266+
DerValue result =
267+
new DerValue(DerValue.tag_Sequence, out.toByteArray());
268+
return result.toByteArray();
268269
}
269270

270271
// set parameter, not supported. See JCA doc
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @bug 8320597
27+
* @summary Verify RSA signature with omitted digest params (should be encoded as NULL)
28+
* for backward compatibility
29+
*/
30+
import java.security.KeyFactory;
31+
import java.security.Signature;
32+
import java.security.spec.X509EncodedKeySpec;
33+
import java.util.Base64;
34+
35+
public class WithoutNULL {
36+
public static void main(String[] args) throws Exception {
37+
38+
// A 1024-bit RSA public key
39+
byte[] key = Base64.getMimeDecoder().decode(
40+
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrfTrEm4KvdFSpGAM7InrFEzALTKdphT9fK6Gu" +
41+
"eVjHtKsuCSEaULCdjhJvPpFK40ONr1JEC1Ywp1UYrfBBdKunnbDZqNZL1cFv+IzF4Yj6JO6pOeHi" +
42+
"1Zpur1GaQRRlYTvzmyWY/AATQDh8JfKObNnDVwXeezFODUG8h5+XL1ZXZQIDAQAB");
43+
44+
// A SHA1withRSA signature on an empty input where the digestAlgorithm
45+
// inside DigestInfo does not have a parameters field.
46+
byte[] sig = Base64.getMimeDecoder().decode(
47+
"D1FpiT44WEXlDfYK880bdorLO+e9qJVXZWiBgqs9dfK7lYQwyEt9dL23mbUAKm5TVEj2ZxtHkEvk" +
48+
"b8oaWkxk069jDTM1RhllPJZkAjeQRbw4gkg4N6wKZz9B/jdSRMNJg/b9QdRYZOHOBxsEHMbUREPV" +
49+
"DoCOLaxB8eIXX0EWkiE=");
50+
51+
Signature s = Signature.getInstance("SHA1withRSA", "SunRsaSign");
52+
s.initVerify(KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(key)));
53+
if (!s.verify(sig)) {
54+
throw new RuntimeException("Does not verify");
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)