src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Signature.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 49700 490292739652
child 50364 00ebc17f3cc6
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49700
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5291
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.*;
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
    33
import java.security.interfaces.*;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
    34
import java.security.spec.AlgorithmParameterSpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.nio.ch.DirectBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.security.rsa.RSASignature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.rsa.RSAPadding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.pkcs11.wrapper.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
17916
e02ddef88f77 8014618: Need to strip leading zeros in TlsPremasterSecret of DHKeyAgreement
xuelei
parents: 12685
diff changeset
    45
import sun.security.util.KeyUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Signature implementation class. This class currently supports the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * following algorithms:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * . DSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   . NONEwithDSA (RawDSA)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   . SHA1withDSA
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
    54
 *   . NONEwithDSAinP1363Format (RawDSAinP1363Format)
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
    55
 *   . SHA1withDSAinP1363Format
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * . RSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   . MD2withRSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   . MD5withRSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   . SHA1withRSA
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11521
diff changeset
    60
 *   . SHA224withRSA
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   . SHA256withRSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   . SHA384withRSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   . SHA512withRSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * . ECDSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   . NONEwithECDSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   . SHA1withECDSA
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11521
diff changeset
    67
 *   . SHA224withECDSA
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   . SHA256withECDSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *   . SHA384withECDSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   . SHA512withECDSA
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
    71
 *   . NONEwithECDSAinP1363Format
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
    72
 *   . SHA1withECDSAinP1363Format
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
    73
 *   . SHA224withECDSAinP1363Format
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
    74
 *   . SHA256withECDSAinP1363Format
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
    75
 *   . SHA384withECDSAinP1363Format
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
    76
 *   . SHA512withECDSAinP1363Format
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Note that the underlying PKCS#11 token may support complete signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * algorithm (e.g. CKM_DSA_SHA1, CKM_MD5_RSA_PKCS), or it may just
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * implement the signature algorithm without hashing (e.g. CKM_DSA, CKM_PKCS),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * or it may only implement the raw public key operation (CKM_RSA_X_509).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * This class uses what is available and adds whatever extra processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
final class P11Signature extends SignatureSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // token instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private final Token token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // algorithm name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // name of the key algorithm, currently either RSA or DSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private final String keyAlgorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // mechanism id
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private final long mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   102
    // digest algorithm OID, if we encode RSA signature ourselves
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private final ObjectIdentifier digestOID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // type, one of T_* below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private final int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // key instance used, if init*() was called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private P11Key p11Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // message digest, if we do the digesting ourselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private final MessageDigest md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // associated session, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private Session session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   117
    // mode, one of M_* below
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private int mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // flag indicating whether an operation is initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private boolean initialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // buffer, for update(byte) or DSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private final byte[] buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // total number of bytes processed in current operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private int bytesProcessed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   129
    // The format, to be used for DSA and ECDSA signatures.
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   130
    // If true, the IEEE P1363 format will be used, the concatenation of
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   131
    // r and s. If false (default), the signature will be formatted as a
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   132
    // DER-encoded ASN.1 sequence of r and s.
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   133
    private boolean p1363Format = false;
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   134
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // constant for signing mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private final static int M_SIGN   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    // constant for verification mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private final static int M_VERIFY = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // constant for type digesting, we do the hashing ourselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private final static int T_DIGEST = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // constant for type update, token does everything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private final static int T_UPDATE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // constant for type raw, used with RawDSA and NONEwithECDSA only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private final static int T_RAW    = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // XXX PKCS#11 v2.20 says "should not be longer than 1024 bits",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // but this is a little arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private final static int RAW_ECDSA_MAX = 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    P11Signature(Token token, String algorithm, long mechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throws NoSuchAlgorithmException, PKCS11Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.token = token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.mechanism = mechanism;
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   157
        byte[] buffer = null;
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   158
        ObjectIdentifier digestOID = null;
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   159
        MessageDigest md = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        switch ((int)mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        case (int)CKM_MD2_RSA_PKCS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        case (int)CKM_MD5_RSA_PKCS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        case (int)CKM_SHA1_RSA_PKCS:
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11521
diff changeset
   164
        case (int)CKM_SHA224_RSA_PKCS:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        case (int)CKM_SHA256_RSA_PKCS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        case (int)CKM_SHA384_RSA_PKCS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        case (int)CKM_SHA512_RSA_PKCS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            keyAlgorithm = "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            type = T_UPDATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            buffer = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        case (int)CKM_DSA_SHA1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            keyAlgorithm = "DSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            type = T_UPDATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            buffer = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        case (int)CKM_ECDSA_SHA1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            keyAlgorithm = "EC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            type = T_UPDATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            buffer = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        case (int)CKM_DSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            keyAlgorithm = "DSA";
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   184
            if (algorithm.equals("DSA") ||
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   185
                algorithm.equals("DSAinP1363Format")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                type = T_DIGEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                md = MessageDigest.getInstance("SHA-1");
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   188
            } else if (algorithm.equals("RawDSA") ||
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   189
                       algorithm.equals("RawDSAinP1363Format")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                type = T_RAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                buffer = new byte[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                throw new ProviderException(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        case (int)CKM_ECDSA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            keyAlgorithm = "EC";
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   198
            if (algorithm.equals("NONEwithECDSA") ||
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   199
                algorithm.equals("NONEwithECDSAinP1363Format")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                type = T_RAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                buffer = new byte[RAW_ECDSA_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                String digestAlg;
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   204
                if (algorithm.equals("SHA1withECDSA") ||
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   205
                    algorithm.equals("SHA1withECDSAinP1363Format")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    digestAlg = "SHA-1";
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   207
                } else if (algorithm.equals("SHA224withECDSA") ||
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   208
                           algorithm.equals("SHA224withECDSAinP1363Format")) {
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11521
diff changeset
   209
                    digestAlg = "SHA-224";
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   210
                } else if (algorithm.equals("SHA256withECDSA") ||
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   211
                           algorithm.equals("SHA256withECDSAinP1363Format")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    digestAlg = "SHA-256";
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   213
                } else if (algorithm.equals("SHA384withECDSA") ||
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   214
                           algorithm.equals("SHA384withECDSAinP1363Format")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    digestAlg = "SHA-384";
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   216
                } else if (algorithm.equals("SHA512withECDSA") ||
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   217
                           algorithm.equals("SHA512withECDSAinP1363Format")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    digestAlg = "SHA-512";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    throw new ProviderException(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                type = T_DIGEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                md = MessageDigest.getInstance(digestAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        case (int)CKM_RSA_PKCS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        case (int)CKM_RSA_X_509:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            keyAlgorithm = "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            type = T_DIGEST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (algorithm.equals("MD5withRSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                md = MessageDigest.getInstance("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                digestOID = AlgorithmId.MD5_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            } else if (algorithm.equals("SHA1withRSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                md = MessageDigest.getInstance("SHA-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                digestOID = AlgorithmId.SHA_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            } else if (algorithm.equals("MD2withRSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                md = MessageDigest.getInstance("MD2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                digestOID = AlgorithmId.MD2_oid;
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11521
diff changeset
   239
            } else if (algorithm.equals("SHA224withRSA")) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11521
diff changeset
   240
                md = MessageDigest.getInstance("SHA-224");
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11521
diff changeset
   241
                digestOID = AlgorithmId.SHA224_oid;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            } else if (algorithm.equals("SHA256withRSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                md = MessageDigest.getInstance("SHA-256");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                digestOID = AlgorithmId.SHA256_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            } else if (algorithm.equals("SHA384withRSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                md = MessageDigest.getInstance("SHA-384");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                digestOID = AlgorithmId.SHA384_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            } else if (algorithm.equals("SHA512withRSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                md = MessageDigest.getInstance("SHA-512");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                digestOID = AlgorithmId.SHA512_oid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                throw new ProviderException("Unknown signature: " + algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new ProviderException("Unknown mechanism: " + mechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   258
        this.buffer = buffer;
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   259
        this.digestOID = digestOID;
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   260
        this.md = md;
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   261
        if (algorithm.endsWith("inP1363Format")) {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   262
            this.p1363Format = true;
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   263
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private void ensureInitialized() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private void cancelOperation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        token.ensureValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (initialized == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if ((session == null) || (token.explicitCancel == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (session.hasObjects() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            session = token.killSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // "cancel" operation by finishing it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // XXX make sure all this always works correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (mode == M_SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                if (type == T_UPDATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    token.p11.C_SignFinal(session.id(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    if (type == T_DIGEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    } else { // T_RAW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        digest = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    token.p11.C_Sign(session.id(), digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                throw new ProviderException("cancel failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } else { // M_VERIFY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                byte[] signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                if (keyAlgorithm.equals("DSA")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    signature = new byte[40];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                } else {
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 10336
diff changeset
   310
                    signature = new byte[(p11Key.length() + 7) >> 3];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (type == T_UPDATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    token.p11.C_VerifyFinal(session.id(), signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    if (type == T_DIGEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    } else { // T_RAW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        digest = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    token.p11.C_Verify(session.id(), digest, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                // will fail since the signature is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                // XXX check error code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    // assumes current state is initialized == false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private void initialize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (session == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                session = token.getOpSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (mode == M_SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                token.p11.C_SignInit(session.id(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        new CK_MECHANISM(mechanism), p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                token.p11.C_VerifyInit(session.id(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        new CK_MECHANISM(mechanism), p11Key.keyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new ProviderException("Initialization failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (bytesProcessed != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            bytesProcessed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (md != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                md.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   355
    private void checkKeySize(String keyAlgo, Key key)
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   356
        throws InvalidKeyException {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   357
        CK_MECHANISM_INFO mechInfo = null;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   358
        try {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   359
            mechInfo = token.getMechanismInfo(mechanism);
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   360
        } catch (PKCS11Exception e) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   361
            // should not happen, ignore for now.
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   362
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   363
        if (mechInfo == null) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   364
            // skip the check if no native info available
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   365
            return;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   366
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   367
        int minKeySize = (int) mechInfo.ulMinKeySize;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   368
        int maxKeySize = (int) mechInfo.ulMaxKeySize;
25213
04804347bc19 8046046: Test sun/security/pkcs11/Signature/TestDSAKeyLength.java fails intermittently on Solaris 11 in 8u40 nightly
valeriep
parents: 21848
diff changeset
   369
        // need to override the MAX keysize for SHA1withDSA
04804347bc19 8046046: Test sun/security/pkcs11/Signature/TestDSAKeyLength.java fails intermittently on Solaris 11 in 8u40 nightly
valeriep
parents: 21848
diff changeset
   370
        if (md != null && mechanism == CKM_DSA && maxKeySize > 1024) {
04804347bc19 8046046: Test sun/security/pkcs11/Signature/TestDSAKeyLength.java fails intermittently on Solaris 11 in 8u40 nightly
valeriep
parents: 21848
diff changeset
   371
               maxKeySize = 1024;
04804347bc19 8046046: Test sun/security/pkcs11/Signature/TestDSAKeyLength.java fails intermittently on Solaris 11 in 8u40 nightly
valeriep
parents: 21848
diff changeset
   372
        }
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   373
        int keySize = 0;
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   374
        if (key instanceof P11Key) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   375
            keySize = ((P11Key) key).length();
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   376
        } else {
49700
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   377
            try {
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   378
                if (keyAlgo.equals("RSA")) {
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   379
                    keySize = ((RSAKey) key).getModulus().bitLength();
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   380
                } else if (keyAlgo.equals("DSA")) {
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   381
                    keySize = ((DSAKey) key).getParams().getP().bitLength();
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   382
                } else if (keyAlgo.equals("EC")) {
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   383
                    keySize = ((ECKey) key).getParams().getCurve().getField().getFieldSize();
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   384
                } else {
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   385
                    throw new ProviderException("Error: unsupported algo " + keyAlgo);
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   386
                }
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   387
            } catch (ClassCastException cce) {
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   388
                throw new InvalidKeyException(keyAlgo +
490292739652 8197441: Signature#initSign/initVerify for an invalid private/public key fails with ClassCastException for SunPKCS11 provider
valeriep
parents: 47216
diff changeset
   389
                    " key must be the right type", cce);
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   390
            }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   391
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   392
        if ((minKeySize != -1) && (keySize < minKeySize)) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   393
            throw new InvalidKeyException(keyAlgo +
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   394
                " key must be at least " + minKeySize + " bits");
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   395
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   396
        if ((maxKeySize != -1) && (keySize > maxKeySize)) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   397
            throw new InvalidKeyException(keyAlgo +
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   398
                " key must be at most " + maxKeySize + " bits");
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   399
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   400
        if (keyAlgo.equals("RSA")) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   401
            checkRSAKeyLength(keySize);
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   402
        }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   403
    }
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   404
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   405
    private void checkRSAKeyLength(int len) throws InvalidKeyException {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   406
        RSAPadding padding;
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   407
        try {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   408
            padding = RSAPadding.getInstance
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   409
                (RSAPadding.PAD_BLOCKTYPE_1, (len + 7) >> 3);
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   410
        } catch (InvalidAlgorithmParameterException iape) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   411
            throw new InvalidKeyException(iape.getMessage());
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   412
        }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   413
        int maxDataSize = padding.getMaxDataSize();
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   414
        int encodedLength;
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   415
        if (algorithm.equals("MD5withRSA") ||
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   416
            algorithm.equals("MD2withRSA")) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   417
            encodedLength = 34;
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   418
        } else if (algorithm.equals("SHA1withRSA")) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   419
            encodedLength = 35;
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11521
diff changeset
   420
        } else if (algorithm.equals("SHA224withRSA")) {
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11521
diff changeset
   421
            encodedLength = 47;
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   422
        } else if (algorithm.equals("SHA256withRSA")) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   423
            encodedLength = 51;
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   424
        } else if (algorithm.equals("SHA384withRSA")) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   425
            encodedLength = 67;
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   426
        } else if (algorithm.equals("SHA512withRSA")) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   427
            encodedLength = 83;
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   428
        } else {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   429
            throw new ProviderException("Unknown signature algo: " + algorithm);
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   430
        }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   431
        if (encodedLength > maxDataSize) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   432
            throw new InvalidKeyException
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   433
                ("Key is too short for this signature algorithm");
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   434
        }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   435
    }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   436
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    // see JCA spec
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   438
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    protected void engineInitVerify(PublicKey publicKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            throws InvalidKeyException {
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   441
        if (publicKey == null) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   442
            throw new InvalidKeyException("Key must not be null");
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   443
        }
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   444
        // Need to check key length whenever a new key is set
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   445
        if (publicKey != p11Key) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   446
            checkKeySize(keyAlgorithm, publicKey);
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   447
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        cancelOperation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        mode = M_VERIFY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        p11Key = P11KeyFactory.convertKey(token, publicKey, keyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    // see JCA spec
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   455
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    protected void engineInitSign(PrivateKey privateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            throws InvalidKeyException {
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   458
        if (privateKey == null) {
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   459
            throw new InvalidKeyException("Key must not be null");
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   460
        }
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   461
        // Need to check RSA key length whenever a new key is set
21848
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   462
        if (privateKey != p11Key) {
3902d25a64b1 7200306: SunPKCS11 provider delays the check of DSA key size for SHA1withDSA to sign() instead of init()
valeriep
parents: 17916
diff changeset
   463
            checkKeySize(keyAlgorithm, privateKey);
5155
d70c73fdc68e 6695485: SignedObject constructor throws ProviderException if it's called using provider "SunPKCS11-Solaris"
valeriep
parents: 2
diff changeset
   464
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        cancelOperation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        mode = M_SIGN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        p11Key = P11KeyFactory.convertKey(token, privateKey, keyAlgorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    // see JCA spec
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   472
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    protected void engineUpdate(byte b) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        case T_UPDATE:
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   477
            buffer[0] = b;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            engineUpdate(buffer, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        case T_DIGEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            md.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            bytesProcessed++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        case T_RAW:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (bytesProcessed >= buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                bytesProcessed = buffer.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            buffer[bytesProcessed++] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            throw new ProviderException("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    // see JCA spec
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   497
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    protected void engineUpdate(byte[] b, int ofs, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        case T_UPDATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                if (mode == M_SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    token.p11.C_SignUpdate(session.id(), 0, b, ofs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    token.p11.C_VerifyUpdate(session.id(), 0, b, ofs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                bytesProcessed += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        case T_DIGEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            md.update(b, ofs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            bytesProcessed += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        case T_RAW:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            if (bytesProcessed + len > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                bytesProcessed = buffer.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            System.arraycopy(b, ofs, buffer, bytesProcessed, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            bytesProcessed += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            throw new ProviderException("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    // see JCA spec
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   535
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    protected void engineUpdate(ByteBuffer byteBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        int len = byteBuffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        case T_UPDATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            if (byteBuffer instanceof DirectBuffer == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                // cannot do better than default impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                super.engineUpdate(byteBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            long addr = ((DirectBuffer)byteBuffer).address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            int ofs = byteBuffer.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                if (mode == M_SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    token.p11.C_SignUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                        (session.id(), addr + ofs, null, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    token.p11.C_VerifyUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        (session.id(), addr + ofs, null, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                bytesProcessed += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                byteBuffer.position(ofs + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            } catch (PKCS11Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                throw new ProviderException("Update failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        case T_DIGEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            md.update(byteBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            bytesProcessed += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        case T_RAW:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            if (bytesProcessed + len > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                bytesProcessed = buffer.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            byteBuffer.get(buffer, bytesProcessed, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            bytesProcessed += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            throw new ProviderException("Internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    // see JCA spec
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   583
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    protected byte[] engineSign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            byte[] signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            if (type == T_UPDATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                int len = keyAlgorithm.equals("DSA") ? 40 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                signature = token.p11.C_SignFinal(session.id(), len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                if (type == T_DIGEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                } else { // T_RAW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    if (mechanism == CKM_DSA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                        if (bytesProcessed != buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                            throw new SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                            ("Data for RawDSA must be exactly 20 bytes long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        digest = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    } else { // CKM_ECDSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                        if (bytesProcessed > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                            throw new SignatureException("Data for NONEwithECDSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                            + " must be at most " + RAW_ECDSA_MAX + " bytes long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        digest = new byte[bytesProcessed];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        System.arraycopy(buffer, 0, digest, 0, bytesProcessed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                if (keyAlgorithm.equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    // DSA and ECDSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    signature = token.p11.C_Sign(session.id(), digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                } else { // RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    byte[] data = encodeSignature(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    if (mechanism == CKM_RSA_X_509) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                        data = pkcs1Pad(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    signature = token.p11.C_Sign(session.id(), data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            }
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   622
            if (keyAlgorithm.equals("RSA")) {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   623
                return signature;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            } else {
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   625
                if (p1363Format) {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   626
                    return signature;
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   627
                } else {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   628
                    return dsaToASN1(signature);
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   629
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
41205
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 39771
diff changeset
   631
        } catch (PKCS11Exception pe) {
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 39771
diff changeset
   632
            throw new ProviderException(pe);
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 39771
diff changeset
   633
        } catch (SignatureException | ProviderException e) {
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 39771
diff changeset
   634
            cancelOperation();
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 39771
diff changeset
   635
            throw e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            session = token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    // see JCA spec
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   643
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    protected boolean engineVerify(byte[] signature) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        ensureInitialized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        try {
28974
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   647
            if (!p1363Format) {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   648
                if (keyAlgorithm.equals("DSA")) {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   649
                    signature = asn1ToDSA(signature);
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   650
                } else if (keyAlgorithm.equals("EC")) {
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   651
                    signature = asn1ToECDSA(signature);
71fe221460f5 8042967: Add variant of DSA Signature algorithms that do not ASN.1 encode the signature bytes
juh
parents: 27936
diff changeset
   652
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            if (type == T_UPDATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                token.p11.C_VerifyFinal(session.id(), signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                if (type == T_DIGEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                } else { // T_RAW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    if (mechanism == CKM_DSA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                        if (bytesProcessed != buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                            throw new SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                            ("Data for RawDSA must be exactly 20 bytes long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        digest = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                        if (bytesProcessed > buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                            throw new SignatureException("Data for NONEwithECDSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                            + " must be at most " + RAW_ECDSA_MAX + " bytes long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                        digest = new byte[bytesProcessed];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                        System.arraycopy(buffer, 0, digest, 0, bytesProcessed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                if (keyAlgorithm.equals("RSA") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                    // DSA and ECDSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    token.p11.C_Verify(session.id(), digest, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                } else { // RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    byte[] data = encodeSignature(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    if (mechanism == CKM_RSA_X_509) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                        data = pkcs1Pad(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    token.p11.C_Verify(session.id(), data, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            return true;
41205
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 39771
diff changeset
   688
        } catch (PKCS11Exception pe) {
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 39771
diff changeset
   689
            long errorCode = pe.getErrorCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if (errorCode == CKR_SIGNATURE_INVALID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            if (errorCode == CKR_SIGNATURE_LEN_RANGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                // return false rather than throwing an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            // ECF bug?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            if (errorCode == CKR_DATA_LEN_RANGE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            }
41205
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 39771
diff changeset
   701
            throw new ProviderException(pe);
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 39771
diff changeset
   702
        }  catch (SignatureException | ProviderException e) {
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 39771
diff changeset
   703
            cancelOperation();
2867bb7f5b53 8149802: Signature.verify() doesn't reset the signature object on exception
valeriep
parents: 39771
diff changeset
   704
            throw e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            session = token.releaseSession(session);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    private byte[] pkcs1Pad(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        try {
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 10336
diff changeset
   713
            int len = (p11Key.length() + 7) >> 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            RSAPadding padding = RSAPadding.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                                        (RSAPadding.PAD_BLOCKTYPE_1, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            byte[] padded = padding.pad(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            return padded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    private byte[] encodeSignature(byte[] digest) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            return RSASignature.encodeSignature(digestOID, digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            throw new SignatureException("Invalid encoding", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
//    private static byte[] decodeSignature(byte[] signature) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
//      return RSASignature.decodeSignature(digestOID, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
//    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    // For DSA and ECDSA signatures, PKCS#11 represents them as a simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    // byte array that contains the concatenation of r and s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    // For DSA, r and s are always exactly 20 bytes long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    // For ECDSA, r and s are of variable length, but we know that each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    // occupies half of the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    private static byte[] dsaToASN1(byte[] signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        int n = signature.length >> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        BigInteger r = new BigInteger(1, P11Util.subarray(signature, 0, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        BigInteger s = new BigInteger(1, P11Util.subarray(signature, n, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            DerOutputStream outseq = new DerOutputStream(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            outseq.putInteger(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            outseq.putInteger(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            DerValue result = new DerValue(DerValue.tag_Sequence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                                           outseq.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            return result.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        } catch (java.io.IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            throw new RuntimeException("Internal error", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   756
    private static byte[] asn1ToDSA(byte[] sig) throws SignatureException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        try {
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   758
            // Enforce strict DER checking for signatures
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   759
            DerInputStream in = new DerInputStream(sig, 0, sig.length, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            DerValue[] values = in.getSequence(2);
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   761
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   762
            // check number of components in the read sequence
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   763
            // and trailing data
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   764
            if ((values.length != 2) || (in.available() != 0)) {
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   765
                throw new IOException("Invalid encoding for signature");
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   766
            }
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   767
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            BigInteger r = values[0].getPositiveBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            BigInteger s = values[1].getPositiveBigInteger();
39771
ab558f578f30 8161571: Verifying ECDSA signatures permits trailing bytes
vinnie
parents: 28974
diff changeset
   770
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            byte[] br = toByteArray(r, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            byte[] bs = toByteArray(s, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            if ((br == null) || (bs == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                throw new SignatureException("Out of range value for R or S");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            return P11Util.concat(br, bs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        } catch (SignatureException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        } catch (Exception e) {
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   780
            throw new SignatureException("Invalid encoding for signature", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   784
    private byte[] asn1ToECDSA(byte[] sig) throws SignatureException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        try {
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   786
            // Enforce strict DER checking for signatures
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   787
            DerInputStream in = new DerInputStream(sig, 0, sig.length, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            DerValue[] values = in.getSequence(2);
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   789
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   790
            // check number of components in the read sequence
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   791
            // and trailing data
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   792
            if ((values.length != 2) || (in.available() != 0)) {
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   793
                throw new IOException("Invalid encoding for signature");
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   794
            }
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   795
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            BigInteger r = values[0].getPositiveBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            BigInteger s = values[1].getPositiveBigInteger();
39771
ab558f578f30 8161571: Verifying ECDSA signatures permits trailing bytes
vinnie
parents: 28974
diff changeset
   798
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            // trim leading zeroes
17916
e02ddef88f77 8014618: Need to strip leading zeros in TlsPremasterSecret of DHKeyAgreement
xuelei
parents: 12685
diff changeset
   800
            byte[] br = KeyUtil.trimZeroes(r.toByteArray());
e02ddef88f77 8014618: Need to strip leading zeros in TlsPremasterSecret of DHKeyAgreement
xuelei
parents: 12685
diff changeset
   801
            byte[] bs = KeyUtil.trimZeroes(s.toByteArray());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            int k = Math.max(br.length, bs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            // r and s each occupy half the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            byte[] res = new byte[k << 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            System.arraycopy(br, 0, res, k - br.length, br.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            System.arraycopy(bs, 0, res, res.length - bs.length, bs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        } catch (Exception e) {
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 42693
diff changeset
   809
            throw new SignatureException("Invalid encoding for signature", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    private static byte[] toByteArray(BigInteger bi, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        byte[] b = bi.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        int n = b.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        if (n == len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        if ((n == len + 1) && (b[0] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            byte[] t = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            System.arraycopy(b, 1, t, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (n > len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        // must be smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        byte[] t = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        System.arraycopy(b, 0, t, (len - n), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    // see JCA spec
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
   834
    @SuppressWarnings("deprecation")
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   835
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    protected void engineSetParameter(String param, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        throw new UnsupportedOperationException("setParameter() not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    // see JCA spec
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   842
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   843
    protected void engineSetParameter(AlgorithmParameterSpec params)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   844
            throws InvalidAlgorithmParameterException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   845
        if (params != null) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   846
            throw new InvalidAlgorithmParameterException("No parameter accepted");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   847
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   848
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   849
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   850
    // see JCA spec
27936
ca9ee8e3d527 8066638: Suppress deprecation warnings in jdk.crypto module
darcy
parents: 25859
diff changeset
   851
    @SuppressWarnings("deprecation")
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   852
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    protected Object engineGetParameter(String param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        throw new UnsupportedOperationException("getParameter() not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   857
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   858
    // see JCA spec
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   859
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   860
    protected AlgorithmParameters engineGetParameters() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   861
        return null;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 49700
diff changeset
   862
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
}