test/jdk/java/security/Provider/SupportsParameter.java
author jlaskey
Thu, 14 Nov 2019 12:33:47 -0400
branchjlaskey-prng
changeset 59084 b8fb85ee91e9
parent 47216 71c04702a3d5
permissions -rw-r--r--
restarting random branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
40416
5d91b2fd668c 8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents: 5506
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
40416
5d91b2fd668c 8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents: 5506
diff changeset
    26
 * @bug 4911081 8130181
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary verify that Provider.Service.supportsParameter() works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.spec.SecretKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class SupportsParameter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        kpg.initialize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        KeyPair kp = kpg.generateKeyPair();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        PublicKey dsaPublicKey = kp.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        PrivateKey dsaPrivateKey = kp.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        PublicKey myPublicKey = new MyPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        PrivateKey myPrivateKey = new MyPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        SecretKey mySecretKey = new MySecretKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Provider p = new MyProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // none specified, always true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        s = p.getService("Signature", "DSA0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        checkSupports(s, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        checkSupports(s, dsaPublicKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        checkSupports(s, dsaPrivateKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        checkSupports(s, myPublicKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        checkSupports(s, myPrivateKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        checkSupports(s, mySecretKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // DSAPublic/PrivateKey specified as classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        s = p.getService("Signature", "DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        checkSupports(s, dsaPublicKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        checkSupports(s, dsaPrivateKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        checkSupports(s, myPublicKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        checkSupports(s, myPrivateKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        checkSupports(s, mySecretKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        // X.509/PKCS#8 specified as formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        s = p.getService("Signature", "DSA2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        checkSupports(s, dsaPublicKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        checkSupports(s, dsaPrivateKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        checkSupports(s, myPublicKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        checkSupports(s, myPrivateKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        checkSupports(s, mySecretKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // MyPublic/PrivateKey + DSAPublicKey specified as classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        s = p.getService("Signature", "DSA3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        checkSupports(s, dsaPublicKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        checkSupports(s, dsaPrivateKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        checkSupports(s, myPublicKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        checkSupports(s, myPrivateKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        checkSupports(s, mySecretKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // MyPublic/PrivateKey + DSAPublicKey specified as classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        s = p.getService("Cipher", "DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        checkSupports(s, dsaPublicKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        checkSupports(s, dsaPrivateKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        checkSupports(s, myPublicKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        checkSupports(s, myPrivateKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        checkSupports(s, mySecretKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Key secretKeySpec = new SecretKeySpec(new byte[8], "DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        checkSupports(s, secretKeySpec, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static void checkSupports(Service s, Key key, boolean r) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (s.supportsParameter(key) != r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new Exception("Result mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        System.out.println("Passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static class MyProvider extends Provider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        MyProvider() {
40416
5d91b2fd668c 8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents: 5506
diff changeset
   107
            super("MyProvider", "1.0", "MyProvider");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            put("Signature.DSA0", "foo.DSA0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            put("Signature.DSA", "foo.DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            put("Signature.DSA SupportedKeyClasses",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                "java.security.interfaces.DSAPublicKey" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                "|java.security.interfaces.DSAPrivateKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            put("Signature.DSA2", "foo.DSA2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            put("Signature.DSA2 SupportedKeyFormats", "X.509|PKCS#8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            put("Signature.DSA3", "foo.DSA3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            put("Signature.DSA3 SupportedKeyClasses",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                "SupportsParameter$MyPrivateKey" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                "|SupportsParameter$MyPublicKey" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                "|java.security.interfaces.DSAPublicKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            put("Cipher.DES", "foo.DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            put("Cipher.DES SupportedKeyFormats", "RAW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            put("Cipher.DES SupportedKeyClasses", "SupportsParameter$MySecretKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static class MyKey implements Key {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        public String getAlgorithm() { return "FOO"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public String getFormat() { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        public byte[] getEncoded() { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private static class MyPrivateKey extends MyKey implements PrivateKey { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private static class MyPublicKey extends MyKey implements PublicKey {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static class MySecretKey extends MyKey implements SecretKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public String getAlgorithm() { return "DES"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}