jdk/src/java.base/share/classes/sun/security/pkcs/PKCS7.java
author darcy
Thu, 23 Apr 2015 18:51:18 -0700
changeset 30033 b9c86c17164a
parent 25859 3317bb8137f4
child 31426 9cd672654f97
permissions -rw-r--r--
8078468: Update security libraries to use diamond with anonymous classes Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 17161
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5462
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    30
import java.net.URI;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.X509CRL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.cert.CRLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    39
import sun.security.timestamp.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.security.x509.X509CertImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.x509.X509CertInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.x509.X509CRLImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * PKCS7 as defined in RSA Laboratories PKCS7 Technical Note. Profile
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Supports only <tt>SignedData</tt> ContentInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * type, where to the type of data signed is plain Data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * For signedData, <tt>crls</tt>, <tt>attributes</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * PKCS#6 Extended Certificates are not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class PKCS7 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private ObjectIdentifier contentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // the ASN.1 members for a signedData (and other) contentTypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private BigInteger version = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private AlgorithmId[] digestAlgorithmIds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private ContentInfo contentInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private X509Certificate[] certificates = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private X509CRL[] crls = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private SignerInfo[] signerInfos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private boolean oldStyle = false; // Is this JDK1.1.x-style?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private Principal[] certIssuerNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    72
    /*
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    73
     * Random number generator for creating nonce values
11842
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
    74
     * (Lazy initialization)
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    75
     */
