src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/CSignature.java
author weijun
Thu, 13 Jun 2019 23:53:21 +0800
changeset 55380 22b3b7983ada
parent 54827 01fa7f06f806
permissions -rw-r--r--
8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54827
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
     2
 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
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: 2596
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: 2596
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
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.mscapi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.ByteBuffer;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    29
import java.security.*;
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    30
import java.security.interfaces.ECPublicKey;
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    31
import java.security.interfaces.RSAPublicKey;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    32
import java.security.spec.AlgorithmParameterSpec;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    33
import java.math.BigInteger;
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
    34
import java.security.spec.MGF1ParameterSpec;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
    35
import java.security.spec.PSSParameterSpec;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
    36
import java.util.Locale;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    37
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    38
import sun.security.rsa.RSAKeyFactory;
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    39
import sun.security.util.ECUtil;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    40
import sun.security.util.KeyUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    43
 * Signature implementation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Objects should be instantiated by calling Signature.getInstance() using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * following algorithm names:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    48
 *  . "NONEwithRSA"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *  . "SHA1withRSA"
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    50
 *  . "SHA256withRSA"
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    51
 *  . "SHA384withRSA"
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    52
 *  . "SHA512withRSA"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *  . "MD5withRSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  . "MD2withRSA"
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
    55
 *  . "RSASSA-PSS"
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    56
 *  . "SHA1withECDSA"
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    57
 *  . "SHA224withECDSA"
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    58
 *  . "SHA256withECDSA"
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    59
 *  . "SHA384withECDSA"
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
    60
 *  . "SHA512withECDSA"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    62
 * NOTE: RSA keys must be at least 512 bits long.
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    63
 *
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    64
 * NOTE: NONEwithRSA must be supplied with a pre-computed message digest.
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    65
 *       Only the following digest algorithms are supported: MD5, SHA-1,
