jdk/src/share/classes/com/sun/crypto/provider/DHPublicKey.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 7043 5e2d1edeb2c7
child 18809 97f5713a0f1a
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.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.ProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PublicKey;
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A public key in X.509 format for the Diffie-Hellman key agreement 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 DHPrivateKey
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 DHPublicKey implements PublicKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
javax.crypto.interfaces.DHPublicKey, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final long serialVersionUID = 7647557958927458271L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // the public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private BigInteger y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // the key bytes, without the algorithm information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private byte[] key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // the encoded key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private byte[] encodedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // the prime modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private BigInteger p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // the base generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private BigInteger g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // the private-value length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Make a DH public key out of a public value <code>y</code>, a prime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * modulus <code>p</code>, and a base generator <code>g</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param y the public value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param p the prime modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param g the base generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @exception InvalidKeyException if the key cannot be encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    DHPublicKey(BigInteger y, BigInteger p, BigInteger g)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this(y, p, g, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Make a DH public key out of a public value <code>y</code>, a prime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * modulus <code>p</code>, a base generator <code>g</code>, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * private-value length <code>l</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param y the public value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param p the prime modulus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param g the base generator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param l the private-value length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @exception ProviderException if the key cannot be encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    DHPublicKey(BigInteger y, BigInteger p, BigInteger g, int l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.p = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.g = g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.l = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            this.key = new DerValue(DerValue.tag_Integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                    this.y.toByteArray()).toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            this.encodedKey = getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new ProviderException("Cannot produce ASN.1 encoding", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Make a DH public key from its DER encoding (X.509).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param encodedKey the encoded key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @exception InvalidKeyException if the encoded key does not represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * a Diffie-Hellman public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    DHPublicKey(byte[] encodedKey) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        InputStream inStream = new ByteArrayInputStream(encodedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            DerValue derKeyVal = new DerValue(inStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (derKeyVal.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                throw new InvalidKeyException ("Invalid key format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             * Parse the algorithm identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            DerValue algid = derKeyVal.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (algid.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                throw new InvalidKeyException("AlgId is not a SEQUENCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            DerInputStream derInStream = algid.toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            ObjectIdentifier oid = derInStream.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (oid == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                throw new InvalidKeyException("Null OID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (derInStream.available() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                throw new InvalidKeyException("Parameters missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
             * Parse the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            DerValue params = derInStream.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (params.tag == DerValue.tag_Null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new InvalidKeyException("Null parameters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (params.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                throw new InvalidKeyException("Parameters not a SEQUENCE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            params.data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            this.p = params.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            this.g = params.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            // Private-value length is OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (params.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                this.l = params.data.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (params.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                throw new InvalidKeyException("Extra parameter data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
             * Parse the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            this.key = derKeyVal.data.getBitString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            parseKeyBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (derKeyVal.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                throw new InvalidKeyException("Excess key data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   175
            this.encodedKey = encodedKey.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new InvalidKeyException("Private-value length too big");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } catch (IOException e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   181
            throw new InvalidKeyException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   182
                "Error parsing key encoding: " + e.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns the encoding format of this key: "X.509"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return "X.509";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Returns the name of the algorithm associated with this key: "DH"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return "DH";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Get the encoding of the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public synchronized byte[] getEncoded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (this.encodedKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                DerOutputStream algid = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                // store oid in algid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                algid.putOID(new ObjectIdentifier(DH_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                // encode parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                DerOutputStream params = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                params.putInteger(this.p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                params.putInteger(this.g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                if (this.l != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    params.putInteger(this.l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                // wrap parameters into SEQUENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                DerValue paramSequence = new DerValue(DerValue.tag_Sequence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                                      params.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                // store parameter SEQUENCE in algid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                algid.putDerValue(paramSequence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                // wrap algid into SEQUENCE, and store it in key encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                DerOutputStream tmpDerKey = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                tmpDerKey.write(DerValue.tag_Sequence, algid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                // store key data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                tmpDerKey.putBitString(this.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                // wrap algid and key into SEQUENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                DerOutputStream derKey = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                derKey.write(DerValue.tag_Sequence, tmpDerKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                this.encodedKey = derKey.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   238
        return this.encodedKey.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Returns the public value, <code>y</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return the public value, <code>y</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public BigInteger getY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return this.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Returns the key parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return the key parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public DHParameterSpec getParams() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (this.l != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return new DHParameterSpec(this.p, this.g, this.l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return new DHParameterSpec(this.p, this.g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        String LINE_SEP = System.getProperty("line.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        StringBuffer strbuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            = new StringBuffer("SunJCE Diffie-Hellman Public Key:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                               + LINE_SEP + "y:" + LINE_SEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                               + Debug.toHexString(this.y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                               + LINE_SEP + "p:" + LINE_SEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                               + Debug.toHexString(this.p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                               + LINE_SEP + "g:" + LINE_SEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                               + Debug.toHexString(this.g));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (this.l != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            strbuf.append(LINE_SEP + "l:" + LINE_SEP + "    " + this.l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return strbuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private void parseKeyBits() throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            DerInputStream in = new DerInputStream(this.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            this.y = in.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } catch (IOException e) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   283
            throw new InvalidKeyException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   284
                "Error parsing key encoding: " + e.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Calculates a hash code value for the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Objects that are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        byte[] enc = getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        for (int i = 1; i < enc.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            retval += enc[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return(retval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (this == obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (!(obj instanceof PublicKey))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        byte[] thisEncoded = this.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        byte[] thatEncoded = ((PublicKey)obj).getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return java.util.Arrays.equals(thisEncoded, thatEncoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Replace the DH public key to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return the standard KeyRep object to be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @throws java.io.ObjectStreamException if a new object representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * this DH public key could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private Object writeReplace() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return new KeyRep(KeyRep.Type.PUBLIC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        getAlgorithm(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        getFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
}