test/jdk/sun/security/tools/jarsigner/WeakSize.java
author weijun
Fri, 15 Nov 2019 09:06:58 +0800
changeset 59104 046e4024e55a
parent 54521 8de62c4af8c7
permissions -rw-r--r--
8214024: Remove the default keytool -keyalg value Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54521
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
     1
/*
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
     2
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
     4
 *
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
     8
 *
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    13
 * accompanied this code).
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    14
 *
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    18
 *
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    21
 * questions.
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    22
 */
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    23
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    24
/*
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    25
 * @test
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    26
 * @bug 8044755
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    27
 * @summary Add a test for algorithm constraints check in jarsigner
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    28
 * @library /test/lib
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    29
 */
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    30
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    31
import jdk.test.lib.SecurityTools;
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    32
import jdk.test.lib.process.OutputAnalyzer;
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    33
import jdk.test.lib.util.JarUtils;
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    34
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    35
import java.nio.file.Path;
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    36
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    37
public class WeakSize {
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    38
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    39
    static OutputAnalyzer kt(String cmd) throws Exception {
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    40
        // The sigalg used is MD2withRSA, which is obsolete.
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    41
        return SecurityTools.keytool("-storepass changeit -keypass changeit "
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    42
                + "-keystore ks -keyalg rsa -sigalg MD2withRSA " + cmd);
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    43
    }
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    44
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    45
    static void gencert(String owner, String cmd) throws Exception {
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    46
        kt("-certreq -alias " + owner + " -file tmp.req");
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    47
        kt("-gencert -infile tmp.req -outfile tmp.cert " + cmd);
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    48
        kt("-import -alias " + owner + " -file tmp.cert");
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    49
    }
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    50
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    51
    public static void main(String[] args) throws Exception {
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    52
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    53
        kt("-genkeypair -alias ca -dname CN=CA -ext bc");
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    54
        kt("-genkeypair -alias signer -dname CN=Signer");
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    55
        gencert("signer", "-alias ca -ext ku=dS -rfc");
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    56
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    57
        JarUtils.createJarFile(Path.of("a.jar"), Path.of("."), Path.of("ks"));
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    58
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    59
        // We always trust a TrustedCertificateEntry
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    60
        SecurityTools.jarsigner("-keystore ks -storepass changeit "
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    61
                + "-strict -debug a.jar ca")
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    62
                .shouldNotContain("chain is invalid");
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    63
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    64
        // An end-entity cert must follow algorithm constraints
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    65
        SecurityTools.jarsigner("-keystore ks -storepass changeit "
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    66
                + "-strict -debug a.jar signer")
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    67
                .shouldContain("chain is invalid");
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    68
    }
8de62c4af8c7 8180573: Refactor sun/security/tools shell tests to plain java tests
weijun
parents:
diff changeset
    69
}