src/java.base/share/classes/sun/security/rsa/RSAPrivateKeyImpl.java
author darcy
Wed, 09 Oct 2019 09:57:41 -0700
changeset 58519 6e017b301287
parent 57950 4612a3cfb927
child 58679 9c3209ff7550
permissions -rw-r--r--
8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes Reviewed-by: mullan, chegar
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: 50204
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.AlgorithmParameterSpec;
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
import sun.security.x509.AlgorithmId;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.pkcs.PKCS8Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    40
 * RSA private key implementation for "RSA", "RSASSA-PSS" algorithms in non-CRT
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    41
 * form (modulus, private exponent only). For CRT private keys, see
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    42
 * RSAPrivateCrtKeyImpl. We need separate classes to ensure correct behavior
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    43
 * in instanceof checks, etc.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Note: RSA keys must be at least 512 bits long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see RSAPrivateCrtKeyImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see RSAKeyFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public final class RSAPrivateKeyImpl extends PKCS8Key implements RSAPrivateKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 50204
diff changeset
    55
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final long serialVersionUID = -33106691987952810L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final BigInteger n;         // modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final BigInteger d;         // private exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    61
    // optional parameters associated with this RSA key
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    62
    // specified in the encoding of its AlgorithmId.
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    63
    // 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
    64
    @SuppressWarnings("serial") // Not statically typed as Serializable
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    65
    private final AlgorithmParameterSpec keyParams;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    66
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Construct a key from its components. Used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * RSAKeyFactory and the RSAKeyPairGenerator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    71
    RSAPrivateKeyImpl(AlgorithmId rsaId, BigInteger n, BigInteger d)
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    72
            throws InvalidKeyException {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    73
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), null);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    74
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.n = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.d = d;
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    77
        this.keyParams = RSAUtil.getParamSpec(rsaId);
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    78
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // generate the encoding
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
    80
        algid = rsaId;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            out.putInteger(0); // version must be 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            out.putInteger(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            out.putInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            out.putInteger(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            out.putInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            out.putInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            out.putInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            out.putInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            out.putInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            DerValue val =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                new DerValue(DerValue.tag_Sequence, out.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            key = val.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new InvalidKeyException(exc);
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
    // see JCA doc
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   102
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public String getAlgorithm() {
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   104
        return algid.getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // see JCA doc
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   108
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // see JCA doc
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   114
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public BigInteger getPrivateExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
50204
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   118
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   119
    // see JCA doc
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   120
    @Override
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   121
    public AlgorithmParameterSpec getParams() {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   122
        return keyParams;
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
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   125
    // return a string representation of this key for debugging
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   126
    @Override
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   127
    public String toString() {
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   128
        return "Sun " + getAlgorithm() + " private key, " + n.bitLength()
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   129
               + " bits" + "\n  params: " + keyParams + "\n  modulus: " + n
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   130
               + "\n  private exponent: " + d;
3195a713e24d 8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents: 47216
diff changeset
   131
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}