jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/P11Digest.java
author valeriep
Tue, 11 Oct 2016 00:04:35 +0000
changeset 41471 18c0f074ed97
parent 25859 3317bb8137f4
permissions -rw-r--r--
8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey Summary: Replaced the reflection calls with the new sun.security.util.MessageDigestSpi2 interface Reviewed-by: xuelei, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
41471
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2016, 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: 5291
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: 5291
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: 5291
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.nio.ch.DirectBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
41471
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 25859
diff changeset
    37
import sun.security.util.MessageDigestSpi2;
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 25859
diff changeset
    38
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * MessageDigest implementation class. This class currently supports
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11507
diff changeset
    44
 * MD2, MD5, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Note that many digest operations are on fairly small amounts of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * (less than 100 bytes total). For example, the 2nd hashing in HMAC or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the PRF in TLS. In order to speed those up, we use some buffering to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * minimize number of the Java->native transitions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
41471
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 25859
diff changeset
    54
final class P11Digest extends MessageDigestSpi implements Cloneable,
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 25859
diff changeset
    55
    MessageDigestSpi2 {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
    57
    /* fields initialized, no session acquired */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final static int S_BLANK    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
    60
    /* data in buffer, session acquired, but digest not initialized */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final static int S_BUFFERED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /* session initialized for digesting */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final static int S_INIT     = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final static int BUFFER_SIZE = 96;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
    74
    // mechanism id object
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
    75
    private final CK_MECHANISM mechanism;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // length of the digest in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private final int digestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // associated session, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private Session session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // current state, one of S_* above
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private int state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // buffer to reduce number of JNI calls
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
    87
    private byte[] buffer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // offset into the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private int bufOfs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    P11Digest(Token token, String algorithm, long mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.algorithm = algorithm;
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
    96
        this.mechanism = new CK_MECHANISM(mechanism);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        switch ((int)mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        case (int)CKM_MD2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        case (int)CKM_MD5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            digestLength = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        case (int)CKM_SHA_1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            digestLength = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            break;
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11507
diff changeset
   105
        case (int)CKM_SHA224:
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11507
diff changeset
   106
            digestLength = 28;
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11507
diff changeset
   107
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        case (int)CKM_SHA256:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            digestLength = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        case (int)CKM_SHA384:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            digestLength = 48;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        case (int)CKM_SHA512:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            digestLength = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            throw new ProviderException("Unknown mechanism: " + mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        buffer = new byte[BUFFER_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        state = S_BLANK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected int engineGetDigestLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return digestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private void fetchSession() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (state == S_BLANK) {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   132
            try {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   133
                session = token.getOpSession();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   134
                state = S_BUFFERED;
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   135
            } catch (PKCS11Exception e) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   136
                throw new ProviderException("No more session available", e);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   137
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    protected void engineReset() {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   143
        token.ensureValid();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   144
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   145
        if (session != null) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   146
            if (state == S_INIT && token.explicitCancel == true) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   147
                session = token.killSession(session);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   148
            } else {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   149
                session = token.releaseSession(session);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   152
        state = S_BLANK;
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   153
        bufOfs = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    protected byte[] engineDigest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            byte[] digest = new byte[digestLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            int n = engineDigest(digest, 0, digestLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } catch (DigestException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new ProviderException("internal error", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    protected int engineDigest(byte[] digest, int ofs, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throws DigestException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (len < digestLength) {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   171
            throw new DigestException("Length must be at least " +
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   172
                    digestLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   174
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        fetchSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (state == S_BUFFERED) {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   179
                n = token.p11.C_DigestSingle(session.id(), mechanism, buffer, 0,
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   180
                        bufOfs, digest, ofs, len);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   181
                bufOfs = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (bufOfs != 0) {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   184
                    token.p11.C_DigestUpdate(session.id(), 0, buffer, 0,
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   185
                            bufOfs);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   186
                    bufOfs = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                n = token.p11.C_DigestFinal(session.id(), digest, ofs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (n != digestLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                throw new ProviderException("internal digest length error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new ProviderException("digest() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } finally {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   197
            engineReset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    protected void engineUpdate(byte in) {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   203
        byte[] temp = { in };
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   204
        engineUpdate(temp, 0, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    protected void engineUpdate(byte[] in, int ofs, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   212
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   213
        fetchSession();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   214
        try {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   215
            if (state == S_BUFFERED) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   216
                token.p11.C_DigestInit(session.id(), mechanism);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   217
                state = S_INIT;
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   218
            }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   219
            if ((bufOfs != 0) && (bufOfs + len > buffer.length)) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   220
                // process the buffered data
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   221
                token.p11.C_DigestUpdate(session.id(), 0, buffer, 0, bufOfs);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   222
                bufOfs = 0;
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   223
            }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   224
            if (bufOfs + len > buffer.length) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   225
                // process the new data
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   226
                token.p11.C_DigestUpdate(session.id(), 0, in, ofs, len);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   227
             } else {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   228
                // buffer the new data
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   229
                System.arraycopy(in, ofs, buffer, bufOfs, len);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   230
                bufOfs += len;
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   231
            }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   232
        } catch (PKCS11Exception e) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   233
            engineReset();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   234
            throw new ProviderException("update() failed", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    // Called by SunJSSE via reflection during the SSL 3.0 handshake if
41471
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 25859
diff changeset
   239
    // the master secret is sensitive.
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 25859
diff changeset
   240
    // Note: Change to protected after this method is moved from
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 25859
diff changeset
   241
    // sun.security.util.MessageSpi2 interface to
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 25859
diff changeset
   242
    // java.security.MessageDigestSpi class
18c0f074ed97 8165275: Replace the reflective call to the implUpdate method in HandshakeMessage::digestKey
valeriep
parents: 25859
diff changeset
   243
    public void engineUpdate(SecretKey key) throws InvalidKeyException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // SunJSSE calls this method only if the key does not have a RAW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // encoding, i.e. if it is sensitive. Therefore, no point in calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // SecretKeyFactory to try to convert it. Just verify it ourselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (key instanceof P11Key == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw new InvalidKeyException("Not a P11Key: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        P11Key p11Key = (P11Key)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (p11Key.token != token) {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   252
            throw new InvalidKeyException("Not a P11Key of this provider: " +
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   253
                    key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   255
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   256
        fetchSession();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if (state == S_BUFFERED) {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   259
                token.p11.C_DigestInit(session.id(), mechanism);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                state = S_INIT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   262
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   263
            if (bufOfs != 0) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   264
                token.p11.C_DigestUpdate(session.id(), 0, buffer, 0, bufOfs);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   265
                bufOfs = 0;
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   266
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            token.p11.C_DigestKey(session.id(), p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } catch (PKCS11Exception e) {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   269
            engineReset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throw new ProviderException("update(SecretKey) failed", e);
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
    // see JCA spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    protected void engineUpdate(ByteBuffer byteBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        int len = byteBuffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   280
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (byteBuffer instanceof DirectBuffer == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            super.engineUpdate(byteBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   285
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   286
        fetchSession();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        long addr = ((DirectBuffer)byteBuffer).address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        int ofs = byteBuffer.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (state == S_BUFFERED) {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   291
                token.p11.C_DigestInit(session.id(), mechanism);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                state = S_INIT;
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   293
            }
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   294
            if (bufOfs != 0) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   295
                token.p11.C_DigestUpdate(session.id(), 0, buffer, 0, bufOfs);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   296
                bufOfs = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            token.p11.C_DigestUpdate(session.id(), addr + ofs, null, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            byteBuffer.position(ofs + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } catch (PKCS11Exception e) {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   301
            engineReset();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw new ProviderException("update() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   306
    public Object clone() throws CloneNotSupportedException {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   307
        P11Digest copy = (P11Digest) super.clone();
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   308
        copy.buffer = buffer.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        try {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   310
            if (session != null) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   311
                copy.session = copy.token.getOpSession();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   313
            if (state == S_INIT) {
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   314
                byte[] stateValues =
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   315
                    token.p11.C_GetOperationState(session.id());
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   316
                token.p11.C_SetOperationState(copy.session.id(),
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   317
                                              stateValues, 0, 0);
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   318
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        } catch (PKCS11Exception e) {
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   320
            throw (CloneNotSupportedException)
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   321
                (new CloneNotSupportedException(algorithm).initCause(e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
11507
bd978326a655 6414899: P11Digest should support cloning
valeriep
parents: 5506
diff changeset
   323
        return copy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
}