src/java.base/share/classes/sun/security/rsa/RSAPrivateKeyImpl.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
child 57950 4612a3cfb927
child 58678 9cf78a70fa4f
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.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.*;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
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
/**
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    40
 * RSA private key implementation for "RSA", "RSASSA-PSS" algorithms in non-CRT
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    41
 * form (modulus, private exponent only). For CRT private keys, see
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    42
 * RSAPrivateCrtKeyImpl. We need separate classes to ensure correct behavior
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final long serialVersionUID = -33106691987952810L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final BigInteger n;         // modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final BigInteger d;         // private exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    60
    // optional parameters associated with this RSA key
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    61
    // specified in the encoding of its AlgorithmId.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    62
    // must be null for "RSA" keys.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    63
    private final AlgorithmParameterSpec keyParams;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    64
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Construct a key from its components. Used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * RSAKeyFactory and the RSAKeyPairGenerator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    69
    RSAPrivateKeyImpl(AlgorithmId rsaId, BigInteger n, BigInteger d)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    70
            throws InvalidKeyException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    71
        RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), null);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    72
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.n = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.d = d;
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    75
        this.keyParams = RSAUtil.getParamSpec(rsaId);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    76
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // generate the encoding
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    78
        algid = rsaId;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            out.putInteger(0); // version must be 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            out.putInteger(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            out.putInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            out.putInteger(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            out.putInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            out.putInteger(0);
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
            DerValue val =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                new DerValue(DerValue.tag_Sequence, out.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            key = val.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch (IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new InvalidKeyException(exc);
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
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   100
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public String getAlgorithm() {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   102
        return algid.getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   106
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public BigInteger getModulus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // see JCA doc
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   112
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public BigInteger getPrivateExponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   116
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   117
    // see JCA doc
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   118
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   119
    public AlgorithmParameterSpec getParams() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   120
        return keyParams;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   121
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   122
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   123
    // return a string representation of this key for debugging
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   124
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   125
    public String toString() {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   126
        return "Sun " + getAlgorithm() + " private key, " + n.bitLength()
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   127
               + " bits" + "\n  params: " + keyParams + "\n  modulus: " + n
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   128
               + "\n  private exponent: " + d;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   129
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}