1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @bug 8056174
27 * @summary Make sure the jarsigner tool still works after it's modified to
28 *          be based on JarSigner API
29 * @library /lib/testlibrary
30 * @modules java.base/sun.security.tools.keytool
31 *          jdk.jartool/sun.security.tools.jarsigner
32 *          java.base/sun.security.pkcs
33 *          java.base/sun.security.x509
34 */
35
36import com.sun.jarsigner.ContentSigner;
37import com.sun.jarsigner.ContentSignerParameters;
38import jdk.testlibrary.JarUtils;
39import sun.security.pkcs.PKCS7;
40
41import java.io.ByteArrayInputStream;
42import java.io.IOException;
43import java.io.InputStream;
44import java.nio.file.Files;
45import java.nio.file.Paths;
46import java.security.NoSuchAlgorithmException;
47import java.security.cert.CertificateException;
48import java.util.*;
49import java.util.jar.Attributes;
50import java.util.jar.JarEntry;
51import java.util.jar.JarFile;
52import java.util.jar.Manifest;
53
54public class Options {
55
56    public static void main(String[] args) throws Exception {
57
58        // Prepares raw file
59        Files.write(Paths.get("a"), "a".getBytes());
60
61        // Pack
62        JarUtils.createJar("a.jar", "a");
63
64        // Prepare a keystore
65        sun.security.tools.keytool.Main.main(
66                ("-keystore jks -storepass changeit -keypass changeit -dname" +
67                        " CN=A -alias a -genkeypair -keyalg rsa").split(" "));
68
69        // -altsign
70        sun.security.tools.jarsigner.Main.main(
71                ("-debug -signedjar altsign.jar -keystore jks -storepass changeit" +
72                        " -altsigner Options$X a.jar a").split(" "));
73
74        try (JarFile jf = new JarFile("altsign.jar")) {
75            JarEntry je = jf.getJarEntry("META-INF/A.RSA");
76            try (InputStream is = jf.getInputStream(je)) {
77                if (!Arrays.equals(is.readAllBytes(), "1234".getBytes())) {
78                    throw new Exception("altsign go wrong");
79                }
80            }
81        }
82
83        // -sigfile, -digestalg, -sigalg, -internalsf, -sectionsonly
84        sun.security.tools.jarsigner.Main.main(
85                ("-debug -signedjar new.jar -keystore jks -storepass changeit" +
86                " -sigfile olala -digestalg SHA1 -sigalg SHA224withRSA" +
87                " -internalsf -sectionsonly a.jar a").split(" "));
88
89        try (JarFile jf = new JarFile("new.jar")) {
90            JarEntry je = jf.getJarEntry("META-INF/OLALA.SF");
91            Objects.requireNonNull(je);     // check -sigfile
92            byte[] sf = null;               // content of .SF
93            try (InputStream is = jf.getInputStream(je)) {
94                sf = is.readAllBytes();     // save for later comparison
95                Attributes attrs = new Manifest(new ByteArrayInputStream(sf))
96                        .getMainAttributes();
97                // check -digestalg
98                if (!attrs.containsKey(new Attributes.Name(
99                        "SHA1-Digest-Manifest-Main-Attributes"))) {
100                    throw new Exception("digestalg incorrect");
101                }
102                // check -sectionsonly
103                if (attrs.containsKey(new Attributes.Name(
104                        "SHA1-Digest-Manifest"))) {
105                    throw new Exception("SF should not have file digest");
106                }
107            }
108
109            je = jf.getJarEntry("META-INF/OLALA.RSA");
110            try (InputStream is = jf.getInputStream(je)) {
111                PKCS7 p7 = new PKCS7(is.readAllBytes());
112                String alg = p7.getSignerInfos()[0]
113                        .getDigestAlgorithmId().getName();
114                if (!alg.equals("SHA-224")) {   // check -sigalg
115                    throw new Exception("PKCS7 signing is using " + alg);
116                }
117                // check -internalsf
118                if (!Arrays.equals(sf, p7.getContentInfo().getData())) {
119                    throw new Exception("SF not in RSA");
120                }
121            }
122
123        }
124
125        // TSA-related ones are checked in ts.sh
126    }
127
128    public static class X extends ContentSigner {
129        @Override
130        public byte[] generateSignedData(ContentSignerParameters parameters,
131                boolean omitContent, boolean applyTimestamp)
132                throws NoSuchAlgorithmException, CertificateException,
133                        IOException {
134            return "1234".getBytes();
135        }
136    }
137}
138