src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/RSAPublicKey.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
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: 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
package sun.security.mscapi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
    29
import java.security.KeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.KeyRep;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
    31
import java.security.ProviderException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    33
import sun.security.rsa.RSAUtil.KeyType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.rsa.RSAPublicKeyImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The handle for an RSA public key using the Microsoft Crypto API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class RSAPublicKey extends Key implements java.security.interfaces.RSAPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9508
diff changeset
    43
    private static final long serialVersionUID = -2289561342425825391L;
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9508
diff changeset
    44
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private byte[] publicKeyBlob = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private byte[] encoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private BigInteger modulus = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private BigInteger exponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Construct an RSAPublicKey object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    RSAPublicKey(long hCryptProv, long hCryptKey, int keyLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    {
40389
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
    55
        super(new NativeHandles(hCryptProv, hCryptKey), keyLength);
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
    56
    }
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
    57
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
    58
    /**
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
    59
     * Construct an RSAPublicKey object.
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
    60
     */
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
    61
    RSAPublicKey(NativeHandles handles, int keyLength)
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
    62
    {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
    63
        super(handles, keyLength);
2
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
     * Returns the standard algorithm name for this key. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * example, "RSA" would indicate that this key is a RSA key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * See Appendix A in the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * "../../../guide/security/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Java Cryptography Architecture API Specification &amp; Reference </a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @return the name of the algorithm associated with this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public String getAlgorithm()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Returns a printable description of the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public String toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        sb.append("RSAPublicKey [size=").append(keyLength)
40389
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
    89
            .append(" bits, type=").append(getKeyType(handles.hCryptKey))
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
    90
            .append(", container=").append(getContainerName(handles.hCryptProv))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            .append("]\n  modulus: ").append(getModulus())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            .append("\n  public exponent: ").append(getPublicExponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Returns the public exponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public BigInteger getPublicExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (exponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   104
            try {
40389
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
   105
                publicKeyBlob = getPublicKeyBlob(handles.hCryptKey);
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   106
                exponent = new BigInteger(1, getExponent(publicKeyBlob));
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   107
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   108
            } catch (KeyException e) {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   109
                throw new ProviderException(e);
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   110
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return exponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Returns the modulus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (modulus == null) {
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   122
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   123
            try {
40389
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 25859
diff changeset
   124
                publicKeyBlob = getPublicKeyBlob(handles.hCryptKey);
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   125
                modulus = new BigInteger(1, getModulus(publicKeyBlob));
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   126
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   127
            } catch (KeyException e) {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   128
                throw new ProviderException(e);
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   129
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return modulus;
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
     * Returns the name of the primary encoding format of this key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * or null if this key does not support encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * The primary encoding format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * named in terms of the appropriate ASN.1 data format, if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * ASN.1 specification for this key exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * For example, the name of the ASN.1 data format for public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * keys is <I>SubjectPublicKeyInfo</I>, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * defined by the X.509 standard; in this case, the returned format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <code>"X.509"</code>. Similarly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * the name of the ASN.1 data format for private keys is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <I>PrivateKeyInfo</I>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * as defined by the PKCS #8 standard; in this case, the returned format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <code>"PKCS#8"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return the primary encoding format of the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public String getFormat()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return "X.509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns the key in its primary encoding format, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * if this key does not support encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return the encoded key, or null if the key does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public byte[] getEncoded()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (encoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            try {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   169
                encoding = RSAPublicKeyImpl.newKey(KeyType.RSA, null,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   170
                    getModulus(), getPublicExponent()).getEncoded();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   172
            } catch (KeyException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return encoding;
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 Object writeReplace() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return new KeyRep(KeyRep.Type.PUBLIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        getFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns the Microsoft CryptoAPI representation of the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   189
    private native byte[] getPublicKeyBlob(long hCryptKey) throws KeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Returns the key's public exponent (in big-endian 2's complement format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   194
    private native byte[] getExponent(byte[] keyBlob) throws KeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Returns the key's modulus (in big-endian 2's complement format).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 9505
diff changeset
   199
    private native byte[] getModulus(byte[] keyBlob) throws KeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}