PKCS7.java revision 3990
0N/A/*
3990N/A * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.pkcs;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.math.BigInteger;
0N/Aimport java.util.*;
0N/Aimport java.security.cert.X509Certificate;
0N/Aimport java.security.cert.CertificateException;
0N/Aimport java.security.cert.X509CRL;
0N/Aimport java.security.cert.CRLException;
0N/Aimport java.security.cert.CertificateFactory;
0N/Aimport java.security.*;
0N/A
0N/Aimport sun.security.util.*;
0N/Aimport sun.security.x509.AlgorithmId;
0N/Aimport sun.security.x509.CertificateIssuerName;
3990N/Aimport sun.security.x509.KeyUsageExtension;
0N/Aimport sun.security.x509.X509CertImpl;
0N/Aimport sun.security.x509.X509CertInfo;
0N/Aimport sun.security.x509.X509CRLImpl;
0N/Aimport sun.security.x509.X500Name;
0N/A
0N/A/**
0N/A * PKCS7 as defined in RSA Laboratories PKCS7 Technical Note. Profile
0N/A * Supports only <tt>SignedData</tt> ContentInfo
0N/A * type, where to the type of data signed is plain Data.
0N/A * For signedData, <tt>crls</tt>, <tt>attributes</tt> and
0N/A * PKCS#6 Extended Certificates are not supported.
0N/A *
0N/A * @author Benjamin Renaud
0N/A */
0N/Apublic class PKCS7 {
0N/A
0N/A private ObjectIdentifier contentType;
0N/A
0N/A // the ASN.1 members for a signedData (and other) contentTypes
0N/A private BigInteger version = null;
0N/A private AlgorithmId[] digestAlgorithmIds = null;
0N/A private ContentInfo contentInfo = null;
0N/A private X509Certificate[] certificates = null;
0N/A private X509CRL[] crls = null;
0N/A private SignerInfo[] signerInfos = null;
0N/A
0N/A private boolean oldStyle = false; // Is this JDK1.1.x-style?
0N/A
0N/A private Principal[] certIssuerNames;
0N/A
0N/A /**
0N/A * Unmarshals a PKCS7 block from its encoded form, parsing the
0N/A * encoded bytes from the InputStream.
0N/A *
0N/A * @param in an input stream holding at least one PKCS7 block.
0N/A * @exception ParsingException on parsing errors.
0N/A * @exception IOException on other errors.
0N/A */
0N/A public PKCS7(InputStream in) throws ParsingException, IOException {
0N/A DataInputStream dis = new DataInputStream(in);
0N/A byte[] data = new byte[dis.available()];
0N/A dis.readFully(data);
0N/A
0N/A parse(new DerInputStream(data));
0N/A }
0N/A
0N/A /**
0N/A * Unmarshals a PKCS7 block from its encoded form, parsing the
0N/A * encoded bytes from the DerInputStream.
0N/A *
0N/A * @param derin a DerInputStream holding at least one PKCS7 block.
0N/A * @exception ParsingException on parsing errors.
0N/A */
0N/A public PKCS7(DerInputStream derin) throws ParsingException {
0N/A parse(derin);
0N/A }
0N/A
0N/A /**
0N/A * Unmarshals a PKCS7 block from its encoded form, parsing the
0N/A * encoded bytes.
0N/A *
0N/A * @param bytes the encoded bytes.
0N/A * @exception ParsingException on parsing errors.
0N/A */
0N/A public PKCS7(byte[] bytes) throws ParsingException {
0N/A try {
0N/A DerInputStream derin = new DerInputStream(bytes);
0N/A parse(derin);
0N/A } catch (IOException ioe1) {
0N/A ParsingException pe = new ParsingException(
0N/A "Unable to parse the encoded bytes");
0N/A pe.initCause(ioe1);
0N/A throw pe;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Parses a PKCS#7 block.
0N/A */
0N/A private void parse(DerInputStream derin)
0N/A throws ParsingException
0N/A {
0N/A try {
0N/A derin.mark(derin.available());
0N/A // try new (i.e., JDK1.2) style
0N/A parse(derin, false);
0N/A } catch (IOException ioe) {
0N/A try {
0N/A derin.reset();
0N/A // try old (i.e., JDK1.1.x) style
0N/A parse(derin, true);
0N/A oldStyle = true;
0N/A } catch (IOException ioe1) {
0N/A ParsingException pe = new ParsingException(
0N/A ioe1.getMessage());
0N/A pe.initCause(ioe1);
0N/A throw pe;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Parses a PKCS#7 block.
0N/A *
0N/A * @param derin the ASN.1 encoding of the PKCS#7 block.
0N/A * @param oldStyle flag indicating whether or not the given PKCS#7 block
0N/A * is encoded according to JDK1.1.x.
0N/A */
0N/A private void parse(DerInputStream derin, boolean oldStyle)
0N/A throws IOException
0N/A {
0N/A contentInfo = new ContentInfo(derin, oldStyle);
0N/A contentType = contentInfo.contentType;
0N/A DerValue content = contentInfo.getContent();
0N/A
0N/A if (contentType.equals(ContentInfo.SIGNED_DATA_OID)) {
0N/A parseSignedData(content);
0N/A } else if (contentType.equals(ContentInfo.OLD_SIGNED_DATA_OID)) {
0N/A // This is for backwards compatibility with JDK 1.1.x
0N/A parseOldSignedData(content);
0N/A } else if (contentType.equals(ContentInfo.NETSCAPE_CERT_SEQUENCE_OID)){
0N/A parseNetscapeCertChain(content);
0N/A } else {
0N/A throw new ParsingException("content type " + contentType +
0N/A " not supported.");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Construct an initialized PKCS7 block.
0N/A *
0N/A * @param digestAlgorithmIds the message digest algorithm identifiers.
0N/A * @param contentInfo the content information.
0N/A * @param certificates an array of X.509 certificates.
2346N/A * @param crls an array of CRLs
0N/A * @param signerInfos an array of signer information.
0N/A */
0N/A public PKCS7(AlgorithmId[] digestAlgorithmIds,
0N/A ContentInfo contentInfo,
0N/A X509Certificate[] certificates,
2346N/A X509CRL[] crls,
0N/A SignerInfo[] signerInfos) {
0N/A
0N/A version = BigInteger.ONE;
0N/A this.digestAlgorithmIds = digestAlgorithmIds;
0N/A this.contentInfo = contentInfo;
0N/A this.certificates = certificates;
2346N/A this.crls = crls;
0N/A this.signerInfos = signerInfos;
0N/A }
0N/A
2346N/A public PKCS7(AlgorithmId[] digestAlgorithmIds,
2346N/A ContentInfo contentInfo,
2346N/A X509Certificate[] certificates,
2346N/A SignerInfo[] signerInfos) {
2346N/A this(digestAlgorithmIds, contentInfo, certificates, null, signerInfos);
2346N/A }
2346N/A
0N/A private void parseNetscapeCertChain(DerValue val)
0N/A throws ParsingException, IOException {
0N/A DerInputStream dis = new DerInputStream(val.toByteArray());
0N/A DerValue[] contents = dis.getSequence(2);
0N/A certificates = new X509Certificate[contents.length];
0N/A
0N/A CertificateFactory certfac = null;
0N/A try {
0N/A certfac = CertificateFactory.getInstance("X.509");
0N/A } catch (CertificateException ce) {
0N/A // do nothing
0N/A }
0N/A
0N/A for (int i=0; i < contents.length; i++) {
0N/A ByteArrayInputStream bais = null;
0N/A try {
0N/A if (certfac == null)
0N/A certificates[i] = new X509CertImpl(contents[i]);
0N/A else {
0N/A byte[] encoded = contents[i].toByteArray();
0N/A bais = new ByteArrayInputStream(encoded);
0N/A certificates[i] =
0N/A (X509Certificate)certfac.generateCertificate(bais);
0N/A bais.close();
0N/A bais = null;
0N/A }
0N/A } catch (CertificateException ce) {
0N/A ParsingException pe = new ParsingException(ce.getMessage());
0N/A pe.initCause(ce);
0N/A throw pe;
0N/A } catch (IOException ioe) {
0N/A ParsingException pe = new ParsingException(ioe.getMessage());
0N/A pe.initCause(ioe);
0N/A throw pe;
0N/A } finally {
0N/A if (bais != null)
0N/A bais.close();
0N/A }
0N/A }
0N/A }
0N/A
0N/A private void parseSignedData(DerValue val)
0N/A throws ParsingException, IOException {
0N/A
0N/A DerInputStream dis = val.toDerInputStream();
0N/A
0N/A // Version
0N/A version = dis.getBigInteger();
0N/A
0N/A // digestAlgorithmIds
0N/A DerValue[] digestAlgorithmIdVals = dis.getSet(1);
0N/A int len = digestAlgorithmIdVals.length;
0N/A digestAlgorithmIds = new AlgorithmId[len];
0N/A try {
0N/A for (int i = 0; i < len; i++) {
0N/A DerValue oid = digestAlgorithmIdVals[i];
0N/A digestAlgorithmIds[i] = AlgorithmId.parse(oid);
0N/A }
0N/A
0N/A } catch (IOException e) {
0N/A ParsingException pe =
0N/A new ParsingException("Error parsing digest AlgorithmId IDs: " +
0N/A e.getMessage());
0N/A pe.initCause(e);
0N/A throw pe;
0N/A }
0N/A // contentInfo
0N/A contentInfo = new ContentInfo(dis);
0N/A
0N/A CertificateFactory certfac = null;
0N/A try {
0N/A certfac = CertificateFactory.getInstance("X.509");
0N/A } catch (CertificateException ce) {
0N/A // do nothing
0N/A }
0N/A
0N/A /*
0N/A * check if certificates (implicit tag) are provided
0N/A * (certificates are OPTIONAL)
0N/A */
0N/A if ((byte)(dis.peekByte()) == (byte)0xA0) {
0N/A DerValue[] certVals = dis.getSet(2, true);
0N/A
0N/A len = certVals.length;
0N/A certificates = new X509Certificate[len];
0N/A
0N/A for (int i = 0; i < len; i++) {
0N/A ByteArrayInputStream bais = null;
0N/A try {
0N/A if (certfac == null)
0N/A certificates[i] = new X509CertImpl(certVals[i]);
0N/A else {
0N/A byte[] encoded = certVals[i].toByteArray();
0N/A bais = new ByteArrayInputStream(encoded);
0N/A certificates[i] =
0N/A (X509Certificate)certfac.generateCertificate(bais);
0N/A bais.close();
0N/A bais = null;
0N/A }
0N/A } catch (CertificateException ce) {
0N/A ParsingException pe = new ParsingException(ce.getMessage());
0N/A pe.initCause(ce);
0N/A throw pe;
0N/A } catch (IOException ioe) {
0N/A ParsingException pe = new ParsingException(ioe.getMessage());
0N/A pe.initCause(ioe);
0N/A throw pe;
0N/A } finally {
0N/A if (bais != null)
0N/A bais.close();
0N/A }
0N/A }
0N/A }
0N/A
0N/A // check if crls (implicit tag) are provided (crls are OPTIONAL)
0N/A if ((byte)(dis.peekByte()) == (byte)0xA1) {
0N/A DerValue[] crlVals = dis.getSet(1, true);
0N/A
0N/A len = crlVals.length;
0N/A crls = new X509CRL[len];
0N/A
0N/A for (int i = 0; i < len; i++) {
0N/A ByteArrayInputStream bais = null;
0N/A try {
0N/A if (certfac == null)
2346N/A crls[i] = new X509CRLImpl(crlVals[i]);
0N/A else {
0N/A byte[] encoded = crlVals[i].toByteArray();
0N/A bais = new ByteArrayInputStream(encoded);
0N/A crls[i] = (X509CRL) certfac.generateCRL(bais);
0N/A bais.close();
0N/A bais = null;
0N/A }
0N/A } catch (CRLException e) {
0N/A ParsingException pe =
0N/A new ParsingException(e.getMessage());
0N/A pe.initCause(e);
0N/A throw pe;
0N/A } finally {
0N/A if (bais != null)
0N/A bais.close();
0N/A }
0N/A }
0N/A }
0N/A
0N/A // signerInfos
0N/A DerValue[] signerInfoVals = dis.getSet(1);
0N/A
0N/A len = signerInfoVals.length;
0N/A signerInfos = new SignerInfo[len];
0N/A
0N/A for (int i = 0; i < len; i++) {
0N/A DerInputStream in = signerInfoVals[i].toDerInputStream();
0N/A signerInfos[i] = new SignerInfo(in);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Parses an old-style SignedData encoding (for backwards
0N/A * compatibility with JDK1.1.x).
0N/A */
0N/A private void parseOldSignedData(DerValue val)
0N/A throws ParsingException, IOException
0N/A {
0N/A DerInputStream dis = val.toDerInputStream();
0N/A
0N/A // Version
0N/A version = dis.getBigInteger();
0N/A
0N/A // digestAlgorithmIds
0N/A DerValue[] digestAlgorithmIdVals = dis.getSet(1);
0N/A int len = digestAlgorithmIdVals.length;
0N/A
0N/A digestAlgorithmIds = new AlgorithmId[len];
0N/A try {
0N/A for (int i = 0; i < len; i++) {
0N/A DerValue oid = digestAlgorithmIdVals[i];
0N/A digestAlgorithmIds[i] = AlgorithmId.parse(oid);
0N/A }
0N/A } catch (IOException e) {
0N/A throw new ParsingException("Error parsing digest AlgorithmId IDs");
0N/A }
0N/A
0N/A // contentInfo
0N/A contentInfo = new ContentInfo(dis, true);
0N/A
0N/A // certificates
0N/A CertificateFactory certfac = null;
0N/A try {
0N/A certfac = CertificateFactory.getInstance("X.509");
0N/A } catch (CertificateException ce) {
0N/A // do nothing
0N/A }
0N/A DerValue[] certVals = dis.getSet(2);
0N/A len = certVals.length;
0N/A certificates = new X509Certificate[len];
0N/A
0N/A for (int i = 0; i < len; i++) {
0N/A ByteArrayInputStream bais = null;
0N/A try {
0N/A if (certfac == null)
0N/A certificates[i] = new X509CertImpl(certVals[i]);
0N/A else {
0N/A byte[] encoded = certVals[i].toByteArray();
0N/A bais = new ByteArrayInputStream(encoded);
0N/A certificates[i] =
0N/A (X509Certificate)certfac.generateCertificate(bais);
0N/A bais.close();
0N/A bais = null;
0N/A }
0N/A } catch (CertificateException ce) {
0N/A ParsingException pe = new ParsingException(ce.getMessage());
0N/A pe.initCause(ce);
0N/A throw pe;
0N/A } catch (IOException ioe) {
0N/A ParsingException pe = new ParsingException(ioe.getMessage());
0N/A pe.initCause(ioe);
0N/A throw pe;
0N/A } finally {
0N/A if (bais != null)
0N/A bais.close();
0N/A }
0N/A }
0N/A
0N/A // crls are ignored.
0N/A dis.getSet(0);
0N/A
0N/A // signerInfos
0N/A DerValue[] signerInfoVals = dis.getSet(1);
0N/A len = signerInfoVals.length;
0N/A signerInfos = new SignerInfo[len];
0N/A for (int i = 0; i < len; i++) {
0N/A DerInputStream in = signerInfoVals[i].toDerInputStream();
0N/A signerInfos[i] = new SignerInfo(in, true);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Encodes the signed data to an output stream.
0N/A *
0N/A * @param out the output stream to write the encoded data to.
0N/A * @exception IOException on encoding errors.
0N/A */
0N/A public void encodeSignedData(OutputStream out) throws IOException {
0N/A DerOutputStream derout = new DerOutputStream();
0N/A encodeSignedData(derout);
0N/A out.write(derout.toByteArray());
0N/A }
0N/A
0N/A /**
0N/A * Encodes the signed data to a DerOutputStream.
0N/A *
0N/A * @param out the DerOutputStream to write the encoded data to.
0N/A * @exception IOException on encoding errors.
0N/A */
0N/A public void encodeSignedData(DerOutputStream out)
0N/A throws IOException
0N/A {
0N/A DerOutputStream signedData = new DerOutputStream();
0N/A
0N/A // version
0N/A signedData.putInteger(version);
0N/A
0N/A // digestAlgorithmIds
0N/A signedData.putOrderedSetOf(DerValue.tag_Set, digestAlgorithmIds);
0N/A
0N/A // contentInfo
0N/A contentInfo.encode(signedData);
0N/A
0N/A // certificates (optional)
0N/A if (certificates != null && certificates.length != 0) {
0N/A // cast to X509CertImpl[] since X509CertImpl implements DerEncoder
0N/A X509CertImpl implCerts[] = new X509CertImpl[certificates.length];
0N/A for (int i = 0; i < certificates.length; i++) {
0N/A if (certificates[i] instanceof X509CertImpl)
0N/A implCerts[i] = (X509CertImpl) certificates[i];
0N/A else {
0N/A try {
0N/A byte[] encoded = certificates[i].getEncoded();
0N/A implCerts[i] = new X509CertImpl(encoded);
0N/A } catch (CertificateException ce) {
0N/A IOException ie = new IOException(ce.getMessage());
0N/A ie.initCause(ce);
0N/A throw ie;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Add the certificate set (tagged with [0] IMPLICIT)
0N/A // to the signed data
0N/A signedData.putOrderedSetOf((byte)0xA0, implCerts);
0N/A }
0N/A
2346N/A // CRLs (optional)
2346N/A if (crls != null && crls.length != 0) {
2346N/A // cast to X509CRLImpl[] since X509CRLImpl implements DerEncoder
3990N/A Set<X509CRLImpl> implCRLs = new HashSet<>(crls.length);
2346N/A for (X509CRL crl: crls) {
2346N/A if (crl instanceof X509CRLImpl)
2346N/A implCRLs.add((X509CRLImpl) crl);
2346N/A else {
2346N/A try {
2346N/A byte[] encoded = crl.getEncoded();
2346N/A implCRLs.add(new X509CRLImpl(encoded));
2346N/A } catch (CRLException ce) {
2346N/A IOException ie = new IOException(ce.getMessage());
2346N/A ie.initCause(ce);
2346N/A throw ie;
2346N/A }
2346N/A }
2346N/A }
2346N/A
2346N/A // Add the CRL set (tagged with [1] IMPLICIT)
2346N/A // to the signed data
2346N/A signedData.putOrderedSetOf((byte)0xA1,
2346N/A implCRLs.toArray(new X509CRLImpl[implCRLs.size()]));
2346N/A }
0N/A
0N/A // signerInfos
0N/A signedData.putOrderedSetOf(DerValue.tag_Set, signerInfos);
0N/A
0N/A // making it a signed data block
0N/A DerValue signedDataSeq = new DerValue(DerValue.tag_Sequence,
0N/A signedData.toByteArray());
0N/A
0N/A // making it a content info sequence
0N/A ContentInfo block = new ContentInfo(ContentInfo.SIGNED_DATA_OID,
0N/A signedDataSeq);
0N/A
0N/A // writing out the contentInfo sequence
0N/A block.encode(out);
0N/A }
0N/A
0N/A /**
3990N/A * Verifying signed data using an external chunked data source.
3990N/A */
3990N/A public static class PKCS7Verifier {
3990N/A
3990N/A private final SignerInfo si; // Signer to verify
3990N/A private final MessageDigest md; // MessageDigest object for chunks
3990N/A private final Signature sig; // Signature object for chunks
3990N/A
3990N/A private PKCS7Verifier(SignerInfo si, MessageDigest md, Signature sig) {
3990N/A this.si = si;
3990N/A this.md = md;
3990N/A this.sig = sig;
3990N/A }
3990N/A
3990N/A public static PKCS7Verifier from(PKCS7 block, SignerInfo si) throws
3990N/A SignatureException, NoSuchAlgorithmException {
3990N/A
3990N/A try {
3990N/A MessageDigest md = null;
3990N/A Signature sig;
3990N/A
3990N/A ContentInfo content = block.getContentInfo();
3990N/A String digestAlgname = si.getDigestAlgorithmId().getName();
3990N/A
3990N/A // if there are authenticate attributes, feed data chunks to
3990N/A // the message digest. In this case, pv.md is not null
3990N/A if (si.authenticatedAttributes != null) {
3990N/A // first, check content type
3990N/A ObjectIdentifier contentType = (ObjectIdentifier)
3990N/A si.authenticatedAttributes.getAttributeValue(
3990N/A PKCS9Attribute.CONTENT_TYPE_OID);
3990N/A if (contentType == null ||
3990N/A !contentType.equals(content.contentType))
3990N/A return null; // contentType does not match, bad SignerInfo
3990N/A
3990N/A // now, check message digest
3990N/A byte[] messageDigest = (byte[])
3990N/A si.authenticatedAttributes.getAttributeValue(
3990N/A PKCS9Attribute.MESSAGE_DIGEST_OID);
3990N/A
3990N/A if (messageDigest == null) // fail if there is no message digest
3990N/A return null;
3990N/A
3990N/A md = MessageDigest.getInstance(digestAlgname);
3990N/A }
3990N/A
3990N/A // put together digest algorithm and encryption algorithm
3990N/A // to form signing algorithm
3990N/A String encryptionAlgname =
3990N/A si.getDigestEncryptionAlgorithmId().getName();
3990N/A
3990N/A // Workaround: sometimes the encryptionAlgname is actually
3990N/A // a signature name
3990N/A String tmp = AlgorithmId.getEncAlgFromSigAlg(encryptionAlgname);
3990N/A if (tmp != null) encryptionAlgname = tmp;
3990N/A String algname = AlgorithmId.makeSigAlg(
3990N/A digestAlgname, encryptionAlgname);
3990N/A
3990N/A sig = Signature.getInstance(algname);
3990N/A X509Certificate cert = si.getCertificate(block);
3990N/A
3990N/A if (cert == null) {
3990N/A return null;
3990N/A }
3990N/A if (cert.hasUnsupportedCriticalExtension()) {
3990N/A throw new SignatureException("Certificate has unsupported "
3990N/A + "critical extension(s)");
3990N/A }
3990N/A
3990N/A // Make sure that if the usage of the key in the certificate is
3990N/A // restricted, it can be used for digital signatures.
3990N/A // XXX We may want to check for additional extensions in the
3990N/A // future.
3990N/A boolean[] keyUsageBits = cert.getKeyUsage();
3990N/A if (keyUsageBits != null) {
3990N/A KeyUsageExtension keyUsage;
3990N/A try {
3990N/A // We don't care whether or not this extension was marked
3990N/A // critical in the certificate.
3990N/A // We're interested only in its value (i.e., the bits set)
3990N/A // and treat the extension as critical.
3990N/A keyUsage = new KeyUsageExtension(keyUsageBits);
3990N/A } catch (IOException ioe) {
3990N/A throw new SignatureException("Failed to parse keyUsage "
3990N/A + "extension");
3990N/A }
3990N/A
3990N/A boolean digSigAllowed = ((Boolean)keyUsage.get(
3990N/A KeyUsageExtension.DIGITAL_SIGNATURE)).booleanValue();
3990N/A
3990N/A boolean nonRepuAllowed = ((Boolean)keyUsage.get(
3990N/A KeyUsageExtension.NON_REPUDIATION)).booleanValue();
3990N/A
3990N/A if (!digSigAllowed && !nonRepuAllowed) {
3990N/A throw new SignatureException("Key usage restricted: "
3990N/A + "cannot be used for "
3990N/A + "digital signatures");
3990N/A }
3990N/A }
3990N/A
3990N/A PublicKey key = cert.getPublicKey();
3990N/A sig.initVerify(key);
3990N/A return new PKCS7Verifier(si, md, sig);
3990N/A } catch (IOException e) {
3990N/A throw new SignatureException("IO error verifying signature:\n" +
3990N/A e.getMessage());
3990N/A
3990N/A } catch (InvalidKeyException e) {
3990N/A throw new SignatureException("InvalidKey: " + e.getMessage());
3990N/A
3990N/A }
3990N/A }
3990N/A
3990N/A public void update(byte[] data, int off, int end)
3990N/A throws SignatureException {
3990N/A if (md != null) {
3990N/A md.update(data, off, end-off);
3990N/A } else {
3990N/A sig.update(data, off, end-off);
3990N/A }
3990N/A }
3990N/A
3990N/A public SignerInfo verify() throws SignatureException {
3990N/A try {
3990N/A // if there are authenticate attributes, get the message
3990N/A // digest and compare it with the digest of data
3990N/A if (md != null) {
3990N/A // now, check message digest
3990N/A byte[] messageDigest = (byte[])
3990N/A si.authenticatedAttributes.getAttributeValue(
3990N/A PKCS9Attribute.MESSAGE_DIGEST_OID);
3990N/A
3990N/A byte[] computedMessageDigest = md.digest();
3990N/A
3990N/A if (!MessageDigest.isEqual(
3990N/A messageDigest, computedMessageDigest)) {
3990N/A return null;
3990N/A }
3990N/A
3990N/A // message digest attribute matched
3990N/A // digest of original data
3990N/A
3990N/A // the data actually signed is the DER encoding of
3990N/A // the authenticated attributes (tagged with
3990N/A // the "SET OF" tag, not 0xA0).
3990N/A byte[] dataSigned = si.authenticatedAttributes.getDerEncoding();
3990N/A sig.update(dataSigned);
3990N/A }
3990N/A
3990N/A if (sig.verify(si.getEncryptedDigest())) {
3990N/A return si;
3990N/A }
3990N/A
3990N/A } catch (IOException e) {
3990N/A throw new SignatureException("IO error verifying signature:\n" +
3990N/A e.getMessage());
3990N/A }
3990N/A return null;
3990N/A }
3990N/A }
3990N/A
3990N/A /**
0N/A * This verifies a given SignerInfo.
0N/A *
0N/A * @param info the signer information.
0N/A * @param bytes the DER encoded content information.
0N/A *
0N/A * @exception NoSuchAlgorithmException on unrecognized algorithms.
0N/A * @exception SignatureException on signature handling errors.
0N/A */
0N/A public SignerInfo verify(SignerInfo info, byte[] bytes)
0N/A throws NoSuchAlgorithmException, SignatureException {
0N/A return info.verify(this, bytes);
0N/A }
0N/A
0N/A /**
0N/A * Returns all signerInfos which self-verify.
0N/A *
0N/A * @param bytes the DER encoded content information.
0N/A *
0N/A * @exception NoSuchAlgorithmException on unrecognized algorithms.
0N/A * @exception SignatureException on signature handling errors.
0N/A */
0N/A public SignerInfo[] verify(byte[] bytes)
0N/A throws NoSuchAlgorithmException, SignatureException {
0N/A
3990N/A List<SignerInfo> intResult = new ArrayList<>();
0N/A for (int i = 0; i < signerInfos.length; i++) {
0N/A
0N/A SignerInfo signerInfo = verify(signerInfos[i], bytes);
0N/A if (signerInfo != null) {
3990N/A intResult.add(signerInfo);
0N/A }
0N/A }
3990N/A if (!intResult.isEmpty()) {
3990N/A return intResult.toArray(new SignerInfo[intResult.size()]);
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns all signerInfos which self-verify.
0N/A *
0N/A * @exception NoSuchAlgorithmException on unrecognized algorithms.
0N/A * @exception SignatureException on signature handling errors.
0N/A */
0N/A public SignerInfo[] verify()
0N/A throws NoSuchAlgorithmException, SignatureException {
0N/A return verify(null);
0N/A }
0N/A
0N/A /**
0N/A * Returns the version number of this PKCS7 block.
0N/A * @return the version or null if version is not specified
0N/A * for the content type.
0N/A */
0N/A public BigInteger getVersion() {
0N/A return version;
0N/A }
0N/A
0N/A /**
0N/A * Returns the message digest algorithms specified in this PKCS7 block.
0N/A * @return the array of Digest Algorithms or null if none are specified
0N/A * for the content type.
0N/A */
0N/A public AlgorithmId[] getDigestAlgorithmIds() {
0N/A return digestAlgorithmIds;
0N/A }
0N/A
0N/A /**
0N/A * Returns the content information specified in this PKCS7 block.
0N/A */
0N/A public ContentInfo getContentInfo() {
0N/A return contentInfo;
0N/A }
0N/A
0N/A /**
0N/A * Returns the X.509 certificates listed in this PKCS7 block.
0N/A * @return a clone of the array of X.509 certificates or null if
0N/A * none are specified for the content type.
0N/A */
0N/A public X509Certificate[] getCertificates() {
0N/A if (certificates != null)
0N/A return certificates.clone();
0N/A else
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the X.509 crls listed in this PKCS7 block.
0N/A * @return a clone of the array of X.509 crls or null if none
0N/A * are specified for the content type.
0N/A */
0N/A public X509CRL[] getCRLs() {
0N/A if (crls != null)
0N/A return crls.clone();
0N/A else
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the signer's information specified in this PKCS7 block.
0N/A * @return the array of Signer Infos or null if none are specified
0N/A * for the content type.
0N/A */
0N/A public SignerInfo[] getSignerInfos() {
0N/A return signerInfos;
0N/A }
0N/A
0N/A /**
0N/A * Returns the X.509 certificate listed in this PKCS7 block
0N/A * which has a matching serial number and Issuer name, or
0N/A * null if one is not found.
0N/A *
0N/A * @param serial the serial number of the certificate to retrieve.
0N/A * @param issuerName the Distinguished Name of the Issuer.
0N/A */
0N/A public X509Certificate getCertificate(BigInteger serial, X500Name issuerName) {
0N/A if (certificates != null) {
0N/A if (certIssuerNames == null)
0N/A populateCertIssuerNames();
0N/A for (int i = 0; i < certificates.length; i++) {
0N/A X509Certificate cert = certificates[i];
0N/A BigInteger thisSerial = cert.getSerialNumber();
0N/A if (serial.equals(thisSerial)
0N/A && issuerName.equals(certIssuerNames[i]))
0N/A {
0N/A return cert;
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Populate array of Issuer DNs from certificates and convert
0N/A * each Principal to type X500Name if necessary.
0N/A */
0N/A private void populateCertIssuerNames() {
0N/A if (certificates == null)
0N/A return;
0N/A
0N/A certIssuerNames = new Principal[certificates.length];
0N/A for (int i = 0; i < certificates.length; i++) {
0N/A X509Certificate cert = certificates[i];
0N/A Principal certIssuerName = cert.getIssuerDN();
0N/A if (!(certIssuerName instanceof X500Name)) {
0N/A // must extract the original encoded form of DN for
0N/A // subsequent name comparison checks (converting to a
0N/A // String and back to an encoded DN could cause the
0N/A // types of String attribute values to be changed)
0N/A try {
0N/A X509CertInfo tbsCert =
0N/A new X509CertInfo(cert.getTBSCertificate());
0N/A certIssuerName = (Principal)
0N/A tbsCert.get(CertificateIssuerName.NAME + "." +
0N/A CertificateIssuerName.DN_NAME);
0N/A } catch (Exception e) {
0N/A // error generating X500Name object from the cert's
0N/A // issuer DN, leave name as is.
0N/A }
0N/A }
0N/A certIssuerNames[i] = certIssuerName;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the PKCS7 block in a printable string form.
0N/A */
0N/A public String toString() {
0N/A String out = "";
0N/A
0N/A out += contentInfo + "\n";
0N/A if (version != null)
0N/A out += "PKCS7 :: version: " + Debug.toHexString(version) + "\n";
0N/A if (digestAlgorithmIds != null) {
0N/A out += "PKCS7 :: digest AlgorithmIds: \n";
0N/A for (int i = 0; i < digestAlgorithmIds.length; i++)
0N/A out += "\t" + digestAlgorithmIds[i] + "\n";
0N/A }
0N/A if (certificates != null) {
0N/A out += "PKCS7 :: certificates: \n";
0N/A for (int i = 0; i < certificates.length; i++)
0N/A out += "\t" + i + ". " + certificates[i] + "\n";
0N/A }
0N/A if (crls != null) {
0N/A out += "PKCS7 :: crls: \n";
0N/A for (int i = 0; i < crls.length; i++)
0N/A out += "\t" + i + ". " + crls[i] + "\n";
0N/A }
0N/A if (signerInfos != null) {
0N/A out += "PKCS7 :: signer infos: \n";
0N/A for (int i = 0; i < signerInfos.length; i++)
0N/A out += ("\t" + i + ". " + signerInfos[i] + "\n");
0N/A }
0N/A return out;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this is a JDK1.1.x-style PKCS#7 block, and false
0N/A * otherwise.
0N/A */
0N/A public boolean isOldStyle() {
0N/A return this.oldStyle;
0N/A }
0N/A}