Path: blob/master/src/java.base/share/classes/sun/security/provider/DSAParameters.java
41159 views
/*1* Copyright (c) 1997, 2011, 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 sun.security.provider;2627import java.io.*;28import java.math.BigInteger;29import java.security.AlgorithmParametersSpi;30import java.security.spec.AlgorithmParameterSpec;31import java.security.spec.DSAParameterSpec;32import java.security.spec.InvalidParameterSpecException;3334import sun.security.util.Debug;35import sun.security.util.DerValue;36import sun.security.util.DerOutputStream;3738/**39* This class implements the parameter set used by the40* Digital Signature Algorithm as specified in the FIPS 18641* standard.42*43* @author Jan Luehe44*45*46* @since 1.247*/4849public class DSAParameters extends AlgorithmParametersSpi {5051// the prime (p)52protected BigInteger p;5354// the sub-prime (q)55protected BigInteger q;5657// the base (g)58protected BigInteger g;5960protected void engineInit(AlgorithmParameterSpec paramSpec)61throws InvalidParameterSpecException {62if (!(paramSpec instanceof DSAParameterSpec)) {63throw new InvalidParameterSpecException64("Inappropriate parameter specification");65}66this.p = ((DSAParameterSpec)paramSpec).getP();67this.q = ((DSAParameterSpec)paramSpec).getQ();68this.g = ((DSAParameterSpec)paramSpec).getG();69}7071protected void engineInit(byte[] params) throws IOException {72DerValue encodedParams = new DerValue(params);7374if (encodedParams.tag != DerValue.tag_Sequence) {75throw new IOException("DSA params parsing error");76}7778encodedParams.data.reset();7980this.p = encodedParams.data.getBigInteger();81this.q = encodedParams.data.getBigInteger();82this.g = encodedParams.data.getBigInteger();8384if (encodedParams.data.available() != 0) {85throw new IOException("encoded params have " +86encodedParams.data.available() +87" extra bytes");88}89}9091protected void engineInit(byte[] params, String decodingMethod)92throws IOException {93engineInit(params);94}9596protected <T extends AlgorithmParameterSpec>97T engineGetParameterSpec(Class<T> paramSpec)98throws InvalidParameterSpecException99{100try {101Class<?> dsaParamSpec = Class.forName102("java.security.spec.DSAParameterSpec");103if (dsaParamSpec.isAssignableFrom(paramSpec)) {104return paramSpec.cast(105new DSAParameterSpec(this.p, this.q, this.g));106} else {107throw new InvalidParameterSpecException108("Inappropriate parameter Specification");109}110} catch (ClassNotFoundException e) {111throw new InvalidParameterSpecException112("Unsupported parameter specification: " + e.getMessage());113}114}115116protected byte[] engineGetEncoded() throws IOException {117DerOutputStream out = new DerOutputStream();118DerOutputStream bytes = new DerOutputStream();119120bytes.putInteger(p);121bytes.putInteger(q);122bytes.putInteger(g);123out.write(DerValue.tag_Sequence, bytes);124return out.toByteArray();125}126127protected byte[] engineGetEncoded(String encodingMethod)128throws IOException {129return engineGetEncoded();130}131132/*133* Returns a formatted string describing the parameters.134*/135protected String engineToString() {136return "\n\tp: " + Debug.toHexString(p)137+ "\n\tq: " + Debug.toHexString(q)138+ "\n\tg: " + Debug.toHexString(g)139+ "\n";140}141}142143144