12700
9c9ad46c7c40 7169496: Problem with the SHA-224 support for SunMSCAPI provider
valeriep
parents: 12685
diff changeset
    66
 *       SHA-256, SHA-384, SHA-512 and a special-purpose digest
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11521
diff changeset
    67
 *       algorithm which is a concatenation of SHA-1 and MD5 digests.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author  Stanley Man-Kit Ho
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    72
abstract class CSignature extends SignatureSpi {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    73
    // private key algorithm name
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    74
    protected String keyAlgorithm;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    75
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // message digest implementation we use
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
    77
    protected MessageDigest messageDigest;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    79
    // message digest name
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    80
    protected String messageDigestAlgorithm;
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    81
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    82
    // flag indicating whether the digest has been reset
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
    83
    protected boolean needsReset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // the signing key
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    86
    protected CPrivateKey privateKey = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // the verification key
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    89
    protected CPublicKey publicKey = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    91
    /**
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    92
     * Constructs a new CSignature. Used by subclasses.
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    93
     */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    94
    CSignature(String keyName, String digestName) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    96
        this.keyAlgorithm = keyName;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    97
        if (digestName != null) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    98
            try {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
    99
                messageDigest = MessageDigest.getInstance(digestName);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   100
                // Get the digest's canonical name
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   101
                messageDigestAlgorithm = messageDigest.getAlgorithm();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   102
            } catch (NoSuchAlgorithmException e) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   103
                throw new ProviderException(e);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   104
            }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   105
        } else {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   106
            messageDigest = null;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   107
            messageDigestAlgorithm = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        needsReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   112
    static class RSA extends CSignature {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   113
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   114
        public RSA(String digestAlgorithm) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   115
            super("RSA", digestAlgorithm);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   116
        }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   117
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   118
        // initialize for signing. See JCA doc
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   119
        @Override
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   120
        protected void engineInitSign(PrivateKey key) throws InvalidKeyException {
55380
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   121
            if (key == null) {
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   122
                throw new InvalidKeyException("Key cannot be null");
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   123
            }
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   124
            if ((key instanceof CPrivateKey) == false
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   125
                    || !key.getAlgorithm().equalsIgnoreCase("RSA")) {
54827
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   126
                throw new InvalidKeyException("Key type not supported: "
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   127
                        + key.getClass() + " " + key.getAlgorithm());
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   128
            }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   129
            privateKey = (CPrivateKey) key;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   130
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   131
            // Check against the local and global values to make sure
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   132
            // the sizes are ok.  Round up to nearest byte.
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   133
            RSAKeyFactory.checkKeyLengths(((privateKey.length() + 7) & ~7),
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   134
                    null, CKeyPairGenerator.RSA.KEY_SIZE_MIN,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   135
                    CKeyPairGenerator.RSA.KEY_SIZE_MAX);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   136
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   137
            this.publicKey = null;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   138
            resetDigest();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   139
        }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   140
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   141
        // initialize for signing. See JCA doc
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   142
        @Override
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   143
        protected void engineInitVerify(PublicKey key) throws InvalidKeyException {
55380
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   144
            if (key == null) {
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   145
                throw new InvalidKeyException("Key cannot be null");
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   146
            }
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   147
            // This signature accepts only RSAPublicKey
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   148
            if ((key instanceof RSAPublicKey) == false) {
55380
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   149
                throw new InvalidKeyException("Key type not supported: "
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   150
                        + key.getClass());
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   151
            }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   152
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   153
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   154
            if ((key instanceof CPublicKey) == false) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   155
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   156
                // convert key to MSCAPI format
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   157
                java.security.interfaces.RSAPublicKey rsaKey =
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   158
                        (java.security.interfaces.RSAPublicKey) key;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   159
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   160
                BigInteger modulus = rsaKey.getModulus();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   161
                BigInteger exponent =  rsaKey.getPublicExponent();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   162
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   163
                // Check against the local and global values to make sure
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   164
                // the sizes are ok.  Round up to the nearest byte.
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   165
                RSAKeyFactory.checkKeyLengths(((modulus.bitLength() + 7) & ~7),
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   166
                        exponent, -1, CKeyPairGenerator.RSA.KEY_SIZE_MAX);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   167
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   168
                byte[] modulusBytes = modulus.toByteArray();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   169
                byte[] exponentBytes = exponent.toByteArray();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   170
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   171
                // Adjust key length due to sign bit
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   172
                int keyBitLength = (modulusBytes[0] == 0)
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   173
                        ? (modulusBytes.length - 1) * 8
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   174
                        : modulusBytes.length * 8;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   175
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   176
                byte[] keyBlob = generatePublicKeyBlob(
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   177
                        keyBitLength, modulusBytes, exponentBytes);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   178
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   179
                try {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   180
                    publicKey = importPublicKey("RSA", keyBlob, keyBitLength);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   181
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   182
                } catch (KeyStoreException e) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   183
                    throw new InvalidKeyException(e);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   184
                }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   185
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   186
            } else {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   187
                publicKey = (CPublicKey) key;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   188
            }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   189
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   190
            this.privateKey = null;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   191
            resetDigest();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   192
        }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   193
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   194
        /**
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   195
         * Returns the signature bytes of all the data
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   196
         * updated so far.
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   197
         * The format of the signature depends on the underlying
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   198
         * signature scheme.
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   199
         *
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   200
         * @return the signature bytes of the signing operation's result.
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   201
         *
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   202
         * @exception SignatureException if the engine is not
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   203
         * initialized properly or if this signature algorithm is unable to
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   204
         * process the input data provided.
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   205
         */
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   206
        @Override
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   207
        protected byte[] engineSign() throws SignatureException {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   208
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   209
            byte[] hash = getDigestValue();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   210
54827
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   211
            if (privateKey.getHCryptKey() == 0) {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   212
                return signCngHash(1, hash, hash.length,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   213
                        0,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   214
                        this instanceof NONEwithRSA ? null : messageDigestAlgorithm,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   215
                        privateKey.getHCryptProvider(), 0);
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   216
            } else {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   217
                // Omit the hash OID when generating a NONEwithRSA signature
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   218
                boolean noHashOID = this instanceof NONEwithRSA;
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   219
                // Sign hash using MS Crypto APIs
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   220
                byte[] result = signHash(noHashOID, hash, hash.length,
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   221
                        messageDigestAlgorithm, privateKey.getHCryptProvider(),
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   222
                        privateKey.getHCryptKey());
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   223
54827
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   224
                // Convert signature array from little endian to big endian
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   225
                return convertEndianArray(result);
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   226
            }
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   227
        }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   228
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   229
        /**
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   230
         * Verifies the passed-in signature.
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   231
         *
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   232
         * @param sigBytes the signature bytes to be verified.
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   233
         *
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   234
         * @return true if the signature was verified, false if not.
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   235
         *
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   236
         * @exception SignatureException if the engine is not
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   237
         * initialized properly, the passed-in signature is improperly
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   238
         * encoded or of the wrong type, if this signature algorithm is unable to
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   239
         * process the input data provided, etc.
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   240
         */
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   241
        @Override
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   242
        protected boolean engineVerify(byte[] sigBytes)
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   243
                throws SignatureException {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   244
            byte[] hash = getDigestValue();
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   245
54827
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   246
            if (publicKey.getHCryptKey() == 0) {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   247
                return verifyCngSignedHash(
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   248
                        1, hash, hash.length,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   249
                        sigBytes, sigBytes.length,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   250
                        0,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   251
                        messageDigestAlgorithm,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   252
                        publicKey.getHCryptProvider(),
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   253
                        0);
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   254
            } else {
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   255
                return verifySignedHash(hash, hash.length,
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   256
                        messageDigestAlgorithm, convertEndianArray(sigBytes),
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   257
                        sigBytes.length, publicKey.getHCryptProvider(),
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   258
                        publicKey.getHCryptKey());
01fa7f06f806 8223063: Support CNG RSA keys
weijun
parents: 53007
diff changeset
   259
            }
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   260
        }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   261
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   262
        /**
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   263
         * Generates a public-key BLOB from a key's components.
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   264
         */
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   265
        // used by CRSACipher
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   266
        static native byte[] generatePublicKeyBlob(
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   267
                int keyBitLength, byte[] modulus, byte[] publicExponent)
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   268
                throws InvalidKeyException;
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   269
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   270
    }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   271
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   272
    // Nested class for NONEwithRSA signatures
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   273
    public static final class NONEwithRSA extends RSA {
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   274
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   275
        // the longest supported digest is 512 bits (SHA-512)
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   276
        private static final int RAW_RSA_MAX = 64;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   277
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   278
        private final byte[] precomputedDigest;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   279
        private int offset = 0;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   280
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   281
        public NONEwithRSA() {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   282
            super(null);
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   283
            precomputedDigest = new byte[RAW_RSA_MAX];
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   284
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   285
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   286
        // Stores the precomputed message digest value.
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   287
        @Override
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   288
        protected void engineUpdate(byte b) throws SignatureException {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   289
            if (offset >= precomputedDigest.length) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   290
                offset = RAW_RSA_MAX + 1;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   291
                return;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   292
            }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   293
            precomputedDigest[offset++] = b;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   294
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   295
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   296
        // Stores the precomputed message digest value.
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   297
        @Override
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   298
        protected void engineUpdate(byte[] b, int off, int len)
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   299
                throws SignatureException {
51216
e429a304c97d 8204196: integer cleanup
ascarpino
parents: 50715
diff changeset
   300
            if (len > (precomputedDigest.length - offset)) {
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   301
                offset = RAW_RSA_MAX + 1;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   302
                return;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   303
            }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   304
            System.arraycopy(b, off, precomputedDigest, offset, len);
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   305
            offset += len;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   306
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   307
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   308
        // Stores the precomputed message digest value.
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   309
        @Override
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   310
        protected void engineUpdate(ByteBuffer byteBuffer) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   311
            int len = byteBuffer.remaining();
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   312
            if (len <= 0) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   313
                return;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   314
            }
51216
e429a304c97d 8204196: integer cleanup
ascarpino
parents: 50715
diff changeset
   315
            if (len > (precomputedDigest.length - offset)) {
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   316
                offset = RAW_RSA_MAX + 1;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   317
                return;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   318
            }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   319
            byteBuffer.get(precomputedDigest, offset, len);
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   320
            offset += len;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   321
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   322
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   323
        @Override
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   324
        protected void resetDigest(){
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   325
            offset = 0;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   326
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   327
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   328
        // Returns the precomputed message digest value.
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   329
        @Override
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   330
        protected byte[] getDigestValue() throws SignatureException {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   331
            if (offset > RAW_RSA_MAX) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   332
                throw new SignatureException("Message digest is too long");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   333
            }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   334
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   335
            // Determine the digest algorithm from the digest length
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   336
            if (offset == 20) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   337
                setDigestName("SHA1");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   338
            } else if (offset == 36) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   339
                setDigestName("SHA1+MD5");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   340
            } else if (offset == 32) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   341
                setDigestName("SHA-256");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   342
            } else if (offset == 48) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   343
                setDigestName("SHA-384");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   344
            } else if (offset == 64) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   345
                setDigestName("SHA-512");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   346
            } else if (offset == 16) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   347
                setDigestName("MD5");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   348
            } else {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   349
                throw new SignatureException(
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   350
                    "Message digest length is not supported");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   351
            }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   352
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   353
            byte[] result = new byte[offset];
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   354
            System.arraycopy(precomputedDigest, 0, result, 0, offset);
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   355
            offset = 0;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   356
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   357
            return result;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   358
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   359
    }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   360
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   361
    public static final class SHA1withRSA extends RSA {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   362
        public SHA1withRSA() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            super("SHA1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   367
    public static final class SHA256withRSA extends RSA {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   368
        public SHA256withRSA() {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   369
            super("SHA-256");
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   370
        }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   371
    }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   372
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   373
    public static final class SHA384withRSA extends RSA {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   374
        public SHA384withRSA() {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   375
            super("SHA-384");
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   376
        }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   377
    }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   378
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   379
    public static final class SHA512withRSA extends RSA {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   380
        public SHA512withRSA() {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   381
            super("SHA-512");
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   382
        }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   383
    }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   384
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   385
    public static final class MD5withRSA extends RSA {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   386
        public MD5withRSA() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            super("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   391
    public static final class MD2withRSA extends RSA {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   392
        public MD2withRSA() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            super("MD2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   397
    public static final class SHA1withECDSA extends ECDSA {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   398
        public SHA1withECDSA() {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   399
            super("SHA-1");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   400
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   401
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   402
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   403
    public static final class SHA224withECDSA extends ECDSA {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   404
        public SHA224withECDSA() {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   405
            super("SHA-224");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   406
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   407
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   408
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   409
    public static final class SHA256withECDSA extends ECDSA {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   410
        public SHA256withECDSA() {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   411
            super("SHA-256");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   412
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   413
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   414
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   415
    public static final class SHA384withECDSA extends ECDSA {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   416
        public SHA384withECDSA() {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   417
            super("SHA-384");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   418
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   419
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   420
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   421
    public static final class SHA512withECDSA extends ECDSA {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   422
        public SHA512withECDSA() {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   423
            super("SHA-512");
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   424
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   425
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   426
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   427
    static class ECDSA extends CSignature {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   428
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   429
        public ECDSA(String messageDigestAlgorithm) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   430
            super("EC", messageDigestAlgorithm);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   431
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   432
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   433
        // initialize for signing. See JCA doc
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   434
        @Override
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   435
        protected void engineInitSign(PrivateKey key) throws InvalidKeyException {
55380
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   436
            if (key == null) {
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   437
                throw new InvalidKeyException("Key cannot be null");
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   438
            }
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   439
            if ((key instanceof CPrivateKey) == false
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   440
                    || !key.getAlgorithm().equalsIgnoreCase("EC")) {
55380
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   441
                throw new InvalidKeyException("Key type not supported: "
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   442
                        + key.getClass() + " " + key.getAlgorithm());
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   443
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   444
            privateKey = (CPrivateKey) key;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   445
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   446
            this.publicKey = null;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   447
            resetDigest();
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   448
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   449
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   450
        // initialize for signing. See JCA doc
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   451
        @Override
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   452
        protected void engineInitVerify(PublicKey key) throws InvalidKeyException {
55380
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   453
            if (key == null) {
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   454
                throw new InvalidKeyException("Key cannot be null");
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   455
            }
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   456
            // This signature accepts only ECPublicKey
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   457
            if ((key instanceof ECPublicKey) == false) {
55380
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   458
                throw new InvalidKeyException("Key type not supported: "
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   459
                        + key.getClass());
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   460
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   461
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   462
            if ((key instanceof CPublicKey) == false) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   463
                try {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   464
                    publicKey = importECPublicKey("EC",
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   465
                            CKey.generateECBlob(key),
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   466
                            KeyUtil.getKeySize(key));
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   467
                } catch (KeyStoreException e) {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   468
                    throw new InvalidKeyException(e);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   469
                }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   470
            } else {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   471
                publicKey = (CPublicKey) key;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   472
            }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   473
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   474
            this.privateKey = null;
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   475
            resetDigest();
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   476
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   477
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   478
        @Override
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   479
        protected byte[] engineSign() throws SignatureException {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   480
            byte[] hash = getDigestValue();
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   481
            byte[] raw = signCngHash(0, hash, hash.length,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   482
                    0,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   483
                    null,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   484
                    privateKey.getHCryptProvider(), 0);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   485
            return ECUtil.encodeSignature(raw);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   486
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   487
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   488
        @Override
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   489
        protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   490
            byte[] hash = getDigestValue();
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   491
            sigBytes = ECUtil.decodeSignature(sigBytes);
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   492
            return verifyCngSignedHash(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   493
                    0,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   494
                    hash, hash.length,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   495
                    sigBytes, sigBytes.length,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   496
                    0,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   497
                    null,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   498
                    publicKey.getHCryptProvider(),
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   499
                    0
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   500
            );
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   501
        }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   502
    }
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   503
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   504
    public static final class PSS extends RSA {
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   505
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   506
        private PSSParameterSpec pssParams = null;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   507
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   508
        // Workaround: Cannot import raw public key to CNG. This signature
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   509
        // will be used for verification if key is not from MSCAPI.
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   510
        private Signature fallbackSignature;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   511
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   512
        public PSS() {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   513
            super(null);
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   514
        }
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   515
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   516
        @Override
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   517
        protected void engineInitSign(PrivateKey key) throws InvalidKeyException {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   518
            super.engineInitSign(key);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   519
            fallbackSignature = null;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   520
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   521
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   522
        @Override
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   523
        protected void engineInitVerify(PublicKey key) throws InvalidKeyException {
55380
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   524
            if (key == null) {
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   525
                throw new InvalidKeyException("Key cannot be null");
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   526
            }
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   527
            // This signature accepts only RSAPublicKey
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   528
            if ((key instanceof java.security.interfaces.RSAPublicKey) == false) {
55380
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   529
                throw new InvalidKeyException("Key type not supported: "
22b3b7983ada 8225180: SignedObject with invalid Key not throwing the InvalidKeyException in Windows
weijun
parents: 54827
diff changeset
   530
                        + key.getClass());
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   531
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   532
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   533
            this.privateKey = null;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   534
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   535
            if (key instanceof CPublicKey) {
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   536
                fallbackSignature = null;
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   537
                publicKey = (CPublicKey) key;
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   538
            } else {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   539
                if (fallbackSignature == null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   540
                    try {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   541
                        fallbackSignature = Signature.getInstance(
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   542
                                "RSASSA-PSS", "SunRsaSign");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   543
                    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   544
                        throw new InvalidKeyException("Invalid key", e);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   545
                    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   546
                }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   547
                fallbackSignature.initVerify(key);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   548
                if (pssParams != null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   549
                    try {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   550
                        fallbackSignature.setParameter(pssParams);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   551
                    } catch (InvalidAlgorithmParameterException e) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   552
                        throw new InvalidKeyException("Invalid params", e);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   553
                    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   554
                }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   555
                publicKey = null;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   556
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   557
            resetDigest();
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   558
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   559
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   560
        @Override
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   561
        protected void engineUpdate(byte b) throws SignatureException {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   562
            ensureInit();
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   563
            if (fallbackSignature != null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   564
                fallbackSignature.update(b);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   565
            } else {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   566
                messageDigest.update(b);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   567
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   568
            needsReset = true;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   569
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   570
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   571
        @Override
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   572
        protected void engineUpdate(byte[] b, int off, int len) throws SignatureException {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   573
            ensureInit();
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   574
            if (fallbackSignature != null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   575
                fallbackSignature.update(b, off, len);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   576
            } else {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   577
                messageDigest.update(b, off, len);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   578
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   579
            needsReset = true;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   580
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   581
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   582
        @Override
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   583
        protected void engineUpdate(ByteBuffer input) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   584
            try {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   585
                ensureInit();
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   586
            } catch (SignatureException se) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   587
                // hack for working around API bug
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   588
                throw new RuntimeException(se.getMessage());
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   589
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   590
            if (fallbackSignature != null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   591
                try {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   592
                    fallbackSignature.update(input);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   593
                } catch (SignatureException se) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   594
                    // hack for working around API bug
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   595
                    throw new RuntimeException(se.getMessage());
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   596
                }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   597
            } else {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   598
                messageDigest.update(input);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   599
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   600
            needsReset = true;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   601
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   602
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   603
        @Override
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   604
        protected byte[] engineSign() throws SignatureException {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   605
            ensureInit();
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   606
            byte[] hash = getDigestValue();
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   607
            return signCngHash(2, hash, hash.length,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   608
                    pssParams.getSaltLength(),
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   609
                    ((MGF1ParameterSpec)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   610
                            pssParams.getMGFParameters()).getDigestAlgorithm(),
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   611
                    privateKey.getHCryptProvider(), privateKey.getHCryptKey());
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   612
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   613
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   614
        @Override
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   615
        protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   616
            ensureInit();
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   617
            if (fallbackSignature != null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   618
                needsReset = false;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   619
                return fallbackSignature.verify(sigBytes);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   620
            } else {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   621
                byte[] hash = getDigestValue();
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   622
                return verifyCngSignedHash(
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   623
                        2, hash, hash.length,
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   624
                        sigBytes, sigBytes.length,
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   625
                        pssParams.getSaltLength(),
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   626
                        ((MGF1ParameterSpec)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   627
                                pssParams.getMGFParameters()).getDigestAlgorithm(),
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   628
                        publicKey.getHCryptProvider(),
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   629
                        publicKey.getHCryptKey()
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   630
                );
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   631
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   632
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   633
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   634
        @Override
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   635
        protected void engineSetParameter(AlgorithmParameterSpec params)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   636
                throws InvalidAlgorithmParameterException {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   637
            if (needsReset) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   638
                throw new ProviderException
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   639
                        ("Cannot set parameters during operations");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   640
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   641
            this.pssParams = validateSigParams(params);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   642
            if (fallbackSignature != null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   643
                fallbackSignature.setParameter(params);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   644
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   645
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   646
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   647
        @Override
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   648
        protected AlgorithmParameters engineGetParameters() {
51229
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 51216
diff changeset
   649
            AlgorithmParameters ap = null;
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 51216
diff changeset
   650
            if (this.pssParams != null) {
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 51216
diff changeset
   651
                try {
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 51216
diff changeset
   652
                    ap = AlgorithmParameters.getInstance("RSASSA-PSS");
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 51216
diff changeset
   653
                    ap.init(this.pssParams);
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 51216
diff changeset
   654
                } catch (GeneralSecurityException gse) {
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 51216
diff changeset
   655
                    throw new ProviderException(gse.getMessage());
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 51216
diff changeset
   656
                }
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   657
            }
51229
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 51216
diff changeset
   658
            return ap;
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   659
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   660
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   661
        private void ensureInit() throws SignatureException {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   662
            if (this.privateKey == null && this.publicKey == null
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   663
                    && fallbackSignature == null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   664
                throw new SignatureException("Missing key");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   665
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   666
            if (this.pssParams == null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   667
                // Parameters are required for signature verification
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   668
                throw new SignatureException
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   669
                        ("Parameters required for RSASSA-PSS signatures");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   670
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   671
            if (fallbackSignature == null && messageDigest == null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   672
                // This could happen if initVerify(softKey), setParameter(),
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   673
                // and initSign() were called. No messageDigest. Create it.
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   674
                try {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   675
                    messageDigest = MessageDigest
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   676
                            .getInstance(pssParams.getDigestAlgorithm());
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   677
                } catch (NoSuchAlgorithmException e) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   678
                    throw new SignatureException(e);
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   679
                }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   680
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   681
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   682
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   683
        /**
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   684
         * Validate the specified Signature PSS parameters.
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   685
         */
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   686
        private PSSParameterSpec validateSigParams(AlgorithmParameterSpec p)
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   687
                throws InvalidAlgorithmParameterException {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   688
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   689
            if (p == null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   690
                throw new InvalidAlgorithmParameterException
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   691
                        ("Parameters cannot be null");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   692
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   693
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   694
            if (!(p instanceof PSSParameterSpec)) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   695
                throw new InvalidAlgorithmParameterException
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   696
                        ("parameters must be type PSSParameterSpec");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   697
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   698
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   699
            // no need to validate again if same as current signature parameters
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   700
            PSSParameterSpec params = (PSSParameterSpec) p;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   701
            if (params == this.pssParams) return params;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   702
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   703
            // now sanity check the parameter values
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   704
            if (!(params.getMGFAlgorithm().equalsIgnoreCase("MGF1"))) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   705
                throw new InvalidAlgorithmParameterException("Only supports MGF1");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   706
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   707
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   708
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   709
            if (params.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   710
                throw new InvalidAlgorithmParameterException
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   711
                        ("Only supports TrailerFieldBC(1)");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   712
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   713
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   714
            AlgorithmParameterSpec algSpec = params.getMGFParameters();
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   715
            if (!(algSpec instanceof MGF1ParameterSpec)) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   716
                throw new InvalidAlgorithmParameterException
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   717
                        ("Only support MGF1ParameterSpec");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   718
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   719
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   720
            MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)algSpec;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   721
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   722
            String msgHashAlg = params.getDigestAlgorithm()
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   723
                    .toLowerCase(Locale.ROOT).replaceAll("-", "");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   724
            if (msgHashAlg.equals("sha")) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   725
                msgHashAlg = "sha1";
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   726
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   727
            String mgf1HashAlg = mgfSpec.getDigestAlgorithm()
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   728
                    .toLowerCase(Locale.ROOT).replaceAll("-", "");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   729
            if (mgf1HashAlg.equals("sha")) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   730
                mgf1HashAlg = "sha1";
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   731
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   732
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   733
            if (!mgf1HashAlg.equals(msgHashAlg)) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   734
                throw new InvalidAlgorithmParameterException
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   735
                        ("MGF1 hash must be the same as message hash");
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   736
            }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   737
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   738
            return params;
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   739
        }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   740
    }
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   741
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   742
    /**
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   743
     * Sign hash using CNG API with HCRYPTKEY.
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   744
     * @param type 0 no padding, 1, pkcs1, 2, pss
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   745
     */
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   746
    native static byte[] signCngHash(
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   747
            int type, byte[] hash,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   748
            int hashSize, int saltLength, String hashAlgorithm,
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   749
            long hCryptProv, long nCryptKey)
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   750
            throws SignatureException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   752
    /**
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   753
     * Verify a signed hash using CNG API with HCRYPTKEY.
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   754
     * @param type 0 no padding, 1, pkcs1, 2, pss
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   755
     */
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   756
    private native static boolean verifyCngSignedHash(
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   757
            int type, byte[] hash, int hashSize,
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   758
            byte[] signature, int signatureSize,
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   759
            int saltLength, String hashAlgorithm,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   760
            long hCryptProv, long hKey) throws SignatureException;
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   761
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   762
    /**
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   763
     * Resets the message digest if needed.
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   764
     */
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   765
    protected void resetDigest() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (needsReset) {
50715
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   767
            if (messageDigest != null) {
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   768
                messageDigest.reset();
46492a773912 8205445: Add RSASSA-PSS Signature support to SunMSCAPI
weijun
parents: 50204
diff changeset
   769
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            needsReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   774
    protected byte[] getDigestValue() throws SignatureException {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   775
        needsReset = false;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   776
        return messageDigest.digest();
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   777
    }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   778
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   779
    protected void setDigestName(String name) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   780
        messageDigestAlgorithm = name;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   781
    }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   782
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * Updates the data to be signed or verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * using the specified byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @param b the byte to use for the update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @exception SignatureException if the engine is not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     */
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   792
    @Override
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   793
    protected void engineUpdate(byte b) throws SignatureException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        messageDigest.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        needsReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * Updates the data to be signed or verified, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * specified array of bytes, starting at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @param b the array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @param off the offset to start from in the array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @param len the number of bytes to use, starting at offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @exception SignatureException if the engine is not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   809
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    protected void engineUpdate(byte[] b, int off, int len)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   811
            throws SignatureException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        messageDigest.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        needsReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * Updates the data to be signed or verified, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * specified ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @param input the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   822
    @Override
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   823
    protected void engineUpdate(ByteBuffer input) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        messageDigest.update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        needsReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * Convert array from big endian to little endian, or vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   831
    private static byte[] convertEndianArray(byte[] byteArray) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (byteArray == null || byteArray.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            return byteArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        byte [] retval = new byte[byteArray.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        // make it big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        for (int i=0;i < byteArray.length;i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            retval[i] = byteArray[byteArray.length - i - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * Sign hash using Microsoft Crypto API with HCRYPTKEY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * The returned data is in little-endian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     */
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   848
    private native static byte[] signHash(boolean noHashOID, byte[] hash,
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   849
        int hashSize, String hashAlgorithm, long hCryptProv, long hCryptKey)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            throws SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Verify a signed hash using Microsoft Crypto API with HCRYPTKEY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    private native static boolean verifySignedHash(byte[] hash, int hashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        String hashAlgorithm, byte[] signature, int signatureSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        long hCryptProv, long hCryptKey) throws SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * Sets the specified algorithm parameter to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * value. This method supplies a general-purpose mechanism through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * which it is possible to set the various parameters of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * A parameter may be any settable parameter for the algorithm, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * a parameter size, or a source of random bits for signature generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * (if appropriate), or an indication of whether or not to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * a specific but optional computation. A uniform algorithm-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * naming scheme for each parameter is desirable but left unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @param param the string identifier of the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @param value the parameter value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @exception InvalidParameterException if <code>param</code> is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * invalid parameter for this signature algorithm engine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * the parameter is already set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * and cannot be set again, a security exception occurs, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * @deprecated Replaced by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * #engineSetParameter(java.security.spec.AlgorithmParameterSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * engineSetParameter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     */
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   883
    @Override
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9533
diff changeset
   884
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    protected void engineSetParameter(String param, Object value)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   886
            throws InvalidParameterException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        throw new InvalidParameterException("Parameter not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   890
    /**
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   891
     * Sets this signature engine with the specified algorithm parameter.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   892
     *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   893
     * @param params the parameters
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   894
     *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   895
     * @exception InvalidAlgorithmParameterException if the given
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   896
     * parameter is invalid
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   897
     */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   898
    @Override
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   899
    protected void engineSetParameter(AlgorithmParameterSpec params)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   900
            throws InvalidAlgorithmParameterException {
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   901
        if (params != null) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   902
            throw new InvalidAlgorithmParameterException("No parameter accepted");
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   903
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   904
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Gets the value of the specified algorithm parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * This method supplies a general-purpose mechanism through which it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * is possible to get the various parameters of this object. A parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * may be any settable parameter for the algorithm, such as a parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * size, or  a source of random bits for signature generation (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * appropriate), or an indication of whether or not to perform a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * specific but optional computation. A uniform algorithm-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * naming scheme for each parameter is desirable but left unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @param param the string name of the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @return the object that represents the parameter value, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * @exception InvalidParameterException if <code>param</code> is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * invalid parameter for this engine, or another exception occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * trying to get this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     */
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   928
    @Override
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9533
diff changeset
   929
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    protected Object engineGetParameter(String param)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   931
           throws InvalidParameterException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        throw new InvalidParameterException("Parameter not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /**
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   936
     * Gets the algorithm parameter from this signature engine.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   937
     *
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   938
     * @return the parameter, or null if no parameter is used.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   939
     */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   940
    @Override
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   941
    protected AlgorithmParameters engineGetParameters() {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   942
        return null;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   943
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   944
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   945
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * Imports a public-key BLOB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   948
    // used by CRSACipher
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   949
    static native CPublicKey importPublicKey(
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 51229
diff changeset
   950
            String alg, byte[] keyBlob, int keySize) throws KeyStoreException;
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   951
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   952
    static native CPublicKey importECPublicKey(
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   953
            String alg, byte[] keyBlob, int keySize) throws KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
}