src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/RSASignature.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/RSASignature.java@3317bb8137f4
child 50204 3195a713e24d
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
11521
d7698e6c5f51 7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents: 10336
diff changeset
     2
 * Copyright (c) 2005, 2012, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.InvalidParameterException;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
    33
import java.security.KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.ProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.SignatureException;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    38
import java.math.BigInteger;
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    39
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    40
import sun.security.rsa.RSAKeyFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * RSA signature implementation. Supports RSA signing using PKCS#1 v1.5 padding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Objects should be instantiated by calling Signature.getInstance() using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * following algorithm names:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    48
 *  . "NONEwithRSA"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *  . "SHA1withRSA"
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    50
 *  . "SHA256withRSA"
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    51
 *  . "SHA384withRSA"
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    52
 *  . "SHA512withRSA"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *  . "MD5withRSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  . "MD2withRSA"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    56
 * 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
    57
 *
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    58
 * 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
    59
 *       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
    60
 *       SHA-256, SHA-384, SHA-512 and a special-purpose digest
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 11521
diff changeset
    61
 *       algorithm which is a concatenation of SHA-1 and MD5 digests.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author  Stanley Man-Kit Ho
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
abstract class RSASignature extends java.security.SignatureSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // message digest implementation we use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final MessageDigest messageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    71
    // message digest name
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    72
    private String messageDigestAlgorithm;
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    73
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    74
    // flag indicating whether the digest has been reset
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private boolean needsReset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // the signing key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private Key privateKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // the verification key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private Key publicKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    83
    /**
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    84
     * Constructs a new RSASignature. Used by Raw subclass.
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    85
     */
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    86
    RSASignature() {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    87
        messageDigest = null;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    88
        messageDigestAlgorithm = null;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
    89
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    91
    /**
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    92
     * Constructs a new RSASignature. Used by subclasses.
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    93
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    RSASignature(String digestName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            messageDigest = MessageDigest.getInstance(digestName);
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    98
            // Get the digest's canonical name
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
    99
            messageDigestAlgorithm = messageDigest.getAlgorithm();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
           throw new ProviderException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        needsReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   108
    // Nested class for NONEwithRSA signatures
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   109
    public static final class Raw extends RSASignature {
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
        // 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
   112
        private static final int RAW_RSA_MAX = 64;
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
        private final byte[] precomputedDigest;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   115
        private int offset = 0;
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
        public Raw() {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   118
            precomputedDigest = new byte[RAW_RSA_MAX];
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   119
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   120
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   121
        // Stores the precomputed message digest value.
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   122
        @Override
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   123
        protected void engineUpdate(byte b) throws SignatureException {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   124
            if (offset >= precomputedDigest.length) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   125
                offset = RAW_RSA_MAX + 1;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   126
                return;
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
            precomputedDigest[offset++] = b;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   129
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   130
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   131
        // Stores the precomputed message digest value.
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   132
        @Override
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   133
        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
   134
                throws SignatureException {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   135
            if (offset + len > precomputedDigest.length) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   136
                offset = RAW_RSA_MAX + 1;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   137
                return;
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
            System.arraycopy(b, off, precomputedDigest, offset, len);
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   140
            offset += len;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   141
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   142
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   143
        // Stores the precomputed message digest value.
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   144
        @Override
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   145
        protected void engineUpdate(ByteBuffer byteBuffer) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   146
            int len = byteBuffer.remaining();
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   147
            if (len <= 0) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   148
                return;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   149
            }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   150
            if (offset + len > precomputedDigest.length) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   151
                offset = RAW_RSA_MAX + 1;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   152
                return;
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
            byteBuffer.get(precomputedDigest, offset, len);
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   155
            offset += len;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   156
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   157
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   158
        @Override
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   159
        protected void resetDigest(){
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   160
            offset = 0;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   161
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   162
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   163
        // Returns the precomputed message digest value.
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   164
        @Override
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   165
        protected byte[] getDigestValue() throws SignatureException {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   166
            if (offset > RAW_RSA_MAX) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   167
                throw new SignatureException("Message digest is too long");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   168
            }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   169
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   170
            // Determine the digest algorithm from the digest length
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   171
            if (offset == 20) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   172
                setDigestName("SHA1");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   173
            } else if (offset == 36) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   174
                setDigestName("SHA1+MD5");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   175
            } else if (offset == 32) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   176
                setDigestName("SHA-256");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   177
            } else if (offset == 48) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   178
                setDigestName("SHA-384");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   179
            } else if (offset == 64) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   180
                setDigestName("SHA-512");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   181
            } else if (offset == 16) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   182
                setDigestName("MD5");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   183
            } else {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   184
                throw new SignatureException(
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   185
                    "Message digest length is not supported");
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   186
            }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   187
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   188
            byte[] result = new byte[offset];
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   189
            System.arraycopy(precomputedDigest, 0, result, 0, offset);
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   190
            offset = 0;
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
            return result;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   193
        }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   194
    }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   195
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public static final class SHA1 extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public SHA1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            super("SHA1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   202
    public static final class SHA256 extends RSASignature {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   203
        public SHA256() {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   204
            super("SHA-256");
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   205
        }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   206
    }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   207
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   208
    public static final class SHA384 extends RSASignature {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   209
        public SHA384() {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   210
            super("SHA-384");
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   211
        }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   212
    }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   213
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   214
    public static final class SHA512 extends RSASignature {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   215
        public SHA512() {
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   216
            super("SHA-512");
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   217
        }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   218
    }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   219
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public static final class MD5 extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        public MD5() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            super("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public static final class MD2 extends RSASignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        public MD2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            super("MD2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   232
    // initialize for signing. See JCA doc
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    protected void engineInitVerify(PublicKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        throws InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        // This signature accepts only RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if ((key instanceof java.security.interfaces.RSAPublicKey) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new InvalidKeyException("Key type not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        java.security.interfaces.RSAPublicKey rsaKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            (java.security.interfaces.RSAPublicKey) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if ((key instanceof sun.security.mscapi.RSAPublicKey) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // convert key to MSCAPI format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   248
            BigInteger modulus = rsaKey.getModulus();
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   249
            BigInteger exponent =  rsaKey.getPublicExponent();
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   250
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   251
            // Check against the local and global values to make sure
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   252
            // the sizes are ok.  Round up to the nearest byte.
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   253
            RSAKeyFactory.checkKeyLengths(((modulus.bitLength() + 7) & ~7),
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   254
                exponent, -1, RSAKeyPairGenerator.KEY_SIZE_MAX);
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   255
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   256
            byte[] modulusBytes = modulus.toByteArray();
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   257
            byte[] exponentBytes = exponent.toByteArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // Adjust key length due to sign bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            int keyBitLength = (modulusBytes[0] == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                ? (modulusBytes.length - 1) * 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                : modulusBytes.length * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            byte[] keyBlob = generatePublicKeyBlob(
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   265
                keyBitLength, modulusBytes, exponentBytes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   267
            try {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   268
                publicKey = importPublicKey(keyBlob, keyBitLength);
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   269
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   270
            } catch (KeyStoreException e) {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   271
                throw new InvalidKeyException(e);
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   272
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            publicKey = (sun.security.mscapi.RSAPublicKey) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   278
        this.privateKey = null;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   279
        resetDigest();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   282
    // initialize for signing. See JCA doc
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   283
    protected void engineInitSign(PrivateKey key) throws InvalidKeyException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        // This signature accepts only RSAPrivateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if ((key instanceof sun.security.mscapi.RSAPrivateKey) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            throw new InvalidKeyException("Key type not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        privateKey = (sun.security.mscapi.RSAPrivateKey) key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   291
        // Check against the local and global values to make sure
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   292
        // 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
   293
        RSAKeyFactory.checkKeyLengths(((privateKey.length() + 7) & ~7),
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   294
            null, RSAKeyPairGenerator.KEY_SIZE_MIN,
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   295
            RSAKeyPairGenerator.KEY_SIZE_MAX);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   297
        this.publicKey = null;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   298
        resetDigest();
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   299
    }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   300
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   301
    /**
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   302
     * Resets the message digest if needed.
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   303
     */
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   304
    protected void resetDigest() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (needsReset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            messageDigest.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            needsReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   311
    protected byte[] getDigestValue() throws SignatureException {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   312
        needsReset = false;
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   313
        return messageDigest.digest();
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   314
    }
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   315
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   316
    protected void setDigestName(String name) {
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   317
        messageDigestAlgorithm = name;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   318
    }
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   319
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Updates the data to be signed or verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * using the specified byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param b the byte to use for the update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @exception SignatureException if the engine is not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    protected void engineUpdate(byte b) throws SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        messageDigest.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        needsReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Updates the data to be signed or verified, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * specified array of bytes, starting at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param b the array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @param off the offset to start from in the array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param len the number of bytes to use, starting at offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @exception SignatureException if the engine is not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    protected void engineUpdate(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        throws SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        messageDigest.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        needsReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Updates the data to be signed or verified, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * specified ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param input the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    protected void engineUpdate(ByteBuffer input)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        messageDigest.update(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        needsReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Returns the signature bytes of all the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * updated so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * The format of the signature depends on the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * signature scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @return the signature bytes of the signing operation's result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @exception SignatureException if the engine is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * initialized properly or if this signature algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    protected byte[] engineSign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   379
        byte[] hash = getDigestValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   381
        // 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
   382
        boolean noHashOID = this instanceof Raw;
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   383
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // Sign hash using MS Crypto APIs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   386
        byte[] result = signHash(noHashOID, hash, hash.length,
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   387
            messageDigestAlgorithm, privateKey.getHCryptProvider(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            privateKey.getHCryptKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // Convert signature array from little endian to big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return convertEndianArray(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Convert array from big endian to little endian, or vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    private byte[] convertEndianArray(byte[] byteArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (byteArray == null || byteArray.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            return byteArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        byte [] retval = new byte[byteArray.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // make it big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        for (int i=0;i < byteArray.length;i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            retval[i] = byteArray[byteArray.length - i - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Sign hash using Microsoft Crypto API with HCRYPTKEY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * The returned data is in little-endian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
9533
13cc5e8eb9f1 6578658: Request for raw RSA (NONEwithRSA) Signature support in SunMSCAPI
vinnie
parents: 9524
diff changeset
   415
    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
   416
        int hashSize, String hashAlgorithm, long hCryptProv, long hCryptKey)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            throws SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Verify a signed hash using Microsoft Crypto API with HCRYPTKEY.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    private native static boolean verifySignedHash(byte[] hash, int hashSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        String hashAlgorithm, byte[] signature, int signatureSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        long hCryptProv, long hCryptKey) throws SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Verifies the passed-in signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param sigBytes the signature bytes to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @return true if the signature was verified, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @exception SignatureException if the engine is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * initialized properly, the passed-in signature is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * encoded or of the wrong type, if this signature algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * process the input data provided, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    protected boolean engineVerify(byte[] sigBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        throws SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    {
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   441
        byte[] hash = getDigestValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return verifySignedHash(hash, hash.length,
9524
8417d0e74ac5 6753664: Support SHA256 (and higher) in SunMSCAPI
vinnie
parents: 9508
diff changeset
   444
            messageDigestAlgorithm, convertEndianArray(sigBytes),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            sigBytes.length, publicKey.getHCryptProvider(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            publicKey.getHCryptKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Sets the specified algorithm parameter to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * value. This method supplies a general-purpose mechanism through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * which it is possible to set the various parameters of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * A parameter may be any settable parameter for the algorithm, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * a parameter size, or a source of random bits for signature generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * (if appropriate), or an indication of whether or not to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * a specific but optional computation. A uniform algorithm-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * naming scheme for each parameter is desirable but left unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param param the string identifier of the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param value the parameter value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @exception InvalidParameterException if <code>param</code> is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * invalid parameter for this signature algorithm engine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * the parameter is already set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * and cannot be set again, a security exception occurs, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @deprecated Replaced by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * #engineSetParameter(java.security.spec.AlgorithmParameterSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * engineSetParameter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9533
diff changeset
   473
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    protected void engineSetParameter(String param, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        throws InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        throw new InvalidParameterException("Parameter not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Gets the value of the specified algorithm parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * This method supplies a general-purpose mechanism through which it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * is possible to get the various parameters of this object. A parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * may be any settable parameter for the algorithm, such as a parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * size, or  a source of random bits for signature generation (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * appropriate), or an indication of whether or not to perform a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * specific but optional computation. A uniform algorithm-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * naming scheme for each parameter is desirable but left unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @param param the string name of the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @return the object that represents the parameter value, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @exception InvalidParameterException if <code>param</code> is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * invalid parameter for this engine, or another exception occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * trying to get this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9533
diff changeset
   503
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    protected Object engineGetParameter(String param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        throws InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        throw new InvalidParameterException("Parameter not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * Generates a public-key BLOB from a key's components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
9505
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents: 5506
diff changeset
   513
    // used by RSACipher
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents: 5506
diff changeset
   514
    static native byte[] generatePublicKeyBlob(
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   515
        int keyBitLength, byte[] modulus, byte[] publicExponent)
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   516
            throws InvalidKeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Imports a public-key BLOB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
9505
3bbc6ff8a905 6888925: SunMSCAPI's Cipher can't use RSA public keys obtained from other sources.
vinnie
parents: 5506
diff changeset
   521
    // used by RSACipher
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   522
    static native RSAPublicKey importPublicKey(byte[] keyBlob, int keySize)
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   523
        throws KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
}