Path: blob/master/src/java.base/share/classes/sun/security/x509/AlgIdDSA.java
41159 views
/*1* Copyright (c) 1996, 2021, 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></em> 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*52* <pre>{@code53* // 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>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{7778@java.io.Serial79private static final long serialVersionUID = 3437177836797504046L;8081/*82* The three unsigned integer parameters.83*/84private BigInteger p , q, g;8586/** Returns the DSS/DSA parameter "P" */87public BigInteger getP () { return p; }8889/** Returns the DSS/DSA parameter "Q" */90public BigInteger getQ () { return q; }9192/** Returns the DSS/DSA parameter "G" */93public BigInteger getG () { return g; }9495/**96* Default constructor. The OID and parameters must be97* deserialized before this algorithm ID is used.98*/99@Deprecated100public AlgIdDSA () {}101102/**103* Constructs a DSS/DSA Algorithm ID from numeric parameters.104* If all three are null, then the parameters portion of the algorithm id105* is set to null. See note in header regarding use.106*107* @param p the DSS/DSA parameter "P"108* @param q the DSS/DSA parameter "Q"109* @param g the DSS/DSA parameter "G"110*/111public AlgIdDSA (BigInteger p, BigInteger q, BigInteger g) {112super (DSA_oid);113114if (p != null || q != null || g != null) {115if (p == null || q == null || g == null)116throw new ProviderException("Invalid parameters for DSS/DSA" +117" Algorithm ID");118try {119this.p = p;120this.q = q;121this.g = g;122initializeParams ();123124} catch (IOException e) {125/* this should not happen */126throw new ProviderException ("Construct DSS/DSA Algorithm ID");127}128}129}130131/**132* Returns "DSA", indicating the Digital Signature Algorithm (DSA) as133* defined by the Digital Signature Standard (DSS), FIPS 186.134*/135public String getName ()136{ return "DSA"; }137138139/*140* For algorithm IDs which haven't been created from a DER encoded141* value, "params" must be created.142*/143private void initializeParams () throws IOException {144DerOutputStream out = new DerOutputStream();145out.putInteger(p);146out.putInteger(q);147out.putInteger(g);148DerOutputStream result = new DerOutputStream();149result.write(DerValue.tag_Sequence, out);150encodedParams = result.toByteArray();151}152153/**154* Parses algorithm parameters P, Q, and G. They're found155* in the "params" member, which never needs to be changed.156*/157protected void decodeParams () throws IOException {158if (encodedParams == null) {159throw new IOException("DSA alg params are null");160}161162DerValue params = new DerValue(encodedParams);163if (params.tag != DerValue.tag_Sequence) {164throw new IOException("DSA alg parsing error");165}166167params.data.reset ();168169this.p = params.data.getBigInteger();170this.q = params.data.getBigInteger();171this.g = params.data.getBigInteger();172173if (params.data.available () != 0)174throw new IOException ("AlgIdDSA params, extra="+175params.data.available ());176}177178179/*180* Returns a formatted string describing the parameters.181*/182public String toString () {183return paramsToString();184}185186/*187* Returns a string describing the parameters.188*/189protected String paramsToString () {190if (encodedParams == null) {191return " null\n";192} else {193return "\n p:\n" + Debug.toHexString(p) +194"\n q:\n" + Debug.toHexString(q) +195"\n g:\n" + Debug.toHexString(g) +196"\n";197}198}199}200201202