0N/A/*
2362N/A * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.provider;
0N/A
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/Aimport java.math.BigInteger;
0N/Aimport java.security.InvalidKeyException;
0N/Aimport java.security.ProviderException;
0N/Aimport java.security.AlgorithmParameters;
0N/Aimport java.security.spec.DSAParameterSpec;
0N/Aimport java.security.spec.InvalidParameterSpecException;
0N/Aimport java.security.interfaces.DSAParams;
0N/A
0N/Aimport sun.security.x509.X509Key;
0N/Aimport sun.security.x509.AlgIdDSA;
0N/Aimport sun.security.util.Debug;
0N/Aimport sun.security.util.DerValue;
0N/Aimport sun.security.util.DerInputStream;
0N/Aimport sun.security.util.DerOutputStream;
0N/A
0N/A/**
0N/A * An X.509 public key for the Digital Signature Algorithm.
0N/A *
0N/A * @author Benjamin Renaud
0N/A *
0N/A *
0N/A * @see DSAPrivateKey
0N/A * @see AlgIdDSA
0N/A * @see DSA
0N/A */
0N/A
0N/Apublic class DSAPublicKey extends X509Key
0N/Aimplements java.security.interfaces.DSAPublicKey, Serializable {
0N/A
0N/A /** use serialVersionUID from JDK 1.1. for interoperability */
0N/A private static final long serialVersionUID = -2994193307391104133L;
0N/A
0N/A /* the public key */
0N/A private BigInteger y;
0N/A
0N/A /*
0N/A * Keep this constructor for backwards compatibility with JDK1.1.
0N/A */
0N/A public DSAPublicKey() {
0N/A }
0N/A
0N/A /**
0N/A * Make a DSA public key out of a public key and three parameters.
0N/A * The p, q, and g parameters may be null, but if so, parameters will need
0N/A * to be supplied from some other source before this key can be used in
0N/A * cryptographic operations. PKIX RFC2459bis explicitly allows DSA public
0N/A * keys without parameters, where the parameters are provided in the
0N/A * issuer's DSA public key.
0N/A *
0N/A * @param y the actual key bits
0N/A * @param p DSA parameter p, may be null if all of p, q, and g are null.
0N/A * @param q DSA parameter q, may be null if all of p, q, and g are null.
0N/A * @param g DSA parameter g, may be null if all of p, q, and g are null.
0N/A */
0N/A public DSAPublicKey(BigInteger y, BigInteger p, BigInteger q,
0N/A BigInteger g)
0N/A throws InvalidKeyException {
0N/A this.y = y;
0N/A algid = new AlgIdDSA(p, q, g);
0N/A
0N/A try {
0N/A key = new DerValue(DerValue.tag_Integer,
0N/A y.toByteArray()).toByteArray();
0N/A encode();
0N/A } catch (IOException e) {
0N/A throw new InvalidKeyException("could not DER encode y: " +
0N/A e.getMessage());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Make a DSA public key from its DER encoding (X.509).
0N/A */
0N/A public DSAPublicKey(byte[] encoded) throws InvalidKeyException {
0N/A decode(encoded);
0N/A }
0N/A
0N/A /**
0N/A * Returns the DSA parameters associated with this key, or null if the
0N/A * parameters could not be parsed.
0N/A */
0N/A public DSAParams getParams() {
0N/A try {
0N/A if (algid instanceof DSAParams) {
0N/A return (DSAParams)algid;
0N/A } else {
0N/A DSAParameterSpec paramSpec;
0N/A AlgorithmParameters algParams = algid.getParameters();
0N/A if (algParams == null) {
0N/A return null;
0N/A }
0N/A paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);
0N/A return (DSAParams)paramSpec;
0N/A }
0N/A } catch (InvalidParameterSpecException e) {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the raw public value, y, without the parameters.
0N/A *
0N/A * @see getParameters
0N/A */
0N/A public BigInteger getY() {
0N/A return y;
0N/A }
0N/A
0N/A public String toString() {
0N/A return "Sun DSA Public Key\n Parameters:" + algid
0N/A + "\n y:\n" + Debug.toHexString(y) + "\n";
0N/A }
0N/A
0N/A protected void parseKeyBits() throws InvalidKeyException {
0N/A try {
0N/A DerInputStream in = new DerInputStream(key);
0N/A y = in.getBigInteger();
0N/A } catch (IOException e) {
0N/A throw new InvalidKeyException("Invalid key: y value\n" +
0N/A e.getMessage());
0N/A }
0N/A }
0N/A}