jdk/src/java.base/share/classes/sun/security/pkcs/SignerInfo.java
author ascarpino
Thu, 21 Jul 2016 10:33:56 -0700
changeset 41580 cc479488428c
parent 34687 d302ed125dc9
child 41582 246512d81eba
permissions -rw-r--r--
8155973: Tighten jar checks Reviewed-by: mullan, igerasim, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
     2
 * Copyright (c) 1996, 2016, 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: 4152
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: 4152
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: 4152
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4152
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.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.math.BigInteger;
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    31
import java.security.CryptoPrimitive;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    32
import java.security.InvalidKeyException;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    33
import java.security.MessageDigest;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    34
import java.security.NoSuchAlgorithmException;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    35
import java.security.Principal;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    36
import java.security.PublicKey;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    37
import java.security.Signature;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    38
import java.security.SignatureException;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    39
import java.security.Timestamp;
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    40
import java.security.cert.CertificateException;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    41
import java.security.cert.CertificateFactory;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    42
import java.security.cert.CertPath;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.ArrayList;
18224
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
    45
import java.util.Arrays;
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    46
import java.util.Collections;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    47
import java.util.EnumSet;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    48
import java.util.Set;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    50
import sun.security.timestamp.TimestampToken;
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    51
import sun.security.util.Debug;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    52
import sun.security.util.DerEncoder;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    53
import sun.security.util.DerInputStream;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    54
import sun.security.util.DerOutputStream;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    55
import sun.security.util.DerValue;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    56
import sun.security.util.DisabledAlgorithmConstraints;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    57
import sun.security.util.HexDumpEncoder;
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    58
import sun.security.util.ObjectIdentifier;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.security.x509.X500Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import sun.security.x509.KeyUsageExtension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * A SignerInfo, as defined in PKCS#7's signedData type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class SignerInfo implements DerEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    70
    // Digest and Signature restrictions
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    71
    private static final Set<CryptoPrimitive> DIGEST_PRIMITIVE_SET =
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    72
            Collections.unmodifiableSet(EnumSet.of(CryptoPrimitive.MESSAGE_DIGEST));
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    73
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    74
    private static final Set<CryptoPrimitive> SIG_PRIMITIVE_SET =
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    75
            Collections.unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE));
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    76
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    77
    private static final DisabledAlgorithmConstraints JAR_DISABLED_CHECK =
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    78
            new DisabledAlgorithmConstraints(
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    79
                    DisabledAlgorithmConstraints.PROPERTY_JAR_DISABLED_ALGS);
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
    80
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    BigInteger version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    X500Name issuerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    BigInteger certificateSerialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    AlgorithmId digestAlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    AlgorithmId digestEncryptionAlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    byte[] encryptedDigest;
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    87
    Timestamp timestamp;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    88
    private boolean hasTimestamp = true;
