src/java.base/share/classes/sun/security/rsa/RSAPrivateCrtKeyImpl.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58519 6e017b301287
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 50918
diff changeset
     2
 * Copyright (c) 2003, 2019, 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.*;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
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.*;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
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
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    40
import static sun.security.rsa.RSAUtil.KeyType;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    43
 * RSA private key implementation for "RSA", "RSASSA-PSS" algorithms in CRT form.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    44
 * For non-CRT private keys, see RSAPrivateKeyImpl. We need separate classes
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
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
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 50918
diff changeset
    58
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final long serialVersionUID = -1326088454257084918L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private BigInteger n;       // modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private BigInteger e;       // public exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private BigInteger d;       // private exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private BigInteger p;       // prime p
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private BigInteger q;       // prime q
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private BigInteger pe;      // prime exponent p
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private BigInteger qe;      // prime exponent q
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private BigInteger coeff;   // CRT coeffcient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    70
    // Optional parameters associated with this RSA key
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    71
    // specified in the encoding of its AlgorithmId.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    72
    // Must be null for "RSA" keys.
58519
6e017b301287 8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes
darcy
parents: 57950
diff changeset
    73
    @SuppressWarnings("serial") // Not statically typed as Serializable
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    74
    private AlgorithmParameterSpec keyParams;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Generate a new key from its encoding. Returns a CRT key if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * and a non-CRT key otherwise. Used by RSAKeyFactory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static RSAPrivateKey newKey(byte[] encoded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        RSAPrivateCrtKeyImpl key = new RSAPrivateCrtKeyImpl(encoded);
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    83
        // check all CRT-specific components are available, if any one
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    84
        // missing, return a non-CRT key instead
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    85
        if ((key.getPublicExponent().signum() == 0) ||
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    86
            (key.getPrimeExponentP().signum() == 0) ||
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    87
            (key.getPrimeExponentQ().signum() == 0) ||
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    88
            (key.getPrimeP().signum() == 0) ||
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    89
            (key.getPrimeQ().signum() == 0) ||
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    90
            (key.getCrtCoefficient().signum() == 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return new RSAPrivateKeyImpl(
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    92
                key.algid,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                key.getModulus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                key.getPrivateExponent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   102
     * Generate a new key from the specified type and components.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   103
     * Returns a CRT key if possible and a non-CRT key otherwise.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   104
     * Used by SunPKCS11 provider.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   105
     */
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   106
    public static RSAPrivateKey newKey(KeyType type,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   107
            AlgorithmParameterSpec params,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   108
            BigInteger n, BigInteger e, BigInteger d,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   109
            BigInteger p, BigInteger q, BigInteger pe, BigInteger qe,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   110
            BigInteger coeff) throws InvalidKeyException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   111
        RSAPrivateKey key;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   112
        AlgorithmId rsaId = RSAUtil.createAlgorithmId(type, params);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   113
        if ((e.signum() == 0) || (p.signum() == 0) ||
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   114
            (q.signum() == 0) || (pe.signum() == 0) ||
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   115
            (qe.signum() == 0) || (coeff.signum() == 0)) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   116
            // if any component is missing, return a non-CRT key
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   117
            return new RSAPrivateKeyImpl(rsaId, n, d);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   118
        } else {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   119
            return new RSAPrivateCrtKeyImpl(rsaId, n, e, d,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   120
                p, q, pe, qe, coeff);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   121
        }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   122
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   123
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   124
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Construct a key from its encoding. Called from newKey above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    RSAPrivateCrtKeyImpl(byte[] encoded) throws InvalidKeyException {
50918
ebff24bd9302 8205720: KeyFactory#getKeySpec and translateKey thorws NullPointerException with Invalid key
valeriep
parents: 50204
diff changeset
   128
        if (encoded == null || encoded.length == 0) {
ebff24bd9302 8205720: KeyFactory#getKeySpec and translateKey thorws NullPointerException with Invalid key
valeriep
parents: 50204
diff changeset
   129
            throw new InvalidKeyException("Missing key encoding");
ebff24bd9302 8205720: KeyFactory#getKeySpec and translateKey thorws NullPointerException with Invalid key
valeriep
parents: 50204
diff changeset
   130
        }
ebff24bd9302 8205720: KeyFactory#getKeySpec and translateKey thorws NullPointerException with Invalid key
valeriep
parents: 50204
diff changeset
   131
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        decode(encoded);
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   133
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   134
        try {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   135
            // this will check the validity of params
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   136
            this.keyParams = RSAUtil.getParamSpec(algid);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   137
        } catch (ProviderException e) {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   138
            throw new InvalidKeyException(e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   139
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   143
     * Construct a RSA key from its components. Used by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * RSAKeyFactory and the RSAKeyPairGenerator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   146
    RSAPrivateCrtKeyImpl(AlgorithmId rsaId,
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   147
            BigInteger n, BigInteger e, BigInteger d,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            BigInteger p, BigInteger q, BigInteger pe, BigInteger qe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            BigInteger coeff) throws InvalidKeyException {
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   150
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   151
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.n = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this.e = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.d = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.p = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.q = q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this.pe = pe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.qe = qe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this.coeff = coeff;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   160
        this.keyParams = RSAUtil.getParamSpec(rsaId);
2596
a1964c157e68 6497740: Limit the size of RSA public keys
wetmore
parents: 2
diff changeset
   161
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // generate the encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        algid = rsaId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            out.putInteger(0); // version must be 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            out.putInteger(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            out.putInteger(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            out.putInteger(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            out.putInteger(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            out.putInteger(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            out.putInteger(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            out.putInteger(qe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            out.putInteger(coeff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            DerValue val =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                new DerValue(DerValue.tag_Sequence, out.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            key = val.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new InvalidKeyException(exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
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
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   185
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public String getAlgorithm() {
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   187
        return algid.getName();
2
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
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   191
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return n;
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
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   197
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public BigInteger getPublicExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return e;
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
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   203
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public BigInteger getPrivateExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return d;
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
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   209
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public BigInteger getPrimeP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return p;
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
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   215
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public BigInteger getPrimeQ() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return q;
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
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   221
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public BigInteger getPrimeExponentP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return pe;
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
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   227
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public BigInteger getPrimeExponentQ() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return qe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    // see JCA doc
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   233
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public BigInteger getCrtCoefficient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return coeff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   238
    // see JCA doc
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   239
    @Override
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   240
    public AlgorithmParameterSpec getParams() {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   241
        return keyParams;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   242
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   243
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   244
    // return a string representation of this key for debugging
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   245
    @Override
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   246
    public String toString() {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   247
        return "SunRsaSign " + getAlgorithm() + " private CRT key, " + n.bitLength()
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   248
               + " bits" + "\n  params: " + keyParams + "\n  modulus: " + n
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   249
               + "\n  private exponent: " + d;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   250
    }
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   251
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Parse the key. Called by PKCS8Key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    protected void parseKeyBits() throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            DerInputStream in = new DerInputStream(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            DerValue derValue = in.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (derValue.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                throw new IOException("Not a SEQUENCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            DerInputStream data = derValue.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            int version = data.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (version != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                throw new IOException("Version must be 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
44260
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   267
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
             * 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
   270
             * 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
   271
             * 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
   272
             *
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   273
             * See CR 6255949
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   274
             */
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   275
            n = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   276
            e = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   277
            d = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   278
            p = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   279
            q = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   280
            pe = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   281
            qe = data.getPositiveBigInteger();
dd947f766e11 8175251: Failed to load RSA private key from pkcs12
valeriep
parents: 32649
diff changeset
   282
            coeff = data.getPositiveBigInteger();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (derValue.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                throw new IOException("Extra data available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            throw new InvalidKeyException("Invalid RSA private key", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
}