11842
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
    76
    private static class SecureRandomHolder {
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
    77
        static final SecureRandom RANDOM;
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
    78
        static {
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
    79
            SecureRandom tmp = null;
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
    80
            try {
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
    81
                tmp = SecureRandom.getInstance("SHA1PRNG");
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
    82
            } catch (NoSuchAlgorithmException e) {
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
    83
                // should not happen
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
    84
            }
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
    85
            RANDOM = tmp;
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    86
        }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    87
    }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    88
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    89
    /*
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    90
     * Object identifier for the timestamping key purpose.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    91
     */
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    92
    private static final String KP_TIMESTAMPING_OID = "1.3.6.1.5.5.7.3.8";
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    93
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    94
    /*
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    95
     * Object identifier for extendedKeyUsage extension
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    96
     */
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    97
    private static final String EXTENDED_KEY_USAGE_OID = "2.5.29.37";
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    98
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Unmarshals a PKCS7 block from its encoded form, parsing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * encoded bytes from the InputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param in an input stream holding at least one PKCS7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @exception ParsingException on parsing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @exception IOException on other errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public PKCS7(InputStream in) throws ParsingException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        DataInputStream dis = new DataInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        byte[] data = new byte[dis.available()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        dis.readFully(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        parse(new DerInputStream(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Unmarshals a PKCS7 block from its encoded form, parsing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * encoded bytes from the DerInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param derin a DerInputStream holding at least one PKCS7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @exception ParsingException on parsing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public PKCS7(DerInputStream derin) throws ParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        parse(derin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Unmarshals a PKCS7 block from its encoded form, parsing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * encoded bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param bytes the encoded bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @exception ParsingException on parsing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public PKCS7(byte[] bytes) throws ParsingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            DerInputStream derin = new DerInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            parse(derin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (IOException ioe1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            ParsingException pe = new ParsingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                "Unable to parse the encoded bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            pe.initCause(ioe1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Parses a PKCS#7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private void parse(DerInputStream derin)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        throws ParsingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            derin.mark(derin.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // try new (i.e., JDK1.2) style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            parse(derin, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                derin.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                // try old (i.e., JDK1.1.x) style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                parse(derin, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                oldStyle = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } catch (IOException ioe1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                ParsingException pe = new ParsingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    ioe1.getMessage());
16849
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   164
                pe.initCause(ioe);
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   165
                pe.addSuppressed(ioe1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Parses a PKCS#7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param derin the ASN.1 encoding of the PKCS#7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param oldStyle flag indicating whether or not the given PKCS#7 block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * is encoded according to JDK1.1.x.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private void parse(DerInputStream derin, boolean oldStyle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        contentInfo = new ContentInfo(derin, oldStyle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        contentType = contentInfo.contentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        DerValue content = contentInfo.getContent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9365
diff changeset
   185
        if (contentType.equals((Object)ContentInfo.SIGNED_DATA_OID)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            parseSignedData(content);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9365
diff changeset
   187
        } else if (contentType.equals((Object)ContentInfo.OLD_SIGNED_DATA_OID)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // This is for backwards compatibility with JDK 1.1.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            parseOldSignedData(content);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9365
diff changeset
   190
        } else if (contentType.equals((Object)
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9365
diff changeset
   191
                       ContentInfo.NETSCAPE_CERT_SEQUENCE_OID)){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            parseNetscapeCertChain(content);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new ParsingException("content type " + contentType +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                       " not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Construct an initialized PKCS7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param digestAlgorithmIds the message digest algorithm identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param contentInfo the content information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param certificates an array of X.509 certificates.
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   205
     * @param crls an array of CRLs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param signerInfos an array of signer information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public PKCS7(AlgorithmId[] digestAlgorithmIds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                 ContentInfo contentInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                 X509Certificate[] certificates,
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   211
                 X509CRL[] crls,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                 SignerInfo[] signerInfos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        version = BigInteger.ONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.digestAlgorithmIds = digestAlgorithmIds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        this.contentInfo = contentInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        this.certificates = certificates;
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   218
        this.crls = crls;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        this.signerInfos = signerInfos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   222
    public PKCS7(AlgorithmId[] digestAlgorithmIds,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   223
                 ContentInfo contentInfo,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   224
                 X509Certificate[] certificates,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   225
                 SignerInfo[] signerInfos) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   226
        this(digestAlgorithmIds, contentInfo, certificates, null, signerInfos);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   227
    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   228
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    private void parseNetscapeCertChain(DerValue val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    throws ParsingException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        DerInputStream dis = new DerInputStream(val.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        DerValue[] contents = dis.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        certificates = new X509Certificate[contents.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        CertificateFactory certfac = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            certfac = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        for (int i=0; i < contents.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            ByteArrayInputStream bais = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                if (certfac == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    certificates[i] = new X509CertImpl(contents[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    byte[] encoded = contents[i].toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    bais = new ByteArrayInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    certificates[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        (X509Certificate)certfac.generateCertificate(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    bais = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                ParsingException pe = new ParsingException(ce.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                pe.initCause(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                ParsingException pe = new ParsingException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                pe.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                if (bais != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private void parseSignedData(DerValue val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        throws ParsingException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        DerInputStream dis = val.toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // Version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        version = dis.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // digestAlgorithmIds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        DerValue[] digestAlgorithmIdVals = dis.getSet(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        int len = digestAlgorithmIdVals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        digestAlgorithmIds = new AlgorithmId[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                DerValue oid = digestAlgorithmIdVals[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                digestAlgorithmIds[i] = AlgorithmId.parse(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            ParsingException pe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                new ParsingException("Error parsing digest AlgorithmId IDs: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                     e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            pe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // contentInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        contentInfo = new ContentInfo(dis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        CertificateFactory certfac = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            certfac = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         * check if certificates (implicit tag) are provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * (certificates are OPTIONAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if ((byte)(dis.peekByte()) == (byte)0xA0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            DerValue[] certVals = dis.getSet(2, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            len = certVals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            certificates = new X509Certificate[len];
16849
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   314
            int count = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                ByteArrayInputStream bais = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                try {
16849
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   319
                    byte tag = certVals[i].getTag();
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   320
                    // We only parse the normal certificate. Other types of
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   321
                    // CertificateChoices ignored.
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   322
                    if (tag == DerValue.tag_Sequence) {
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   323
                        if (certfac == null) {
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   324
                            certificates[count] = new X509CertImpl(certVals[i]);
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   325
                        } else {
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   326
                            byte[] encoded = certVals[i].toByteArray();
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   327
                            bais = new ByteArrayInputStream(encoded);
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   328
                            certificates[count] =
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   329
                                (X509Certificate)certfac.generateCertificate(bais);
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   330
                            bais.close();
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   331
                            bais = null;
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   332
                        }
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   333
                        count++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    ParsingException pe = new ParsingException(ce.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    pe.initCause(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    ParsingException pe = new ParsingException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    pe.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    if (bais != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
16849
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   348
            if (count != len) {
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   349
                certificates = Arrays.copyOf(certificates, count);
7556ca6597d5 8011745: Unknown CertificateChoices
weijun
parents: 14421
diff changeset
   350
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // check if crls (implicit tag) are provided (crls are OPTIONAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if ((byte)(dis.peekByte()) == (byte)0xA1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            DerValue[] crlVals = dis.getSet(1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            len = crlVals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            crls = new X509CRL[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                ByteArrayInputStream bais = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    if (certfac == null)
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   364
                        crls[i] = new X509CRLImpl(crlVals[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        byte[] encoded = crlVals[i].toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        bais = new ByteArrayInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        crls[i] = (X509CRL) certfac.generateCRL(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        bais = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                } catch (CRLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    ParsingException pe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        new ParsingException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    pe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    if (bais != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // signerInfos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        DerValue[] signerInfoVals = dis.getSet(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        len = signerInfoVals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        signerInfos = new SignerInfo[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            DerInputStream in = signerInfoVals[i].toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            signerInfos[i] = new SignerInfo(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Parses an old-style SignedData encoding (for backwards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * compatibility with JDK1.1.x).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    private void parseOldSignedData(DerValue val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        DerInputStream dis = val.toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // Version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        version = dis.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        // digestAlgorithmIds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        DerValue[] digestAlgorithmIdVals = dis.getSet(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        int len = digestAlgorithmIdVals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        digestAlgorithmIds = new AlgorithmId[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                DerValue oid = digestAlgorithmIdVals[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                digestAlgorithmIds[i] = AlgorithmId.parse(oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            throw new ParsingException("Error parsing digest AlgorithmId IDs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        // contentInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        contentInfo = new ContentInfo(dis, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        CertificateFactory certfac = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            certfac = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        DerValue[] certVals = dis.getSet(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        len = certVals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        certificates = new X509Certificate[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            ByteArrayInputStream bais = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                if (certfac == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    certificates[i] = new X509CertImpl(certVals[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    byte[] encoded = certVals[i].toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    bais = new ByteArrayInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    certificates[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        (X509Certificate)certfac.generateCertificate(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    bais = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                ParsingException pe = new ParsingException(ce.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                pe.initCause(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                ParsingException pe = new ParsingException(ioe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                pe.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if (bais != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        // crls are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        dis.getSet(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // signerInfos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        DerValue[] signerInfoVals = dis.getSet(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        len = signerInfoVals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        signerInfos = new SignerInfo[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            DerInputStream in = signerInfoVals[i].toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            signerInfos[i] = new SignerInfo(in, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Encodes the signed data to an output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param out the output stream to write the encoded data to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public void encodeSignedData(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        DerOutputStream derout = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        encodeSignedData(derout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        out.write(derout.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Encodes the signed data to a DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param out the DerOutputStream to write the encoded data to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    public void encodeSignedData(DerOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        DerOutputStream signedData = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        // version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        signedData.putInteger(version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // digestAlgorithmIds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        signedData.putOrderedSetOf(DerValue.tag_Set, digestAlgorithmIds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        // contentInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        contentInfo.encode(signedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        // certificates (optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (certificates != null && certificates.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            // cast to X509CertImpl[] since X509CertImpl implements DerEncoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            X509CertImpl implCerts[] = new X509CertImpl[certificates.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            for (int i = 0; i < certificates.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                if (certificates[i] instanceof X509CertImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    implCerts[i] = (X509CertImpl) certificates[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                        byte[] encoded = certificates[i].getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        implCerts[i] = new X509CertImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    } catch (CertificateException ce) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9365
diff changeset
   520
                        throw new IOException(ce);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            // Add the certificate set (tagged with [0] IMPLICIT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            // to the signed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            signedData.putOrderedSetOf((byte)0xA0, implCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   530
        // CRLs (optional)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   531
        if (crls != null && crls.length != 0) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   532
            // cast to X509CRLImpl[] since X509CRLImpl implements DerEncoder
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   533
            Set<X509CRLImpl> implCRLs = new HashSet<>(crls.length);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   534
            for (X509CRL crl: crls) {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   535
                if (crl instanceof X509CRLImpl)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   536
                    implCRLs.add((X509CRLImpl) crl);
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   537
                else {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   538
                    try {
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   539
                        byte[] encoded = crl.getEncoded();
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   540
                        implCRLs.add(new X509CRLImpl(encoded));
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   541
                    } catch (CRLException ce) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9365
diff changeset
   542
                        throw new IOException(ce);
5462
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   543
                    }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   544
                }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   545
            }
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   546
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   547
            // Add the CRL set (tagged with [1] IMPLICIT)
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   548
            // to the signed data
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   549
            signedData.putOrderedSetOf((byte)0xA1,
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   550
                    implCRLs.toArray(new X509CRLImpl[implCRLs.size()]));
cb614e59f7f9 6890876: jarsigner can add CRL info into signed jar
weijun
parents: 2
diff changeset
   551
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        // signerInfos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        signedData.putOrderedSetOf(DerValue.tag_Set, signerInfos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        // making it a signed data block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        DerValue signedDataSeq = new DerValue(DerValue.tag_Sequence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                              signedData.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        // making it a content info sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        ContentInfo block = new ContentInfo(ContentInfo.SIGNED_DATA_OID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                                            signedDataSeq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // writing out the contentInfo sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        block.encode(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * This verifies a given SignerInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @param info the signer information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @param bytes the DER encoded content information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @exception NoSuchAlgorithmException on unrecognized algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @exception SignatureException on signature handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public SignerInfo verify(SignerInfo info, byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    throws NoSuchAlgorithmException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return info.verify(this, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Returns all signerInfos which self-verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @param bytes the DER encoded content information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @exception NoSuchAlgorithmException on unrecognized algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @exception SignatureException on signature handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    public SignerInfo[] verify(byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    throws NoSuchAlgorithmException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   593
        Vector<SignerInfo> intResult = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        for (int i = 0; i < signerInfos.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            SignerInfo signerInfo = verify(signerInfos[i], bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            if (signerInfo != null) {
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   598
                intResult.addElement(signerInfo);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9365
diff changeset
   601
        if (!intResult.isEmpty()) {
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   602
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   603
            SignerInfo[] result = new SignerInfo[intResult.size()];
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   604
            intResult.copyInto(result);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   605
            return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Returns all signerInfos which self-verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @exception NoSuchAlgorithmException on unrecognized algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @exception SignatureException on signature handling errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public SignerInfo[] verify()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    throws NoSuchAlgorithmException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return verify(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Returns the version number of this PKCS7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @return the version or null if version is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *         for the content type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    public  BigInteger getVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        return version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * Returns the message digest algorithms specified in this PKCS7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @return the array of Digest Algorithms or null if none are specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *         for the content type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    public AlgorithmId[] getDigestAlgorithmIds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return  digestAlgorithmIds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Returns the content information specified in this PKCS7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    public ContentInfo getContentInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return contentInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Returns the X.509 certificates listed in this PKCS7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @return a clone of the array of X.509 certificates or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *         none are specified for the content type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    public X509Certificate[] getCertificates() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        if (certificates != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            return certificates.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Returns the X.509 crls listed in this PKCS7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @return a clone of the array of X.509 crls or null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *         are specified for the content type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    public X509CRL[] getCRLs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if (crls != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            return crls.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Returns the signer's information specified in this PKCS7 block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @return the array of Signer Infos or null if none are specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *         for the content type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public SignerInfo[] getSignerInfos() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return signerInfos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * Returns the X.509 certificate listed in this PKCS7 block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * which has a matching serial number and Issuer name, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * null if one is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @param serial the serial number of the certificate to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @param issuerName the Distinguished Name of the Issuer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public X509Certificate getCertificate(BigInteger serial, X500Name issuerName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (certificates != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            if (certIssuerNames == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                populateCertIssuerNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            for (int i = 0; i < certificates.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                X509Certificate cert = certificates[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                BigInteger thisSerial = cert.getSerialNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                if (serial.equals(thisSerial)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    && issuerName.equals(certIssuerNames[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    return cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * Populate array of Issuer DNs from certificates and convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * each Principal to type X500Name if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    private void populateCertIssuerNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        if (certificates == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        certIssuerNames = new Principal[certificates.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        for (int i = 0; i < certificates.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            X509Certificate cert = certificates[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            Principal certIssuerName = cert.getIssuerDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            if (!(certIssuerName instanceof X500Name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                // must extract the original encoded form of DN for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                // subsequent name comparison checks (converting to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                // String and back to an encoded DN could cause the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                // types of String attribute values to be changed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    X509CertInfo tbsCert =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                        new X509CertInfo(cert.getTBSCertificate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    certIssuerName = (Principal)
14421
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 13361
diff changeset
   725
                        tbsCert.get(X509CertInfo.ISSUER + "." +
a64b2cc9d429 7198416: CertificateIssuerName and CertificateSubjectName are redundant
mullan
parents: 13361
diff changeset
   726
                                    X509CertInfo.DN_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    // error generating X500Name object from the cert's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    // issuer DN, leave name as is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            certIssuerNames[i] = certIssuerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * Returns the PKCS7 block in a printable string form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        String out = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        out += contentInfo + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        if (version != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            out += "PKCS7 :: version: " + Debug.toHexString(version) + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (digestAlgorithmIds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            out += "PKCS7 :: digest AlgorithmIds: \n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            for (int i = 0; i < digestAlgorithmIds.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                out += "\t" + digestAlgorithmIds[i] + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        if (certificates != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            out += "PKCS7 :: certificates: \n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            for (int i = 0; i < certificates.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                out += "\t" + i + ".   " + certificates[i] + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if (crls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            out += "PKCS7 :: crls: \n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            for (int i = 0; i < crls.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                out += "\t" + i + ".   " + crls[i] + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if (signerInfos != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            out += "PKCS7 :: signer infos: \n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            for (int i = 0; i < signerInfos.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                out += ("\t" + i + ".  " + signerInfos[i] + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * Returns true if this is a JDK1.1.x-style PKCS#7 block, and false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    public boolean isOldStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        return this.oldStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   775
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   776
    /**
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   777
     * Assembles a PKCS #7 signed data message that optionally includes a
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   778
     * signature timestamp.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   779
     *
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   780
     * @param signature the signature bytes
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   781
     * @param signerChain the signer's X.509 certificate chain
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   782
     * @param content the content that is signed; specify null to not include
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   783
     *        it in the PKCS7 data
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   784
     * @param signatureAlgorithm the name of the signature algorithm
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   785
     * @param tsaURI the URI of the Timestamping Authority; or null if no
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   786
     *         timestamp is requested
17161
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   787
     * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   788
     *         numerical object identifier; or null if we leave the TSA server
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   789
     *         to choose one. This argument is only used when tsaURI is provided
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   790
     * @return the bytes of the encoded PKCS #7 signed data message
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   791
     * @throws NoSuchAlgorithmException The exception is thrown if the signature
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   792
     *         algorithm is unrecognised.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   793
     * @throws CertificateException The exception is thrown if an error occurs
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   794
     *         while processing the signer's certificate or the TSA's
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   795
     *         certificate.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   796
     * @throws IOException The exception is thrown if an error occurs while
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   797
     *         generating the signature timestamp or while generating the signed
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   798
     *         data message.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   799
     */
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   800
    public static byte[] generateSignedData(byte[] signature,
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   801
                                            X509Certificate[] signerChain,
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   802
                                            byte[] content,
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   803
                                            String signatureAlgorithm,
17161
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   804
                                            URI tsaURI,
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   805
                                            String tSAPolicyID,
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   806
                                            String tSADigestAlg)
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   807
        throws CertificateException, IOException, NoSuchAlgorithmException
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   808
    {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   809
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   810
        // Generate the timestamp token
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   811
        PKCS9Attributes unauthAttrs = null;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   812
        if (tsaURI != null) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   813
            // Timestamp the signature
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   814
            HttpTimestamper tsa = new HttpTimestamper(tsaURI);
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   815
            byte[] tsToken = generateTimestampToken(
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   816
                    tsa, tSAPolicyID, tSADigestAlg, signature);
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   817
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   818
            // Insert the timestamp token into the PKCS #7 signer info element
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   819
            // (as an unsigned attribute)
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   820
            unauthAttrs =
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   821
                new PKCS9Attributes(new PKCS9Attribute[]{
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   822
                    new PKCS9Attribute(
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   823
                        PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   824
                        tsToken)});
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   825
        }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   826
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   827
        // Create the SignerInfo
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   828
        X500Name issuerName =
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   829
            X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   830
        BigInteger serialNumber = signerChain[0].getSerialNumber();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   831
        String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   832
        String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   833
        SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   834
                                               AlgorithmId.get(digAlg), null,
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   835
                                               AlgorithmId.get(encAlg),
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   836
                                               signature, unauthAttrs);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   837
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   838
        // Create the PKCS #7 signed data message
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   839
        SignerInfo[] signerInfos = {signerInfo};
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   840
        AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   841
        // Include or exclude content
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   842
        ContentInfo contentInfo = (content == null)
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   843
            ? new ContentInfo(ContentInfo.DATA_OID, null)
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   844
            : new ContentInfo(content);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   845
        PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   846
                                signerChain, signerInfos);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   847
        ByteArrayOutputStream p7out = new ByteArrayOutputStream();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   848
        pkcs7.encodeSignedData(p7out);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   849
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   850
        return p7out.toByteArray();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   851
    }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   852
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   853
    /**
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   854
     * Requests, processes and validates a timestamp token from a TSA using
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   855
     * common defaults. Uses the following defaults in the timestamp request:
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   856
     * SHA-1 for the hash algorithm, a 64-bit nonce, and request certificate
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   857
     * set to true.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   858
     *
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   859
     * @param tsa the timestamping authority to use
17161
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   860
     * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   861
     *         numerical object identifier; or null if we leave the TSA server
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   862
     *         to choose one
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   863
     * @param toBeTimestamped the token that is to be timestamped
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   864
     * @return the encoded timestamp token
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   865
     * @throws IOException The exception is thrown if an error occurs while
17161
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   866
     *                     communicating with the TSA, or a non-null
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   867
     *                     TSAPolicyID is specified in the request but it
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   868
     *                     does not match the one in the reply
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   869
     * @throws CertificateException The exception is thrown if the TSA's
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   870
     *                     certificate is not permitted for timestamping.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   871
     */
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   872
    private static byte[] generateTimestampToken(Timestamper tsa,
17161
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   873
                                                 String tSAPolicyID,
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   874
                                                 String tSADigestAlg,
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   875
                                                 byte[] toBeTimestamped)
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   876
        throws IOException, CertificateException
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   877
    {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   878
        // Generate a timestamp
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   879
        MessageDigest messageDigest = null;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   880
        TSRequest tsQuery = null;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   881
        try {
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   882
            messageDigest = MessageDigest.getInstance(tSADigestAlg);
17161
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   883
            tsQuery = new TSRequest(tSAPolicyID, toBeTimestamped, messageDigest);
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   884
        } catch (NoSuchAlgorithmException e) {
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   885
            throw new IllegalArgumentException(e);
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   886
        }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   887
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   888
        // Generate a nonce
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   889
        BigInteger nonce = null;
11842
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
   890
        if (SecureRandomHolder.RANDOM != null) {
c2259ebc75e1 7142339: PKCS7.java is needlessly creating SHA1PRNG SecureRandom instances when timestamping is not done
vinnie
parents: 10788
diff changeset
   891
            nonce = new BigInteger(64, SecureRandomHolder.RANDOM);
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   892
            tsQuery.setNonce(nonce);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   893
        }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   894
        tsQuery.requestCertificate(true);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   895
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   896
        TSResponse tsReply = tsa.generateTimestamp(tsQuery);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   897
        int status = tsReply.getStatusCode();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   898
        // Handle TSP error
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   899
        if (status != 0 && status != 1) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   900
            throw new IOException("Error generating timestamp: " +
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   901
                tsReply.getStatusCodeAsText() + " " +
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   902
                tsReply.getFailureCodeAsText());
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   903
        }
17161
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   904
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   905
        if (tSAPolicyID != null &&
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   906
                !tSAPolicyID.equals(tsReply.getTimestampToken().getPolicyID())) {
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   907
            throw new IOException("TSAPolicyID changed in "
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   908
                    + "timestamp token");
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 16849
diff changeset
   909
        }
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   910
        PKCS7 tsToken = tsReply.getToken();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   911
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   912
        TimestampToken tst = tsReply.getTimestampToken();
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   913
        try {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   914
            if (!tst.getHashAlgorithm().equals(AlgorithmId.get(tSADigestAlg))) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   915
                throw new IOException("Digest algorithm not " + tSADigestAlg + " in "
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   916
                                      + "timestamp token");
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   917
            }
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   918
        } catch (NoSuchAlgorithmException nase) {
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   919
            throw new IllegalArgumentException();   // should have been caught before
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   920
        }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   921
        if (!MessageDigest.isEqual(tst.getHashedMessage(),
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   922
                                   tsQuery.getHashedMessage())) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   923
            throw new IOException("Digest octets changed in timestamp token");
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   924
        }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   925
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   926
        BigInteger replyNonce = tst.getNonce();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   927
        if (replyNonce == null && nonce != null) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   928
            throw new IOException("Nonce missing in timestamp token");
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   929
        }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   930
        if (replyNonce != null && !replyNonce.equals(nonce)) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   931
            throw new IOException("Nonce changed in timestamp token");
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   932
        }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   933
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   934
        // Examine the TSA's certificate (if present)
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   935
        for (SignerInfo si: tsToken.getSignerInfos()) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   936
            X509Certificate cert = si.getCertificate(tsToken);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   937
            if (cert == null) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   938
                // Error, we've already set tsRequestCertificate = true
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   939
                throw new CertificateException(
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   940
                "Certificate not included in timestamp token");
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   941
            } else {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   942
                if (!cert.getCriticalExtensionOIDs().contains(
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   943
                        EXTENDED_KEY_USAGE_OID)) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   944
                    throw new CertificateException(
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   945
                    "Certificate is not valid for timestamping");
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   946
                }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   947
                List<String> keyPurposes = cert.getExtendedKeyUsage();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   948
                if (keyPurposes == null ||
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   949
                        !keyPurposes.contains(KP_TIMESTAMPING_OID)) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   950
                    throw new CertificateException(
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   951
                    "Certificate is not valid for timestamping");
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   952
                }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   953
            }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   954
        }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   955
        return tsReply.getEncodedToken();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   956
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
}