Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/tools/jarsigner/TimestampedSigner.java
38923 views
/*1* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.tools.jarsigner;2627import java.io.IOException;28import java.net.URI;29import java.security.NoSuchAlgorithmException;30import java.security.cert.CertificateException;31import java.security.cert.X509Certificate;3233import com.sun.jarsigner.*;34import sun.security.pkcs.PKCS7;35import sun.security.util.*;36import sun.security.x509.*;3738/**39* This class implements a content signing service.40* It generates a timestamped signature for a given content according to41* <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>.42* The signature along with a trusted timestamp and the signer's certificate43* are all packaged into a standard PKCS #7 Signed Data message.44*45* @author Vincent Ryan46*/4748public final class TimestampedSigner extends ContentSigner {4950/*51* Object identifier for the subject information access X.509 certificate52* extension.53*/54private static final String SUBJECT_INFO_ACCESS_OID = "1.3.6.1.5.5.7.1.11";5556/*57* Object identifier for the timestamping access descriptors.58*/59private static final ObjectIdentifier AD_TIMESTAMPING_Id;60static {61ObjectIdentifier tmp = null;62try {63tmp = new ObjectIdentifier("1.3.6.1.5.5.7.48.3");64} catch (IOException e) {65// ignore66}67AD_TIMESTAMPING_Id = tmp;68}6970/**71* Instantiates a content signer that supports timestamped signatures.72*/73public TimestampedSigner() {74}7576/**77* Generates a PKCS #7 signed data message that includes a signature78* timestamp.79* This method is used when a signature has already been generated.80* The signature, a signature timestamp, the signer's certificate chain,81* and optionally the content that was signed, are packaged into a PKCS #782* signed data message.83*84* @param params The non-null input parameters.85* @param omitContent true if the content should be omitted from the86* signed data message. Otherwise the content is included.87* @param applyTimestamp true if the signature should be timestamped.88* Otherwise timestamping is not performed.89* @return A PKCS #7 signed data message including a signature timestamp.90* @throws NoSuchAlgorithmException The exception is thrown if the signature91* algorithm is unrecognised.92* @throws CertificateException The exception is thrown if an error occurs93* while processing the signer's certificate or the TSA's94* certificate.95* @throws IOException The exception is thrown if an error occurs while96* generating the signature timestamp or while generating the signed97* data message.98* @throws NullPointerException The exception is thrown if parameters is99* null.100*/101public byte[] generateSignedData(ContentSignerParameters params,102boolean omitContent, boolean applyTimestamp)103throws NoSuchAlgorithmException, CertificateException, IOException {104105if (params == null) {106throw new NullPointerException();107}108109// Parse the signature algorithm to extract the digest110// algorithm. The expected format is:111// "<digest>with<encryption>"112// or "<digest>with<encryption>and<mgf>"113String signatureAlgorithm = params.getSignatureAlgorithm();114115X509Certificate[] signerChain = params.getSignerCertificateChain();116byte[] signature = params.getSignature();117118// Include or exclude content119byte[] content = (omitContent == true) ? null : params.getContent();120121URI tsaURI = null;122if (applyTimestamp) {123tsaURI = params.getTimestampingAuthority();124if (tsaURI == null) {125// Examine TSA cert126tsaURI = getTimestampingURI(127params.getTimestampingAuthorityCertificate());128if (tsaURI == null) {129throw new CertificateException(130"Subject Information Access extension not found");131}132}133}134String tSADigestAlg = "SHA-256";135if (params instanceof JarSignerParameters) {136tSADigestAlg = ((JarSignerParameters)params).getTSADigestAlg();137}138return PKCS7.generateSignedData(signature, signerChain, content,139params.getSignatureAlgorithm(), tsaURI,140params.getTSAPolicyID(),141tSADigestAlg);142}143144/**145* Examine the certificate for a Subject Information Access extension146* (<a href="http://tools.ietf.org/html/rfc5280">RFC 5280</a>).147* The extension's {@code accessMethod} field should contain the object148* identifier defined for timestamping: 1.3.6.1.5.5.7.48.3 and its149* {@code accessLocation} field should contain an HTTP or HTTPS URL.150*151* @param tsaCertificate An X.509 certificate for the TSA.152* @return An HTTP or HTTPS URI or null if none was found.153*/154public static URI getTimestampingURI(X509Certificate tsaCertificate) {155156if (tsaCertificate == null) {157return null;158}159// Parse the extensions160try {161byte[] extensionValue =162tsaCertificate.getExtensionValue(SUBJECT_INFO_ACCESS_OID);163if (extensionValue == null) {164return null;165}166DerInputStream der = new DerInputStream(extensionValue);167der = new DerInputStream(der.getOctetString());168DerValue[] derValue = der.getSequence(5);169AccessDescription description;170GeneralName location;171URIName uri;172for (int i = 0; i < derValue.length; i++) {173description = new AccessDescription(derValue[i]);174if (description.getAccessMethod()175.equals((Object)AD_TIMESTAMPING_Id)) {176location = description.getAccessLocation();177if (location.getType() == GeneralNameInterface.NAME_URI) {178uri = (URIName) location.getName();179if (uri.getScheme().equalsIgnoreCase("http") ||180uri.getScheme().equalsIgnoreCase("https")) {181return uri.getURI();182}183}184}185}186} catch (IOException ioe) {187// ignore188}189return null;190}191}192193194