test/jdk/sun/security/tools/jarsigner/JarSigningNonAscii.java
author weijun
Wed, 09 May 2018 09:50:25 +0800
changeset 50064 e4a7bacf99b1
parent 47216 71c04702a3d5
child 50539 7bf4f1b5e438
permissions -rw-r--r--
8202816: Update JarSigning.keystore Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50064
e4a7bacf99b1 8202816: Update JarSigning.keystore
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
50064
e4a7bacf99b1 8202816: Update JarSigning.keystore
weijun
parents: 47216
diff changeset
    26
 * @bug 4924188 8202816
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary sign a JAR file that has entry names with non-ASCII characters.
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 14342
diff changeset
    28
 * @modules jdk.jartool/sun.security.tools.jarsigner
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 30820
diff changeset
    29
 * @run main/othervm JarSigningNonAscii
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.tools.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 30820
diff changeset
    34
import java.security.Security;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class JarSigningNonAscii {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static String jarFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static String keystore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        String srcDir = System.getProperty("test.src", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        String destDir = System.getProperty("test.classes", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        String unsignedJar = srcDir + "/JarSigning_RU.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        String signedJar = destDir + "/JarSigning_RU.signed.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        String keystore = srcDir + "/JarSigning.keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        // remove signed jar if it exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            File removeMe = new File(signedJar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            removeMe.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        // sign the provided jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        String[] jsArgs = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                        "-keystore", keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                        "-storepass", "bbbbbb",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                        "-signedJar", signedJar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                        unsignedJar, "b"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        };
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 7524
diff changeset
    68
        sun.security.tools.jarsigner.Main.main(jsArgs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        //  verify the signed jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         * can not do this because JarSigner calls System.exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         * with an exit code that jtreg does not like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        String[] vArgs = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                "-verify",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                "-keystore", keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                "-storepass", "bbbbbb",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                "-verbose",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                signedJar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        JarSigner.main(vArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        JarEntry je;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        JarFile jf = new JarFile(signedJar, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Vector entriesVec = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        byte[] buffer = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Enumeration entries = jf.entries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        while (entries.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            je = (JarEntry)entries.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            entriesVec.addElement(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            InputStream is = jf.getInputStream(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            while ((n = is.read(buffer, 0, buffer.length)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                // we just read. this will throw a SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                // if  a signature/digest check fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        jf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        Manifest man = jf.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int isSignedCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            Enumeration e = entriesVec.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                je = (JarEntry) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                String name = je.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                Certificate[] certs = je.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                if ((certs != null) && (certs.length > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    isSignedCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
7524
ec12e1e6fa20 7004035: signed jar with only META-INF/* inside is not verifiable
weijun
parents: 5506
diff changeset
   119
        if (isSignedCount != 4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new SecurityException("error signing JAR file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.out.println("jar verified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}