1 |
769 |
jeremybenn |
/* GnuDHPublicKey.java --
|
2 |
|
|
Copyright (C) 2003, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is a part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2 of the License, or (at
|
9 |
|
|
your option) any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; if not, write to the Free Software
|
18 |
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
|
19 |
|
|
USA
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.javax.crypto.key.dh;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.Registry;
|
42 |
|
|
import gnu.java.security.action.GetPropertyAction;
|
43 |
|
|
import gnu.java.security.key.IKeyPairCodec;
|
44 |
|
|
|
45 |
|
|
import java.math.BigInteger;
|
46 |
|
|
import java.security.AccessController;
|
47 |
|
|
|
48 |
|
|
import javax.crypto.interfaces.DHPublicKey;
|
49 |
|
|
|
50 |
|
|
/**
|
51 |
|
|
* An implementation of the Diffie-Hellman public key.
|
52 |
|
|
* <p>
|
53 |
|
|
* Reference:
|
54 |
|
|
* <ol>
|
55 |
|
|
* <li><a href="http://www.ietf.org/rfc/rfc2631.txt">Diffie-Hellman Key
|
56 |
|
|
* Agreement Method</a><br>
|
57 |
|
|
* Eric Rescorla.</li>
|
58 |
|
|
* </ol>
|
59 |
|
|
*/
|
60 |
|
|
public class GnuDHPublicKey
|
61 |
|
|
extends GnuDHKey
|
62 |
|
|
implements DHPublicKey
|
63 |
|
|
{
|
64 |
|
|
private BigInteger y;
|
65 |
|
|
/** String representation of this key. Cached for speed. */
|
66 |
|
|
private transient String str;
|
67 |
|
|
|
68 |
|
|
/**
|
69 |
|
|
* Convenience constructor. Calls the constructor with five arguments passing
|
70 |
|
|
* {@link Registry#RAW_ENCODING_ID} as the value of its first argument.
|
71 |
|
|
*
|
72 |
|
|
* @param q a prime divisor of p-1.
|
73 |
|
|
* @param p the public prime.
|
74 |
|
|
* @param g the generator of the group.
|
75 |
|
|
* @param y the public value y.
|
76 |
|
|
*/
|
77 |
|
|
public GnuDHPublicKey(BigInteger q, BigInteger p, BigInteger g, BigInteger y)
|
78 |
|
|
{
|
79 |
|
|
this(Registry.RAW_ENCODING_ID, q, p, g, y);
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
/**
|
83 |
|
|
* Constructs a new instance of <code>GnuDHPublicKey</code> given the
|
84 |
|
|
* designated parameters.
|
85 |
|
|
*
|
86 |
|
|
* @param preferredFormat the identifier of the encoding format to use by
|
87 |
|
|
* default when externalizing the key.
|
88 |
|
|
* @param q a prime divisor of p-1.
|
89 |
|
|
* @param p the public prime.
|
90 |
|
|
* @param g the generator of the group.
|
91 |
|
|
* @param y the public value y.
|
92 |
|
|
*/
|
93 |
|
|
public GnuDHPublicKey(int preferredFormat, BigInteger q, BigInteger p,
|
94 |
|
|
BigInteger g, BigInteger y)
|
95 |
|
|
{
|
96 |
|
|
super(preferredFormat == Registry.ASN1_ENCODING_ID ? Registry.X509_ENCODING_ID
|
97 |
|
|
: preferredFormat,
|
98 |
|
|
q, p, g);
|
99 |
|
|
this.y = y;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
/**
|
103 |
|
|
* A class method that takes the output of the <code>encodePublicKey()</code>
|
104 |
|
|
* method of a DH keypair codec object (an instance implementing
|
105 |
|
|
* {@link IKeyPairCodec} for DSS keys, and re-constructs an instance of this
|
106 |
|
|
* object.
|
107 |
|
|
*
|
108 |
|
|
* @param k the contents of a previously encoded instance of this object.
|
109 |
|
|
* @exception ArrayIndexOutOfBoundsException if there is not enough bytes, in
|
110 |
|
|
* <code>k</code>, to represent a valid encoding of an
|
111 |
|
|
* instance of this object.
|
112 |
|
|
* @exception IllegalArgumentException if the byte sequence does not represent
|
113 |
|
|
* a valid encoding of an instance of this object.
|
114 |
|
|
*/
|
115 |
|
|
public static GnuDHPublicKey valueOf(byte[] k)
|
116 |
|
|
{
|
117 |
|
|
// try RAW codec
|
118 |
|
|
if (k[0] == Registry.MAGIC_RAW_DH_PUBLIC_KEY[0])
|
119 |
|
|
try
|
120 |
|
|
{
|
121 |
|
|
return (GnuDHPublicKey) new DHKeyPairRawCodec().decodePublicKey(k);
|
122 |
|
|
}
|
123 |
|
|
catch (IllegalArgumentException ignored)
|
124 |
|
|
{
|
125 |
|
|
}
|
126 |
|
|
// try X.509 codec
|
127 |
|
|
return (GnuDHPublicKey) new DHKeyPairX509Codec().decodePublicKey(k);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
public BigInteger getY()
|
131 |
|
|
{
|
132 |
|
|
return y;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
/**
|
136 |
|
|
* Returns the encoded form of this public key according to the designated
|
137 |
|
|
* format.
|
138 |
|
|
*
|
139 |
|
|
* @param format the desired format identifier of the resulting encoding.
|
140 |
|
|
* @return the byte sequence encoding this key according to the designated
|
141 |
|
|
* format.
|
142 |
|
|
* @exception IllegalArgumentException if the format is not supported.
|
143 |
|
|
*/
|
144 |
|
|
public byte[] getEncoded(int format)
|
145 |
|
|
{
|
146 |
|
|
byte[] result;
|
147 |
|
|
switch (format)
|
148 |
|
|
{
|
149 |
|
|
case IKeyPairCodec.RAW_FORMAT:
|
150 |
|
|
result = new DHKeyPairRawCodec().encodePublicKey(this);
|
151 |
|
|
break;
|
152 |
|
|
case IKeyPairCodec.X509_FORMAT:
|
153 |
|
|
result = new DHKeyPairX509Codec().encodePublicKey(this);
|
154 |
|
|
break;
|
155 |
|
|
default:
|
156 |
|
|
throw new IllegalArgumentException("Unsupported encoding format: "
|
157 |
|
|
+ format);
|
158 |
|
|
}
|
159 |
|
|
return result;
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
/**
|
163 |
|
|
* Returns <code>true</code> if the designated object is an instance of
|
164 |
|
|
* {@link DHPublicKey} and has the same parameter values as this one.
|
165 |
|
|
*
|
166 |
|
|
* @param obj the other non-null DH key to compare to.
|
167 |
|
|
* @return <code>true</code> if the designated object is of the same type
|
168 |
|
|
* and value as this one.
|
169 |
|
|
*/
|
170 |
|
|
public boolean equals(Object obj)
|
171 |
|
|
{
|
172 |
|
|
if (obj == null)
|
173 |
|
|
return false;
|
174 |
|
|
|
175 |
|
|
if (! (obj instanceof DHPublicKey))
|
176 |
|
|
return false;
|
177 |
|
|
|
178 |
|
|
DHPublicKey that = (DHPublicKey) obj;
|
179 |
|
|
return super.equals(that) && y.equals(that.getY());
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
public String toString()
|
183 |
|
|
{
|
184 |
|
|
if (str == null)
|
185 |
|
|
{
|
186 |
|
|
String ls = (String) AccessController.doPrivileged
|
187 |
|
|
(new GetPropertyAction("line.separator"));
|
188 |
|
|
str = new StringBuilder(this.getClass().getName()).append("(")
|
189 |
|
|
.append(super.toString()).append(",").append(ls)
|
190 |
|
|
.append("y=0x").append(y.toString(16)).append(ls)
|
191 |
|
|
.append(")")
|
192 |
|
|
.toString();
|
193 |
|
|
}
|
194 |
|
|
return str;
|
195 |
|
|
}
|
196 |
|
|
}
|