test/jdk/sun/security/pkcs11/KeyStore/Basic.java
author valeriep
Fri, 29 Mar 2019 00:39:49 +0000
changeset 54333 2a29e62446bd
parent 47216 71c04702a3d5
permissions -rw-r--r--
8220016: Clean up redundant RSA services in the SunJSSE provider Summary: Removed duplicated RSA signature/KF/KPG support in SunJSSE Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54333
2a29e62446bd 8220016: Clean up redundant RSA services in the SunJSSE provider
valeriep
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2019, 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
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.KeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.KeyPairGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.KeyPair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.SecureRandom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AuthProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.ProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.security.auth.login.LoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import com.sun.security.auth.module.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import com.sun.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class Basic extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final char SEP = File.separatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static String DIR = System.getProperty("DIR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static char[] tokenPwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final char[] ibuttonPwd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                        new char[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final char[] activcardPwd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                        new char[] { '1', '1', '2', '2', '3', '3' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final char[] nssPwd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                        new char[] { 't', 'e', 's', 't', '1', '2' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final char[] solarisPwd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                        new char[] { 'p', 'i', 'n' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final char[] sca1000Pwd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                        new char[] { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final char[] sPwd = { 'f', 'o', 'o' };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static SecretKey sk1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static SecretKey sk2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static SecretKey sk3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static SecretKey sk4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static RSAPrivateCrtKey pk1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static PrivateKey pk2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static PrivateKey pk3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static Certificate[] chain1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static Certificate[] chain2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static Certificate[] chain3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static Certificate[] chain4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static X509Certificate randomCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final String KS_TYPE = "PKCS11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static class FooEntry implements KeyStore.Entry { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static class P11SecretKey implements SecretKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        String alg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public P11SecretKey(String alg, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            this.alg = alg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        public String getAlgorithm() { return alg; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        public String getFormat() { return "raw"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        public byte[] getEncoded() { return new byte[length/8]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public static void main(String[] args) throws Exception {
37353
e8e684b89c81 8153545: sun/security/pkcs11/Provider/Login.sh fails on Linux
amjiang
parents: 31270
diff changeset
   106
        main(new Basic(), args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public void main(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.provider = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // get private keys
54333
2a29e62446bd 8220016: Clean up redundant RSA services in the SunJSSE provider
valeriep
parents: 47216
diff changeset
   114
        KeyFactory kf = KeyFactory.getInstance("RSA");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        KeyFactory dsaKf = KeyFactory.getInstance("DSA", "SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        ObjectInputStream ois1 = new ObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        (new FileInputStream(new File(DIR, "pk1.key")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        byte[] keyBytes = (byte[])ois1.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        ois1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        PrivateKey tmpKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        pk1 = (RSAPrivateCrtKey)tmpKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        ObjectInputStream ois2 = new ObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        (new FileInputStream(new File(DIR, "pk2.key")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        keyBytes = (byte[])ois2.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        ois2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        pk2 = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        ObjectInputStream ois3 = new ObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        (new FileInputStream(new File(DIR, "pk3.key")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        keyBytes = (byte[])ois3.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        pk3 = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ois3.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // get cert chains for private keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        CertificateFactory cf = CertificateFactory.getInstance("X.509", "SUN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Certificate caCert = (X509Certificate)cf.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        (new FileInputStream(new File(DIR, "ca.cert")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Certificate ca2Cert = (X509Certificate)cf.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        (new FileInputStream(new File(DIR, "ca2.cert")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Certificate pk1cert = (X509Certificate)cf.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        (new FileInputStream(new File(DIR, "pk1.cert")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        Certificate pk1cert2 = (X509Certificate)cf.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        (new FileInputStream(new File(DIR, "pk1.cert2")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Certificate pk2cert = (X509Certificate)cf.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        (new FileInputStream(new File(DIR, "pk2.cert")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        Certificate pk3cert = (X509Certificate)cf.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        (new FileInputStream(new File(DIR, "pk3.cert")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        chain1 = new Certificate[] { pk1cert, caCert };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        chain2 = new Certificate[] { pk2cert, caCert };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        chain3 = new Certificate[] { pk3cert, caCert };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        chain4 = new Certificate[] { pk1cert2, ca2Cert };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // create secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        sk1 = new P11SecretKey("DES", 64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        sk2 = new P11SecretKey("DESede", 192);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        sk3 = new P11SecretKey("AES", 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        sk4 = new P11SecretKey("RC4", 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // read randomCert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        randomCert = (X509Certificate)cf.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        (new FileInputStream(new File(DIR, "random.cert")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        doTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private static void doTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        String token = System.getProperty("TOKEN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        String test = System.getProperty("TEST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (token == null || token.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            throw new Exception("token arg required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (test == null || test.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new Exception("test arg required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if ("ibutton".equals(token)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            tokenPwd = ibuttonPwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else if ("activcard".equals(token)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            tokenPwd = activcardPwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else if ("nss".equals(token)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            tokenPwd = nssPwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else if ("sca1000".equals(token)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            tokenPwd = sca1000Pwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else if ("solaris".equals(token)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            tokenPwd = solarisPwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if ("list".equals(test)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            Basic.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else if ("basic".equals(test)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            int testnum = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if ("ibutton".equals(token)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                // pkey and setAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                testnum = Basic.pkey(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                testnum = Basic.setAttribute(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            } else if ("activcard".equals(token)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                // sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                testnum = Basic.signAlias(testnum, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            } else if ("nss".equals(token)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                // setAttribute, pkey, sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                testnum = Basic.setAttribute(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                testnum = Basic.pkey(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                testnum = Basic.sign(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                testnum = Basic.copy(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } else if ("solaris".equals(token)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                testnum = Basic.setAttribute(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                testnum = Basic.pkey(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                testnum = Basic.sign(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                testnum = Basic.skey(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                testnum = Basic.copy(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            } else if ("sca1000".equals(token)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                // setAttribute, pkey, sign, skey, copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                testnum = Basic.setAttribute(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                testnum = Basic.pkey(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                testnum = Basic.sign(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                testnum = Basic.skey(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                testnum = Basic.copy(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } else if ("pkey".equals(test)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            Basic.pkey(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else if ("skey".equals(test)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            Basic.skey(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else if ("setAttribute".equals(test)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            Basic.setAttribute(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else if ("copy".equals(test)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            Basic.copy(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else if ("sign".equals(test)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            Basic.sign(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } else if ("module".equals(test)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            Basic.module();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } else if ("nss-extended".equals(test)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            // this only works if NSS_TEST is set to true in P11KeyStore.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            int testnum = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            testnum = Basic.setAttribute(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            testnum = Basic.pkey(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            testnum = Basic.sign(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            testnum = Basic.extended(testnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            System.out.println("unrecognized command");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private static int sign(int testnum) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            ks = KeyStore.getInstance(KS_TYPE, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            ks.load(null, tokenPwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (!ks.containsAlias("pk1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            ks.setKeyEntry("pk1", pk1, null, chain1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return signAlias(testnum, "pk1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private static int signAlias(int testnum, String alias) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            ks = KeyStore.getInstance(KS_TYPE, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            ks.load(null, tokenPwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            Enumeration enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                alias = (String)enu.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        PrivateKey pkey = (PrivateKey)ks.getKey(alias, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if ("RSA".equals(pkey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            System.out.println("got [" + alias + "] signing key: " + pkey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                ("expected RSA, got " + pkey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        Signature s = Signature.getInstance("MD5WithRSA", ks.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        s.initSign(pkey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        System.out.println("initialized signature object with key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        s.update("hello".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        System.out.println("signature object updated with [hello] bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        byte[] signed = s.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        System.out.println("received signature " + signed.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        " bytes in length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        Signature v = Signature.getInstance("MD5WithRSA", ks.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        v.initVerify(ks.getCertificate(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        v.update("hello".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        v.verify(signed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        System.out.println("signature verified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return testnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private static int copy(int testnum) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            ks = KeyStore.getInstance(KS_TYPE, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            ks.load(null, tokenPwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        KeyFactory kf = KeyFactory.getInstance("RSA", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        PrivateKey pkSession = (PrivateKey)kf.translateKey(pk3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        System.out.println("pkSession = " + pkSession);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        ks.setKeyEntry("pkSession", pkSession, null, chain3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        KeyStore.PrivateKeyEntry pke =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                (KeyStore.PrivateKeyEntry)ks.getEntry("pkSession", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        System.out.println("pkSession = " + pke.getPrivateKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        Certificate[] chain = pke.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (chain.length != chain3.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if (!chain[i].equals(chain3[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return testnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private static void list() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        int testnum = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        ks = KeyStore.getInstance(KS_TYPE, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // check instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (ks.getProvider() instanceof java.security.AuthProvider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            System.out.println("keystore provider instance of AuthProvider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new SecurityException("did not get AuthProvider KeyStore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        // load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        ks.load(null, tokenPwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        Enumeration enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            System.out.println("alias " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                (String)enu.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private static void module() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        // perform Security.addProvider of P11 provider
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30820
diff changeset
   370
        Security.addProvider(getSunPKCS11(System.getProperty("CUSTOM_P11_CONFIG")));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        String KS_PROVIDER = "SunPKCS11-" + System.getProperty("TOKEN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        KeyStoreLoginModule m = new KeyStoreLoginModule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        Subject s = new Subject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        Map options = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        options.put("keyStoreURL", "NONE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        options.put("keyStoreType", KS_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        options.put("keyStoreProvider", KS_PROVIDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        options.put("debug", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        m.initialize(s, new TextCallbackHandler(), new HashMap(), options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        m.login();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        m.commit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        System.out.println("authenticated subject = " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        m.logout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        System.out.println("authenticated subject = " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * SCA1000 does not handle extended secret key tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * . Blowfish (CKR_TEMPLATE_INCOMPLETE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * . AES (CKR_TEMPLATE_INCOMPLETE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * . RC4 (CKR_ATTRIBUTE_TYPE_INVALID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * so do this instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private static int skey(int testnum) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            ks = KeyStore.getInstance(KS_TYPE, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            ks.load(null, tokenPwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // delete all old aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        Enumeration enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            String next = (String)enu.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            ks.deleteEntry(next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            System.out.println("deleted entry for: " + next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // set good ske 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        ks.setKeyEntry("sk1", sk1, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // set good ske 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        ks.setKeyEntry("sk2", sk2, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        // getEntry good ske 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        KeyStore.SecretKeyEntry ske =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                (KeyStore.SecretKeyEntry)ks.getEntry("sk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if ("DES".equals(ske.getSecretKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                ("expected DES, got " + ske.getSecretKey().getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // getEntry good ske 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk2", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if ("DESede".equals(ske.getSecretKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                ("expected DESede, got " + ske.getSecretKey().getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        // getKey good ske 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        SecretKey skey = (SecretKey)ks.getKey("sk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if ("DES".equals(skey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                ("expected DES, got " + skey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        // getKey good ske 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        skey = (SecretKey)ks.getKey("sk2", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if ("DESede".equals(skey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                ("expected DESede, got " + skey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            System.out.println("alias " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                (String)enu.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (count == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            throw new SecurityException("expected 2 aliases");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        // size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (ks.size() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            throw new SecurityException("expected size 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        // isCertificateEntry sk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (!ks.isCertificateEntry("sk1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            throw new SecurityException("expected ske");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        // isKeyEntry sk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (ks.isKeyEntry("sk1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            throw new SecurityException("expected ske");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        // entryInstanceOf sk2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (ks.entryInstanceOf("sk2", KeyStore.SecretKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            throw new SecurityException("expected ske");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return testnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    private static int setAttribute(int testnum) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            ks = KeyStore.getInstance(KS_TYPE, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            ks.load(null, tokenPwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if (!ks.containsAlias("pk1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            // set good pke 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            ks.setKeyEntry("pk1", pk1, null, chain1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        // delete all old aliases except pk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        Enumeration enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            String next = (String)enu.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            if (!"pk1".equals(next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                ks.deleteEntry(next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                System.out.println("deleted entry for: " + next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        KeyStore.PrivateKeyEntry pke =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                (KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        System.out.println("pk1 = " + pke.getPrivateKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        Certificate[] chain = pke.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (chain.length != chain1.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            if (!chain[i].equals(chain1[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         * test change alias only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        // test C_SetAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        PrivateKey pkey = pke.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        ks.setEntry("pk1SA",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                new KeyStore.PrivateKeyEntry(pkey, chain1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        // aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        String newAlias = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            newAlias = (String)enu.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            System.out.println("alias " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                                count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                                " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                                newAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (count == 1 && "pk1SA".equals(newAlias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            throw new SecurityException("expected 1 alias");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        // size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        if (ks.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            throw new SecurityException("expected size 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        if (pke != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            throw new SecurityException("expected not to find pk1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        System.out.println("pk1SA = " + pke.getPrivateKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        chain = pke.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (chain.length != chain1.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if (!chain[i].equals(chain1[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * test change cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        pkey = pke.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        ks.setEntry("pk1SA-2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                new KeyStore.PrivateKeyEntry(pkey, chain4),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        // aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        newAlias = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            newAlias = (String)enu.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            System.out.println("alias " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                                " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                                newAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (count == 1 && "pk1SA-2".equals(newAlias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            throw new SecurityException("expected 1 alias");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        // size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        if (ks.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            throw new SecurityException("expected size 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (pke != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            throw new SecurityException("expected not to find pk1SA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1SA-2", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        System.out.println("pk1SA-2 = " + pke.getPrivateKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        chain = pke.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (chain.length != chain4.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if (!chain[i].equals(chain4[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return testnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    private static int pkey(int testnum) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if (ks == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            ks = KeyStore.getInstance(KS_TYPE, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            ks.load(null, tokenPwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        // check instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        if (ks.getProvider() instanceof java.security.AuthProvider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            System.out.println("keystore provider instance of AuthProvider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            throw new SecurityException("did not get AuthProvider KeyStore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        // delete all old aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        Enumeration enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            String next = (String)enu.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            ks.deleteEntry(next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            System.out.println("deleted entry for: " + next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        // set good pke 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        ks.setKeyEntry("pk1", pk1, null, chain1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        // set good pke 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        ks.setEntry("pk2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                new KeyStore.PrivateKeyEntry(pk2, chain2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        // getEntry good pke 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        KeyStore.PrivateKeyEntry pke =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                (KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        System.out.println("pk1 = " + pke.getPrivateKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        Certificate[] chain = pke.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (chain.length != chain1.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (!chain[i].equals(chain1[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        // getKey good pke 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        PrivateKey pkey = (PrivateKey)ks.getKey("pk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        System.out.println("pk1 = " + pkey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        if ("RSA".equals(pkey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                ("expected RSA, got " + pkey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        // getCertificate chain chain 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        chain = ks.getCertificateChain("pk1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if (chain.length != chain1.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (!chain[i].equals(chain1[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        // getEntry good pke 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk2", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if ("RSA".equals(pke.getPrivateKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                ("expected RSA, got " + pke.getPrivateKey().getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        System.out.println("pk2 = " + pke.getPrivateKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        chain = pke.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        if (chain.length != chain2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (!chain[i].equals(chain2[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        // getKey good pke 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        pkey = (PrivateKey)ks.getKey("pk2", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if ("RSA".equals(pkey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                ("expected RSA, got " + pkey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        // getCertificate chain chain 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        chain = ks.getCertificateChain("pk2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if (chain.length != chain2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            if (!chain[i].equals(chain2[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        // aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            System.out.println("alias " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                                count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                                (String)enu.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if (count == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            throw new SecurityException("expected 2 aliases");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        // size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if (ks.size() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            throw new SecurityException("expected size 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        // getCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if (ks.getCertificate("pk1").equals(chain1[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            throw new SecurityException("expected certificate pk1 end entity");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        // containsAlias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        if (ks.containsAlias("pk1") && ks.containsAlias("pk2") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            !ks.containsAlias("foobar") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            !ks.containsAlias("pk1.2") && !ks.containsAlias("pk2.2")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            throw new SecurityException("unexpected aliases encountered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        // isKeyEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        if (ks.isKeyEntry("pk1") && ks.isKeyEntry("pk2") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            !ks.isKeyEntry("foobar")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            throw new SecurityException("isKeyEntry failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        // isCertificateEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        if (!ks.isCertificateEntry("foobar") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            !ks.isCertificateEntry("pk1") && !ks.isCertificateEntry("pk2")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            throw new SecurityException("isCertificateEntry failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        // getCertificateAlias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (ks.getCertificateAlias(chain1[0]).equals("pk1") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            ks.getCertificateAlias(chain2[0]).equals("pk2") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            ks.getCertificateAlias(randomCert) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            throw new SecurityException("getCertificateAlias failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (ks.entryInstanceOf("pk1", KeyStore.PrivateKeyEntry.class) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            ks.entryInstanceOf("pk2", KeyStore.PrivateKeyEntry.class) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        !ks.entryInstanceOf("pk1", KeyStore.TrustedCertificateEntry.class) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        !ks.entryInstanceOf("pk2", KeyStore.TrustedCertificateEntry.class) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        !ks.entryInstanceOf("foobar", KeyStore.TrustedCertificateEntry.class) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
          !ks.entryInstanceOf("foobar", KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            throw new SecurityException("entryInstanceOf failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        ks.deleteEntry("pk2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if (ks.containsAlias("pk1") && !ks.containsAlias("pk2")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            throw new SecurityException("deleteEntry failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        // getEntry good pke 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        System.out.println("pk1 = " + pke.getPrivateKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        chain = pke.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        if (chain.length != chain1.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            if (!chain[i].equals(chain1[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        // aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            System.out.println("alias " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                                count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                                (String)enu.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        if (count == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            throw new SecurityException("expected 1 alias");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        // size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        if (ks.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            throw new SecurityException("expected size 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        return testnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    private static int extended(int testnum) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        // setEntry unknown entry type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            ks.setEntry("foo", new FooEntry(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            throw new SecurityException("setEntry should have failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        // getEntry random foo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        if (ks.getEntry("foo", null) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            throw new SecurityException("expected null entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        // set good ske 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        ks.setKeyEntry("sk1", sk1, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        // set good ske 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        ks.setKeyEntry("sk2", sk2, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        // set good ske 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        ks.setEntry("sk3",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                new KeyStore.SecretKeyEntry(sk3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        // set good ske 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        ks.setEntry("sk4",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                new KeyStore.SecretKeyEntry(sk4),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        // getEntry good ske 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        KeyStore.SecretKeyEntry ske =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                (KeyStore.SecretKeyEntry)ks.getEntry("sk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        if ("DES".equals(ske.getSecretKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                ("expected DES, got " + ske.getSecretKey().getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        // getEntry good ske 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk2", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        if ("DESede".equals(ske.getSecretKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                ("expected DESede, got " + ske.getSecretKey().getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        // getEntry good ske 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk3", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        if ("AES".equals(ske.getSecretKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                ("expected AES, got " + ske.getSecretKey().getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        // getEntry good ske 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        ske = (KeyStore.SecretKeyEntry)ks.getEntry("sk4", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        if ("ARCFOUR".equals(ske.getSecretKey().getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                ("expected ARCFOUR, got " + ske.getSecretKey().getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        // getKey good ske 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        SecretKey skey = (SecretKey)ks.getKey("sk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        if ("DES".equals(skey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                ("expected DES, got " + skey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        // getKey good ske 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        skey = (SecretKey)ks.getKey("sk2", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if ("DESede".equals(skey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                ("expected DESede, got " + skey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        // getKey good ske 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        skey = (SecretKey)ks.getKey("sk3", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        if ("AES".equals(skey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                ("expected AES, got " + skey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        // getKey good ske 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        skey = (SecretKey)ks.getKey("sk4", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        if ("ARCFOUR".equals(skey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                ("expected ARCFOUR, got " + skey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        // aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        Enumeration enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            System.out.println("alias " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                                count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                                " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                                (String)enu.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        if (count == 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            throw new SecurityException("expected 5 aliases");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        // size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        if (ks.size() == 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            throw new SecurityException("expected size 5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        // set good pke 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        ks.setEntry("pk2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                new KeyStore.PrivateKeyEntry(pk2, chain2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        // set good pke 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        ks.setEntry("pk3",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                new KeyStore.PrivateKeyEntry(pk3, chain3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        // getEntry good pke 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        KeyStore.PrivateKeyEntry pke =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                (KeyStore.PrivateKeyEntry)ks.getEntry("pk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        System.out.println("pk1 = " + pke.getPrivateKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        Certificate[] chain = pke.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        if (chain.length != chain1.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            if (!chain[i].equals(chain1[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        // getEntry good pke 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk2", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        System.out.println("pk2 = " + pke.getPrivateKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        chain = pke.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        if (chain.length != chain2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            if (!chain[i].equals(chain2[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        // getEntry good pke 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        pke = (KeyStore.PrivateKeyEntry)ks.getEntry("pk3", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        System.out.println("pk3 = " + pke.getPrivateKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        chain = pke.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        if (chain.length != chain3.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            if (!chain[i].equals(chain3[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        // getKey good pke 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        PrivateKey pkey = (PrivateKey)ks.getKey("pk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        if ("RSA".equals(pkey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                ("expected RSA, got " + pkey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        // getCertificate chain chain 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        chain = ks.getCertificateChain("pk1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        if (chain.length != chain1.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            if (!chain[i].equals(chain1[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        // getKey good pke 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        pkey = (PrivateKey)ks.getKey("pk2", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if ("RSA".equals(pkey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                ("expected RSA, got " + pkey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        // getCertificate chain chain 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        chain = ks.getCertificateChain("pk2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        if (chain.length != chain2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            if (!chain[i].equals(chain2[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        // getKey good pke 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        pkey = (PrivateKey)ks.getKey("pk3", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        if ("RSA".equals(pkey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                ("expected RSA, got " + pkey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        // getCertificate chain chain 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        chain = ks.getCertificateChain("pk3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        if (chain.length != chain3.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            throw new SecurityException("received chain not correct length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            if (!chain[i].equals(chain3[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                throw new SecurityException("received chain not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        // aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            System.out.println("alias " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                                count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                                " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                                (String)enu.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        if (count == 7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            throw new SecurityException("expected 7 aliases");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        // size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        if (ks.size() == 7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            throw new SecurityException("expected size 7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        // getCertificate good chain 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        if (ks.getCertificate("pk1").equals(chain1[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            throw new SecurityException("retrieved cert not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        // getCertificate good chain 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        if (ks.getCertificate("pk3").equals(chain3[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            throw new SecurityException("retrieved cert not equal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        // getKey good ske 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        skey = (SecretKey)ks.getKey("sk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        if ("DES".equals(skey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                ("expected DES, got " + skey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        // getKey good ske 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        skey = (SecretKey)ks.getKey("sk4", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if ("ARCFOUR".equals(skey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                ("expected ARCFOUR, got " + skey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        // getKey good pke 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        pkey = (PrivateKey)ks.getKey("pk1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        if ("RSA".equals(pkey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                ("expected RSA, got " + pkey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        // getKey good pke 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        pkey = (PrivateKey)ks.getKey("pk3", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        if ("RSA".equals(pkey.getAlgorithm())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                ("expected RSA, got " + pkey.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        // contains alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        if (!ks.containsAlias("pk1") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                !ks.containsAlias("pk2") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                !ks.containsAlias("pk3") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                !ks.containsAlias("sk1") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                !ks.containsAlias("sk2") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                !ks.containsAlias("sk3") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                !ks.containsAlias("sk4")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            throw new SecurityException("did not contain all aliases");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        // getCertificateAlias pk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        if (ks.getCertificateAlias(chain1[0]).equals("pk1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            throw new SecurityException("expected cert pk1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        // getCertificateAlias pk3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        if (ks.getCertificateAlias(chain3[0]).equals("pk3")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            throw new SecurityException("expected cert pk3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        // isCertificateEntry pk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        if (!ks.isCertificateEntry("pk1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            throw new SecurityException("expected pke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        // isCertificateEntry pk3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        if (!ks.isCertificateEntry("pk3")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            throw new SecurityException("expected pke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        // isCertificateEntry sk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        if (!ks.isCertificateEntry("sk1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            throw new SecurityException("expected ske");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        // isCertificateEntry sk4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        if (!ks.isCertificateEntry("sk4")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            throw new SecurityException("expected ske");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        // isKeyEntry pk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        if (ks.isKeyEntry("pk1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            throw new SecurityException("expected pke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        // isKeyEntry pk3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        if (ks.isKeyEntry("pk3")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            throw new SecurityException("expected pke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        // isKeyEntry sk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        if (ks.isKeyEntry("sk1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            throw new SecurityException("expected ske");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        // isKeyEntry sk4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        if (ks.isKeyEntry("sk4")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            throw new SecurityException("expected ske");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        // isCertificateEntry random foo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        if (!ks.isCertificateEntry("foo")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            throw new SecurityException("expected foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        // isKeyEntry random foo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        if (!ks.isKeyEntry("foo")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            throw new SecurityException("expected foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        // entryInstanceOf pk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        if (!ks.entryInstanceOf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                ("pk1", KeyStore.TrustedCertificateEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            throw new SecurityException("expected tce");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        // entryInstanceOf pk3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        if (!ks.entryInstanceOf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                ("pk3", KeyStore.TrustedCertificateEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            throw new SecurityException("expected tce");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        // entryInstanceOf sk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        if (!ks.entryInstanceOf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                ("sk1", KeyStore.TrustedCertificateEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            throw new SecurityException("expected tce");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        // entryInstanceOf sk4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        if (!ks.entryInstanceOf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                ("sk4", KeyStore.TrustedCertificateEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            throw new SecurityException("expected tce");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        // entryInstanceOf pk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        if (ks.entryInstanceOf("pk1", KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            throw new SecurityException("expected pke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        // entryInstanceOf pk3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        if (ks.entryInstanceOf("pk3", KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            throw new SecurityException("expected pke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        // entryInstanceOf sk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        if (!ks.entryInstanceOf("sk1", KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            throw new SecurityException("expected pke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        // entryInstanceOf sk4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        if (!ks.entryInstanceOf("sk4", KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            throw new SecurityException("expected pke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        // entryInstanceOf sk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        if (ks.entryInstanceOf("sk1", KeyStore.SecretKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            throw new SecurityException("expected ske");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        // entryInstanceOf sk4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        if (ks.entryInstanceOf("sk4", KeyStore.SecretKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            throw new SecurityException("expected ske");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        // entryInstanceOf pk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        if (!ks.entryInstanceOf("pk1", KeyStore.SecretKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            throw new SecurityException("expected ske");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        // entryInstanceOf pk3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        if (!ks.entryInstanceOf("pk3", KeyStore.SecretKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            throw new SecurityException("expected ske");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        // getEntry random foobar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        if (ks.getEntry("foobar", null) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            throw new SecurityException("expected null entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        // deleteEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        ks.deleteEntry("pk1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        ks.deleteEntry("pk3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        ks.deleteEntry("sk2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        ks.deleteEntry("sk3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        // aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        enu = ks.aliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        while (enu.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            System.out.println("alias " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                                count +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                                " = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                                (String)enu.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        if (count == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            throw new SecurityException("expected 3 aliases");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        // size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        if (ks.size() == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            throw new SecurityException("expected size 6");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        // entryInstanceOf sk1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        if (!ks.entryInstanceOf("sk1", KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            throw new SecurityException("expected pke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        // entryInstanceOf sk4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        if (!ks.entryInstanceOf("sk4", KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            throw new SecurityException("expected pke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        // entryInstanceOf pk2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        if (ks.entryInstanceOf("pk2", KeyStore.PrivateKeyEntry.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            throw new SecurityException("expected pke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        System.out.println("test " + testnum++ + " passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        return testnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
}