src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11PSSSignature.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 55530 6aa047de311b
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     1
/*
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     4
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    10
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    15
 * accompanied this code).
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    16
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    20
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    23
 * questions.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    24
 */
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    25
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    26
package sun.security.pkcs11;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    27
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    29
import java.io.IOException;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    30
import java.nio.ByteBuffer;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    31
import sun.nio.ch.DirectBuffer;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    32
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    33
import java.util.Hashtable;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    34
import java.util.Arrays;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    35
import java.security.*;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    36
import java.security.spec.AlgorithmParameterSpec;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    37
import java.security.spec.MGF1ParameterSpec;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    38
import java.security.spec.PSSParameterSpec;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    39
import java.security.interfaces.*;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    40
import sun.security.pkcs11.wrapper.*;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    41
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    42
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    43
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    44
/**
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    45
 * RSASSA-PSS Signature implementation class. This class currently supports the
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    46
 * following algorithms:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    47
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    48
 * . RSA-PSS:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    49
 *   . RSASSA-PSS
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    50
 *   . SHA1withRSASSA-PSS
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    51
 *   . SHA224withRSASSA-PSS
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    52
 *   . SHA256withRSASSA-PSS
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    53
 *   . SHA384withRSASSA-PSS
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    54
 *   . SHA512withRSASSA-PSS
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    55
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    56
 * Note that the underlying PKCS#11 token may support complete signature
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    57
 * algorithm (e.g. CKM_<md>_RSA_PKCS_PSS), or it may just
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    58
 * implement the signature algorithm without hashing (i.e. CKM_RSA_PKCS_PSS).
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    59
 * This class uses what is available and adds whatever extra processing
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    60
 * is needed.
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    61
 *
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    62
 * @since   13
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    63
 */
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    64
final class P11PSSSignature extends SignatureSpi {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    65
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    66
    private final static boolean DEBUG = false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    67
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    68
    // mappings of digest algorithms and their output length in bytes
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    69
    private static final Hashtable<String, Integer> DIGEST_LENGTHS =
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    70
        new Hashtable<String, Integer>();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    71
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    72
    static {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    73
        DIGEST_LENGTHS.put("SHA-1", 20);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    74
        DIGEST_LENGTHS.put("SHA", 20);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    75
        DIGEST_LENGTHS.put("SHA1", 20);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    76
        DIGEST_LENGTHS.put("SHA-224", 28);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    77
        DIGEST_LENGTHS.put("SHA224", 28);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    78
        DIGEST_LENGTHS.put("SHA-256", 32);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    79
        DIGEST_LENGTHS.put("SHA256", 32);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    80
        DIGEST_LENGTHS.put("SHA-384", 48);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    81
        DIGEST_LENGTHS.put("SHA384", 48);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    82
        DIGEST_LENGTHS.put("SHA-512", 64);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    83
        DIGEST_LENGTHS.put("SHA512", 64);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    84
        DIGEST_LENGTHS.put("SHA-512/224", 28);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    85
        DIGEST_LENGTHS.put("SHA512/224", 28);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    86
        DIGEST_LENGTHS.put("SHA-512/256", 32);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    87
        DIGEST_LENGTHS.put("SHA512/256", 32);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    88
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    89
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    90
    // utility method for comparing digest algorithms
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    91
    // NOTE that first argument is assumed to be standard digest name
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    92
    private static boolean isDigestEqual(String stdAlg, String givenAlg) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    93
        if (stdAlg == null || givenAlg == null) return false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    94
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    95
        if (givenAlg.indexOf("-") != -1) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    96
            return stdAlg.equalsIgnoreCase(givenAlg);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    97
        } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    98
            if (stdAlg.equals("SHA-1")) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
    99
                return (givenAlg.equalsIgnoreCase("SHA")
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   100
                        || givenAlg.equalsIgnoreCase("SHA1"));
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   101
            } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   102
                StringBuilder sb = new StringBuilder(givenAlg);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   103
                // case-insensitive check
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   104
                if (givenAlg.regionMatches(true, 0, "SHA", 0, 3)) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   105
                    givenAlg = sb.insert(3, "-").toString();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   106
                    return stdAlg.equalsIgnoreCase(givenAlg);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   107
                } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   108
                    throw new ProviderException("Unsupported digest algorithm "
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   109
                            + givenAlg);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   110
                }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   111
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   112
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   113
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   114
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   115
    // token instance
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   116
    private final Token token;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   117
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   118
    // algorithm name
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   119
    private final String algorithm;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   120
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   121
    // name of the key algorithm, currently just RSA
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   122
    private static final String KEY_ALGO = "RSA";
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   123
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   124
    // mechanism id
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   125
    private final CK_MECHANISM mechanism;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   126
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   127
    // type, one of T_* below
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   128
    private final int type;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   129
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   130
    // key instance used, if init*() was called
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   131
    private P11Key p11Key = null;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   132
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   133
    // PSS parameters and the flag controlling its access
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   134
    private PSSParameterSpec sigParams = null;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   135
    private boolean isActive = false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   136
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   137
    // message digest alg, if implied by the algorithm name
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   138
    private final String mdAlg;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   139
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   140
    // message digest, if we do the digesting ourselves
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   141
    private MessageDigest md = null;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   142
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   143
    // associated session, if any
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   144
    private Session session;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   145
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   146
    // mode, one of M_* below
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   147
    private int mode;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   148
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   149
    // flag indicating whether an operation is initialized
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   150
    private boolean initialized = false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   151
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   152
    // buffer, for update(byte)
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   153
    private final byte[] buffer = new byte[1];
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   154
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   155
    // total number of bytes processed in current operation
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   156
    private int bytesProcessed = 0;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   157
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   158
    // constant for signing mode
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   159
    private final static int M_SIGN   = 1;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   160
    // constant for verification mode
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   161
    private final static int M_VERIFY = 2;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   162
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   163
    // constant for type digesting, we do the hashing ourselves
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   164
    private final static int T_DIGEST = 1;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   165
    // constant for type update, token does everything
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   166
    private final static int T_UPDATE = 2;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   167
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   168
    P11PSSSignature(Token token, String algorithm, long mechId)
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   169
            throws NoSuchAlgorithmException, PKCS11Exception {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   170
        super();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   171
        this.token = token;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   172
        this.algorithm = algorithm;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   173
        this.mechanism = new CK_MECHANISM(mechId);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   174
        int idx = algorithm.indexOf("with");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   175
        this.mdAlg = (idx == -1? null : algorithm.substring(0, idx));
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   176
        switch ((int)mechId) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   177
        case (int)CKM_SHA1_RSA_PKCS_PSS:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   178
        case (int)CKM_SHA224_RSA_PKCS_PSS:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   179
        case (int)CKM_SHA256_RSA_PKCS_PSS:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   180
        case (int)CKM_SHA384_RSA_PKCS_PSS:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   181
        case (int)CKM_SHA512_RSA_PKCS_PSS:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   182
            type = T_UPDATE;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   183
            break;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   184
        case (int)CKM_RSA_PKCS_PSS:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   185
            type = T_DIGEST;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   186
            break;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   187
        default:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   188
            throw new ProviderException("Unsupported mechanism: " + mechId);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   189
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   190
        this.md = null;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   191
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   192
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   193
    private void ensureInitialized() throws SignatureException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   194
        token.ensureValid();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   195
        if (this.p11Key == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   196
            throw new SignatureException("Missing key");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   197
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   198
        if (this.sigParams == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   199
            if (this.mdAlg == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   200
                // PSS Parameters are required for signature verification
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   201
                throw new SignatureException
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   202
                    ("Parameters required for RSASSA-PSS signature");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   203
            } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   204
                int saltLen = DIGEST_LENGTHS.get(this.mdAlg).intValue();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   205
                // generate default params for both sign and verify?
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   206
                this.sigParams = new PSSParameterSpec(this.mdAlg,
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   207
                        "MGF1", new MGF1ParameterSpec(this.mdAlg),
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   208
                        saltLen, PSSParameterSpec.TRAILER_FIELD_BC);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   209
                this.mechanism.setParameter(new CK_RSA_PKCS_PSS_PARAMS(
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   210
                        this.mdAlg, "MGF1", this.mdAlg,
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   211
                        DIGEST_LENGTHS.get(this.mdAlg).intValue()));
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   212
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   213
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   214
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   215
        if (initialized == false) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   216
            initialize();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   217
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   218
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   219
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   220
    // reset the states to the pre-initialized values
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   221
    private void reset(boolean doCancel) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   222
        if (!initialized) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   223
            return;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   224
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   225
        initialized = false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   226
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   227
            if (session == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   228
                return;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   229
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   230
            if (doCancel && token.explicitCancel) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   231
                cancelOperation();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   232
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   233
        } finally {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   234
            p11Key.releaseKeyID();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   235
            mechanism.freeHandle();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   236
            session = token.releaseSession(session);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   237
            isActive = false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   238
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   239
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   240
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   241
    private void cancelOperation() {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   242
        token.ensureValid();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   243
        if (DEBUG) System.out.print("Cancelling operation");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   244
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   245
        if (session.hasObjects() == false) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   246
            if (DEBUG) System.out.println(" by killing session");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   247
            session = token.killSession(session);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   248
            return;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   249
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   250
        // "cancel" operation by finishing it
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   251
        if (mode == M_SIGN) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   252
            try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   253
                if (type == T_UPDATE) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   254
                    if (DEBUG) System.out.println(" by C_SignFinal");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   255
                    token.p11.C_SignFinal(session.id(), 0);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   256
                } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   257
                    byte[] digest =
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   258
                        (md == null? new byte[0] : md.digest());
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   259
                    if (DEBUG) System.out.println(" by C_Sign");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   260
                    token.p11.C_Sign(session.id(), digest);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   261
                }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   262
            } catch (PKCS11Exception e) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   263
                throw new ProviderException("cancel failed", e);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   264
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   265
        } else { // M_VERIFY
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   266
            try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   267
                byte[] signature =
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   268
                    new byte[(p11Key.length() + 7) >> 3];
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   269
                if (type == T_UPDATE) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   270
                    if (DEBUG) System.out.println(" by C_VerifyFinal");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   271
                    token.p11.C_VerifyFinal(session.id(), signature);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   272
                } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   273
                    byte[] digest =
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   274
                        (md == null? new byte[0] : md.digest());
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   275
                    if (DEBUG) System.out.println(" by C_Verify");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   276
                    token.p11.C_Verify(session.id(), digest, signature);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   277
                }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   278
            } catch (PKCS11Exception e) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   279
                // will fail since the signature is incorrect
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   280
                // XXX check error code
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   281
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   282
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   283
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   284
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   285
    // assumes current state is initialized == false
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   286
    private void initialize() {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   287
        if (DEBUG) System.out.println("Initializing");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   288
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   289
        if (p11Key == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   290
            throw new ProviderException(
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   291
                    "No Key found, call initSign/initVerify first");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   292
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   293
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   294
        long keyID = p11Key.getKeyID();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   295
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   296
            if (session == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   297
                session = token.getOpSession();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   298
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   299
            if (mode == M_SIGN) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   300
                token.p11.C_SignInit(session.id(), mechanism, keyID);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   301
            } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   302
                token.p11.C_VerifyInit(session.id(), mechanism, keyID);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   303
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   304
        } catch (PKCS11Exception e) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   305
            p11Key.releaseKeyID();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   306
            session = token.releaseSession(session);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   307
            throw new ProviderException("Initialization failed", e);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   308
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   309
        if (bytesProcessed != 0) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   310
            bytesProcessed = 0;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   311
            if (md != null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   312
                md.reset();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   313
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   314
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   315
        initialized = true;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   316
        isActive = false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   317
        if (DEBUG) System.out.println("Initialized");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   318
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   319
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   320
    private void checkKeySize(Key key) throws InvalidKeyException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   321
        if (DEBUG) System.out.print("Checking Key");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   322
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   323
        if (!key.getAlgorithm().equals(KEY_ALGO)) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   324
            throw new InvalidKeyException("Only " + KEY_ALGO +
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   325
                " keys are supported");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   326
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   327
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   328
        CK_MECHANISM_INFO mechInfo = null;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   329
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   330
            mechInfo = token.getMechanismInfo(mechanism.mechanism);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   331
        } catch (PKCS11Exception e) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   332
            // should not happen, ignore for now
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   333
            if (DEBUG) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   334
                System.out.println("Unexpected exception");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   335
                e.printStackTrace();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   336
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   337
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   338
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   339
        int keySize = 0; // in bytes
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   340
        if (mechInfo != null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   341
            // check against available native info
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   342
            int minKeySize = (int) mechInfo.ulMinKeySize;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   343
            int maxKeySize = (int) mechInfo.ulMaxKeySize;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   344
            if (key instanceof P11Key) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   345
                keySize = (((P11Key) key).length() + 7) >> 3;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   346
            } else if (key instanceof RSAKey) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   347
                keySize = ((RSAKey) key).getModulus().bitLength() >> 3;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   348
            } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   349
                throw new InvalidKeyException("Unrecognized key type " + key);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   350
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   351
            if ((minKeySize != -1) && (keySize < minKeySize)) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   352
                throw new InvalidKeyException(KEY_ALGO +
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   353
                    " key must be at least " + minKeySize + " bytes");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   354
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   355
            if ((maxKeySize != -1) && (keySize > maxKeySize)) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   356
                throw new InvalidKeyException(KEY_ALGO +
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   357
                    " key must be at most " + maxKeySize + " bytes");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   358
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   359
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   360
        if (this.sigParams != null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   361
            String digestAlg = this.sigParams.getDigestAlgorithm();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   362
            int sLen = this.sigParams.getSaltLength();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   363
            int hLen = DIGEST_LENGTHS.get(digestAlg).intValue();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   364
            int minKeyLen = Math.addExact(Math.addExact(sLen, hLen), 2);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   365
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   366
            if (keySize < minKeyLen) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   367
                throw new InvalidKeyException
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   368
                    ("Key is too short for current params, need min " + minKeyLen);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   369
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   370
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   371
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   372
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   373
    private void setSigParams(AlgorithmParameterSpec p)
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   374
            throws InvalidAlgorithmParameterException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   375
        if (p == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   376
            throw new InvalidAlgorithmParameterException("PSS Parameter required");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   377
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   378
        if (!(p instanceof PSSParameterSpec)) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   379
            throw new InvalidAlgorithmParameterException
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   380
                ("Only PSSParameterSpec is supported");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   381
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   382
        // no need to validate again if same as current signature parameters
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   383
        PSSParameterSpec params = (PSSParameterSpec) p;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   384
        if (params == this.sigParams) return;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   385
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   386
        String digestAlgorithm = params.getDigestAlgorithm();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   387
        if (this.mdAlg != null && !isDigestEqual(digestAlgorithm, this.mdAlg)) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   388
            throw new InvalidAlgorithmParameterException
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   389
                    ("Digest algorithm in Signature parameters must be " +
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   390
                     this.mdAlg);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   391
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   392
        Integer digestLen = DIGEST_LENGTHS.get(digestAlgorithm);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   393
        if (digestLen == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   394
            throw new InvalidAlgorithmParameterException
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   395
                ("Unsupported digest algorithm in Signature parameters: " +
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   396
                 digestAlgorithm);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   397
        }
55530
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   398
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   399
        if (!(params.getMGFAlgorithm().equalsIgnoreCase("MGF1"))) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   400
            throw new InvalidAlgorithmParameterException("Only supports MGF1");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   401
        }
55530
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   402
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   403
        // defaults to the digest algorithm unless overridden
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   404
        String mgfDigestAlgo = digestAlgorithm;
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   405
        AlgorithmParameterSpec mgfParams = params.getMGFParameters();
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   406
        if (mgfParams != null) {
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   407
            if (!(mgfParams instanceof MGF1ParameterSpec)) {
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   408
                throw new InvalidAlgorithmParameterException
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   409
                        ("Only MGF1ParameterSpec is supported");
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   410
            }
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   411
            mgfDigestAlgo = ((MGF1ParameterSpec)mgfParams).getDigestAlgorithm();
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   412
        }
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   413
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   414
        if (params.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   415
            throw new InvalidAlgorithmParameterException
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   416
                ("Only supports TrailerFieldBC(1)");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   417
        }
55530
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   418
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   419
        int saltLen = params.getSaltLength();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   420
        if (this.p11Key != null) {
55530
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   421
            int maxSaltLen = ((this.p11Key.length() + 7) >> 3) -
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   422
                    digestLen.intValue() - 2;
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   423
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   424
            if (DEBUG) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   425
                System.out.println("Max saltLen = " + maxSaltLen);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   426
                System.out.println("Curr saltLen = " + saltLen);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   427
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   428
            if (maxSaltLen < 0 || saltLen > maxSaltLen) {
55530
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   429
                throw new InvalidAlgorithmParameterException
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   430
                        ("Invalid with current key size");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   431
            }
55530
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   432
        } else if (DEBUG) {
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   433
            System.out.println("No key available for validating saltLen");
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   434
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   435
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   436
        // validated, now try to store the parameter internally
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   437
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   438
            this.mechanism.setParameter(
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   439
                    new CK_RSA_PKCS_PSS_PARAMS(digestAlgorithm, "MGF1",
55530
6aa047de311b 8226651: Setting the mgfHash in CK_RSA_PKCS_PSS_PARAMS has no effect
valeriep
parents: 55332
diff changeset
   440
                            mgfDigestAlgo, saltLen));
55332
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   441
            this.sigParams = params;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   442
        } catch (IllegalArgumentException iae) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   443
            throw new InvalidAlgorithmParameterException(iae);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   444
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   445
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   446
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   447
    // see JCA spec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   448
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   449
    protected void engineInitVerify(PublicKey publicKey)
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   450
            throws InvalidKeyException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   451
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   452
        if (publicKey == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   453
            throw new InvalidKeyException("Key must not be null");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   454
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   455
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   456
        // Need to check key length whenever a new key is set
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   457
        if (publicKey != p11Key) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   458
            checkKeySize(publicKey);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   459
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   460
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   461
        reset(true);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   462
        mode = M_VERIFY;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   463
        p11Key = P11KeyFactory.convertKey(token, publicKey, KEY_ALGO);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   464
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   465
        // For PSS, defer PKCS11 initialization calls to update/doFinal as it
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   466
        // needs both key and params
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   467
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   468
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   469
    // see JCA spec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   470
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   471
    protected void engineInitSign(PrivateKey privateKey)
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   472
            throws InvalidKeyException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   473
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   474
        if (privateKey == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   475
            throw new InvalidKeyException("Key must not be null");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   476
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   477
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   478
        // Need to check RSA key length whenever a new key is set
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   479
        if (privateKey != p11Key) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   480
            checkKeySize(privateKey);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   481
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   482
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   483
        reset(true);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   484
        mode = M_SIGN;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   485
        p11Key = P11KeyFactory.convertKey(token, privateKey, KEY_ALGO);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   486
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   487
        // For PSS, defer PKCS11 initialization calls to update/doFinal as it
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   488
        // needs both key and params
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   489
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   490
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   491
    // see JCA spec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   492
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   493
    protected void engineUpdate(byte b) throws SignatureException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   494
        ensureInitialized();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   495
        isActive = true;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   496
        buffer[0] = b;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   497
        engineUpdate(buffer, 0, 1);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   498
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   499
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   500
    // see JCA spec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   501
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   502
    protected void engineUpdate(byte[] b, int ofs, int len)
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   503
            throws SignatureException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   504
        ensureInitialized();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   505
        if (len == 0) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   506
            return;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   507
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   508
        // check for overflow
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   509
        if (len + bytesProcessed < 0) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   510
            throw new ProviderException("Processed bytes limits exceeded.");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   511
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   512
        isActive = true;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   513
        switch (type) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   514
        case T_UPDATE:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   515
            try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   516
                if (mode == M_SIGN) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   517
                    System.out.println(this + ": Calling C_SignUpdate");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   518
                    token.p11.C_SignUpdate(session.id(), 0, b, ofs, len);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   519
                } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   520
                    System.out.println(this + ": Calling C_VerfifyUpdate");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   521
                    token.p11.C_VerifyUpdate(session.id(), 0, b, ofs, len);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   522
                }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   523
                bytesProcessed += len;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   524
            } catch (PKCS11Exception e) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   525
                reset(false);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   526
                throw new ProviderException(e);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   527
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   528
            break;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   529
        case T_DIGEST:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   530
            // should not happen as this should be covered by earlier checks
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   531
            if (md == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   532
                throw new ProviderException("PSS Parameters required");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   533
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   534
            md.update(b, ofs, len);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   535
            bytesProcessed += len;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   536
            break;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   537
        default:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   538
            throw new ProviderException("Internal error");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   539
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   540
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   541
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   542
    // see JCA spec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   543
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   544
    protected void engineUpdate(ByteBuffer byteBuffer) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   545
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   546
            ensureInitialized();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   547
        } catch (SignatureException se) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   548
            throw new ProviderException(se);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   549
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   550
        int len = byteBuffer.remaining();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   551
        if (len <= 0) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   552
            return;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   553
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   554
        isActive = true;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   555
        switch (type) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   556
        case T_UPDATE:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   557
            if (byteBuffer instanceof DirectBuffer == false) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   558
                // cannot do better than default impl
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   559
                super.engineUpdate(byteBuffer);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   560
                return;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   561
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   562
            long addr = ((DirectBuffer)byteBuffer).address();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   563
            int ofs = byteBuffer.position();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   564
            try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   565
                if (mode == M_SIGN) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   566
                    System.out.println(this + ": Calling C_SignUpdate");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   567
                    token.p11.C_SignUpdate
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   568
                        (session.id(), addr + ofs, null, 0, len);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   569
                } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   570
                    System.out.println(this + ": Calling C_VerifyUpdate");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   571
                    token.p11.C_VerifyUpdate
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   572
                        (session.id(), addr + ofs, null, 0, len);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   573
                }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   574
                bytesProcessed += len;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   575
                byteBuffer.position(ofs + len);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   576
            } catch (PKCS11Exception e) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   577
                reset(false);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   578
                throw new ProviderException("Update failed", e);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   579
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   580
            break;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   581
        case T_DIGEST:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   582
            // should not happen as this should be covered by earlier checks
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   583
            if (md == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   584
                throw new ProviderException("PSS Parameters required");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   585
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   586
            md.update(byteBuffer);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   587
            bytesProcessed += len;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   588
            break;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   589
        default:
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   590
            reset(false);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   591
            throw new ProviderException("Internal error");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   592
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   593
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   594
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   595
    // see JCA spec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   596
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   597
    protected byte[] engineSign() throws SignatureException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   598
        ensureInitialized();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   599
        boolean doCancel = true;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   600
        if (DEBUG) System.out.print("Generating signature");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   601
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   602
            byte[] signature;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   603
            if (type == T_UPDATE) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   604
                if (DEBUG) System.out.println(" by C_SignFinal");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   605
                signature = token.p11.C_SignFinal(session.id(), 0);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   606
            } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   607
                if (md == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   608
                    throw new ProviderException("PSS Parameters required");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   609
                }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   610
                byte[] digest = md.digest();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   611
                if (DEBUG) System.out.println(" by C_Sign");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   612
                signature = token.p11.C_Sign(session.id(), digest);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   613
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   614
            doCancel = false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   615
            return signature;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   616
        } catch (PKCS11Exception pe) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   617
            doCancel = false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   618
            throw new ProviderException(pe);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   619
        } catch (ProviderException e) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   620
            throw e;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   621
        } finally {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   622
            reset(doCancel);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   623
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   624
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   625
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   626
    // see JCA spec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   627
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   628
    protected boolean engineVerify(byte[] signature) throws SignatureException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   629
        ensureInitialized();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   630
        boolean doCancel = true;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   631
        if (DEBUG) System.out.print("Verifying signature");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   632
        try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   633
            if (type == T_UPDATE) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   634
                if (DEBUG) System.out.println(" by C_VerifyFinal");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   635
                token.p11.C_VerifyFinal(session.id(), signature);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   636
            } else {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   637
                if (md == null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   638
                    throw new ProviderException("PSS Parameters required");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   639
                }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   640
                byte[] digest = md.digest();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   641
                if (DEBUG) System.out.println(" by C_Verify");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   642
                token.p11.C_Verify(session.id(), digest, signature);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   643
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   644
            doCancel = false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   645
            return true;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   646
        } catch (PKCS11Exception pe) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   647
            doCancel = false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   648
            long errorCode = pe.getErrorCode();
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   649
            if (errorCode == CKR_SIGNATURE_INVALID) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   650
                return false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   651
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   652
            if (errorCode == CKR_SIGNATURE_LEN_RANGE) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   653
                // return false rather than throwing an exception
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   654
                return false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   655
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   656
            // ECF bug?
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   657
            if (errorCode == CKR_DATA_LEN_RANGE) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   658
                return false;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   659
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   660
            throw new ProviderException(pe);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   661
        }  catch (ProviderException e) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   662
            throw e;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   663
        } finally {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   664
            reset(doCancel);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   665
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   666
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   667
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   668
    // see JCA spec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   669
    @SuppressWarnings("deprecation")
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   670
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   671
    protected void engineSetParameter(String param, Object value)
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   672
            throws InvalidParameterException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   673
        throw new UnsupportedOperationException("setParameter() not supported");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   674
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   675
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   676
    // see JCA spec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   677
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   678
    protected void engineSetParameter(AlgorithmParameterSpec params)
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   679
            throws InvalidAlgorithmParameterException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   680
        // disallow changing parameters when update has been called
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   681
        if (isActive) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   682
            throw new ProviderException
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   683
                ("Cannot set parameters during operations");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   684
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   685
        setSigParams(params);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   686
        if (type == T_DIGEST) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   687
            try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   688
                this.md = MessageDigest.getInstance(sigParams.getDigestAlgorithm());
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   689
            } catch (NoSuchAlgorithmException nsae) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   690
                throw new InvalidAlgorithmParameterException(nsae);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   691
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   692
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   693
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   694
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   695
    // see JCA spec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   696
    @SuppressWarnings("deprecation")
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   697
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   698
    protected Object engineGetParameter(String param)
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   699
            throws InvalidParameterException {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   700
        throw new UnsupportedOperationException("getParameter() not supported");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   701
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   702
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   703
    // see JCA spec
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   704
    @Override
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   705
    protected AlgorithmParameters engineGetParameters() {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   706
        if (this.sigParams != null) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   707
            try {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   708
                AlgorithmParameters ap = AlgorithmParameters.getInstance("RSASSA-PSS");
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   709
                ap.init(this.sigParams);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   710
                return ap;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   711
            } catch (GeneralSecurityException e) {
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   712
                throw new RuntimeException(e);
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   713
            }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   714
        }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   715
        return null;
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   716
    }
f492567244ab 8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
valeriep
parents:
diff changeset
   717
}