jdk/src/share/classes/com/sun/crypto/provider/DHPrivateKey.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 7043 5e2d1edeb2c7
child 18809 97f5713a0f1a
child 20813 0ad12d66a652
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
     2
 * Copyright (c) 1997, 2011, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 com.sun.crypto.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.KeyRep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.ProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.spec.DHParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A private key in PKCS#8 format for the Diffie-Hellman key agreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see DHPublicKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see java.security.KeyAgreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
final class DHPrivateKey implements PrivateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
javax.crypto.interfaces.DHPrivateKey, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final long serialVersionUID = 7565477590005668886L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // only supported version of PKCS#8 PrivateKeyInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final BigInteger PKCS8_VERSION = BigInteger.ZERO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private BigInteger x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // the key bytes, without the algorithm information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private byte[] key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // the encoded key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private byte[] encodedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // the prime modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private BigInteger p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // the base generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private BigInteger g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // the private-value length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private int l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Make a DH private key out of a private value <code>x</code>, a prime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * modulus <code>p</code>, and a base generator <code>g</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param x the private value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param p the prime modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param g the base generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @exception ProviderException if the key cannot be encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    DHPrivateKey(BigInteger x, BigInteger p, BigInteger g)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this(x, p, g, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Make a DH private key out of a private value <code>x</code>, a prime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * modulus <code>p</code>, a base generator <code>g</code>, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * private-value length <code>l</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param x the private value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param p the prime modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param g the base generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param l the private-value length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @exception InvalidKeyException if the key cannot be encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    DHPrivateKey(BigInteger x, BigInteger p, BigInteger g, int l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.p = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.g = g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.l = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            this.key = new DerValue(DerValue.tag_Integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                    this.x.toByteArray()).toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            this.encodedKey = getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new ProviderException("Cannot produce ASN.1 encoding", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Make a DH private key from its DER encoding (PKCS #8).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param encodedKey the encoded key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @exception InvalidKeyException if the encoded key does not represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * a Diffie-Hellman private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    DHPrivateKey(byte[] encodedKey) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        InputStream inStream = new ByteArrayInputStream(encodedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            DerValue val = new DerValue(inStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (val.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                throw new InvalidKeyException ("Key not a SEQUENCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            // version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            BigInteger parsedVersion = val.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (!parsedVersion.equals(PKCS8_VERSION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                throw new IOException("version mismatch: (supported: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                      PKCS8_VERSION + ", parsed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                      parsedVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // privateKeyAlgorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            DerValue algid = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (algid.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                throw new InvalidKeyException("AlgId is not a SEQUENCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            DerInputStream derInStream = algid.toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            ObjectIdentifier oid = derInStream.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (oid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                throw new InvalidKeyException("Null OID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (derInStream.available() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                throw new InvalidKeyException("Parameters missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            // parse the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            DerValue params = derInStream.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (params.tag == DerValue.tag_Null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                throw new InvalidKeyException("Null parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (params.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                throw new InvalidKeyException("Parameters not a SEQUENCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            params.data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            this.p = params.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            this.g = params.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // Private-value length is OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (params.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                this.l = params.data.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (params.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                throw new InvalidKeyException("Extra parameter data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            // privateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            this.key = val.data.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            parseKeyBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            // ignore OPTIONAL attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   184
            this.encodedKey = encodedKey.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            InvalidKeyException ike = new InvalidKeyException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                "Private-value length too big");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            ike.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw ike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } catch (IOException e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   192
            InvalidKeyException ike = new InvalidKeyException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   193
                "Error parsing key encoding: " + e.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            ike.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw ike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Returns the encoding format of this key: "PKCS#8"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return "PKCS#8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Returns the name of the algorithm associated with this key: "DH"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return "DH";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Get the encoding of the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public synchronized byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (this.encodedKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                // version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                tmp.putInteger(PKCS8_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                // privateKeyAlgorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                DerOutputStream algid = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                // store OID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                algid.putOID(new ObjectIdentifier(DH_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                // encode parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                DerOutputStream params = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                params.putInteger(this.p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                params.putInteger(this.g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                if (this.l != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    params.putInteger(this.l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                // wrap parameters into SEQUENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                DerValue paramSequence = new DerValue(DerValue.tag_Sequence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                                      params.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                // store parameter SEQUENCE in algid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                algid.putDerValue(paramSequence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                // wrap algid into SEQUENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                tmp.write(DerValue.tag_Sequence, algid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                // privateKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                tmp.putOctetString(this.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                // make it a SEQUENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                DerOutputStream derKey = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                derKey.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                this.encodedKey = derKey.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   258
        return this.encodedKey.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns the private value, <code>x</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return the private value, <code>x</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public BigInteger getX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return this.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Returns the key parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return the key parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public DHParameterSpec getParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (this.l != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return new DHParameterSpec(this.p, this.g, this.l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return new DHParameterSpec(this.p, this.g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        String LINE_SEP = System.getProperty("line.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        StringBuffer strbuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            = new StringBuffer("SunJCE Diffie-Hellman Private Key:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                               + LINE_SEP + "x:" + LINE_SEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                               + Debug.toHexString(this.x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                               + LINE_SEP + "p:" + LINE_SEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                               + Debug.toHexString(this.p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                               + LINE_SEP + "g:" + LINE_SEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                               + Debug.toHexString(this.g));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (this.l != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            strbuf.append(LINE_SEP + "l:" + LINE_SEP + "    " + this.l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return strbuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private void parseKeyBits() throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            DerInputStream in = new DerInputStream(this.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            this.x = in.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } catch (IOException e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   303
            InvalidKeyException ike = new InvalidKeyException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   304
                "Error parsing key encoding: " + e.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            ike.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            throw ike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Calculates a hash code value for the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Objects that are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        byte[] enc = getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        for (int i = 1; i < enc.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            retval += enc[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return(retval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (this == obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (!(obj instanceof PrivateKey))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        byte[] thisEncoded = this.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        byte[] thatEncoded = ((PrivateKey)obj).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return java.util.Arrays.equals(thisEncoded, thatEncoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Replace the DH private key to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @return the standard KeyRep object to be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @throws java.io.ObjectStreamException if a new object representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * this DH private key could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    private Object writeReplace() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return new KeyRep(KeyRep.Type.PRIVATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        getFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
}