jdk/src/share/classes/com/sun/crypto/provider/DHParameters.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 5506 202f599c92aa
child 24578 bbf15045dfbb
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: 5506
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 sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AlgorithmParametersSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.spec.InvalidParameterSpecException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.spec.DHParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class implements the parameter set used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Diffie-Hellman key agreement as defined in the PKCS #3 standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public final class DHParameters extends AlgorithmParametersSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // The prime (p).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private BigInteger p = BigInteger.ZERO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // The base (g).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private BigInteger g = BigInteger.ZERO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // The private-value length (l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected void engineInit(AlgorithmParameterSpec paramSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        throws InvalidParameterSpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            if (!(paramSpec instanceof DHParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    ("Inappropriate parameter specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            this.p = ((DHParameterSpec)paramSpec).getP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            this.g = ((DHParameterSpec)paramSpec).getG();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            this.l = ((DHParameterSpec)paramSpec).getL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected void engineInit(byte[] params) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            DerValue encodedParams = new DerValue(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (encodedParams.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                throw new IOException("DH params parsing error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            encodedParams.data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            this.p = encodedParams.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            this.g = encodedParams.data.getBigInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            // Private-value length is OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (encodedParams.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                this.l = encodedParams.data.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (encodedParams.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    ("DH parameter parsing error: Extra data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new IOException("Private-value length too big");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected void engineInit(byte[] params, String decodingMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            engineInit(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    97
    protected <T extends AlgorithmParameterSpec>
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    98
        T engineGetParameterSpec(Class<T> paramSpec)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        throws InvalidParameterSpecException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (DHParameterSpec.class.isAssignableFrom(paramSpec)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   102
            return paramSpec.cast(new DHParameterSpec(this.p, this.g, this.l));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new InvalidParameterSpecException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                ("Inappropriate parameter Specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected byte[] engineGetEncoded() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        bytes.putInteger(this.p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        bytes.putInteger(this.g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // Private-value length is OPTIONAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (this.l > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            bytes.putInteger(this.l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        out.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    protected byte[] engineGetEncoded(String encodingMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return engineGetEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Returns a formatted string describing the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    protected String engineToString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        String LINE_SEP = System.getProperty("line.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        StringBuffer strbuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            = new StringBuffer("SunJCE Diffie-Hellman Parameters:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                               + LINE_SEP + "p:" + LINE_SEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                               + Debug.toHexString(this.p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                               + LINE_SEP + "g:" + LINE_SEP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                               + Debug.toHexString(this.g));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (this.l != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            strbuf.append(LINE_SEP + "l:" + LINE_SEP + "    " + this.l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return strbuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}