test/jdk/java/security/KeyStore/EntryMethods.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
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: 35315
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 1.5, 03/06/24
40416
5d91b2fd668c 8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents: 35315
diff changeset
    26
 * @bug 4850376 8130850 8130181
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Provide generic storage KeyStore storage facilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class EntryMethods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    extends Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    implements KeyStore.Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static FileInputStream pre15fis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static char[] password = {'f', 'o', 'o', 'b', 'a', 'r'};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static char[] badPwd = {'b', 'a', 'd', 'p', 'w', 'd'};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static class FooProtect implements KeyStore.ProtectionParameter { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static class FooParameter implements KeyStore.LoadStoreParameter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        public KeyStore.ProtectionParameter getProtectionParameter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
32005
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    51
    public static class MyLoadStoreParameter
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    52
        implements KeyStore.LoadStoreParameter {
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    53
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    54
        private KeyStore.ProtectionParameter protection;
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    55
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    56
        MyLoadStoreParameter(KeyStore.ProtectionParameter protection) {
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    57
            this.protection = protection;
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    58
        }
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    59
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    60
        public KeyStore.ProtectionParameter getProtectionParameter() {
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    61
            return protection;
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    62
        }
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    63
    }
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
    64
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static class FooEntry implements KeyStore.Entry { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public EntryMethods() throws Exception {
40416
5d91b2fd668c 8130181: Deprecate java.security.Provider(String, double, String), add Provider(Strin
valeriep
parents: 35315
diff changeset
    68
        super("EntryMethods", "0.0", "EntryMethods");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        pre15fis = new FileInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            (System.getProperty("test.src") + "/EntryMethods.pre15.keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                put("KeyStore.Pre15KeyStore", "EntryMethods$Pre15");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                put("KeyStore.Post15KeyStore", "EntryMethods$Post15");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                put("KeyStore.UnrecoverableKeyStore",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                        "EntryMethods$UnrecoverableKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        EntryMethods entry = new EntryMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // test pre-JDK1.5 KeyStore throws UnsupportedOperationExceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // for new methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        KeyStore pre15KS = KeyStore.getInstance("Pre15KeyStore", entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        testPre15(pre15KS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // test post-JDK1.5 KeyStore does right thing with new methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        KeyStore post15KS = KeyStore.getInstance("Post15KeyStore", entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        testPost15(post15KS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // test post-JDK1.5 KeyStore can throw new UnrecoverableEntryException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        KeyStore uKS = KeyStore.getInstance("UnrecoverableKeyStore", entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        testUnrecoverable(uKS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static void testPre15(KeyStore ks) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int tNum = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        KeyStore.Entry e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // TEST load null param
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        ks.load((KeyStore.LoadStoreParameter)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // TEST load random param
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            ks.load(new FooParameter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new SecurityException("[Pre1.5] test " + tNum + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
32005
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
   118
        } catch (NoSuchAlgorithmException nsae) {
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
   119
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
32005
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
   123
        // TEST load custom param
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
   124
        ks.load(new MyLoadStoreParameter(
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
   125
            new KeyStore.PasswordProtection(password)));
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
   126
        System.out.println("[Pre1.5] test " + tNum++ + " passed");
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
   127
2c716d9dac38 8130850: Support loading a keystore with a custom KeyStore.LoadStoreParameter class
vinnie
parents: 31422
diff changeset
   128
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // TEST store random param
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        ks.load(pre15fis, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // setup for later user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        KeyStore.Entry pkeNew = ks.getEntry("privkey",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        KeyStore.Entry tceNew = ks.getEntry("trustedcert", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            ks.store(new FooParameter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new SecurityException("[Pre1.5] test " + tNum + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // TEST store null param
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            ks.store(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new SecurityException("[Pre1.5] test " + tNum + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            // good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // TEST getEntry with alias/protParam - use invalid alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        e = ks.getEntry("notPresent",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                "expected null entry returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // TEST getEntry with alias/null protParam - get private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            e = ks.getEntry("privkey", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                "expected UnrecoverableEntryException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } catch (UnrecoverableEntryException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // TEST getEntry with alias/bad password - get private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            e = ks.getEntry("privkey",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        new KeyStore.PasswordProtection(badPwd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                "expected UnrecoverableEntryException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } catch (UnrecoverableEntryException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // TEST getEntry with alias/unknown protection - get private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            e = ks.getEntry("privkey", new FooProtect());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                "expected UnsupportedOperationException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // TEST getEntry with alias/protParam - get private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        e = ks.getEntry("privkey", new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (e instanceof KeyStore.PrivateKeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                "expected PrivateKeyEntry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // TEST getEntry with alias/null protParam - get trusted cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        e = ks.getEntry("trustedcert", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (e instanceof KeyStore.TrustedCertificateEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new SecurityException("[Pre1.5] test " + tNum + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // TEST getEntry with alias/non-null protParam - get trusted cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            e = ks.getEntry("trustedcert",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            throw new SecurityException("[Pre1.5] test " + tNum + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } catch (UnsupportedOperationException uoe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // TEST setEntry with alias/entry/protParam - use invalid alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            ks.setEntry("foo", new FooEntry(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                "expected KeyStoreException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // TEST setEntry with alias/entry/null protParam - set private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            ks.setEntry("newPrivKey", pkeNew, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                "expected KeyStoreException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // TEST setEntry with alias/entry/random protParam - set private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            ks.setEntry("newPrivKey", pkeNew, new FooProtect());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                "expected KeyStoreException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // TEST setEntry with alias/entry/protParam - set private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        ks.setEntry("newPrivKey", pkeNew,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        e = ks.getEntry("newPrivKey",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (e instanceof KeyStore.PrivateKeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                "expected PrivateKeyEntry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // TEST setEntry with alias/entry/non null protParam - set trusted cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            ks.setEntry("newTrustedcert", tceNew,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                "expected KeyStoreException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            // good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // TEST setEntry with alias/entry/null protParam - set trusted cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        ks.setEntry("newTrustedcert", tceNew, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        e = ks.getEntry("newTrustedcert", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (e instanceof KeyStore.TrustedCertificateEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throw new SecurityException("[Pre1.5] test " + tNum + " failed - " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                "expected TrustedCertificateEntry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // TEST entryInstanceOf - invalid alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (ks.entryInstanceOf("foo", EntryMethods.class) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throw new SecurityException("[Pre1.5] test " + tNum + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // TEST entryInstanceOf - false case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (ks.entryInstanceOf("privkey", EntryMethods.class) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            throw new SecurityException("[Pre1.5] test " + tNum + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // TEST entryInstanceOf - true case, trustedcert entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (ks.entryInstanceOf("trustedcert",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                KeyStore.TrustedCertificateEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw new SecurityException("[Pre1.5] test " + tNum + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // TEST entryInstanceOf - true case, private key entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (ks.entryInstanceOf("privkey",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            System.out.println("[Pre1.5] test " + tNum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new SecurityException("[Pre1.5] test " + tNum + " failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private static void testPost15(KeyStore ks) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        KeyStore.Entry e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        ks.load(new EntryMethods.FooParameter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        ks.store(new EntryMethods.FooParameter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        e = ks.getEntry("foo", new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (!(e instanceof EntryMethods.FooEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        ("testPost15 getEntry(String, ProtParm) " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        "expected EntryMethods.FooEntry returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        ks.setEntry("foo", new EntryMethods.FooEntry(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (!ks.entryInstanceOf("foo", KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        ("testPost15 entryInstanceOf(String, Class) " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        "expected true returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        System.out.println("[Post1.5] tests all passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private static void testUnrecoverable(KeyStore ks) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        ks.load(new EntryMethods.FooParameter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            ks.getEntry("foo", new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                ("UnrecoverableEntryException not thrown for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                "getEntry(String, ProtectionParam)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } catch (UnrecoverableEntryException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            // good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            System.out.println("[UnrecoverableEntry] test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public static class Pre15 extends KeyStoreSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   371
        private static KeyStore jks = getJKS();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   373
        private static KeyStore getJKS() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            try {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   375
                return (KeyStore) KeyStore.getInstance("JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   381
        public Pre15() {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   382
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        public Key engineGetKey(String alias, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            throws NoSuchAlgorithmException, UnrecoverableKeyException {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   386
            try {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   387
                return jks.getKey(alias, password);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   388
            } catch (KeyStoreException ke) {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   389
                throw new RuntimeException("Unexpected exception", ke);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   390
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        public java.security.cert.Certificate[] engineGetCertificateChain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                (String alias) {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   395
            try {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   396
                return jks.getCertificateChain(alias);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   397
            } catch (KeyStoreException ke) {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   398
                throw new RuntimeException("Unexpected exception", ke);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   399
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        public java.security.cert.Certificate engineGetCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                (String alias) {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   404
            try {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   405
                return jks.getCertificate(alias);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   406
            } catch (KeyStoreException ke) {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   407
                throw new RuntimeException("Unexpected exception", ke);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   408
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        public Date engineGetCreationDate(String alias) {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   412
            try {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   413
                return jks.getCreationDate(alias);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   414
            } catch (KeyStoreException ke) {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   415
                throw new RuntimeException("Unexpected exception", ke);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   416
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        public void engineSetKeyEntry(String alias, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                   char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                   java.security.cert.Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            throws KeyStoreException {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   423
            jks.setKeyEntry(alias, key, password, chain);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        public void engineSetKeyEntry(String alias, byte[] key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                                   java.security.cert.Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            throws KeyStoreException {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   429
            jks.setKeyEntry(alias, key, chain);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        public void engineSetCertificateEntry(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                           java.security.cert.Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            throws KeyStoreException {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   435
            jks.setCertificateEntry(alias, cert);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        public void engineDeleteEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            throws KeyStoreException {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   440
            jks.deleteEntry(alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        public Enumeration engineAliases() {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   444
            try {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   445
                return jks.aliases();
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   446
            } catch (KeyStoreException ke) {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   447
                throw new RuntimeException("Unexpected exception", ke);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   448
            }
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   449
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        public boolean engineContainsAlias(String alias) {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   453
            try {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   454
                return jks.containsAlias(alias);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   455
            } catch (KeyStoreException ke) {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   456
                throw new RuntimeException("Unexpected exception", ke);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   457
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        public int engineSize() {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   461
            try {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   462
                return jks.size();
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   463
            } catch (KeyStoreException ke) {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   464
                throw new RuntimeException("Unexpected exception", ke);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   465
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        public boolean engineIsKeyEntry(String alias) {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   469
            try {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   470
                return jks.isKeyEntry(alias);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   471
            } catch (KeyStoreException ke) {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   472
                throw new RuntimeException("Unexpected exception", ke);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   473
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        public boolean engineIsCertificateEntry(String alias) {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   477
            try {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   478
                return jks.isCertificateEntry(alias);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   479
            } catch (KeyStoreException ke) {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   480
                throw new RuntimeException("Unexpected exception", ke);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   481
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        public String engineGetCertificateAlias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                (java.security.cert.Certificate cert) {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   486
            try {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   487
                return jks.getCertificateAlias(cert);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   488
            } catch (KeyStoreException ke) {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   489
                throw new RuntimeException("Unexpected exception", ke);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   490
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        public void engineStore(OutputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            throws IOException, NoSuchAlgorithmException, CertificateException {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   495
            try {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   496
                jks.store(stream, password);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   497
            } catch (KeyStoreException ke) {
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   498
                throw new RuntimeException("Unexpected exception", ke);
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   499
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        public void engineLoad(InputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            throws IOException, NoSuchAlgorithmException, CertificateException {
31422
67cd03bc9cce 8130007: Update security tests to use Security.getProvider to get security provider
mchung
parents: 5506
diff changeset
   504
            jks.load(stream, password);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public static class Post15 extends Pre15 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        public void engineStore(KeyStore.LoadStoreParameter parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            throws IOException, NoSuchAlgorithmException, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            if (!(parameter instanceof EntryMethods.FooParameter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                throw new IOException("Post15 engineStore method expected " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        "FooParameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        public void engineLoad(KeyStore.LoadStoreParameter parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            throws IOException, NoSuchAlgorithmException, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            if (!(parameter instanceof EntryMethods.FooParameter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                throw new IOException("Post15 engineLoadFrom method expected " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        "FooParameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        public KeyStore.Entry engineGetEntry(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        KeyStore.ProtectionParameter protectionParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        throws UnrecoverableEntryException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (!alias.equals("foo")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                        ("Post15 engineGetEntry(String, ProtectionParam) " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        "expected [foo] alias");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            KeyStore.PasswordProtection pwdParam =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                                (KeyStore.PasswordProtection)protectionParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            if (pwdParam.getPassword().length != 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                        ("Post15 engineGetEntry(String, ProtectionParam) " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                        "expected [foobar] password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            return new EntryMethods.FooEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        public void engineSetEntry(String alias, KeyStore.Entry entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        KeyStore.ProtectionParameter protectionParam) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if (!alias.equals("foo") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                !(entry instanceof EntryMethods.FooEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                        ("Post15 engineSetEntry(String, entry, ProtParm) " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        "expected [foo] alias and EntryMethods.FooEntry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            KeyStore.PasswordProtection pwdParam =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                (KeyStore.PasswordProtection)protectionParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            if (pwdParam.getPassword().length != 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                        ("Post15 engineSetEntry(String, entry, ProtParm) " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        "expected [foobar] password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        public boolean engineEntryInstanceOf(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                                             Class<? extends KeyStore.Entry> entryClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            if (!alias.equals("foo") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                entryClass != KeyStore.PrivateKeyEntry.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        ("Post15 engineEntryInstanceOf(String, Class) " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        "expected [foo] alias " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        "and [KeyStore.PrivateKeyEntry] class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public static class UnrecoverableKS extends Post15 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        public KeyStore.Entry engineGetEntry(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                        KeyStore.ProtectionParameter protectionParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        throws UnrecoverableEntryException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            throw new UnrecoverableEntryException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
}