jdk/src/jdk.dev/share/classes/sun/security/tools/jarsigner/TimestampedSigner.java
author juh
Tue, 07 Oct 2014 22:23:19 -0700
changeset 26967 c182469301ee
parent 25859 3317bb8137f4
permissions -rw-r--r--
8037550: Update RFC references in javadoc to RFC 5280 Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 2007, 2014, 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
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10788
diff changeset
    26
package sun.security.tools.jarsigner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.jarsigner.*;
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    35
import sun.security.pkcs.PKCS7;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This class implements a content signing service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * It generates a timestamped signature for a given content according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The signature along with a trusted timestamp and the signer's certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * are all packaged into a standard PKCS #7 Signed Data message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public final class TimestampedSigner extends ContentSigner {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Object identifier for the subject information access X.509 certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final String SUBJECT_INFO_ACCESS_OID = "1.3.6.1.5.5.7.1.11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Object identifier for the timestamping access descriptors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final ObjectIdentifier AD_TIMESTAMPING_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        ObjectIdentifier tmp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            tmp = new ObjectIdentifier("1.3.6.1.5.5.7.48.3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        AD_TIMESTAMPING_Id = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Instantiates a content signer that supports timestamped signatures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public TimestampedSigner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Generates a PKCS #7 signed data message that includes a signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * timestamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * This method is used when a signature has already been generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * The signature, a signature timestamp, the signer's certificate chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * and optionally the content that was signed, are packaged into a PKCS #7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * signed data message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
    85
     * @param params The non-null input parameters.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param omitContent true if the content should be omitted from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *        signed data message. Otherwise the content is included.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param applyTimestamp true if the signature should be timestamped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *        Otherwise timestamping is not performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @return A PKCS #7 signed data message including a signature timestamp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @throws NoSuchAlgorithmException The exception is thrown if the signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *         algorithm is unrecognised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @throws CertificateException The exception is thrown if an error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *         while processing the signer's certificate or the TSA's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *         certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @throws IOException The exception is thrown if an error occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *         generating the signature timestamp or while generating the signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *         data message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @throws NullPointerException The exception is thrown if parameters is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *         null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   102
    public byte[] generateSignedData(ContentSignerParameters params,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        boolean omitContent, boolean applyTimestamp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throws NoSuchAlgorithmException, CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   106
        if (params == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   110
        // Parse the signature algorithm to extract the digest
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   111
        // algorithm. The expected format is:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        //     "<digest>with<encryption>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // or  "<digest>with<encryption>and<mgf>"
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   114
        String signatureAlgorithm = params.getSignatureAlgorithm();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   116
        X509Certificate[] signerChain = params.getSignerCertificateChain();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   117
        byte[] signature = params.getSignature();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // Include or exclude content
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   120
        byte[] content = (omitContent == true) ? null : params.getContent();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   122
        URI tsaURI = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (applyTimestamp) {
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   124
            tsaURI = params.getTimestampingAuthority();
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   125
            if (tsaURI == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                // Examine TSA cert
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   127
                tsaURI = getTimestampingURI(
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   128
                    params.getTimestampingAuthorityCertificate());
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   129
                if (tsaURI == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    throw new CertificateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        "Subject Information Access extension not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   135
        return PKCS7.generateSignedData(signature, signerChain, content,
17161
df1ec0e2f0e7 8009636: JARSigner including TimeStamp PolicyID (TSAPolicyID) as defined in RFC3161
weijun
parents: 14182
diff changeset
   136
                                        params.getSignatureAlgorithm(), tsaURI,
24034
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   137
                                        params.getTSAPolicyID(),
31fe17eef94a 8038837: Add support to jarsigner for specifying timestamp hash algorithm
weijun
parents: 23010
diff changeset
   138
                                        params.getTSADigestAlg());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Examine the certificate for a Subject Information Access extension
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   143
     * (<a href="http://tools.ietf.org/html/rfc5280">RFC 5280</a>).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * The extension's <tt>accessMethod</tt> field should contain the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * identifier defined for timestamping: 1.3.6.1.5.5.7.48.3 and its
8998
96d4e9ea2423 7030174: Jarsigner should accept TSACert with an HTTPS id-ad-timeStamping SIA
weijun
parents: 8556
diff changeset
   146
     * <tt>accessLocation</tt> field should contain an HTTP or HTTPS URL.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param tsaCertificate An X.509 certificate for the TSA.
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   149
     * @return An HTTP or HTTPS URI or null if none was found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   151
    public static URI getTimestampingURI(X509Certificate tsaCertificate) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (tsaCertificate == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // Parse the extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            byte[] extensionValue =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                tsaCertificate.getExtensionValue(SUBJECT_INFO_ACCESS_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (extensionValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            DerInputStream der = new DerInputStream(extensionValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            der = new DerInputStream(der.getOctetString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            DerValue[] derValue = der.getSequence(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            AccessDescription description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            GeneralName location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            URIName uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            for (int i = 0; i < derValue.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                description = new AccessDescription(derValue[i]);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8998
diff changeset
   171
                if (description.getAccessMethod()
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 8998
diff changeset
   172
                        .equals((Object)AD_TIMESTAMPING_Id)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    location = description.getAccessLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    if (location.getType() == GeneralNameInterface.NAME_URI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        uri = (URIName) location.getName();
8998
96d4e9ea2423 7030174: Jarsigner should accept TSACert with an HTTPS id-ad-timeStamping SIA
weijun
parents: 8556
diff changeset
   176
                        if (uri.getScheme().equalsIgnoreCase("http") ||
96d4e9ea2423 7030174: Jarsigner should accept TSACert with an HTTPS id-ad-timeStamping SIA
weijun
parents: 8556
diff changeset
   177
                                uri.getScheme().equalsIgnoreCase("https")) {
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 10336
diff changeset
   178
                            return uri.getURI();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
}