Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/x509/AlgIdDSA.java
38831 views
/*1* Copyright (c) 1996, 2013, 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.x509;2627import java.io.IOException;28import java.math.BigInteger;29import java.security.*;30import java.security.interfaces.DSAParams;3132import sun.security.util.*;333435/**36* This class identifies DSS/DSA Algorithm variants, which are distinguished37* by using different algorithm parameters <em>P, Q, G</em>. It uses the38* NIST/IETF standard DER encoding. These are used to implement the Digital39* Signature Standard (DSS), FIPS 186.40*41* <P><em><b>NOTE:</b> DSS/DSA Algorithm IDs may be created without these42* parameters. Use of DSS/DSA in modes where parameters are43* either implicit (e.g. a default applicable to a site or a larger scope),44* or are derived from some Certificate Authority's DSS certificate, is45* not supported directly. The application is responsible for creating a key46* containing the required parameters prior to using the key in cryptographic47* operations. The follwoing is an example of how this may be done assuming48* that we have a certificate called <code>currentCert</code> which doesn't49* contain DSS/DSA parameters and we need to derive DSS/DSA parameters50* from a CA's certificate called <code>caCert</code>.51* <p>52* <code><pre>53* // key containing parameters to use54* DSAPublicKey cAKey = (DSAPublicKey)(caCert.getPublicKey());55* // key without parameters56* DSAPublicKey nullParamsKey = (DSAPublicKey)(currentCert.getPublicKey());57*58* DSAParams cAKeyParams = cAKey.getParams();59* KeyFactory kf = KeyFactory.getInstance("DSA");60* DSAPublicKeySpec ks = new DSAPublicKeySpec(nullParamsKey.getY(),61* cAKeyParams.getP(),62* cAKeyParams.getQ(),63* cAKeyParams.getG());64* DSAPublicKey usableKey = kf.generatePublic(ks);65* </pre></code>66*67* @see java.security.interfaces.DSAParams68* @see java.security.interfaces.DSAPublicKey69* @see java.security.KeyFactory70* @see java.security.spec.DSAPublicKeySpec71*72* @author David Brownell73*/74public final75class AlgIdDSA extends AlgorithmId implements DSAParams76{7778private static final long serialVersionUID = 3437177836797504046L;7980/*81* The three unsigned integer parameters.82*/83private BigInteger p , q, g;8485/** Returns the DSS/DSA parameter "P" */86public BigInteger getP () { return p; }8788/** Returns the DSS/DSA parameter "Q" */89public BigInteger getQ () { return q; }9091/** Returns the DSS/DSA parameter "G" */92public BigInteger getG () { return g; }9394/**95* Default constructor. The OID and parameters must be96* deserialized before this algorithm ID is used.97*/98@Deprecated99public AlgIdDSA () {}100101AlgIdDSA (DerValue val) throws IOException102{ super(val.getOID()); }103104/**105* Construct an AlgIdDSA from an X.509 encoded byte array.106*/107public AlgIdDSA (byte[] encodedAlg) throws IOException108{ super (new DerValue(encodedAlg).getOID()); }109110/**111* Constructs a DSS/DSA Algorithm ID from unsigned integers that112* define the algorithm parameters. Those integers are encoded113* as big-endian byte arrays.114*115* @param p the DSS/DSA parameter "P"116* @param q the DSS/DSA parameter "Q"117* @param g the DSS/DSA parameter "G"118*/119public AlgIdDSA (byte p [], byte q [], byte g [])120throws IOException121{122this (new BigInteger (1, p),123new BigInteger (1, q),124new BigInteger (1, g));125}126127/**128* Constructs a DSS/DSA Algorithm ID from numeric parameters.129* If all three are null, then the parameters portion of the algorithm id130* is set to null. See note in header regarding use.131*132* @param p the DSS/DSA parameter "P"133* @param q the DSS/DSA parameter "Q"134* @param g the DSS/DSA parameter "G"135*/136public AlgIdDSA (BigInteger p, BigInteger q, BigInteger g)137{138super (DSA_oid);139140if (p != null || q != null || g != null) {141if (p == null || q == null || g == null)142throw new ProviderException("Invalid parameters for DSS/DSA" +143" Algorithm ID");144try {145this.p = p;146this.q = q;147this.g = g;148initializeParams ();149150} catch (IOException e) {151/* this should not happen */152throw new ProviderException ("Construct DSS/DSA Algorithm ID");153}154}155}156157/**158* Returns "DSA", indicating the Digital Signature Algorithm (DSA) as159* defined by the Digital Signature Standard (DSS), FIPS 186.160*/161public String getName ()162{ return "DSA"; }163164165/*166* For algorithm IDs which haven't been created from a DER encoded167* value, "params" must be created.168*/169private void initializeParams ()170throws IOException171{172DerOutputStream out = new DerOutputStream ();173174out.putInteger(p);175out.putInteger(q);176out.putInteger(g);177params = new DerValue (DerValue.tag_Sequence,out.toByteArray ());178}179180/**181* Parses algorithm parameters P, Q, and G. They're found182* in the "params" member, which never needs to be changed.183*/184protected void decodeParams ()185throws IOException186{187if (params == null)188throw new IOException("DSA alg params are null");189if (params.tag != DerValue.tag_Sequence)190throw new IOException("DSA alg parsing error");191192params.data.reset ();193194this.p = params.data.getBigInteger();195this.q = params.data.getBigInteger();196this.g = params.data.getBigInteger();197198if (params.data.available () != 0)199throw new IOException ("AlgIdDSA params, extra="+200params.data.available ());201}202203204/*205* Returns a formatted string describing the parameters.206*/207public String toString ()208{ return paramsToString (); }209210/*211* Returns a string describing the parameters.212*/213protected String paramsToString ()214{215if (params == null)216return " null\n";217else218return219"\n p:\n" + Debug.toHexString(p) +220"\n q:\n" + Debug.toHexString(q) +221"\n g:\n" + Debug.toHexString(g) +222"\n";223}224}225226227