jdk/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyStore.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 43248 5e15de85a1a0
child 44546 10bdbc025c7f
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32634
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.KeyStore.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.crypto.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.security.auth.login.LoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.security.auth.callback.Callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.security.auth.callback.PasswordCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.security.auth.callback.CallbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.security.auth.callback.UnsupportedCallbackException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import sun.security.util.DerValue;
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 10336
diff changeset
    68
import sun.security.util.ECUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import sun.security.pkcs11.Secmod.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import static sun.security.pkcs11.P11Util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 291
diff changeset
    76
import sun.security.rsa.RSAKeyFactory;
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 291
diff changeset
    77
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
final class P11KeyStore extends KeyStoreSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final CK_ATTRIBUTE ATTR_CLASS_CERT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                        new CK_ATTRIBUTE(CKA_CLASS, CKO_CERTIFICATE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static final CK_ATTRIBUTE ATTR_CLASS_PKEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final CK_ATTRIBUTE ATTR_CLASS_SKEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        new CK_ATTRIBUTE(CKA_CLASS, CKO_SECRET_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static final CK_ATTRIBUTE ATTR_X509_CERT_TYPE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        new CK_ATTRIBUTE(CKA_CERTIFICATE_TYPE, CKC_X_509);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final CK_ATTRIBUTE ATTR_TOKEN_TRUE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        new CK_ATTRIBUTE(CKA_TOKEN, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // XXX for testing purposes only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    //  - NSS doesn't support persistent secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    //    (key type gets mangled if secret key is a token key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    //  - if debug is turned on, then this is set to false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static CK_ATTRIBUTE ATTR_SKEY_TOKEN_TRUE = ATTR_TOKEN_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final CK_ATTRIBUTE ATTR_TRUSTED_TRUE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        new CK_ATTRIBUTE(CKA_TRUSTED, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final CK_ATTRIBUTE ATTR_PRIVATE_TRUE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        new CK_ATTRIBUTE(CKA_PRIVATE, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final long NO_HANDLE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final long FINDOBJECTS_MAX = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final String ALIAS_SEP = "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static final boolean NSS_TEST = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        Debug.getInstance("pkcs11keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static boolean CKA_TRUSTED_SUPPORTED = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // If multiple certs are found to share the same CKA_LABEL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    // at load time (NSS-style keystore), then the keystore is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // and the unique keystore aliases are mapped to the entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // However, write capabilities are disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private boolean writeDisabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // Map of unique keystore aliases to entries in the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private HashMap<String, AliasInfo> aliasMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // whether to use NSS Secmod info for trust attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private final boolean useSecmodTrust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // if useSecmodTrust == true, which type of trust we are interested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private Secmod.TrustType nssTrustType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * The underlying token may contain multiple certs belonging to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * same "personality" (for example, a signing cert and encryption cert),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * all sharing the same CKA_LABEL.  These must be resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * into unique keystore aliases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * In addition, private keys and certs may not have a CKA_LABEL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * It is assumed that a private key and corresponding certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * share the same CKA_ID, and that the CKA_ID is unique across the token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * The CKA_ID may not be human-readable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * These pairs must be resolved into unique keystore aliases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Furthermore, secret keys are assumed to have a CKA_LABEL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * unique across the entire token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * When the KeyStore is loaded, instances of this class are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * created to represent the private keys/secret keys/certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * that reside on the token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private static class AliasInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // CKA_CLASS - entry type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        private CK_ATTRIBUTE type = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
   154
        // CKA_LABEL of cert and secret key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        private String label = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
   157
        // CKA_ID of the private key/cert pair
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        private byte[] id = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // CKA_TRUSTED - true if cert is trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        private boolean trusted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // either end-entity cert or trusted cert depending on 'type'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        private X509Certificate cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // chain
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 28059
diff changeset
   167
        private X509Certificate[] chain = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // true if CKA_ID for private key and cert match up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        private boolean matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // SecretKeyEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        public AliasInfo(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            this.type = ATTR_CLASS_SKEY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // PrivateKeyEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        public AliasInfo(String label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        byte[] id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        boolean trusted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            this.type = ATTR_CLASS_PKEY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            this.trusted = trusted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            this.cert = cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (type == ATTR_CLASS_PKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                sb.append("\ttype=[private key]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } else if (type == ATTR_CLASS_SKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                sb.append("\ttype=[secret key]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } else if (type == ATTR_CLASS_CERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                sb.append("\ttype=[trusted cert]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            sb.append("\tlabel=[" + label + "]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (id == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                sb.append("\tid=[null]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                sb.append("\tid=" + P11KeyStore.getID(id) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            sb.append("\ttrusted=[" + trusted + "]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            sb.append("\tmatched=[" + matched + "]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (cert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                sb.append("\tcert=[null]\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                sb.append("\tcert=[\tsubject: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        cert.getSubjectX500Principal() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        "\n\t\tissuer: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        cert.getIssuerX500Principal() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        "\n\t\tserialNum: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        cert.getSerialNumber().toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * callback handler for passing password to Provider.login method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    private static class PasswordCallbackHandler implements CallbackHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        private char[] password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        private PasswordCallbackHandler(char[] password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (password != null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   231
                this.password = password.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public void handle(Callback[] callbacks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                throws IOException, UnsupportedCallbackException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (!(callbacks[0] instanceof PasswordCallback)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                throw new UnsupportedCallbackException(callbacks[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            PasswordCallback pc = (PasswordCallback)callbacks[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            pc.setPassword(password);  // this clones the password if not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                Arrays.fill(password, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
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
     * getTokenObject return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * if object is not found, type is set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * otherwise, type is set to the requested type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private static class THandle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        private final long handle;              // token object handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        private final CK_ATTRIBUTE type;        // CKA_CLASS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        private THandle(long handle, CK_ATTRIBUTE type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            this.handle = handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    P11KeyStore(Token token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        this.useSecmodTrust = token.provider.nssUseSecmodTrust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Returns the key associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * The key must have been associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * the alias by a call to <code>setKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * or by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param password the password, which must be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return the requested key, or null if the given alias does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * or does not identify a key-related entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * key cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @exception UnrecoverableKeyException if the key cannot be recovered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public synchronized Key engineGetKey(String alias, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                throws NoSuchAlgorithmException, UnrecoverableKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (password != null && !token.config.getKeyStoreCompatibilityMode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw new NoSuchAlgorithmException("password must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        AliasInfo aliasInfo = aliasMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (aliasInfo == null || aliasInfo.type == ATTR_CLASS_CERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (aliasInfo.type == ATTR_CLASS_PKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                THandle h = getTokenObject(session,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                        aliasInfo.type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                        aliasInfo.id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                        null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (h.type == ATTR_CLASS_PKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    return loadPkey(session, h.handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                THandle h = getTokenObject(session,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                        ATTR_CLASS_SKEY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                        null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                        alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if (h.type == ATTR_CLASS_SKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    return loadSkey(session, h.handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            // did not find anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return null;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   327
        } catch (PKCS11Exception | KeyStoreException e) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   328
            throw new ProviderException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Returns the certificate chain associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * The certificate chain must have been associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * by a call to <code>setKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * or by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <code>PrivateKeyEntry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return the certificate chain (ordered with the user's certificate first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * and the root certificate authority last), or null if the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * does not exist or does not contain a certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public synchronized Certificate[] engineGetCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        AliasInfo aliasInfo = aliasMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (aliasInfo == null || aliasInfo.type != ATTR_CLASS_PKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return aliasInfo.chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Returns the certificate associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <p> If the given alias name identifies an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * created by a call to <code>setCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <code>TrustedCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * then the trusted certificate contained in that entry is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <p> If the given alias name identifies an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * created by a call to <code>setKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <code>PrivateKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * then the first element of the certificate chain in that entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * (if a chain exists) is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @return the certificate, or null if the given alias does not exist or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * does not contain a certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public synchronized Certificate engineGetCertificate(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        AliasInfo aliasInfo = aliasMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (aliasInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return aliasInfo.cert;
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
     * Returns the creation date of the entry identified by the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @return the creation date of this entry, or null if the given alias does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public Date engineGetCreationDate(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        throw new ProviderException(new UnsupportedOperationException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Assigns the given key to the given alias, protecting it with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <p>If the given key is of type <code>java.security.PrivateKey</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * it must be accompanied by a certificate chain certifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @param key the key to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param password the password to protect the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * key (only required if the given key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @exception KeyStoreException if the given key cannot be protected, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * this operation fails for some other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public synchronized void engineSetKeyEntry(String alias, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                   char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                   Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        checkWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (!(key instanceof PrivateKey) && !(key instanceof SecretKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            throw new KeyStoreException("key must be PrivateKey or SecretKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        } else if (key instanceof PrivateKey && chain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                ("PrivateKey must be accompanied by non-null chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        } else if (key instanceof SecretKey && chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                ("SecretKey must be accompanied by null chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        } else if (password != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    !token.config.getKeyStoreCompatibilityMode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            throw new KeyStoreException("Password must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        KeyStore.Entry entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                entry = new KeyStore.PrivateKeyEntry((PrivateKey)key, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            } else if (key instanceof SecretKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                entry = new KeyStore.SecretKeyEntry((SecretKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   452
        } catch (NullPointerException | IllegalArgumentException e) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   453
            throw new KeyStoreException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        engineSetEntry(alias, entry, new KeyStore.PasswordProtection(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Assigns the given key (that has already been protected) to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * <p>If the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * <code>java.security.PrivateKey</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * it must be accompanied by a certificate chain certifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @param key the key (in protected format) to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * key (only useful if the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @exception KeyStoreException if this operation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public void engineSetKeyEntry(String alias, byte[] key, Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        throw new ProviderException(new UnsupportedOperationException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Assigns the given certificate to the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * <p> If the given alias identifies an existing entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * created by a call to <code>setCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <code>TrustedCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * the trusted certificate in the existing entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * is overridden by the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param cert the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @exception KeyStoreException if the given alias already exists and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * not identify an entry containing a trusted certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * or this operation fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public synchronized void engineSetCertificateEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        (String alias, Certificate cert) throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        checkWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (cert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            throw new KeyStoreException("invalid null certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        KeyStore.Entry entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        entry = new KeyStore.TrustedCertificateEntry(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        engineSetEntry(alias, entry, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Deletes the entry identified by the given alias from this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @exception KeyStoreException if the entry cannot be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    public synchronized void engineDeleteEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (token.isWriteProtected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            throw new KeyStoreException("token write-protected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        checkWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        deleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * XXX - not sure whether to keep this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    private boolean deleteEntry(String alias) throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        AliasInfo aliasInfo = aliasMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if (aliasInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            aliasMap.remove(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                if (aliasInfo.type == ATTR_CLASS_CERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    // trusted certificate entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    return destroyCert(aliasInfo.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                } else if (aliasInfo.type == ATTR_CLASS_PKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    // private key entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    return destroyPkey(aliasInfo.id) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                destroyChain(aliasInfo.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                } else if (aliasInfo.type == ATTR_CLASS_SKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    // secret key entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    return destroySkey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    throw new KeyStoreException("unexpected entry type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   558
            } catch (PKCS11Exception | CertificateException e) {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   559
                throw new KeyStoreException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Lists all the alias names of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @return enumeration of the alias names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public synchronized Enumeration<String> engineAliases() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        // don't want returned enumeration to iterate off actual keySet -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        // otherwise applications that iterate and modify the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        // may run into concurrent modification problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return Collections.enumeration(new HashSet<String>(aliasMap.keySet()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Checks if the given alias exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @return true if the alias exists, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public synchronized boolean engineContainsAlias(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        return aliasMap.containsKey(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Retrieves the number of entries in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @return the number of entries in this keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public synchronized int engineSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        return aliasMap.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Returns true if the entry identified by the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * was created by a call to <code>setKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * <code>PrivateKeyEntry</code> or a <code>SecretKeyEntry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @param alias the alias for the keystore entry to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * key-related, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    public synchronized boolean engineIsKeyEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        AliasInfo aliasInfo = aliasMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        if (aliasInfo == null || aliasInfo.type == ATTR_CLASS_CERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * Returns true if the entry identified by the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * was created by a call to <code>setCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * <code>TrustedCertificateEntry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @param alias the alias for the keystore entry to be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @return true if the entry identified by the given alias contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * trusted certificate, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    public synchronized boolean engineIsCertificateEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        AliasInfo aliasInfo = aliasMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (aliasInfo == null || aliasInfo.type != ATTR_CLASS_CERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * Returns the (alias) name of the first keystore entry whose certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * matches the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <p>This method attempts to match the given certificate with each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * keystore entry. If the entry being considered was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * created by a call to <code>setCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * <code>TrustedCertificateEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * then the given certificate is compared to that entry's certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * <p> If the entry being considered was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * created by a call to <code>setKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * or created by a call to <code>setEntry</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * <code>PrivateKeyEntry</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * then the given certificate is compared to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * element of that entry's certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @param cert the certificate to match with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return the alias name of the first entry with matching certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * or null if no such entry exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public synchronized String engineGetCertificateAlias(Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        Enumeration<String> e = engineAliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            String alias = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            Certificate tokenCert = engineGetCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            if (tokenCert != null && tokenCert.equals(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                return alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * engineStore currently is a No-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Entries are stored to the token during engineSetEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @param stream this must be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @param password this must be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    public synchronized void engineStore(OutputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        throws IOException, NoSuchAlgorithmException, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if (stream != null && !token.config.getKeyStoreCompatibilityMode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            throw new IOException("output stream must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (password != null && !token.config.getKeyStoreCompatibilityMode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            throw new IOException("password must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * engineStore currently is a No-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * Entries are stored to the token during engineSetEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @param param this must be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @exception IllegalArgumentException if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *          <code>KeyStore.LoadStoreParameter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *          input is not <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    public synchronized void engineStore(KeyStore.LoadStoreParameter param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        throws IOException, NoSuchAlgorithmException, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (param != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                ("LoadStoreParameter must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * Loads the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @param stream the input stream, which must be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @param password the password used to unlock the keystore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *          or <code>null</code> if the token supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *          CKF_PROTECTED_AUTHENTICATION_PATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @exception IOException if the given <code>stream</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *          <code>null</code>, if the token supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *          CKF_PROTECTED_AUTHENTICATION_PATH and a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *          password is given, of if the token login operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    public synchronized void engineLoad(InputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        throws IOException, NoSuchAlgorithmException, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if (NSS_TEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            ATTR_SKEY_TOKEN_TRUE = new CK_ATTRIBUTE(CKA_TOKEN, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        if (stream != null && !token.config.getKeyStoreCompatibilityMode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            throw new IOException("input stream must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        if (useSecmodTrust) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            nssTrustType = Secmod.TrustType.ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                login(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                login(new PasswordCallbackHandler(password));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            }
32634
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   753
        } catch(LoginException e) {
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   754
            Throwable cause = e.getCause();
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   755
            if (cause instanceof PKCS11Exception) {
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   756
                PKCS11Exception pe = (PKCS11Exception) cause;
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   757
                if (pe.getErrorCode() == CKR_PIN_INCORRECT) {
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   758
                    // if password is wrong, the cause of the IOException
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   759
                    // should be an UnrecoverableKeyException
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   760
                    throw new IOException("load failed",
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   761
                            new UnrecoverableKeyException().initCause(e));
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   762
                }
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   763
            }
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   764
            throw new IOException("load failed", e);
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   765
        }
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   766
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   767
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            if (mapLabels() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                // CKA_LABELs are shared by multiple certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                writeDisabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                dumpTokenMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            }
32634
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31538
diff changeset
   775
        } catch (KeyStoreException | PKCS11Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   776
            throw new IOException("load failed", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * Loads the keystore using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * <code>KeyStore.LoadStoreParameter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * <p> The <code>LoadStoreParameter.getProtectionParameter()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * method is expected to return a <code>KeyStore.PasswordProtection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * object.  The password is retrieved from that object and used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * to unlock the PKCS#11 token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * <p> If the token supports a CKF_PROTECTED_AUTHENTICATION_PATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * then the provided password must be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @param param the <code>KeyStore.LoadStoreParameter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @exception IllegalArgumentException if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *          <code>KeyStore.LoadStoreParameter</code> is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *          or if that parameter returns a <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *          <code>ProtectionParameter</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *          input is not recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @exception IOException if the token supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *          CKF_PROTECTED_AUTHENTICATION_PATH and the provided password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *          is non-null, or if the token login operation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    public synchronized void engineLoad(KeyStore.LoadStoreParameter param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                throws IOException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        if (NSS_TEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            ATTR_SKEY_TOKEN_TRUE = new CK_ATTRIBUTE(CKA_TOKEN, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        // if caller wants to pass a NULL password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // force it to pass a non-NULL PasswordProtection that returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        // a NULL password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (param == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                        ("invalid null LoadStoreParameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        if (useSecmodTrust) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            if (param instanceof Secmod.KeyStoreLoadParameter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                nssTrustType = ((Secmod.KeyStoreLoadParameter)param).getTrustType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                nssTrustType = Secmod.TrustType.ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        CallbackHandler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        KeyStore.ProtectionParameter pp = param.getProtectionParameter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if (pp instanceof PasswordProtection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            char[] password = ((PasswordProtection)pp).getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                handler = new PasswordCallbackHandler(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        } else if (pp instanceof CallbackHandlerProtection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            handler = ((CallbackHandlerProtection)pp).getCallbackHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                        ("ProtectionParameter must be either " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                        "PasswordProtection or CallbackHandlerProtection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            login(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            if (mapLabels() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                // CKA_LABELs are shared by multiple certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                writeDisabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                dumpTokenMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   855
        } catch (LoginException | KeyStoreException | PKCS11Exception e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            throw new IOException("load failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    private void login(CallbackHandler handler) throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        if ((token.tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            token.provider.login(null, handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            // token supports protected authentication path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            // (external pin-pad, for example)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            if (handler != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                !token.config.getKeyStoreCompatibilityMode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                throw new LoginException("can not specify password if token " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                                "supports protected authentication path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            // must rely on application-set or default handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            // if one is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            token.provider.login(null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * Get a <code>KeyStore.Entry</code> for the specified alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * @param alias get the <code>KeyStore.Entry</code> for this alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @param protParam this must be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * @return the <code>KeyStore.Entry</code> for the specified alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *          or <code>null</code> if there is no such entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @exception KeyStoreException if the operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *          entry cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @exception UnrecoverableEntryException if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *          <code>protParam</code> were insufficient or invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    public synchronized KeyStore.Entry engineGetEntry(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                        KeyStore.ProtectionParameter protParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                throws KeyStoreException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                UnrecoverableEntryException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        if (protParam != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            protParam instanceof KeyStore.PasswordProtection &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            ((KeyStore.PasswordProtection)protParam).getPassword() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            !token.config.getKeyStoreCompatibilityMode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            throw new KeyStoreException("ProtectionParameter must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        AliasInfo aliasInfo = aliasMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        if (aliasInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                debug.println("engineGetEntry did not find alias [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                        alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                        "] in map");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            if (aliasInfo.type == ATTR_CLASS_CERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                // trusted certificate entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                    debug.println("engineGetEntry found trusted cert entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                return new KeyStore.TrustedCertificateEntry(aliasInfo.cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            } else if (aliasInfo.type == ATTR_CLASS_SKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                // secret key entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                    debug.println("engineGetEntry found secret key entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                THandle h = getTokenObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                        (session, ATTR_CLASS_SKEY, null, aliasInfo.label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                if (h.type != ATTR_CLASS_SKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                        ("expected but could not find secret key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    SecretKey skey = loadSkey(session, h.handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                    return new KeyStore.SecretKeyEntry(skey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                // private key entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                    debug.println("engineGetEntry found private key entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                THandle h = getTokenObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                        (session, ATTR_CLASS_PKEY, aliasInfo.id, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                if (h.type != ATTR_CLASS_PKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                        ("expected but could not find private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                    PrivateKey pkey = loadPkey(session, h.handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                    Certificate[] chain = aliasInfo.chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    if ((pkey != null) && (chain != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                        return new KeyStore.PrivateKeyEntry(pkey, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                            debug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                                ("engineGetEntry got null cert chain or private key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        } catch (PKCS11Exception pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            throw new KeyStoreException(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * Save a <code>KeyStore.Entry</code> under the specified alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * <p> If an entry already exists for the specified alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * it is overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * <p> This KeyStore implementation only supports the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * entry types, and only supports X509Certificates in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * TrustedCertificateEntries.  Also, this implementation does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * protecting entries using a different password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * from the one used for token login.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * <p> Entries are immediately stored on the token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @param alias save the <code>KeyStore.Entry</code> under this alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @param entry the <code>Entry</code> to save
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * @param protParam this must be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @exception KeyStoreException if this operation fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    public synchronized void engineSetEntry(String alias, KeyStore.Entry entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                        KeyStore.ProtectionParameter protParam)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        checkWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        if (protParam != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            protParam instanceof KeyStore.PasswordProtection &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            ((KeyStore.PasswordProtection)protParam).getPassword() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            !token.config.getKeyStoreCompatibilityMode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            throw new KeyStoreException(new UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                                ("ProtectionParameter must be null"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        if (token.isWriteProtected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            throw new KeyStoreException("token write-protected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        if (entry instanceof KeyStore.TrustedCertificateEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            if (useSecmodTrust == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                // PKCS #11 does not allow app to modify trusted certs -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                throw new KeyStoreException(new UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                                    ("trusted certificates may only be set by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                                    "token initialization application"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            }
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43248
diff changeset
  1025
            Secmod.Module module = token.provider.nssModule;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            if ((module.type != ModuleType.KEYSTORE) && (module.type != ModuleType.FIPS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                // XXX allow TRUSTANCHOR module
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                throw new KeyStoreException("Trusted certificates can only be "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                    + "added to the NSS KeyStore module");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            Certificate cert = ((TrustedCertificateEntry)entry).getTrustedCertificate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            if (cert instanceof X509Certificate == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                throw new KeyStoreException("Certificate must be an X509Certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            X509Certificate xcert = (X509Certificate)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            AliasInfo info = aliasMap.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            if (info != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                // XXX try to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                deleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                storeCert(alias, xcert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                module.setTrust(token, xcert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                mapLabels();
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1045
            } catch (PKCS11Exception | CertificateException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                throw new KeyStoreException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            if (entry instanceof KeyStore.PrivateKeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                PrivateKey key =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                        ((KeyStore.PrivateKeyEntry)entry).getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                if (!(key instanceof P11Key) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    !(key instanceof RSAPrivateKey) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    !(key instanceof DSAPrivateKey) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    !(key instanceof DHPrivateKey) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                    !(key instanceof ECPrivateKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    throw new KeyStoreException("unsupported key type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                                                key.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                // only support X509Certificate chains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                Certificate[] chain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                    ((KeyStore.PrivateKeyEntry)entry).getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                if (!(chain instanceof X509Certificate[])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                    throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                        (new UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                                ("unsupported certificate array type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                                chain.getClass().getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    boolean updatedAlias = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    Set<String> aliases = aliasMap.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    for (String oldAlias : aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                        // see if there's an existing entry with the same info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        AliasInfo aliasInfo = aliasMap.get(oldAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                        if (aliasInfo.type == ATTR_CLASS_PKEY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                            aliasInfo.cert.getPublicKey().equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                                        (chain[0].getPublicKey())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                            // found existing entry -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                            // caller is renaming entry or updating cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                            // set new CKA_LABEL/CKA_ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                            // and update certs if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                            updatePkey(alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                                        aliasInfo.id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                                        (X509Certificate[])chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                                        !aliasInfo.cert.equals(chain[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                            updatedAlias = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                    if (!updatedAlias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                        // caller adding new entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                        engineDeleteEntry(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                        storePkey(alias, (KeyStore.PrivateKeyEntry)entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1107
                } catch (PKCS11Exception | CertificateException pe) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    throw new KeyStoreException(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            } else if (entry instanceof KeyStore.SecretKeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                KeyStore.SecretKeyEntry ske = (KeyStore.SecretKeyEntry)entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                SecretKey skey = ske.getSecretKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                try {
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1117
                    // first check if the key already exists
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1118
                    AliasInfo aliasInfo = aliasMap.get(alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1120
                    if (aliasInfo != null) {
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1121
                        engineDeleteEntry(alias);
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1122
                    }
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1123
                    storeSkey(alias, ske);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                } catch (PKCS11Exception pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                    throw new KeyStoreException(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                throw new KeyStoreException(new UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                    ("unsupported entry type: " + entry.getClass().getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                // XXX  NSS does not write out the CKA_ID we pass to them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                // therefore we must re-map labels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                // (can not simply update aliasMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                mapLabels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                    dumpTokenMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1145
            } catch (PKCS11Exception | CertificateException pe) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                throw new KeyStoreException(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            debug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                ("engineSetEntry added new entry for [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                "] to token");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * Determines if the keystore <code>Entry</code> for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * <code>alias</code> is an instance or subclass of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * <code>entryClass</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @param entryClass the entry class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * @return true if the keystore <code>Entry</code> for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *          <code>alias</code> is an instance or subclass of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *          specified <code>entryClass</code>, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    public synchronized boolean engineEntryInstanceOf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                (String alias, Class<? extends KeyStore.Entry> entryClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        return super.engineEntryInstanceOf(alias, entryClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    private X509Certificate loadCert(Session session, long oHandle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                throws PKCS11Exception, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                        { new CK_ATTRIBUTE(CKA_VALUE) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        token.p11.C_GetAttributeValue(session.id(), oHandle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        byte[] bytes = attrs[0].getByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        if (bytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            throw new CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                        ("unexpectedly retrieved null byte array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        return (X509Certificate)cf.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                        (new ByteArrayInputStream(bytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    private X509Certificate[] loadChain(Session session,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                                        X509Certificate endCert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                throws PKCS11Exception, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        ArrayList<X509Certificate> lChain = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        if (endCert.getSubjectX500Principal().equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            (endCert.getIssuerX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            // self signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            return new X509Certificate[] { endCert };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            lChain = new ArrayList<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            lChain.add(endCert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        // try loading remaining certs in chain by following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        // issuer->subject links
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        X509Certificate next = endCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                        ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                        ATTR_CLASS_CERT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                        new CK_ATTRIBUTE(CKA_SUBJECT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                                next.getIssuerX500Principal().getEncoded()) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            long[] ch = findObjects(session, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            if (ch == null || ch.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                // done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                // if more than one found, use first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                if (debug != null && ch.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    debug.println("engineGetEntry found " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                                ch.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                                " certificate entries for subject [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                                next.getIssuerX500Principal().toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                                "] in token - using first entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                next = loadCert(session, ch[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                lChain.add(next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                if (next.getSubjectX500Principal().equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                    (next.getIssuerX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                    // self signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        return lChain.toArray(new X509Certificate[lChain.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    private SecretKey loadSkey(Session session, long oHandle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                        new CK_ATTRIBUTE(CKA_KEY_TYPE) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        token.p11.C_GetAttributeValue(session.id(), oHandle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        long kType = attrs[0].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        String keyType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        int keyLength = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        // XXX NSS mangles the stored key type for secret key token objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        if (kType == CKK_DES || kType == CKK_DES3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            if (kType == CKK_DES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                keyType = "DES";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                keyLength = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            } else if (kType == CKK_DES3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                keyType = "DESede";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                keyLength = 192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            if (kType == CKK_AES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                keyType = "AES";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            } else if (kType == CKK_BLOWFISH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                keyType = "Blowfish";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            } else if (kType == CKK_RC4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                keyType = "ARCFOUR";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                    debug.println("unknown key type [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                                kType +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                                "] - using 'Generic Secret'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                keyType = "Generic Secret";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            // XXX NSS problem CKR_ATTRIBUTE_TYPE_INVALID?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            if (NSS_TEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                keyLength = 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                attrs = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_VALUE_LEN) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                token.p11.C_GetAttributeValue(session.id(), oHandle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                keyLength = (int)attrs[0].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        return P11Key.secretKey(session, oHandle, keyType, keyLength, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    private PrivateKey loadPkey(Session session, long oHandle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        throws PKCS11Exception, KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                        new CK_ATTRIBUTE(CKA_KEY_TYPE) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        token.p11.C_GetAttributeValue(session.id(), oHandle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        long kType = attrs[0].getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        String keyType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        int keyLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        if (kType == CKK_RSA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            keyType = "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            attrs = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_MODULUS) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            token.p11.C_GetAttributeValue(session.id(), oHandle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            BigInteger modulus = attrs[0].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            keyLength = modulus.bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 291
diff changeset
  1315
            // This check will combine our "don't care" values here
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 291
diff changeset
  1316
            // with the system-wide min/max values.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 291
diff changeset
  1317
            try {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 291
diff changeset
  1318
                RSAKeyFactory.checkKeyLengths(keyLength, null,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 291
diff changeset
  1319
                    -1, Integer.MAX_VALUE);
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 291
diff changeset
  1320
            } catch (InvalidKeyException e) {
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 291
diff changeset
  1321
                throw new KeyStoreException(e.getMessage());
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 291
diff changeset
  1322
            }
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 291
diff changeset
  1323
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            return P11Key.privateKey(session,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                                oHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                                keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                                keyLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        } else if (kType == CKK_DSA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            keyType = "DSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            attrs = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_PRIME) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            token.p11.C_GetAttributeValue(session.id(), oHandle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            BigInteger prime = attrs[0].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            keyLength = prime.bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            return P11Key.privateKey(session,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                                oHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                                keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                                keyLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        } else if (kType == CKK_DH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            keyType = "DH";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            attrs = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_PRIME) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            token.p11.C_GetAttributeValue(session.id(), oHandle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            BigInteger prime = attrs[0].getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            keyLength = prime.bitLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            return P11Key.privateKey(session,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                                oHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                                keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                                keyLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        } else if (kType == CKK_EC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                new CK_ATTRIBUTE(CKA_EC_PARAMS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            token.p11.C_GetAttributeValue(session.id(), oHandle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            byte[] encodedParams = attrs[0].getByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            try {
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 10336
diff changeset
  1368
                ECParameterSpec params =
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 10336
diff changeset
  1369
                    ECUtil.getECParameterSpec(null, encodedParams);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                keyLength = params.getCurve().getField().getFieldSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                // we do not want to accept key with unsupported parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                throw new KeyStoreException("Unsupported parameters", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            return P11Key.privateKey(session, oHandle, "EC", keyLength, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                debug.println("unknown key type [" + kType + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            throw new KeyStoreException("unknown key type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * XXX  On ibutton, when you C_SetAttribute(CKA_ID) for a private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     *      it not only changes the CKA_ID of the private key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     *      it changes the CKA_ID of the corresponding cert too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     *      And vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * XXX  On ibutton, CKR_DEVICE_ERROR if you C_SetAttribute(CKA_ID)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *      for a private key, and then try to delete the corresponding cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     *      So this code reverses the order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     *      After the cert is first destroyed (if necessary),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     *      then the CKA_ID of the private key can be changed successfully.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * @param replaceCert if true, then caller is updating alias info for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     *                  existing cert (only update CKA_ID/CKA_LABEL).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *                  if false, then caller is updating cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     *                  (delete old end cert and add new chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    private void updatePkey(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                        byte[] cka_id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                        X509Certificate[] chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                        boolean replaceCert) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                KeyStoreException, CertificateException, PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        // always set replaceCert to true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        // NSS does not allow resetting of CKA_LABEL on an existing cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        // (C_SetAttribute call succeeds, but is ignored)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        replaceCert = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            // first get private key object handle and hang onto it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            THandle h = getTokenObject(session, ATTR_CLASS_PKEY, cka_id, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            long pKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            if (h.type == ATTR_CLASS_PKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                pKeyHandle = h.handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                        ("expected but could not find private key " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                        "with CKA_ID " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                        getID(cka_id));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            // next find existing end entity cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            h = getTokenObject(session, ATTR_CLASS_CERT, cka_id, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            if (h.type != ATTR_CLASS_CERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                        ("expected but could not find certificate " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                        "with CKA_ID " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                        getID(cka_id));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                if (replaceCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                    // replacing existing cert and chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                    destroyChain(cka_id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                    // renaming alias for existing cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                    CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                        new CK_ATTRIBUTE(CKA_LABEL, alias),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                        new CK_ATTRIBUTE(CKA_ID, alias) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                    token.p11.C_SetAttributeValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                        (session.id(), h.handle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            // add new chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            if (replaceCert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                // add all certs in chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                storeChain(alias, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                // already updated alias info for existing end cert -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                // just update CA certs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                storeCaCerts(chain, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            // finally update CKA_ID for private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            // ibutton may have already done this (that is ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                                new CK_ATTRIBUTE(CKA_ID, alias) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            token.p11.C_SetAttributeValue(session.id(), pKeyHandle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                debug.println("updatePkey set new alias [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                                alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                                "] for private key entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    private void updateP11Pkey(String alias, CK_ATTRIBUTE attribute, P11Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        // if token key, update alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        // if session key, convert to token key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            if (key.tokenObject == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                // token key - set new CKA_ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                                new CK_ATTRIBUTE(CKA_ID, alias) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                token.p11.C_SetAttributeValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                                (session.id(), key.keyID, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                    debug.println("updateP11Pkey set new alias [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                                alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                                "] for key entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                // session key - convert to token key and set CKA_ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                    ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                    new CK_ATTRIBUTE(CKA_ID, alias),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                if (attribute != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                    attrs = addAttribute(attrs, attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                token.p11.C_CopyObject(session.id(), key.keyID, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                    debug.println("updateP11Pkey copied private session key " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                                "for [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                                alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                                "] to token entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    private void storeCert(String alias, X509Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                throws PKCS11Exception, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        ArrayList<CK_ATTRIBUTE> attrList = new ArrayList<CK_ATTRIBUTE>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        attrList.add(ATTR_TOKEN_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        attrList.add(ATTR_CLASS_CERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        attrList.add(ATTR_X509_CERT_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        attrList.add(new CK_ATTRIBUTE(CKA_SUBJECT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                                cert.getSubjectX500Principal().getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        attrList.add(new CK_ATTRIBUTE(CKA_ISSUER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                                cert.getIssuerX500Principal().getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        attrList.add(new CK_ATTRIBUTE(CKA_SERIAL_NUMBER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                                cert.getSerialNumber().toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        attrList.add(new CK_ATTRIBUTE(CKA_VALUE, cert.getEncoded()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        if (alias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            attrList.add(new CK_ATTRIBUTE(CKA_LABEL, alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            attrList.add(new CK_ATTRIBUTE(CKA_ID, alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            // ibutton requires something to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            // - alias must be unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            attrList.add(new CK_ATTRIBUTE(CKA_ID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                        getID(cert.getSubjectX500Principal().getName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                                        (X500Principal.CANONICAL), cert)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            token.p11.C_CreateObject(session.id(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                        attrList.toArray(new CK_ATTRIBUTE[attrList.size()]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    private void storeChain(String alias, X509Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                throws PKCS11Exception, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        // add new chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        // end cert has CKA_LABEL and CKA_ID set to alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        // other certs in chain have neither set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        storeCert(alias, chain[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        storeCaCerts(chain, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    private void storeCaCerts(X509Certificate[] chain, int start)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                throws PKCS11Exception, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        // do not add duplicate CA cert if already in token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        // XXX   ibutton stores duplicate CA certs, NSS does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        HashSet<X509Certificate> cacerts = new HashSet<X509Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                        ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                        ATTR_CLASS_CERT };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            long[] handles = findObjects(session, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            // load certs currently on the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            for (long handle : handles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                cacerts.add(loadCert(session, handle));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        for (int i = start; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            if (!cacerts.contains(chain[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                storeCert(null, chain[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            } else if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                debug.println("ignoring duplicate CA cert for [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                        chain[i].getSubjectX500Principal() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                        "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
    private void storeSkey(String alias, KeyStore.SecretKeyEntry ske)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                throws PKCS11Exception, KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        SecretKey skey = ske.getSecretKey();
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1620
        // No need to specify CKA_CLASS, CKA_KEY_TYPE, CKA_VALUE since
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1621
        // they are handled in P11SecretKeyFactory.createKey() method.
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1622
        CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1623
            ATTR_SKEY_TOKEN_TRUE,
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1624
            ATTR_PRIVATE_TRUE,
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1625
            new CK_ATTRIBUTE(CKA_LABEL, alias),
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1626
        };
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1627
        try {
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1628
            P11SecretKeyFactory.convertKey(token, skey, null, attrs);
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1629
        } catch (InvalidKeyException ike) {
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1630
            // re-throw KeyStoreException to match javadoc
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1631
            throw new KeyStoreException("Cannot convert to PKCS11 keys", ike);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1634
        // update global alias map
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1635
        aliasMap.put(alias, new AliasInfo(alias));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1637
        if (debug != null) {
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1638
            debug.println("storeSkey created token secret key for [" +
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  1639
                          alias + "]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    private static CK_ATTRIBUTE[] addAttribute(CK_ATTRIBUTE[] attrs, CK_ATTRIBUTE attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        int n = attrs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        CK_ATTRIBUTE[] newAttrs = new CK_ATTRIBUTE[n + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        System.arraycopy(attrs, 0, newAttrs, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        newAttrs[n] = attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        return newAttrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    private void storePkey(String alias, KeyStore.PrivateKeyEntry pke)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        throws PKCS11Exception, CertificateException, KeyStoreException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        PrivateKey key = pke.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        CK_ATTRIBUTE[] attrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        // If the key is a token object on this token, update it instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        // of creating a duplicate key object.
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
  1659
        // Otherwise, treat a P11Key like any other key, if it is extractable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        if (key instanceof P11Key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            P11Key p11Key = (P11Key)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            if (p11Key.tokenObject && (p11Key.token == this.token)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                updateP11Pkey(alias, null, p11Key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                storeChain(alias, (X509Certificate[])pke.getCertificateChain());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        boolean useNDB = token.config.getNssNetscapeDbWorkaround();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        PublicKey publicKey = pke.getCertificate().getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        if (key instanceof RSAPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
            X509Certificate cert = (X509Certificate)pke.getCertificate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            attrs = getRsaPrivKeyAttrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                (alias, (RSAPrivateKey)key, cert.getSubjectX500Principal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        } else if (key instanceof DSAPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            DSAPrivateKey dsaKey = (DSAPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            CK_ATTRIBUTE[] idAttrs = getIdAttributes(key, publicKey, false, useNDB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            if (idAttrs[0] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                idAttrs[0] = new CK_ATTRIBUTE(CKA_ID, alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                ATTR_CLASS_PKEY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                ATTR_PRIVATE_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                idAttrs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                new CK_ATTRIBUTE(CKA_PRIME, dsaKey.getParams().getP()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                new CK_ATTRIBUTE(CKA_SUBPRIME, dsaKey.getParams().getQ()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                new CK_ATTRIBUTE(CKA_BASE, dsaKey.getParams().getG()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                new CK_ATTRIBUTE(CKA_VALUE, dsaKey.getX()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            if (idAttrs[1] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                attrs = addAttribute(attrs, idAttrs[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            attrs = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                (TemplateManager.O_IMPORT, CKO_PRIVATE_KEY, CKK_DSA, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                debug.println("storePkey created DSA template");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        } else if (key instanceof DHPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            DHPrivateKey dhKey = (DHPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            CK_ATTRIBUTE[] idAttrs = getIdAttributes(key, publicKey, false, useNDB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            if (idAttrs[0] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                idAttrs[0] = new CK_ATTRIBUTE(CKA_ID, alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                ATTR_CLASS_PKEY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                ATTR_PRIVATE_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_DH),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                idAttrs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                new CK_ATTRIBUTE(CKA_PRIME, dhKey.getParams().getP()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                new CK_ATTRIBUTE(CKA_BASE, dhKey.getParams().getG()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                new CK_ATTRIBUTE(CKA_VALUE, dhKey.getX()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            if (idAttrs[1] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                attrs = addAttribute(attrs, idAttrs[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            attrs = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                (TemplateManager.O_IMPORT, CKO_PRIVATE_KEY, CKK_DH, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        } else if (key instanceof ECPrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            ECPrivateKey ecKey = (ECPrivateKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            CK_ATTRIBUTE[] idAttrs = getIdAttributes(key, publicKey, false, useNDB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            if (idAttrs[0] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                idAttrs[0] = new CK_ATTRIBUTE(CKA_ID, alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 10336
diff changeset
  1744
            byte[] encodedParams =
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 10336
diff changeset
  1745
                ECUtil.encodeECParameterSpec(null, ecKey.getParams());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                ATTR_CLASS_PKEY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                ATTR_PRIVATE_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_EC),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                idAttrs[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                new CK_ATTRIBUTE(CKA_VALUE, ecKey.getS()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                new CK_ATTRIBUTE(CKA_EC_PARAMS, encodedParams),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            if (idAttrs[1] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                attrs = addAttribute(attrs, idAttrs[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            attrs = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                (TemplateManager.O_IMPORT, CKO_PRIVATE_KEY, CKK_EC, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                debug.println("storePkey created EC template");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        } else if (key instanceof P11Key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            // sensitive/non-extractable P11Key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            P11Key p11Key = (P11Key)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            if (p11Key.token != this.token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                    ("Cannot move sensitive keys across tokens");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            CK_ATTRIBUTE netscapeDB = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            if (useNDB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                // Note that this currently fails due to an NSS bug.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                // They do not allow the CKA_NETSCAPE_DB attribute to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                // specified during C_CopyObject() and fail with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                // CKR_ATTRIBUTE_READ_ONLY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                // But if we did not specify it, they would fail with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                // CKA_TEMPLATE_INCOMPLETE, so leave this code in here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                CK_ATTRIBUTE[] idAttrs = getIdAttributes(key, publicKey, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                netscapeDB = idAttrs[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            // Update the key object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            updateP11Pkey(alias, netscapeDB, p11Key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            storeChain(alias, (X509Certificate[])pke.getCertificateChain());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            throw new KeyStoreException("unsupported key type: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
            // create private key entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            token.p11.C_CreateObject(session.id(), attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                debug.println("storePkey created token key for [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                                alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                                "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        storeChain(alias, (X509Certificate[])pke.getCertificateChain());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    private CK_ATTRIBUTE[] getRsaPrivKeyAttrs(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                                RSAPrivateKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                                X500Principal subject) throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        // subject is currently ignored - could be used to set CKA_SUBJECT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        CK_ATTRIBUTE[] attrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        if (key instanceof RSAPrivateCrtKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                debug.println("creating RSAPrivateCrtKey attrs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                ATTR_CLASS_PKEY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                ATTR_PRIVATE_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                new CK_ATTRIBUTE(CKA_ID, alias),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                new CK_ATTRIBUTE(CKA_MODULUS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                                rsaKey.getModulus()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                                rsaKey.getPrivateExponent()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                                rsaKey.getPublicExponent()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                new CK_ATTRIBUTE(CKA_PRIME_1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                                rsaKey.getPrimeP()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                new CK_ATTRIBUTE(CKA_PRIME_2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                                rsaKey.getPrimeQ()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                new CK_ATTRIBUTE(CKA_EXPONENT_1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                                rsaKey.getPrimeExponentP()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                new CK_ATTRIBUTE(CKA_EXPONENT_2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                                rsaKey.getPrimeExponentQ()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                new CK_ATTRIBUTE(CKA_COEFFICIENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                                rsaKey.getCrtCoefficient()) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            attrs = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                (TemplateManager.O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                debug.println("creating RSAPrivateKey attrs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  1857
            RSAPrivateKey rsaKey = key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                ATTR_CLASS_PKEY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                ATTR_PRIVATE_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                new CK_ATTRIBUTE(CKA_ID, alias),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                new CK_ATTRIBUTE(CKA_MODULUS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                                rsaKey.getModulus()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                                rsaKey.getPrivateExponent()) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            attrs = token.getAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                (TemplateManager.O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * Compute the CKA_ID and/or CKA_NETSCAPE_DB attributes that should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * used for this private key. It uses the same algorithm to calculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * values as NSS. The public and private keys MUST match for the result to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * be correct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * It returns a 2 element array with CKA_ID at index 0 and CKA_NETSCAPE_DB
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * at index 1. The boolean flags determine what is to be calculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * If false or if we could not calculate the value, that element is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * NOTE that we currently do not use the CKA_ID value calculated by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
    private CK_ATTRIBUTE[] getIdAttributes(PrivateKey privateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            PublicKey publicKey, boolean id, boolean netscapeDb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        if ((id || netscapeDb) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        String alg = privateKey.getAlgorithm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        if (id && alg.equals("RSA") && (publicKey instanceof RSAPublicKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            // CKA_NETSCAPE_DB not needed for RSA public keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            BigInteger n = ((RSAPublicKey)publicKey).getModulus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            attrs[0] = new CK_ATTRIBUTE(CKA_ID, sha1(getMagnitude(n)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        } else if (alg.equals("DSA") && (publicKey instanceof DSAPublicKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            BigInteger y = ((DSAPublicKey)publicKey).getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            if (id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                attrs[0] = new CK_ATTRIBUTE(CKA_ID, sha1(getMagnitude(y)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            if (netscapeDb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                attrs[1] = new CK_ATTRIBUTE(CKA_NETSCAPE_DB, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        } else if (alg.equals("DH") && (publicKey instanceof DHPublicKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            BigInteger y = ((DHPublicKey)publicKey).getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            if (id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                attrs[0] = new CK_ATTRIBUTE(CKA_ID, sha1(getMagnitude(y)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            if (netscapeDb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                attrs[1] = new CK_ATTRIBUTE(CKA_NETSCAPE_DB, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        } else if (alg.equals("EC") && (publicKey instanceof ECPublicKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            ECPublicKey ecPub = (ECPublicKey)publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            ECPoint point = ecPub.getW();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
            ECParameterSpec params = ecPub.getParams();
17491
7a33824ec8c5 7194075: Various classes of sunec.jar are duplicated in rt.jar
vinnie
parents: 10336
diff changeset
  1920
            byte[] encodedPoint = ECUtil.encodePoint(point, params.getCurve());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            if (id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                attrs[0] = new CK_ATTRIBUTE(CKA_ID, sha1(encodedPoint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            if (netscapeDb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                attrs[1] = new CK_ATTRIBUTE(CKA_NETSCAPE_DB, encodedPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            throw new RuntimeException("Unknown key algorithm " + alg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        return attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * return true if cert destroyed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    private boolean destroyCert(byte[] cka_id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                throws PKCS11Exception, KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            THandle h = getTokenObject(session, ATTR_CLASS_CERT, cka_id, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            if (h.type != ATTR_CLASS_CERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            token.p11.C_DestroyObject(session.id(), h.handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                debug.println("destroyCert destroyed cert with CKA_ID [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                                                getID(cka_id) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                                                "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * return true if chain destroyed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    private boolean destroyChain(byte[] cka_id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        throws PKCS11Exception, CertificateException, KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            THandle h = getTokenObject(session, ATTR_CLASS_CERT, cka_id, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            if (h.type != ATTR_CLASS_CERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                    debug.println("destroyChain could not find " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                        "end entity cert with CKA_ID [0x" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                        Functions.toHexString(cka_id) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                        "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            X509Certificate endCert = loadCert(session, h.handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            token.p11.C_DestroyObject(session.id(), h.handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                debug.println("destroyChain destroyed end entity cert " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                        "with CKA_ID [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                        getID(cka_id) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                        "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            // build chain following issuer->subject links
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            X509Certificate next = endCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                if (next.getSubjectX500Principal().equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                    (next.getIssuerX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                    // self signed - done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                        ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                        ATTR_CLASS_CERT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                        new CK_ATTRIBUTE(CKA_SUBJECT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                                  next.getIssuerX500Principal().getEncoded()) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                long[] ch = findObjects(session, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                if (ch == null || ch.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                    // done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                    // if more than one found, use first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                    if (debug != null && ch.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                        debug.println("destroyChain found " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                                ch.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                                " certificate entries for subject [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                                next.getIssuerX500Principal() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                                "] in token - using first entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                    next = loadCert(session, ch[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                    // only delete if not part of any other chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                    attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                        ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                        ATTR_CLASS_CERT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                        new CK_ATTRIBUTE(CKA_ISSUER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                                next.getSubjectX500Principal().getEncoded()) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                    long[] issuers = findObjects(session, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                    boolean destroyIt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                    if (issuers == null || issuers.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                        // no other certs with this issuer -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                        // destroy it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                        destroyIt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                    } else if (issuers.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                        X509Certificate iCert = loadCert(session, issuers[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                        if (next.equals(iCert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                            // only cert with issuer is itself (self-signed) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                            // destroy it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                            destroyIt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                    if (destroyIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                        token.p11.C_DestroyObject(session.id(), ch[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                            debug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                                ("destroyChain destroyed cert in chain " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                                "with subject [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                                next.getSubjectX500Principal() + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                            debug.println("destroyChain did not destroy " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                                "shared cert in chain with subject [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                                next.getSubjectX500Principal() + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * return true if secret key destroyed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
    private boolean destroySkey(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                throws PKCS11Exception, KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            THandle h = getTokenObject(session, ATTR_CLASS_SKEY, null, alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            if (h.type != ATTR_CLASS_SKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                    debug.println("destroySkey did not find secret key " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                        "with CKA_LABEL [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                        alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                        "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            token.p11.C_DestroyObject(session.id(), h.handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     * return true if private key destroyed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
    private boolean destroyPkey(byte[] cka_id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                throws PKCS11Exception, KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
            THandle h = getTokenObject(session, ATTR_CLASS_PKEY, cka_id, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
            if (h.type != ATTR_CLASS_PKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                    debug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                        ("destroyPkey did not find private key with CKA_ID [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                        getID(cka_id) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                        "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            token.p11.C_DestroyObject(session.id(), h.handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * build [alias + issuer + serialNumber] string from a cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    private String getID(String alias, X509Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        X500Principal issuer = cert.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        BigInteger serialNum = cert.getSerialNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        return alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                ALIAS_SEP +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                issuer.getName(X500Principal.CANONICAL) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                ALIAS_SEP +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                serialNum.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     * build CKA_ID string from bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
    private static String getID(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        boolean printable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        for (int i = 0; i < bytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
            if (!DerValue.isPrintableStringChar((char)bytes[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                printable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
        if (!printable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
            return "0x" + Functions.toHexString(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                return new String(bytes, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
            } catch (UnsupportedEncodingException uee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                return "0x" + Functions.toHexString(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * find an object on the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * @param type either ATTR_CLASS_CERT, ATTR_CLASS_PKEY, or ATTR_CLASS_SKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * @param cka_id the CKA_ID if type is ATTR_CLASS_CERT or ATTR_CLASS_PKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     * @param cka_label the CKA_LABEL if type is ATTR_CLASS_SKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
    private THandle getTokenObject(Session session,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                                CK_ATTRIBUTE type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                                byte[] cka_id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                                String cka_label)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                throws PKCS11Exception, KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        CK_ATTRIBUTE[] attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
        if (type == ATTR_CLASS_SKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                        ATTR_SKEY_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                        new CK_ATTRIBUTE(CKA_LABEL, cka_label),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                        type };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                        ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                        new CK_ATTRIBUTE(CKA_ID, cka_id),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                        type };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
        long[] h = findObjects(session, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        if (h.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                if (type == ATTR_CLASS_SKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                    debug.println("getTokenObject did not find secret key " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                                "with CKA_LABEL [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                                cka_label +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                                "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                } else if (type == ATTR_CLASS_CERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                    debug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                        ("getTokenObject did not find cert with CKA_ID [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                        getID(cka_id) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                        "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                    debug.println("getTokenObject did not find private key " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                        "with CKA_ID [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                        getID(cka_id) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                        "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
        } else if (h.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
            // found object handle - return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
            return new THandle(h[0], type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            // found multiple object handles -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
            // see if token ignored CKA_LABEL during search (e.g. NSS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
            if (type == ATTR_CLASS_SKEY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                ArrayList<THandle> list = new ArrayList<THandle>(h.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                for (int i = 0; i < h.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                    CK_ATTRIBUTE[] label = new CK_ATTRIBUTE[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                                        { new CK_ATTRIBUTE(CKA_LABEL) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                    token.p11.C_GetAttributeValue(session.id(), h[i], label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                    if (label[0].pValue != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                        cka_label.equals(new String(label[0].getCharArray()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                        list.add(new THandle(h[i], ATTR_CLASS_SKEY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                if (list.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                    // yes, there was only one CKA_LABEL that matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                    return list.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                    throw new KeyStoreException("invalid KeyStore state: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                        "found " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                        list.size() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                        " secret keys sharing CKA_LABEL [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                        cka_label +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                        "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
            } else if (type == ATTR_CLASS_CERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                throw new KeyStoreException("invalid KeyStore state: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                        "found " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                        h.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                        " certificates sharing CKA_ID " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                        getID(cka_id));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                throw new KeyStoreException("invalid KeyStore state: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                        "found " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                        h.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                        " private keys sharing CKA_ID " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                        getID(cka_id));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        return new THandle(NO_HANDLE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     * Create a mapping of all key pairs, trusted certs, and secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     * on the token into logical KeyStore entries unambiguously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     * accessible via an alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     * If the token is removed, the map may contain stale values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
     * KeyStore.load should be called to re-create the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     * Assume all private keys and matching certs share a unique CKA_ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     * Assume all secret keys have a unique CKA_LABEL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     * @return true if multiple certs found sharing the same CKA_LABEL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     *          (if so, write capabilities are disabled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
    private boolean mapLabels() throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                PKCS11Exception, CertificateException, KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        CK_ATTRIBUTE[] trustedAttr = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                                new CK_ATTRIBUTE(CKA_TRUSTED) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
            // get all private key CKA_IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
            ArrayList<byte[]> pkeyIDs = new ArrayList<byte[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
            CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                ATTR_CLASS_PKEY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            long[] handles = findObjects(session, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            for (long handle : handles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                attrs = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_ID) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                token.p11.C_GetAttributeValue(session.id(), handle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                if (attrs[0].pValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                    pkeyIDs.add(attrs[0].getByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            // Get all certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            // If cert does not have a CKA_LABEL nor CKA_ID, it is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            // Get the CKA_LABEL for each cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            // (if the cert does not have a CKA_LABEL, use the CKA_ID).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
            // Map each cert to the its CKA_LABEL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            // (multiple certs may be mapped to a single CKA_LABEL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
            HashMap<String, HashSet<AliasInfo>> certMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                                new HashMap<String, HashSet<AliasInfo>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                ATTR_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                ATTR_CLASS_CERT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            handles = findObjects(session, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            for (long handle : handles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                attrs = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_LABEL) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                String cka_label = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                byte[] cka_id = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
                    token.p11.C_GetAttributeValue(session.id(), handle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
                    if (attrs[0].pValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
                        // there is a CKA_LABEL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                        cka_label = new String(attrs[0].getCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                } catch (PKCS11Exception pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                    if (pe.getErrorCode() != CKR_ATTRIBUTE_TYPE_INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                        throw pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                    // GetAttributeValue for CKA_LABEL not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                    // XXX SCA1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                // get CKA_ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                attrs = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_ID) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                token.p11.C_GetAttributeValue(session.id(), handle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                if (attrs[0].pValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                    if (cka_label == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                        // no cka_label nor cka_id - ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                    if (cka_label == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                        // use CKA_ID as CKA_LABEL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
                        cka_label = getID(attrs[0].getByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                    cka_id = attrs[0].getByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                X509Certificate cert = loadCert(session, handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                // get CKA_TRUSTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                boolean cka_trusted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                if (useSecmodTrust) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                    cka_trusted = Secmod.getInstance().isTrusted(cert, nssTrustType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                    if (CKA_TRUSTED_SUPPORTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                            token.p11.C_GetAttributeValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                                    (session.id(), handle, trustedAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                            cka_trusted = trustedAttr[0].getBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                        } catch (PKCS11Exception pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                            if (pe.getErrorCode() == CKR_ATTRIBUTE_TYPE_INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                                // XXX  NSS, ibutton, sca1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                                CKA_TRUSTED_SUPPORTED = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                                    debug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                                            ("CKA_TRUSTED attribute not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
                HashSet<AliasInfo> infoSet = certMap.get(cka_label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                if (infoSet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
                    infoSet = new HashSet<AliasInfo>(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
                    certMap.put(cka_label, infoSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                // initially create private key entry AliasInfo entries -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                // these entries will get resolved into their true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                // entry types later
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                infoSet.add(new AliasInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                                (cka_label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                                cka_id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                                cka_trusted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                                cert));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
            // create list secret key CKA_LABELS -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            // if there are duplicates (either between secret keys,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            // or between a secret key and another object),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            // throw an exception
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  2402
            HashMap<String, AliasInfo> sKeyMap =
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  2403
                    new HashMap<String, AliasInfo>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            attrs = new CK_ATTRIBUTE[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                ATTR_SKEY_TOKEN_TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
                ATTR_CLASS_SKEY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            handles = findObjects(session, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            for (long handle : handles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                attrs = new CK_ATTRIBUTE[] { new CK_ATTRIBUTE(CKA_LABEL) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
                token.p11.C_GetAttributeValue(session.id(), handle, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
                if (attrs[0].pValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                    // there is a CKA_LABEL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                    String cka_label = new String(attrs[0].getCharArray());
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  2418
                    if (sKeyMap.get(cka_label) == null) {
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  2419
                        sKeyMap.put(cka_label, new AliasInfo(cka_label));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                        throw new KeyStoreException("invalid KeyStore state: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
                                "found multiple secret keys sharing same " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
                                "CKA_LABEL [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
                                cka_label +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                                "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
            // update global aliasMap with alias mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            ArrayList<AliasInfo> matchedCerts =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                                mapPrivateKeys(pkeyIDs, certMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
            boolean sharedLabel = mapCerts(matchedCerts, certMap);
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  2434
            mapSecretKeys(sKeyMap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
            return sharedLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
     * for each private key CKA_ID, find corresponding cert with same CKA_ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
     * if found cert, see if cert CKA_LABEL is unique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
     *     if CKA_LABEL unique, map private key/cert alias to that CKA_LABEL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
     *     if CKA_LABEL not unique, map private key/cert alias to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
     *                   CKA_LABEL + ALIAS_SEP + ISSUER + ALIAS_SEP + SERIAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
     * if cert not found, ignore private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
     * (don't support private key entries without a cert chain yet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
     * @return a list of AliasInfo entries that represents all matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
    private ArrayList<AliasInfo> mapPrivateKeys(ArrayList<byte[]> pkeyIDs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
                        HashMap<String, HashSet<AliasInfo>> certMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                throws PKCS11Exception, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  2458
        // reset global alias map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
        aliasMap = new HashMap<String, AliasInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        // list of matched certs that we will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
        ArrayList<AliasInfo> matchedCerts = new ArrayList<AliasInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
        for (byte[] pkeyID : pkeyIDs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
            // try to find a matching CKA_ID in a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
            boolean foundMatch = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
            Set<String> certLabels = certMap.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
            for (String certLabel : certLabels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                // get cert CKA_IDs (if present) for each cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                HashSet<AliasInfo> infoSet = certMap.get(certLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                for (AliasInfo aliasInfo : infoSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                    if (Arrays.equals(pkeyID, aliasInfo.id)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                        // found private key with matching cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                        if (infoSet.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                            // unique CKA_LABEL - use certLabel as alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                            aliasInfo.matched = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                            aliasMap.put(certLabel, aliasInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                            // create new alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
                            aliasInfo.matched = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                            aliasMap.put(getID(certLabel, aliasInfo.cert),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                                        aliasInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                        matchedCerts.add(aliasInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                        foundMatch = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                if (foundMatch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
            if (!foundMatch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
                    debug.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
                        ("did not find match for private key with CKA_ID [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                        getID(pkeyID) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                        "] (ignoring entry)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
        return matchedCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     * for each cert not matched with a private key but is CKA_TRUSTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     *     if CKA_LABEL unique, map cert to CKA_LABEL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
     *     if CKA_LABEL not unique, map cert to [label+issuer+serialNum]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     * if CKA_TRUSTED not supported, treat all certs not part of a chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     * as trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     * @return true if multiple certs found sharing the same CKA_LABEL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
    private boolean mapCerts(ArrayList<AliasInfo> matchedCerts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                        HashMap<String, HashSet<AliasInfo>> certMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                throws PKCS11Exception, CertificateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        // load all cert chains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        for (AliasInfo aliasInfo : matchedCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
                session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
                aliasInfo.chain = loadChain(session, aliasInfo.cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
                token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        // find all certs in certMap not part of a cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
        // - these are trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        boolean sharedLabel = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
        Set<String> certLabels = certMap.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
        for (String certLabel : certLabels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
            HashSet<AliasInfo> infoSet = certMap.get(certLabel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
            for (AliasInfo aliasInfo : infoSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
                if (aliasInfo.matched == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                    // already found a private key match for this cert -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
                    // just continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
                    aliasInfo.trusted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
                // cert in this aliasInfo is not matched yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
                // if CKA_TRUSTED_SUPPORTED == true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                // then check if cert is trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                if (CKA_TRUSTED_SUPPORTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                    if (aliasInfo.trusted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                        // trusted certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
                        if (mapTrustedCert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                                (certLabel, aliasInfo, infoSet) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                            sharedLabel = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                // CKA_TRUSTED_SUPPORTED == false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                // XXX treat all certs not part of a chain as trusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                // XXX Unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                // boolean partOfChain = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                // for (AliasInfo matchedInfo : matchedCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                //     for (int i = 0; i < matchedInfo.chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                //      if (matchedInfo.chain[i].equals(aliasInfo.cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                //          partOfChain = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                //          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                //      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                //     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                //     if (partOfChain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                //      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                //     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                // if (!partOfChain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                //     if (mapTrustedCert(certLabel,aliasInfo,infoSet) == true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                //      sharedLabel = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                //     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                // } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                //    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                //      debug.println("ignoring unmatched/untrusted cert " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                //          "that is part of cert chain - cert subject is [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
                //          aliasInfo.cert.getSubjectX500Principal().getName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                //                              (X500Principal.CANONICAL) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
                //          "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                //     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        return sharedLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
    private boolean mapTrustedCert(String certLabel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                                AliasInfo aliasInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                                HashSet<AliasInfo> infoSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
        boolean sharedLabel = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
        aliasInfo.type = ATTR_CLASS_CERT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        aliasInfo.trusted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
        if (infoSet.size() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
            // unique CKA_LABEL - use certLabel as alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
            aliasMap.put(certLabel, aliasInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
            // create new alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
            sharedLabel = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
            aliasMap.put(getID(certLabel, aliasInfo.cert), aliasInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        return sharedLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
     * If the secret key shares a CKA_LABEL with another entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
     * throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
     */
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  2633
    private void mapSecretKeys(HashMap<String, AliasInfo> sKeyMap)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
                throws KeyStoreException {
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  2635
        for (String label : sKeyMap.keySet()) {
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  2636
            if (aliasMap.containsKey(label)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
                throw new KeyStoreException("invalid KeyStore state: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
                        "found secret key sharing CKA_LABEL [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                        label +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                        "] with another token object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
        }
291
be2e0a87d658 6599979: KeyStore.setEntry/setKeyEntry() do not override existing entry for secret key objects
valeriep
parents: 2
diff changeset
  2643
        aliasMap.putAll(sKeyMap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
    private void dumpTokenMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
        Set<String> aliases = aliasMap.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
        System.out.println("Token Alias Map:");
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
  2649
        if (aliases.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
            System.out.println("  [empty]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
            for (String s : aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                System.out.println("  " + s + aliasMap.get(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
    private void checkWrite() throws KeyStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
        if (writeDisabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
                ("This PKCS11KeyStore does not support write capabilities");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
    private final static long[] LONG0 = new long[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
    private static long[] findObjects(Session session, CK_ATTRIBUTE[] attrs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
        Token token = session.token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
        long[] handles = LONG0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
        token.p11.C_FindObjectsInit(session.id(), attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
            long[] h = token.p11.C_FindObjects(session.id(), FINDOBJECTS_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
            if (h.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
            handles = P11Util.concat(handles, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
        token.p11.C_FindObjectsFinal(session.id());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
        return handles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
}