jdk/src/jdk.crypto.token/share/classes/sun/security/pkcs11/P11TlsPrfGenerator.java
author clanger
Mon, 23 Jan 2017 14:04:44 +0100
changeset 43247 8d242299a219
parent 42693 6645de32a866
permissions -rw-r--r--
8173197: (se) WindowsSelectorImpl.c does not compile with VS2010 Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
     2
 * Copyright (c) 2005, 2014, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.crypto.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.internal.spec.TlsPrfParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import static sun.security.pkcs11.TemplateManager.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * KeyGenerator for the TLS PRF. Note that although the PRF is used in a number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * of places during the handshake, this class is usually only used to calculate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the Finished messages. The reason is that for those other uses more specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * PKCS#11 mechanisms have been defined (CKM_SSL3_MASTER_KEY_DERIVE, etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>This class supports the CKM_TLS_PRF mechanism from PKCS#11 v2.20 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * the older NSS private mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
final class P11TlsPrfGenerator extends KeyGeneratorSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final static String MSG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            "TlsPrfGenerator must be initialized using a TlsPrfParameterSpec";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // mechanism id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
    66
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private TlsPrfParameterSpec spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private P11Key p11Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    P11TlsPrfGenerator(Token token, String algorithm, long mechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.mechanism = mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
    83
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (params instanceof TlsPrfParameterSpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new InvalidAlgorithmParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.spec = (TlsPrfParameterSpec)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        SecretKey key = spec.getSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            key = NULL_KEY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            p11Key = P11SecretKeyFactory.convertKey(token, key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new InvalidAlgorithmParameterException("init() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
19186
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   101
    // SecretKeySpec does not allow zero length keys, so we define our
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   102
    // own class.
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   103
    //
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   104
    // As an anonymous class cannot make any guarantees about serialization
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   105
    // compatibility, it is nonsensical for an anonymous class to define a
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   106
    // serialVersionUID. Suppress warnings relative to missing serialVersionUID
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   107
    // field in the anonymous subclass of serializable SecretKey.
2b66b27d9ea9 7127524: P11TlsPrfGenerator has anonymous inner class with serialVersionUID
xuelei
parents: 10336
diff changeset
   108
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final SecretKey NULL_KEY = new SecretKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        public byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return "Generic";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected void engineInit(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (spec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new IllegalStateException("TlsPrfGenerator must be initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        byte[] label = P11Util.getBytesUTF8(spec.getLabel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        byte[] seed = spec.getSeed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (mechanism == CKM_NSS_TLS_PRF_GENERAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                token.p11.C_SignInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    (session.id(), new CK_MECHANISM(mechanism), p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                token.p11.C_SignUpdate(session.id(), 0, label, 0, label.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                token.p11.C_SignUpdate(session.id(), 0, seed, 0, seed.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                byte[] out = token.p11.C_SignFinal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                    (session.id(), spec.getOutputLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                return new SecretKeySpec(out, "TlsPrf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw new ProviderException("Could not calculate PRF", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // mechanism == CKM_TLS_PRF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        byte[] out = new byte[spec.getOutputLength()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        CK_TLS_PRF_PARAMS params = new CK_TLS_PRF_PARAMS(seed, label, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            long keyID = token.p11.C_DeriveKey(session.id(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                new CK_MECHANISM(mechanism, params), p11Key.keyID, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // ignore keyID, returned PRF bytes are in 'out'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return new SecretKeySpec(out, "TlsPrf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new ProviderException("Could not calculate PRF", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}