undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
97
test/jdk/sun/security/x509/AlgorithmId/NonStandardNames.java
Normal file
97
test/jdk/sun/security/x509/AlgorithmId/NonStandardNames.java
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 7180907 8277224
|
||||
* @library /test/lib
|
||||
* @summary Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
|
||||
* @modules java.base/sun.security.pkcs
|
||||
* java.base/sun.security.tools.keytool
|
||||
* java.base/sun.security.util
|
||||
* java.base/sun.security.x509
|
||||
* @compile -XDignore.symbol.file NonStandardNames.java
|
||||
* @run main NonStandardNames
|
||||
*/
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.Signature;
|
||||
import java.security.cert.X509Certificate;
|
||||
import jdk.test.lib.security.SecurityUtils;
|
||||
import sun.security.pkcs.ContentInfo;
|
||||
import sun.security.pkcs.PKCS7;
|
||||
import sun.security.pkcs.PKCS9Attribute;
|
||||
import sun.security.pkcs.PKCS9Attributes;
|
||||
import sun.security.pkcs.SignerInfo;
|
||||
import sun.security.tools.keytool.CertAndKeyGen;
|
||||
import sun.security.x509.AlgorithmId;
|
||||
import sun.security.x509.X500Name;
|
||||
|
||||
public class NonStandardNames {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
byte[] data = "Hello".getBytes();
|
||||
X500Name n = new X500Name("cn=Me");
|
||||
|
||||
String kpgAlgorithm = "RSA";
|
||||
CertAndKeyGen cakg = new CertAndKeyGen(kpgAlgorithm, "SHA256withRSA");
|
||||
cakg.generate(SecurityUtils.getTestKeySize(kpgAlgorithm));
|
||||
X509Certificate cert = cakg.getSelfCertificate(n, 1000);
|
||||
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
PKCS9Attributes authed = new PKCS9Attributes(new PKCS9Attribute[]{
|
||||
new PKCS9Attribute(PKCS9Attribute.CONTENT_TYPE_OID, ContentInfo.DATA_OID),
|
||||
new PKCS9Attribute(PKCS9Attribute.MESSAGE_DIGEST_OID, md.digest(data)),
|
||||
new PKCS9Attribute(PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_OID, "test".getBytes())
|
||||
});
|
||||
|
||||
// test PKCS9Attributes.toString(), PKCS9Attributes.getAttributes()
|
||||
System.out.println(authed);
|
||||
|
||||
Signature s = Signature.getInstance("SHA256withRSA");
|
||||
s.initSign(cakg.getPrivateKey());
|
||||
s.update(authed.getDerEncoding());
|
||||
byte[] sig = s.sign();
|
||||
|
||||
SignerInfo signerInfo = new SignerInfo(
|
||||
n,
|
||||
cert.getSerialNumber(),
|
||||
AlgorithmId.get("SHA-256"),
|
||||
authed,
|
||||
AlgorithmId.get("SHA256withRSA"),
|
||||
sig,
|
||||
null
|
||||
);
|
||||
|
||||
PKCS7 pkcs7 = new PKCS7(
|
||||
new AlgorithmId[] {signerInfo.getDigestAlgorithmId()},
|
||||
new ContentInfo(data),
|
||||
new X509Certificate[] {cert},
|
||||
new SignerInfo[] {signerInfo});
|
||||
|
||||
if (pkcs7.verify(signerInfo, data) == null) {
|
||||
throw new Exception("Not verified");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue