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