jdk/src/java.base/share/classes/sun/security/rsa/RSAPrivateCrtKeyImpl.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
child 44260 dd947f766e11
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
20813
0ad12d66a652 8011071: Better crypto provider handling
ascarpino
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2013, 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.rsa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
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
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.pkcs.PKCS8Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Key implementation for RSA private keys, CRT form. For non-CRT private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * keys, see RSAPrivateKeyImpl. We need separate classes to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * correct behavior in instanceof checks, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Note: RSA keys must be at least 512 bits long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see RSAPrivateKeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see RSAKeyFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public final class RSAPrivateCrtKeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        extends PKCS8Key implements RSAPrivateCrtKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final long serialVersionUID = -1326088454257084918L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private BigInteger n;       // modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private BigInteger e;       // public exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private BigInteger d;       // private exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private BigInteger p;       // prime p
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private BigInteger q;       // prime q
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private BigInteger pe;      // prime exponent p
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private BigInteger qe;      // prime exponent q
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private BigInteger coeff;   // CRT coeffcient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // algorithmId used to identify RSA keys
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
    66
    static final AlgorithmId rsaId =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        new AlgorithmId(AlgorithmId.RSAEncryption_oid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Generate a new key from its encoding. Returns a CRT key if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * and a non-CRT key otherwise. Used by RSAKeyFactory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static RSAPrivateKey newKey(byte[] encoded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        RSAPrivateCrtKeyImpl key = new RSAPrivateCrtKeyImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (key.getPublicExponent().signum() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            // public exponent is missing, return a non-CRT key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return new RSAPrivateKeyImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                key.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                key.getPrivateExponent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Construct a key from its encoding. Called from newKey above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    RSAPrivateCrtKeyImpl(byte[] encoded) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        decode(encoded);
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
    92
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Construct a key from its components. Used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * RSAKeyFactory and the RSAKeyPairGenerator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    RSAPrivateCrtKeyImpl(BigInteger n, BigInteger e, BigInteger d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            BigInteger p, BigInteger q, BigInteger pe, BigInteger qe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            BigInteger coeff) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.n = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.e = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.d = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.p = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.q = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.pe = pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.qe = qe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.coeff = coeff;
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   110
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   111
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // generate the encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        algid = rsaId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            out.putInteger(0); // version must be 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            out.putInteger(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            out.putInteger(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            out.putInteger(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            out.putInteger(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            out.putInteger(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            out.putInteger(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            out.putInteger(qe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            out.putInteger(coeff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            DerValue val =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                new DerValue(DerValue.tag_Sequence, out.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            key = val.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } catch (IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new InvalidKeyException(exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return "RSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public BigInteger getPublicExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public BigInteger getPrivateExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public BigInteger getPrimeP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public BigInteger getPrimeQ() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public BigInteger getPrimeExponentP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public BigInteger getPrimeExponentQ() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return qe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // see JCA doc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public BigInteger getCrtCoefficient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return coeff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Parse the key. Called by PKCS8Key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    protected void parseKeyBits() throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            DerInputStream in = new DerInputStream(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            DerValue derValue = in.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (derValue.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                throw new IOException("Not a SEQUENCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            DerInputStream data = derValue.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            int version = data.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (version != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                throw new IOException("Version must be 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            n = getBigInteger(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            e = getBigInteger(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            d = getBigInteger(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            p = getBigInteger(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            q = getBigInteger(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            pe = getBigInteger(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            qe = getBigInteger(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            coeff = getBigInteger(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (derValue.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                throw new IOException("Extra data available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new InvalidKeyException("Invalid RSA private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Read a BigInteger from the DerInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    static BigInteger getBigInteger(DerInputStream data) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        BigInteger b = data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         * Some implementations do not correctly encode ASN.1 INTEGER values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * in 2's complement format, resulting in a negative integer when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * decoded. Correct the error by converting it to a positive integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * See CR 6255949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (b.signum() < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            b = new BigInteger(1, b.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
}