18224
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
    89
    private static final Debug debug = Debug.getInstance("jar");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    PKCS9Attributes authenticatedAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    PKCS9Attributes unauthenticatedAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public SignerInfo(X500Name  issuerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                      BigInteger serial,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                      AlgorithmId digestAlgorithmId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                      AlgorithmId digestEncryptionAlgorithmId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                      byte[] encryptedDigest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.version = BigInteger.ONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.issuerName = issuerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.certificateSerialNumber = serial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.digestAlgorithmId = digestAlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.digestEncryptionAlgorithmId = digestEncryptionAlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.encryptedDigest = encryptedDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public SignerInfo(X500Name  issuerName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                      BigInteger serial,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                      AlgorithmId digestAlgorithmId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                      PKCS9Attributes authenticatedAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                      AlgorithmId digestEncryptionAlgorithmId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                      byte[] encryptedDigest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                      PKCS9Attributes unauthenticatedAttributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this.version = BigInteger.ONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.issuerName = issuerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.certificateSerialNumber = serial;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.digestAlgorithmId = digestAlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.authenticatedAttributes = authenticatedAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.digestEncryptionAlgorithmId = digestEncryptionAlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.encryptedDigest = encryptedDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.unauthenticatedAttributes = unauthenticatedAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Parses a PKCS#7 signer info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public SignerInfo(DerInputStream derin)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        throws IOException, ParsingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this(derin, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Parses a PKCS#7 signer info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p>This constructor is used only for backwards compatibility with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * PKCS#7 blocks that were generated using JDK1.1.x.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param derin the ASN.1 encoding of the signer info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param oldStyle flag indicating whether or not the given signer info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * is encoded according to JDK1.1.x.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public SignerInfo(DerInputStream derin, boolean oldStyle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        throws IOException, ParsingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        version = derin.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // issuerAndSerialNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        DerValue[] issuerAndSerialNumber = derin.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        byte[] issuerBytes = issuerAndSerialNumber[0].toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        issuerName = new X500Name(new DerValue(DerValue.tag_Sequence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                               issuerBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        certificateSerialNumber = issuerAndSerialNumber[1].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // digestAlgorithmId
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        DerValue tmp = derin.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        digestAlgorithmId = AlgorithmId.parse(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // authenticatedAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (oldStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // In JDK1.1.x, the authenticatedAttributes are always present,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // encoded as an empty Set (Set of length zero)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            derin.getSet(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // check if set of auth attributes (implicit tag) is provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // (auth attributes are OPTIONAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if ((byte)(derin.peekByte()) == (byte)0xA0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                authenticatedAttributes = new PKCS9Attributes(derin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // digestEncryptionAlgorithmId - little RSA naming scheme -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // signature == encryption...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        tmp = derin.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        digestEncryptionAlgorithmId = AlgorithmId.parse(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // encryptedDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        encryptedDigest = derin.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // unauthenticatedAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (oldStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // In JDK1.1.x, the unauthenticatedAttributes are always present,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            // encoded as an empty Set (Set of length zero)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            derin.getSet(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // check if set of unauth attributes (implicit tag) is provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            // (unauth attributes are OPTIONAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (derin.available() != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                && (byte)(derin.peekByte()) == (byte)0xA1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                unauthenticatedAttributes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    new PKCS9Attributes(derin, true);// ignore unsupported attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // all done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (derin.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new ParsingException("extra data at the end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        derEncode(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * DER encode this object onto an output stream.
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 31426
diff changeset
   211
     * Implements the {@code DerEncoder} interface.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * the output stream on which to write the DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @exception IOException on encoding error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public void derEncode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        DerOutputStream seq = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        seq.putInteger(version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        DerOutputStream issuerAndSerialNumber = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        issuerName.encode(issuerAndSerialNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        issuerAndSerialNumber.putInteger(certificateSerialNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        seq.write(DerValue.tag_Sequence, issuerAndSerialNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        digestAlgorithmId.encode(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // encode authenticated attributes if there are any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (authenticatedAttributes != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            authenticatedAttributes.encode((byte)0xA0, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        digestEncryptionAlgorithmId.encode(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        seq.putOctetString(encryptedDigest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // encode unauthenticated attributes if there are any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (unauthenticatedAttributes != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            unauthenticatedAttributes.encode((byte)0xA1, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        tmp.write(DerValue.tag_Sequence, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        out.write(tmp.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Returns the (user) certificate pertaining to this SignerInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public X509Certificate getCertificate(PKCS7 block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return block.getCertificate(certificateSerialNumber, issuerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Returns the certificate chain pertaining to this SignerInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public ArrayList<X509Certificate> getCertificateChain(PKCS7 block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        X509Certificate userCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        userCert = block.getCertificate(certificateSerialNumber, issuerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (userCert == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   268
        ArrayList<X509Certificate> certList = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        certList.add(userCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        X509Certificate[] pkcsCerts = block.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (pkcsCerts == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            || userCert.getSubjectDN().equals(userCert.getIssuerDN())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return certList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        Principal issuer = userCert.getIssuerDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            boolean match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            int i = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            while (i < pkcsCerts.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                if (issuer.equals(pkcsCerts[i].getSubjectDN())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    // next cert in chain found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    certList.add(pkcsCerts[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    // if selected cert is self-signed, we're done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    // constructing the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    if (pkcsCerts[i].getSubjectDN().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                            pkcsCerts[i].getIssuerDN())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        start = pkcsCerts.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        issuer = pkcsCerts[i].getIssuerDN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        X509Certificate tmpCert = pkcsCerts[start];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        pkcsCerts[start] = pkcsCerts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        pkcsCerts[i] = tmpCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        start++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (!match)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return certList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /* Returns null if verify fails, this signerInfo if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
       verify succeeds. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    SignerInfo verify(PKCS7 block, byte[] data)
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   314
    throws NoSuchAlgorithmException, SignatureException {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   315
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   316
        try {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   317
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   318
            ContentInfo content = block.getContentInfo();
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   319
            if (data == null) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   320
                data = content.getContentBytes();
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   321
            }
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   322
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   323
            String digestAlgname = getDigestAlgorithmId().getName();
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   324
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   325
            byte[] dataSigned;
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   326
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   327
            // if there are authenticate attributes, get the message
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   328
            // digest and compare it with the digest of data
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   329
            if (authenticatedAttributes == null) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   330
                dataSigned = data;
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   331
            } else {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   332
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   333
                // first, check content type
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   334
                ObjectIdentifier contentType = (ObjectIdentifier)
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   335
                       authenticatedAttributes.getAttributeValue(
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   336
                         PKCS9Attribute.CONTENT_TYPE_OID);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   337
                if (contentType == null ||
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30033
diff changeset
   338
                    !contentType.equals(content.contentType))
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   339
                    return null;  // contentType does not match, bad SignerInfo
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   340
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   341
                // now, check message digest
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   342
                byte[] messageDigest = (byte[])
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   343
                    authenticatedAttributes.getAttributeValue(
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   344
                         PKCS9Attribute.MESSAGE_DIGEST_OID);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   345
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   346
                if (messageDigest == null) // fail if there is no message digest
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   347
                    return null;
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   348
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   349
                // check that algorithm is not restricted
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   350
                if (!JAR_DISABLED_CHECK.permits(DIGEST_PRIMITIVE_SET,
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   351
                        digestAlgname, null)) {
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   352
                    throw new SignatureException("Digest check failed. " +
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   353
                            "Disabled algorithm used: " + digestAlgname);
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   354
                }
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   355
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   356
                MessageDigest md = MessageDigest.getInstance(digestAlgname);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   357
                byte[] computedMessageDigest = md.digest(data);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   358
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   359
                if (messageDigest.length != computedMessageDigest.length)
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   360
                    return null;
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   361
                for (int i = 0; i < messageDigest.length; i++) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   362
                    if (messageDigest[i] != computedMessageDigest[i])
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   363
                        return null;
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   364
                }
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   366
                // message digest attribute matched
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   367
                // digest of original data
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   368
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   369
                // the data actually signed is the DER encoding of
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   370
                // the authenticated attributes (tagged with
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   371
                // the "SET OF" tag, not 0xA0).
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   372
                dataSigned = authenticatedAttributes.getDerEncoding();
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   373
            }
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   374
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   375
            // put together digest algorithm and encryption algorithm
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   376
            // to form signing algorithm
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   377
            String encryptionAlgname =
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   378
                getDigestEncryptionAlgorithmId().getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   380
            // Workaround: sometimes the encryptionAlgname is actually
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   381
            // a signature name
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   382
            String tmp = AlgorithmId.getEncAlgFromSigAlg(encryptionAlgname);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   383
            if (tmp != null) encryptionAlgname = tmp;
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   384
            String algname = AlgorithmId.makeSigAlg(
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   385
                    digestAlgname, encryptionAlgname);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   386
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   387
            // check that algorithm is not restricted
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   388
            if (!JAR_DISABLED_CHECK.permits(SIG_PRIMITIVE_SET, algname, null)) {
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   389
                throw new SignatureException("Signature check failed. " +
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   390
                        "Disabled algorithm used: " + algname);
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   391
            }
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   392
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   393
            X509Certificate cert = getCertificate(block);
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   394
            PublicKey key = cert.getPublicKey();
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   395
            if (cert == null) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   396
                return null;
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   397
            }
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   398
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   399
            // check if the public key is restricted
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   400
            if (!JAR_DISABLED_CHECK.permits(SIG_PRIMITIVE_SET, key)) {
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   401
                throw new SignatureException("Public key check failed. " +
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   402
                        "Disabled algorithm used: " + key.getAlgorithm());
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   403
            }
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   404
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   405
            if (cert.hasUnsupportedCriticalExtension()) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   406
                throw new SignatureException("Certificate has unsupported "
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   407
                                             + "critical extension(s)");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   409
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   410
            // Make sure that if the usage of the key in the certificate is
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   411
            // restricted, it can be used for digital signatures.
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   412
            // XXX We may want to check for additional extensions in the
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   413
            // future.
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   414
            boolean[] keyUsageBits = cert.getKeyUsage();
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   415
            if (keyUsageBits != null) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   416
                KeyUsageExtension keyUsage;
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   417
                try {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   418
                    // We don't care whether or not this extension was marked
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   419
                    // critical in the certificate.
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   420
                    // We're interested only in its value (i.e., the bits set)
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   421
                    // and treat the extension as critical.
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   422
                    keyUsage = new KeyUsageExtension(keyUsageBits);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   423
                } catch (IOException ioe) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   424
                    throw new SignatureException("Failed to parse keyUsage "
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   425
                                                 + "extension");
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   426
                }
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   427
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9365
diff changeset
   428
                boolean digSigAllowed = keyUsage.get(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9365
diff changeset
   429
                        KeyUsageExtension.DIGITAL_SIGNATURE).booleanValue();
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   430
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9365
diff changeset
   431
                boolean nonRepuAllowed = keyUsage.get(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9365
diff changeset
   432
                        KeyUsageExtension.NON_REPUDIATION).booleanValue();
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   433
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   434
                if (!digSigAllowed && !nonRepuAllowed) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   435
                    throw new SignatureException("Key usage restricted: "
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   436
                                                 + "cannot be used for "
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   437
                                                 + "digital signatures");
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   438
                }
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   439
            }
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   440
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   441
            Signature sig = Signature.getInstance(algname);
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   442
            sig.initVerify(key);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   443
            sig.update(dataSigned);
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   444
            if (sig.verify(encryptedDigest)) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   445
                return this;
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   446
            }
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   447
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   448
        } catch (IOException e) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   449
            throw new SignatureException("IO error verifying signature:\n" +
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   450
                                         e.getMessage());
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   451
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   452
        } catch (InvalidKeyException e) {
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   453
            throw new SignatureException("InvalidKey: " + e.getMessage());
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   454
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
9365
469cd39a25de 7040803: regression: bugster fail to start
igor
parents: 9248
diff changeset
   456
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /* Verify the content of the pkcs7 block. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    SignerInfo verify(PKCS7 block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    throws NoSuchAlgorithmException, SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return verify(block, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public BigInteger getVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    public X500Name getIssuerName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return issuerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public BigInteger getCertificateSerialNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return certificateSerialNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public AlgorithmId getDigestAlgorithmId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return digestAlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public PKCS9Attributes getAuthenticatedAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return authenticatedAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public AlgorithmId getDigestEncryptionAlgorithmId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return digestEncryptionAlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public byte[] getEncryptedDigest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return encryptedDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    public PKCS9Attributes getUnauthenticatedAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        return unauthenticatedAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   498
    /*
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   499
     * Extracts a timestamp from a PKCS7 SignerInfo.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   500
     *
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   501
     * Examines the signer's unsigned attributes for a
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 31426
diff changeset
   502
     * {@code signatureTimestampToken} attribute. If present,
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   503
     * then it is parsed to extract the date and time at which the
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   504
     * timestamp was generated.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   505
     *
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   506
     * @param info A signer information element of a PKCS 7 block.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   507
     *
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   508
     * @return A timestamp token or null if none is present.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   509
     * @throws IOException if an error is encountered while parsing the
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   510
     *         PKCS7 data.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   511
     * @throws NoSuchAlgorithmException if an error is encountered while
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   512
     *         verifying the PKCS7 object.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   513
     * @throws SignatureException if an error is encountered while
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   514
     *         verifying the PKCS7 object.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   515
     * @throws CertificateException if an error is encountered while generating
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   516
     *         the TSA's certpath.
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   517
     */
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   518
    public Timestamp getTimestamp()
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   519
        throws IOException, NoSuchAlgorithmException, SignatureException,
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   520
               CertificateException
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   521
    {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   522
        if (timestamp != null || !hasTimestamp)
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   523
            return timestamp;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   524
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   525
        if (unauthenticatedAttributes == null) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   526
            hasTimestamp = false;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   527
            return null;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   528
        }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   529
        PKCS9Attribute tsTokenAttr =
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   530
            unauthenticatedAttributes.getAttribute(
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   531
                PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_OID);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   532
        if (tsTokenAttr == null) {
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   533
            hasTimestamp = false;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   534
            return null;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   535
        }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   536
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   537
        PKCS7 tsToken = new PKCS7((byte[])tsTokenAttr.getValue());
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   538
        // Extract the content (an encoded timestamp token info)
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   539
        byte[] encTsTokenInfo = tsToken.getContentInfo().getData();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   540
        // Extract the signer (the Timestamping Authority)
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   541
        // while verifying the content
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   542
        SignerInfo[] tsa = tsToken.verify(encTsTokenInfo);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   543
        // Expect only one signer
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   544
        ArrayList<X509Certificate> chain = tsa[0].getCertificateChain(tsToken);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   545
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   546
        CertPath tsaChain = cf.generateCertPath(chain);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   547
        // Create a timestamp token info object
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   548
        TimestampToken tsTokenInfo = new TimestampToken(encTsTokenInfo);
18224
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   549
        // Check that the signature timestamp applies to this signature
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   550
        verifyTimestamp(tsTokenInfo);
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   551
        // Create a timestamp object
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   552
        timestamp = new Timestamp(tsTokenInfo.getDate(), tsaChain);
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   553
        return timestamp;
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   554
    }
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   555
18224
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   556
    /*
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   557
     * Check that the signature timestamp applies to this signature.
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   558
     * Match the hash present in the signature timestamp token against the hash
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   559
     * of this signature.
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   560
     */
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   561
    private void verifyTimestamp(TimestampToken token)
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   562
        throws NoSuchAlgorithmException, SignatureException {
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   563
        String digestAlgname = token.getHashAlgorithm().getName();
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   564
        // check that algorithm is not restricted
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   565
        if (!JAR_DISABLED_CHECK.permits(DIGEST_PRIMITIVE_SET, digestAlgname,
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   566
                null)) {
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   567
            throw new SignatureException("Timestamp token digest check failed. " +
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   568
                    "Disabled algorithm used: " + digestAlgname);
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   569
        }
18224
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   570
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   571
        MessageDigest md =
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 34687
diff changeset
   572
            MessageDigest.getInstance(digestAlgname);
18224
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   573
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   574
        if (!Arrays.equals(token.getHashedMessage(),
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   575
            md.digest(encryptedDigest))) {
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   576
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   577
            throw new SignatureException("Signature timestamp (#" +
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   578
                token.getSerialNumber() + ") generated on " + token.getDate() +
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   579
                " is inapplicable");
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   580
        }
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   581
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   582
        if (debug != null) {
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   583
            debug.println();
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   584
            debug.println("Detected signature timestamp (#" +
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   585
                token.getSerialNumber() + ") generated on " + token.getDate());
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   586
            debug.println();
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   587
        }
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   588
    }
95b6fb2f35a0 8009235: Improve handling of TSA data
vinnie
parents: 10788
diff changeset
   589
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        HexDumpEncoder hexDump = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        String out = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        out += "Signer Info for (issuer): " + issuerName + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        out += "\tversion: " + Debug.toHexString(version) + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        out += "\tcertificateSerialNumber: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
               Debug.toHexString(certificateSerialNumber) + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        out += "\tdigestAlgorithmId: " + digestAlgorithmId + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (authenticatedAttributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            out += "\tauthenticatedAttributes: " + authenticatedAttributes +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                   "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        out += "\tdigestEncryptionAlgorithmId: " + digestEncryptionAlgorithmId +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        out += "\tencryptedDigest: " + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            hexDump.encodeBuffer(encryptedDigest) + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if (unauthenticatedAttributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            out += "\tunauthenticatedAttributes: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                   unauthenticatedAttributes + "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
}