jdk/src/share/classes/sun/security/pkcs11/P11TlsPrfGenerator.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 19186 2b66b27d9ea9
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private TlsPrfParameterSpec spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private P11Key p11Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    P11TlsPrfGenerator(Token token, String algorithm, long mechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.mechanism = mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected void engineInit(SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected void engineInit(AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            SecureRandom random) throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (params instanceof TlsPrfParameterSpec == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new InvalidAlgorithmParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.spec = (TlsPrfParameterSpec)params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        SecretKey key = spec.getSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            key = NULL_KEY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            p11Key = P11SecretKeyFactory.convertKey(token, key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } catch (InvalidKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new InvalidAlgorithmParameterException("init() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // SecretKeySpec does not allow zero length keys, so we define our own class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final SecretKey NULL_KEY = new SecretKey() {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   101
        private static final long serialVersionUID = -8090049519656411362L;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   102
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        public byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return "RAW";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return "Generic";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected void engineInit(int keysize, SecureRandom random) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        throw new InvalidParameterException(MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    protected SecretKey engineGenerateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (spec == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IllegalStateException("TlsPrfGenerator must be initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        byte[] label = P11Util.getBytesUTF8(spec.getLabel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        byte[] seed = spec.getSeed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (mechanism == CKM_NSS_TLS_PRF_GENERAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                token.p11.C_SignInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    (session.id(), new CK_MECHANISM(mechanism), p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                token.p11.C_SignUpdate(session.id(), 0, label, 0, label.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                token.p11.C_SignUpdate(session.id(), 0, seed, 0, seed.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                byte[] out = token.p11.C_SignFinal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                    (session.id(), spec.getOutputLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                return new SecretKeySpec(out, "TlsPrf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                throw new ProviderException("Could not calculate PRF", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // mechanism == CKM_TLS_PRF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        byte[] out = new byte[spec.getOutputLength()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        CK_TLS_PRF_PARAMS params = new CK_TLS_PRF_PARAMS(seed, label, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        Session session = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            long keyID = token.p11.C_DeriveKey(session.id(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                new CK_MECHANISM(mechanism, params), p11Key.keyID, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // ignore keyID, returned PRF bytes are in 'out'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return new SecretKeySpec(out, "TlsPrf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new ProviderException("Could not calculate PRF", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}