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