src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Mac.java
author valeriep
Tue, 08 Oct 2019 00:01:20 +0000
changeset 58489 2faeaa5933a6
parent 53257 5170dc2bcf64
permissions -rw-r--r--
8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4 Summary: For CK_GCM_PARAMS, try the spec definition first before falling back to the header file definition Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58489
2faeaa5933a6 8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents: 53257
diff changeset
     2
 * Copyright (c) 2003, 2019, 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.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.MacSpi;
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
 * MAC implementation class. This class currently supports HMAC using
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    42
 * MD5, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 and the SSL3 MAC
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    43
 * using MD5 and SHA-1.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Note that unlike other classes (e.g. Signature), this does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * composite various operations if the token only supports part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * required functionality. The MAC implementations in SunJCE already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * do exactly that by implementing an MAC on top of MessageDigests. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * could not do any better than they.
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
final class P11Mac extends MacSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // mechanism object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // length of the MAC in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final int macLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // key instance used, if operation active
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
    // associated session, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private Session session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
    74
    // initialization status
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
    75
    private boolean initialized;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // one byte buffer for the update(byte) method, initialized on demand
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private byte[] oneByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    P11Mac(Token token, String algorithm, long mechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throws PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Long params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        switch ((int)mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        case (int)CKM_MD5_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            macLength = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        case (int)CKM_SHA_1_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            macLength = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            break;
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    93
        case (int)CKM_SHA224_HMAC:
58489
2faeaa5933a6 8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents: 53257
diff changeset
    94
        case (int)CKM_SHA512_224_HMAC:
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    95
            macLength = 28;
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    96
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        case (int)CKM_SHA256_HMAC:
58489
2faeaa5933a6 8229243: SunPKCS11-Solaris provider tests failing on Solaris 11.4
valeriep
parents: 53257
diff changeset
    98
        case (int)CKM_SHA512_256_HMAC:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            macLength = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        case (int)CKM_SHA384_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            macLength = 48;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        case (int)CKM_SHA512_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            macLength = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        case (int)CKM_SSL3_MD5_MAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            macLength = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            params = Long.valueOf(16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        case (int)CKM_SSL3_SHA1_MAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            macLength = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            params = Long.valueOf(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new ProviderException("Unknown mechanism: " + mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        ckMechanism = new CK_MECHANISM(mechanism, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   121
    // reset the states to the pre-initialized values
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   122
    private void reset(boolean doCancel) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   123
        if (!initialized) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   124
            return;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   125
        }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   126
        initialized = false;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   127
        try {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   128
            if (session == null) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   129
                return;
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   130
            }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   131
            if (doCancel && token.explicitCancel) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   132
                cancelOperation();
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   133
            }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   134
        } finally {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   135
            p11Key.releaseKeyID();
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   136
            session = token.releaseSession(session);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private void cancelOperation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        token.ensureValid();
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   142
        if (session.hasObjects() == false) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   143
            session = token.killSession(session);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return;
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   145
        } else {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   146
            try {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   147
                token.p11.C_SignFinal(session.id(), 0);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   148
            } catch (PKCS11Exception e) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   149
                throw new ProviderException("Cancel failed", e);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   150
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   152
    }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   153
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   154
    private void ensureInitialized() throws PKCS11Exception {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   155
        if (!initialized) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   156
            initialize();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private void initialize() throws PKCS11Exception {
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   161
        if (p11Key == null) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   162
            throw new ProviderException(
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   163
                    "Operation cannot be performed without calling engineInit first");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   165
        token.ensureValid();
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   166
        long p11KeyID = p11Key.getKeyID();
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   167
        try {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   168
            if (session == null) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   169
                session = token.getOpSession();
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   170
            }
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   171
            token.p11.C_SignInit(session.id(), ckMechanism, p11KeyID);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   172
        } catch (PKCS11Exception e) {
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   173
            p11Key.releaseKeyID();
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   174
            session = token.releaseSession(session);
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   175
            throw e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   177
        initialized = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    protected int engineGetMacLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return macLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    protected void engineReset() {
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   187
        reset(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    protected void engineInit(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                ("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   197
        reset(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        p11Key = P11SecretKeyFactory.convertKey(token, key, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new InvalidKeyException("init() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    protected byte[] engineDoFinal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            ensureInitialized();
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   210
            return token.p11.C_SignFinal(session.id(), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } catch (PKCS11Exception e) {
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   212
            reset(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new ProviderException("doFinal() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } finally {
53257
5170dc2bcf64 6913047: Long term memory leak when using PKCS11 and JCE exceeds 32 bit process address space
mbalao
parents: 47216
diff changeset
   215
            reset(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    protected void engineUpdate(byte input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (oneByte == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
           oneByte = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        oneByte[0] = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        engineUpdate(oneByte, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    protected void engineUpdate(byte[] b, int ofs, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            token.p11.C_SignUpdate(session.id(), 0, b, ofs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new ProviderException("update() failed", e);
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
    // see JCE spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    protected void engineUpdate(ByteBuffer byteBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            int len = byteBuffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (byteBuffer instanceof DirectBuffer == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                super.engineUpdate(byteBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            long addr = ((DirectBuffer)byteBuffer).address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            int ofs = byteBuffer.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            token.p11.C_SignUpdate(session.id(), addr + ofs, null, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            byteBuffer.position(ofs + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            throw new ProviderException("update() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
}