src/java.base/share/classes/sun/security/rsa/RSAPrivateCrtKeyImpl.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 56592 b1902b22005e
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) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2596
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.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.*;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    32
import java.security.spec.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.*;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.pkcs.PKCS8Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    40
import static sun.security.rsa.RSAUtil.KeyType;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    43
 * RSA private key implementation for "RSA", "RSASSA-PSS" algorithms in CRT form.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    44
 * For non-CRT private keys, see RSAPrivateKeyImpl. We need separate classes
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    45
 * to ensure correct behavior in instanceof checks, etc.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Note: RSA keys must be at least 512 bits long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see RSAPrivateKeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see RSAKeyFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public final class RSAPrivateCrtKeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        extends PKCS8Key implements RSAPrivateCrtKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final long serialVersionUID = -1326088454257084918L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private BigInteger n;       // modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private BigInteger e;       // public exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private BigInteger d;       // private exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private BigInteger p;       // prime p
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private BigInteger q;       // prime q
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private BigInteger pe;      // prime exponent p
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private BigInteger qe;      // prime exponent q
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private BigInteger coeff;   // CRT coeffcient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    69
    // Optional parameters associated with this RSA key
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    70
    // specified in the encoding of its AlgorithmId.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    71
    // Must be null for "RSA" keys.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    72
    private AlgorithmParameterSpec keyParams;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Generate a new key from its encoding. Returns a CRT key if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * and a non-CRT key otherwise. Used by RSAKeyFactory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static RSAPrivateKey newKey(byte[] encoded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        RSAPrivateCrtKeyImpl key = new RSAPrivateCrtKeyImpl(encoded);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    81
        // check all CRT-specific components are available, if any one
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    82
        // missing, return a non-CRT key instead
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    83
        if ((key.getPublicExponent().signum() == 0) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    84
            (key.getPrimeExponentP().signum() == 0) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    85
            (key.getPrimeExponentQ().signum() == 0) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    86
            (key.getPrimeP().signum() == 0) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    87
            (key.getPrimeQ().signum() == 0) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    88
            (key.getCrtCoefficient().signum() == 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return new RSAPrivateKeyImpl(
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    90
                key.algid,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                key.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                key.getPrivateExponent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   100
     * Generate a new key from the specified type and components.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   101
     * Returns a CRT key if possible and a non-CRT key otherwise. 
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   102
     * Used by SunPKCS11 provider.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   103
     */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   104
    public static RSAPrivateKey newKey(KeyType type,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   105
            AlgorithmParameterSpec params,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   106
            BigInteger n, BigInteger e, BigInteger d,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   107
            BigInteger p, BigInteger q, BigInteger pe, BigInteger qe,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   108
            BigInteger coeff) throws InvalidKeyException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   109
        RSAPrivateKey key;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   110
        AlgorithmId rsaId = RSAUtil.createAlgorithmId(type, params);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   111
        if ((e.signum() == 0) || (p.signum() == 0) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   112
            (q.signum() == 0) || (pe.signum() == 0) ||
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   113
            (qe.signum() == 0) || (coeff.signum() == 0)) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   114
            // if any component is missing, return a non-CRT key
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   115
            return new RSAPrivateKeyImpl(rsaId, n, d);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   116
        } else {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   117
            return new RSAPrivateCrtKeyImpl(rsaId, n, e, d,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   118
                p, q, pe, qe, coeff);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   119
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   120
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   121
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   122
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Construct a key from its encoding. Called from newKey above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    RSAPrivateCrtKeyImpl(byte[] encoded) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        decode(encoded);
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   127
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   128
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   129
            // this will check the validity of params
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   130
            this.keyParams = RSAUtil.getParamSpec(algid);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   131
        } catch (ProviderException e) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   132
            throw new InvalidKeyException(e);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   133
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   137
     * Construct a RSA key from its components. Used by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * RSAKeyFactory and the RSAKeyPairGenerator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   140
    RSAPrivateCrtKeyImpl(AlgorithmId rsaId,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   141
            BigInteger n, BigInteger e, BigInteger d,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            BigInteger p, BigInteger q, BigInteger pe, BigInteger qe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            BigInteger coeff) throws InvalidKeyException {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   144
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   145
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.n = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        this.e = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        this.d = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this.p = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.q = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this.pe = pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.qe = qe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this.coeff = coeff;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   154
        this.keyParams = RSAUtil.getParamSpec(rsaId);
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   155
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // generate the encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        algid = rsaId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            out.putInteger(0); // version must be 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            out.putInteger(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            out.putInteger(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            out.putInteger(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            out.putInteger(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            out.putInteger(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            out.putInteger(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            out.putInteger(qe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            out.putInteger(coeff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            DerValue val =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                new DerValue(DerValue.tag_Sequence, out.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            key = val.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } catch (IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new InvalidKeyException(exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   179
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public String getAlgorithm() {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   181
        return algid.getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   185
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   191
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public BigInteger getPublicExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   197
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public BigInteger getPrivateExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   203
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public BigInteger getPrimeP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   209
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public BigInteger getPrimeQ() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   215
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public BigInteger getPrimeExponentP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   221
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public BigInteger getPrimeExponentQ() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return qe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   227
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public BigInteger getCrtCoefficient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return coeff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   232
    // see JCA doc
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   233
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   234
    public AlgorithmParameterSpec getParams() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   235
        return keyParams;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   236
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   237
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   238
    // return a string representation of this key for debugging
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   239
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   240
    public String toString() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   241
        return "SunRsaSign " + getAlgorithm() + " private CRT key, " + n.bitLength()
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   242
               + " bits" + "\n  params: " + keyParams + "\n  modulus: " + n
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   243
               + "\n  private exponent: " + d;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   244
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   245
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Parse the key. Called by PKCS8Key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    protected void parseKeyBits() throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            DerInputStream in = new DerInputStream(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            DerValue derValue = in.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (derValue.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                throw new IOException("Not a SEQUENCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            DerInputStream data = derValue.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            int version = data.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if (version != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                throw new IOException("Version must be 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
44260
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   261
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   262
            /*
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   263
             * Some implementations do not correctly encode ASN.1 INTEGER values
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   264
             * in 2's complement format, resulting in a negative integer when
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   265
             * decoded. Correct the error by converting it to a positive integer.
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   266
             *
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   267
             * See CR 6255949
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   268
             */
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   269
            n = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   270
            e = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   271
            d = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   272
            p = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   273
            q = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   274
            pe = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   275
            qe = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   276
            coeff = data.getPositiveBigInteger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (derValue.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                throw new IOException("Extra data available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throw new InvalidKeyException("Invalid RSA private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
}