jdk/src/share/classes/sun/security/ssl/RSASignature.java
author smarks
Fri, 14 Jan 2011 15:31:45 -0800
changeset 7990 57019dc81b66
parent 5506 202f599c92aa
child 14664 e71aa0962e70
permissions -rw-r--r--
7012003: diamond conversion for ssl Reviewed-by: wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 2007, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Signature implementation for the SSL/TLS RSA Signature variant with both
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * MD5 and SHA-1 MessageDigests. Used for explicit RSA server authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * (RSA signed server key exchange for RSA_EXPORT and DHE_RSA) and RSA client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * authentication (RSA signed certificate verify message).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * It conforms to the standard JCA Signature API. It is registered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * SunJSSE provider to avoid more complicated getInstance() code and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * negative interaction with the JCA mechanisms for hardware providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The class should be instantiated via the getInstance() method in this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * which returns the implementation from the prefered provider. The internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * implementation allows the hashes to be explicitly set, which is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * for RSA client authentication. It can be obtained via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * getInternalInstance() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This class is not thread safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public final class RSASignature extends SignatureSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final Signature rawRsa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private MessageDigest md5, sha;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // flag indicating if the MessageDigests are in reset state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean isReset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public RSASignature() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        rawRsa = JsseJce.getSignature(JsseJce.SIGNATURE_RAWRSA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        isReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Get an implementation for the RSA signature. Follows the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * JCA getInstance() model, so it return the implementation from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * provider with the highest precedence, which may be this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static Signature getInstance() throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return JsseJce.getSignature(JsseJce.SIGNATURE_SSLRSA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Get an internal implementation for the RSA signature. Used for RSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * client authentication, which needs the ability to set the digests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * to externally provided values via the setHashes() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static Signature getInternalInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return Signature.getInstance(JsseJce.SIGNATURE_SSLRSA, "SunJSSE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Set the MD5 and SHA hashes to the provided objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static void setHashes(Signature sig, MessageDigest md5, MessageDigest sha) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        sig.setParameter("hashes", new MessageDigest[] {md5, sha});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Reset the MessageDigests unless they are already reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (isReset == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            md5.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            sha.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            isReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static void checkNull(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new InvalidKeyException("Key must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected void engineInitVerify(PublicKey publicKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        checkNull(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        rawRsa.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    protected void engineInitSign(PrivateKey privateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        engineInitSign(privateKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected void engineInitSign(PrivateKey privateKey, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        checkNull(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        rawRsa.initSign(privateKey, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    // lazily initialize the MessageDigests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private void initDigests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (md5 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            md5 = JsseJce.getMD5();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            sha = JsseJce.getSHA();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected void engineUpdate(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        initDigests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        isReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        md5.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        sha.update(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected void engineUpdate(byte[] b, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        initDigests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        isReset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        md5.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        sha.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private byte[] getDigest() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            initDigests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            byte[] data = new byte[36];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            md5.digest(data, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            sha.digest(data, 16, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            isReset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } catch (DigestException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new SignatureException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    protected byte[] engineSign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        rawRsa.update(getDigest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return rawRsa.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return engineVerify(sigBytes, 0, sigBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected boolean engineVerify(byte[] sigBytes, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        rawRsa.update(getDigest());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return rawRsa.verify(sigBytes, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    protected void engineSetParameter(String param, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (param.equals("hashes") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                ("Parameter not supported: " + param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (value instanceof MessageDigest[] == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new InvalidParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                ("value must be MessageDigest[]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        MessageDigest[] digests = (MessageDigest[])value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        md5 = digests[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        sha = digests[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    protected Object engineGetParameter(String param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        throw new InvalidParameterException("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}