|
1 /* |
|
2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package sun.security.provider; |
|
27 |
|
28 import java.util.*; |
|
29 import java.io.*; |
|
30 import java.math.BigInteger; |
|
31 import java.security.InvalidKeyException; |
|
32 import java.security.ProviderException; |
|
33 import java.security.AlgorithmParameters; |
|
34 import java.security.spec.DSAParameterSpec; |
|
35 import java.security.spec.InvalidParameterSpecException; |
|
36 import java.security.interfaces.DSAParams; |
|
37 |
|
38 import sun.security.x509.X509Key; |
|
39 import sun.security.x509.AlgIdDSA; |
|
40 import sun.security.util.BitArray; |
|
41 import sun.security.util.Debug; |
|
42 import sun.security.util.DerValue; |
|
43 import sun.security.util.DerInputStream; |
|
44 import sun.security.util.DerOutputStream; |
|
45 |
|
46 /** |
|
47 * An X.509 public key for the Digital Signature Algorithm. |
|
48 * |
|
49 * @author Benjamin Renaud |
|
50 * |
|
51 * |
|
52 * @see DSAPrivateKey |
|
53 * @see AlgIdDSA |
|
54 * @see DSA |
|
55 */ |
|
56 |
|
57 public class DSAPublicKey extends X509Key |
|
58 implements java.security.interfaces.DSAPublicKey, Serializable { |
|
59 |
|
60 /** use serialVersionUID from JDK 1.1. for interoperability */ |
|
61 private static final long serialVersionUID = -2994193307391104133L; |
|
62 |
|
63 /* the public key */ |
|
64 private BigInteger y; |
|
65 |
|
66 /* |
|
67 * Keep this constructor for backwards compatibility with JDK1.1. |
|
68 */ |
|
69 public DSAPublicKey() { |
|
70 } |
|
71 |
|
72 /** |
|
73 * Make a DSA public key out of a public key and three parameters. |
|
74 * The p, q, and g parameters may be null, but if so, parameters will need |
|
75 * to be supplied from some other source before this key can be used in |
|
76 * cryptographic operations. PKIX RFC2459bis explicitly allows DSA public |
|
77 * keys without parameters, where the parameters are provided in the |
|
78 * issuer's DSA public key. |
|
79 * |
|
80 * @param y the actual key bits |
|
81 * @param p DSA parameter p, may be null if all of p, q, and g are null. |
|
82 * @param q DSA parameter q, may be null if all of p, q, and g are null. |
|
83 * @param g DSA parameter g, may be null if all of p, q, and g are null. |
|
84 */ |
|
85 public DSAPublicKey(BigInteger y, BigInteger p, BigInteger q, |
|
86 BigInteger g) |
|
87 throws InvalidKeyException { |
|
88 this.y = y; |
|
89 algid = new AlgIdDSA(p, q, g); |
|
90 |
|
91 try { |
|
92 byte[] keyArray = new DerValue(DerValue.tag_Integer, |
|
93 y.toByteArray()).toByteArray(); |
|
94 setKey(new BitArray(keyArray.length*8, keyArray)); |
|
95 encode(); |
|
96 } catch (IOException e) { |
|
97 throw new InvalidKeyException("could not DER encode y: " + |
|
98 e.getMessage()); |
|
99 } |
|
100 } |
|
101 |
|
102 /** |
|
103 * Make a DSA public key from its DER encoding (X.509). |
|
104 */ |
|
105 public DSAPublicKey(byte[] encoded) throws InvalidKeyException { |
|
106 decode(encoded); |
|
107 } |
|
108 |
|
109 /** |
|
110 * Returns the DSA parameters associated with this key, or null if the |
|
111 * parameters could not be parsed. |
|
112 */ |
|
113 public DSAParams getParams() { |
|
114 try { |
|
115 if (algid instanceof DSAParams) { |
|
116 return (DSAParams)algid; |
|
117 } else { |
|
118 DSAParameterSpec paramSpec; |
|
119 AlgorithmParameters algParams = algid.getParameters(); |
|
120 if (algParams == null) { |
|
121 return null; |
|
122 } |
|
123 paramSpec = algParams.getParameterSpec(DSAParameterSpec.class); |
|
124 return (DSAParams)paramSpec; |
|
125 } |
|
126 } catch (InvalidParameterSpecException e) { |
|
127 return null; |
|
128 } |
|
129 } |
|
130 |
|
131 /** |
|
132 * Get the raw public value, y, without the parameters. |
|
133 * |
|
134 * @see getParameters |
|
135 */ |
|
136 public BigInteger getY() { |
|
137 return y; |
|
138 } |
|
139 |
|
140 public String toString() { |
|
141 return "Sun DSA Public Key\n Parameters:" + algid |
|
142 + "\n y:\n" + Debug.toHexString(y) + "\n"; |
|
143 } |
|
144 |
|
145 protected void parseKeyBits() throws InvalidKeyException { |
|
146 try { |
|
147 DerInputStream in = new DerInputStream(getKey().toByteArray()); |
|
148 y = in.getBigInteger(); |
|
149 } catch (IOException e) { |
|
150 throw new InvalidKeyException("Invalid key: y value\n" + |
|
151 e.getMessage()); |
|
152 } |
|
153 } |
|
154 } |