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