@@ -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
0 commit comments