test/jdk/sun/security/tools/jarsigner/compatibility/JdkUtils.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57581 57a391a23f7f
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
     1
/*
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
     4
 *
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
     7
 * published by the Free Software Foundation.
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
     8
 *
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    13
 * accompanied this code).
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    14
 *
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    18
 *
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    21
 * questions.
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    22
 */
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    23
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    24
import java.security.KeyPairGenerator;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    25
import java.security.MessageDigest;
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    26
import java.security.NoSuchAlgorithmException;
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    27
import java.security.Signature;
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    28
import static java.util.Arrays.asList;
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    29
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    30
/*
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    31
 * This class is used for returning some specific JDK information.
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    32
 */
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    33
public class JdkUtils {
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    34
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    35
    private enum Alg {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    36
        KEY, SIG, DIGEST;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    37
    }
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    38
57581
57a391a23f7f 8228403: SignTwice.java failed with java.io.FileNotFoundException: File name too long
jjiang
parents: 57488
diff changeset
    39
    static final String M_JAVA_VERSION = "javaVersion";
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    40
    static final String M_JAVA_RUNTIME_VERSION = "javaRuntimeVersion";
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    41
    static final String M_IS_SUPPORTED_KEYALG = "isSupportedKeyalg";
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    42
    static final String M_IS_SUPPORTED_SIGALG = "isSupportedSigalg";
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    43
    static final String M_IS_SUPPORTED_DIGESTALG = "isSupportedDigestalg";
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    44
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    45
    // Returns the JDK build version.
57581
57a391a23f7f 8228403: SignTwice.java failed with java.io.FileNotFoundException: File name too long
jjiang
parents: 57488
diff changeset
    46
    static String javaVersion() {
57a391a23f7f 8228403: SignTwice.java failed with java.io.FileNotFoundException: File name too long
jjiang
parents: 57488
diff changeset
    47
        return System.getProperty("java.version");
57a391a23f7f 8228403: SignTwice.java failed with java.io.FileNotFoundException: File name too long
jjiang
parents: 57488
diff changeset
    48
    }
57a391a23f7f 8228403: SignTwice.java failed with java.io.FileNotFoundException: File name too long
jjiang
parents: 57488
diff changeset
    49
57a391a23f7f 8228403: SignTwice.java failed with java.io.FileNotFoundException: File name too long
jjiang
parents: 57488
diff changeset
    50
    // Returns the JDK build runtime version.
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    51
    static String javaRuntimeVersion() {
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    52
        return System.getProperty("java.runtime.version");
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    53
    }
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    54
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    55
    // Checks if the specified algorithm is supported by the JDK.
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    56
    static boolean isSupportedAlg(Alg algType, String algName) {
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    57
        try {
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    58
            switch (algType) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    59
            case KEY:
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    60
                return KeyPairGenerator.getInstance(algName) != null;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    61
            case SIG:
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    62
                return Signature.getInstance(algName) != null;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    63
            case DIGEST:
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    64
                return MessageDigest.getInstance(algName) != null;
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    65
            }
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    66
        } catch (NoSuchAlgorithmException e) { }
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    67
        System.out.println(algName + " is not supported yet.");
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    68
        return false;
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    69
    }
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    70
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    71
    public static void main(String[] args) {
57581
57a391a23f7f 8228403: SignTwice.java failed with java.io.FileNotFoundException: File name too long
jjiang
parents: 57488
diff changeset
    72
        if (M_JAVA_VERSION.equals(args[0])) {
57a391a23f7f 8228403: SignTwice.java failed with java.io.FileNotFoundException: File name too long
jjiang
parents: 57488
diff changeset
    73
            System.out.print(javaVersion());
57a391a23f7f 8228403: SignTwice.java failed with java.io.FileNotFoundException: File name too long
jjiang
parents: 57488
diff changeset
    74
        } else if (M_JAVA_RUNTIME_VERSION.equals(args[0])) {
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    75
            System.out.print(javaRuntimeVersion());
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    76
        } else if (M_IS_SUPPORTED_KEYALG.equals(args[0])) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    77
            System.out.print(isSupportedAlg(Alg.KEY, args[1]));
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    78
        } else if (M_IS_SUPPORTED_SIGALG.equals(args[0])) {
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    79
            System.out.print(isSupportedAlg(Alg.SIG, args[1]));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    80
        } else if (M_IS_SUPPORTED_DIGESTALG.equals(args[0])) {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    81
            System.out.print(isSupportedAlg(Alg.DIGEST, args[1]));
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    82
        } else {
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    83
            throw new IllegalArgumentException("invalid: " + asList(args));
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    84
        }
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    85
    }
57488
94691d8e746f 8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents: 47216
diff changeset
    86
46156
79e8a865c5b8 8179614: Test for jarsigner on verifying jars that are signed and timestamped by other JDK releases
mli
parents:
diff changeset
    87
}