Path: blob/master/test/jdk/sun/security/pkcs/pkcs7/SignerOrder.java
41153 views
/*1* Copyright (c) 2015, 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 804835726* @summary test PKCS7 data signing, encoding and verification27* @library /test/lib28* @modules java.base/sun.security.pkcs29* java.base/sun.security.util30* java.base/sun.security.x50931* @run main SignerOrder32*/33import java.io.ByteArrayOutputStream;34import java.io.IOException;35import java.math.BigInteger;36import java.security.KeyPair;37import java.security.KeyPairGenerator;38import java.security.PrivateKey;39import java.security.Signature;40import java.security.SignatureException;41import java.security.cert.X509Certificate;42import java.util.Date;43import sun.security.pkcs.ContentInfo;44import sun.security.pkcs.PKCS7;45import sun.security.pkcs.SignerInfo;46import sun.security.util.DerOutputStream;47import sun.security.x509.AlgorithmId;48import sun.security.x509.CertificateAlgorithmId;49import sun.security.x509.CertificateSerialNumber;50import sun.security.x509.CertificateValidity;51import sun.security.x509.CertificateVersion;52import sun.security.x509.CertificateX509Key;53import sun.security.x509.X500Name;54import sun.security.x509.X509CertImpl;55import sun.security.x509.X509CertInfo;56import sun.security.x509.X509Key;57import jdk.test.lib.hexdump.HexPrinter;5859public class SignerOrder {6061//signer infos62static final byte[] data1 = "12345".getBytes();63static final byte[] data2 = "abcde".getBytes();6465public static void main(String[] argv) throws Exception {6667SignerInfo[] signerInfos = new SignerInfo[9];68SimpleSigner signer1 = new SimpleSigner(null, null, null, null);69signerInfos[8] = signer1.genSignerInfo(data1);70signerInfos[7] = signer1.genSignerInfo(new byte[]{});71signerInfos[6] = signer1.genSignerInfo(data2);7273SimpleSigner signer2 = new SimpleSigner(null, null, null, null);74signerInfos[5] = signer2.genSignerInfo(data1);75signerInfos[4] = signer2.genSignerInfo(new byte[]{});76signerInfos[3] = signer2.genSignerInfo(data2);7778SimpleSigner signer3 = new SimpleSigner(null, null, null, null);79signerInfos[2] = signer3.genSignerInfo(data1);80signerInfos[1] = signer3.genSignerInfo(new byte[]{});81signerInfos[0] = signer3.genSignerInfo(data2);8283ContentInfo contentInfo = new ContentInfo(data1);8485AlgorithmId[] algIds = {new AlgorithmId(AlgorithmId.SHA256_oid)};8687X509Certificate[] certs = {signer3.getCert(), signer2.getCert(),88signer1.getCert()};8990PKCS7 pkcs71 = new PKCS7(algIds, contentInfo,91certs,92signerInfos);9394System.out.println("SignerInfos in original.");95printSignerInfos(pkcs71.getSignerInfos());9697DerOutputStream out = new DerOutputStream();98pkcs71.encodeSignedData(out);99100PKCS7 pkcs72 = new PKCS7(out.toByteArray());101System.out.println("\nSignerInfos read back in:");102printSignerInfos(pkcs72.getSignerInfos());103104System.out.println("Verified signers of original:");105SignerInfo[] verifs1 = pkcs71.verify();106107System.out.println("Verified signers of after read-in:");108SignerInfo[] verifs2 = pkcs72.verify();109110if (verifs1.length != verifs2.length) {111throw new RuntimeException("Length or Original vs read-in "112+ "should be same");113}114}115116static void printSignerInfos(SignerInfo signerInfo) throws IOException {117ByteArrayOutputStream strm = new ByteArrayOutputStream();118signerInfo.derEncode(strm);119System.out.println("SignerInfo, length: "120+ strm.toByteArray().length);121HexPrinter.simple().format(strm.toByteArray());122System.out.println("\n");123strm.reset();124}125126static void printSignerInfos(SignerInfo[] signerInfos) throws IOException {127ByteArrayOutputStream strm = new ByteArrayOutputStream();128for (int i = 0; i < signerInfos.length; i++) {129signerInfos[i].derEncode(strm);130System.out.println("SignerInfo[" + i + "], length: "131+ strm.toByteArray().length);132HexPrinter.simple().format(strm.toByteArray());133System.out.println("\n");134strm.reset();135}136}137138}139140/**141* A simple extension of sun.security.x509.X500Signer that adds a no-fuss142* signing algorithm.143*/144class SimpleSigner {145146private final Signature sig;147private final X500Name agent;148private final AlgorithmId digestAlgId;149private final AlgorithmId encryptionAlgId;150private final AlgorithmId algId; // signature algid;151//combines digest + encryption152private final X509Key publicKey;153private final PrivateKey privateKey;154private final X509Certificate cert;155156public SimpleSigner(String digestAlg,157String encryptionAlg,158KeyPair keyPair,159X500Name agent) throws Exception {160161if (agent == null) {162agent = new X500Name("cn=test");163}164if (digestAlg == null) {165digestAlg = "SHA";166}167if (encryptionAlg == null) {168encryptionAlg = "DSA";169}170if (keyPair == null) {171KeyPairGenerator keyGen =172KeyPairGenerator.getInstance(encryptionAlg);173keyGen.initialize(1024);174keyPair = keyGen.generateKeyPair();175}176publicKey = (X509Key) keyPair.getPublic();177privateKey = keyPair.getPrivate();178179if ("DSA".equals(encryptionAlg)) {180this.sig = Signature.getInstance(encryptionAlg);181} else { // RSA182this.sig = Signature.getInstance(digestAlg + "/" + encryptionAlg);183}184this.sig.initSign(privateKey);185186this.agent = agent;187this.digestAlgId = AlgorithmId.get(digestAlg);188this.encryptionAlgId = AlgorithmId.get(encryptionAlg);189this.algId = AlgorithmId.get(this.sig.getAlgorithm());190191this.cert = getSelfCert();192}193194/**195* Take the data and sign it.196*197* @param buf buffer holding the next chunk of the data to be signed198* @param offset starting point of to-be-signed data199* @param len how many bytes of data are to be signed200* @return the signature for the input data.201* @exception SignatureException on errors.202*/203public byte[] simpleSign(byte[] buf, int offset, int len)204throws SignatureException {205sig.update(buf, offset, len);206return sig.sign();207}208209/**210* Returns the digest algorithm used to sign.211*/212public AlgorithmId getDigestAlgId() {213return digestAlgId;214}215216/**217* Returns the encryption algorithm used to sign.218*/219public AlgorithmId getEncryptionAlgId() {220return encryptionAlgId;221}222223/**224* Returns the name of the signing agent.225*/226public X500Name getSigner() {227return agent;228}229230public X509Certificate getCert() {231return cert;232}233234private X509Certificate getSelfCert() throws Exception {235long validity = 1000;236X509CertImpl certLocal;237Date firstDate, lastDate;238239firstDate = new Date();240lastDate = new Date();241lastDate.setTime(lastDate.getTime() + validity + 1000);242243CertificateValidity interval = new CertificateValidity(firstDate,244lastDate);245246X509CertInfo info = new X509CertInfo();247// Add all mandatory attributes248info.set(X509CertInfo.VERSION,249new CertificateVersion(CertificateVersion.V1));250info.set(X509CertInfo.SERIAL_NUMBER,251new CertificateSerialNumber(252(int) (firstDate.getTime() / 1000)));253info.set(X509CertInfo.ALGORITHM_ID,254new CertificateAlgorithmId(algId));255info.set(X509CertInfo.SUBJECT, agent);256info.set(X509CertInfo.KEY, new CertificateX509Key(publicKey));257info.set(X509CertInfo.VALIDITY, interval);258info.set(X509CertInfo.ISSUER, agent);259260certLocal = new X509CertImpl(info);261certLocal.sign(privateKey, algId.getName());262263return certLocal;264}265266public SignerInfo genSignerInfo(byte[] data) throws SignatureException {267return new SignerInfo((X500Name) cert.getIssuerDN(),268new BigInteger("" + cert.getSerialNumber()),269getDigestAlgId(), algId,270simpleSign(data, 0, data.length));271}272}273274275