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