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