Path: blob/master/test/jdk/sun/security/pkcs/pkcs7/TwoHash.java
41153 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 825549426* @summary Make sure the signature algorithm to verify a PKCS7 block is27* DIGwithENC instead of HASHwithENC.28* @modules java.base/sun.security.pkcs29* java.base/sun.security.tools.keytool30* java.base/sun.security.x50931*/3233import sun.security.pkcs.PKCS7;34import sun.security.tools.keytool.CertAndKeyGen;35import sun.security.x509.X500Name;3637import java.nio.charset.StandardCharsets;38import java.security.cert.X509Certificate;3940public class TwoHash {41public static void main(String[] args) throws Exception {4243byte[] content = "Hello You fool I love you".getBytes();4445CertAndKeyGen cak = new CertAndKeyGen("EC", "SHA512withECDSA");46cak.generate("secp256r1");47byte[] signature = PKCS7.generateNewSignedData(48"SHA256withECDSA",49null,50cak.getPrivateKey(),51new X509Certificate[] {cak.getSelfCertificate(new X500Name("CN=Me"), 1000)},52content,53false,54true, // direct sign, so that RFC 6211 check is not possible55null);5657// The original signature should verify.58if (new PKCS7(signature).verify(content) == null) {59throw new RuntimeException("Should be verified");60}6162// Modify the SHA256withECDSA signature algorithm (OID encoded as63// "06 08 2A 86 48 CE 3D 04 03 02") to SHA384withECDSA (OID encoded as64// "06 08 2A 86 48 CE 3D 04 03 03"). ISO_8859_1 charset is chosen65// because it's a strictly one byte per char encoding.66String s = new String(signature, StandardCharsets.ISO_8859_1);67String s1 = s.replace(68"\u0006\u0008\u002A\u0086\u0048\u00CE\u003D\u0004\u0003\u0002",69"\u0006\u0008\u002A\u0086\u0048\u00CE\u003D\u0004\u0003\u0003");70byte[] modified = s1.getBytes(StandardCharsets.ISO_8859_1);7172// The modified signature should still verify because the HASH73// part of signature algorithm is ignored.74if (new PKCS7(modified).verify(content) == null) {75throw new RuntimeException("Should be verified");76}77}78}798081