jdk/test/sun/security/tools/jarsigner/JarSigningNonAscii.java
author ascarpino
Thu, 21 Jul 2016 10:33:56 -0700
changeset 41580 cc479488428c
parent 30820 0d4717a011d3
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: 30820
diff changeset
     2
 * Copyright (c) 2003, 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
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4924188
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 {
41580
cc479488428c 8155973: Tighten jar checks
ascarpino
parents: 30820
diff changeset
    45
        Security.setProperty("jdk.jar.disabledAlgorithms", "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        String srcDir = System.getProperty("test.src", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        String destDir = System.getProperty("test.classes", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        String unsignedJar = srcDir + "/JarSigning_RU.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        String signedJar = destDir + "/JarSigning_RU.signed.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        String keystore = srcDir + "/JarSigning.keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // remove signed jar if it exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            File removeMe = new File(signedJar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            removeMe.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // sign the provided jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        String[] jsArgs = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                        "-keystore", keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                        "-storepass", "bbbbbb",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                        "-signedJar", signedJar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        unsignedJar, "b"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                        };
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 7524
diff changeset
    69
        sun.security.tools.jarsigner.Main.main(jsArgs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        //  verify the signed jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         * can not do this because JarSigner calls System.exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         * with an exit code that jtreg does not like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        String[] vArgs = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                "-verify",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                "-keystore", keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                "-storepass", "bbbbbb",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                "-verbose",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                signedJar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        JarSigner.main(vArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        JarEntry je;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        JarFile jf = new JarFile(signedJar, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Vector entriesVec = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        byte[] buffer = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Enumeration entries = jf.entries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        while (entries.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            je = (JarEntry)entries.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            entriesVec.addElement(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            InputStream is = jf.getInputStream(je);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            while ((n = is.read(buffer, 0, buffer.length)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                // we just read. this will throw a SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                // if  a signature/digest check fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        jf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        Manifest man = jf.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int isSignedCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            Enumeration e = entriesVec.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                je = (JarEntry) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                String name = je.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                Certificate[] certs = je.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if ((certs != null) && (certs.length > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    isSignedCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
7524
ec12e1e6fa20 7004035: signed jar with only META-INF/* inside is not verifiable
weijun
parents: 5506
diff changeset
   120
        if (isSignedCount != 4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new SecurityException("error signing JAR file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        System.out.println("jar verified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}