Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/DHParameters.java
41161 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.crypto.provider;2627import java.io.*;28import sun.security.util.*;29import java.math.BigInteger;30import java.security.AlgorithmParametersSpi;31import java.security.spec.AlgorithmParameterSpec;32import java.security.spec.InvalidParameterSpecException;33import javax.crypto.spec.DHParameterSpec;3435/**36* This class implements the parameter set used by the37* Diffie-Hellman key agreement as defined in the PKCS #3 standard.38*39* @author Jan Luehe40*41*/42public final class DHParameters extends AlgorithmParametersSpi {4344// The prime (p).45private BigInteger p = BigInteger.ZERO;4647// The base (g).48private BigInteger g = BigInteger.ZERO;4950// The private-value length (l)51private int l = 0;5253protected void engineInit(AlgorithmParameterSpec paramSpec)54throws InvalidParameterSpecException {55if (!(paramSpec instanceof DHParameterSpec)) {56throw new InvalidParameterSpecException57("Inappropriate parameter specification");58}59this.p = ((DHParameterSpec)paramSpec).getP();60this.g = ((DHParameterSpec)paramSpec).getG();61this.l = ((DHParameterSpec)paramSpec).getL();62}6364protected void engineInit(byte[] params) throws IOException {65try {66DerValue encodedParams = new DerValue(params);6768if (encodedParams.tag != DerValue.tag_Sequence) {69throw new IOException("DH params parsing error");70}7172encodedParams.data.reset();7374this.p = encodedParams.data.getBigInteger();75this.g = encodedParams.data.getBigInteger();7677// Private-value length is OPTIONAL78if (encodedParams.data.available() != 0) {79this.l = encodedParams.data.getInteger();80}8182if (encodedParams.data.available() != 0) {83throw new IOException84("DH parameter parsing error: Extra data");85}86} catch (NumberFormatException e) {87throw new IOException("Private-value length too big");88}89}9091protected void engineInit(byte[] params, String decodingMethod)92throws IOException {93engineInit(params);94}9596protected <T extends AlgorithmParameterSpec>97T engineGetParameterSpec(Class<T> paramSpec)98throws InvalidParameterSpecException {99100if (DHParameterSpec.class.isAssignableFrom(paramSpec)) {101return paramSpec.cast(new DHParameterSpec(this.p, this.g, this.l));102} else {103throw new InvalidParameterSpecException104("Inappropriate parameter Specification");105}106}107108protected byte[] engineGetEncoded() throws IOException {109DerOutputStream out = new DerOutputStream();110DerOutputStream bytes = new DerOutputStream();111112bytes.putInteger(this.p);113bytes.putInteger(this.g);114// Private-value length is OPTIONAL115if (this.l > 0) {116bytes.putInteger(this.l);117}118out.write(DerValue.tag_Sequence, bytes);119return out.toByteArray();120}121122protected byte[] engineGetEncoded(String encodingMethod)123throws IOException {124return engineGetEncoded();125}126127/*128* Returns a formatted string describing the parameters.129*/130protected String engineToString() {131String LINE_SEP = System.lineSeparator();132133StringBuilder sb134= new StringBuilder("SunJCE Diffie-Hellman Parameters:"135+ LINE_SEP + "p:" + LINE_SEP136+ Debug.toHexString(this.p)137+ LINE_SEP + "g:" + LINE_SEP138+ Debug.toHexString(this.g));139if (this.l != 0)140sb.append(LINE_SEP + "l:" + LINE_SEP + " " + this.l);141return sb.toString();142}143}144145146