1 |
769 |
jeremybenn |
/* GnuDHPrivateKey.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.Configuration;
|
42 |
|
|
import gnu.java.security.Registry;
|
43 |
|
|
import gnu.java.security.action.GetPropertyAction;
|
44 |
|
|
import gnu.java.security.key.IKeyPairCodec;
|
45 |
|
|
|
46 |
|
|
import java.math.BigInteger;
|
47 |
|
|
import java.security.AccessController;
|
48 |
|
|
|
49 |
|
|
import javax.crypto.interfaces.DHPrivateKey;
|
50 |
|
|
|
51 |
|
|
/**
|
52 |
|
|
* An implementation of the Diffie-Hellman private key.
|
53 |
|
|
* <p>
|
54 |
|
|
* Reference:
|
55 |
|
|
* <ol>
|
56 |
|
|
* <li><a href="http://www.ietf.org/rfc/rfc2631.txt">Diffie-Hellman Key
|
57 |
|
|
* Agreement Method</a><br>
|
58 |
|
|
* Eric Rescorla.</li>
|
59 |
|
|
* </ol>
|
60 |
|
|
*/
|
61 |
|
|
public class GnuDHPrivateKey
|
62 |
|
|
extends GnuDHKey
|
63 |
|
|
implements DHPrivateKey
|
64 |
|
|
{
|
65 |
|
|
/** The private exponent. */
|
66 |
|
|
private final BigInteger x;
|
67 |
|
|
/** String representation of this key. Cached for speed. */
|
68 |
|
|
private transient String str;
|
69 |
|
|
|
70 |
|
|
/**
|
71 |
|
|
* Convenience constructor. Calls the constructor with five arguments passing
|
72 |
|
|
* {@link Registry#RAW_ENCODING_ID} as the value of its first argument.
|
73 |
|
|
*
|
74 |
|
|
* @param q a prime divisor of p-1.
|
75 |
|
|
* @param p the public prime.
|
76 |
|
|
* @param g the generator of the group.
|
77 |
|
|
* @param x the private value x.
|
78 |
|
|
*/
|
79 |
|
|
public GnuDHPrivateKey(BigInteger q, BigInteger p, BigInteger g, BigInteger x)
|
80 |
|
|
{
|
81 |
|
|
this(Registry.RAW_ENCODING_ID, q, p, g, x);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
/**
|
85 |
|
|
* Constructs a new instance of <code>GnuDHPrivateKey</code> given the
|
86 |
|
|
* designated parameters.
|
87 |
|
|
*
|
88 |
|
|
* @param preferredFormat the identifier of the encoding format to use by
|
89 |
|
|
* default when externalizing the key.
|
90 |
|
|
* @param q a prime divisor of p-1.
|
91 |
|
|
* @param p the public prime.
|
92 |
|
|
* @param g the generator of the group.
|
93 |
|
|
* @param x the private value x.
|
94 |
|
|
*/
|
95 |
|
|
public GnuDHPrivateKey(int preferredFormat, BigInteger q, BigInteger p,
|
96 |
|
|
BigInteger g, BigInteger x)
|
97 |
|
|
{
|
98 |
|
|
super(preferredFormat == Registry.ASN1_ENCODING_ID ? Registry.PKCS8_ENCODING_ID
|
99 |
|
|
: preferredFormat,
|
100 |
|
|
q, p, g);
|
101 |
|
|
this.x = x;
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
/**
|
105 |
|
|
* A class method that takes the output of the <code>encodePrivateKey()</code>
|
106 |
|
|
* method of a DH keypair codec object (an instance implementing
|
107 |
|
|
* {@link IKeyPairCodec} for DH keys, and re-constructs an instance of this
|
108 |
|
|
* object.
|
109 |
|
|
*
|
110 |
|
|
* @param k the contents of a previously encoded instance of this object.
|
111 |
|
|
* @exception ArrayIndexOutOfBoundsException if there is not enough bytes, in
|
112 |
|
|
* <code>k</code>, to represent a valid encoding of an
|
113 |
|
|
* instance of this object.
|
114 |
|
|
* @exception IllegalArgumentException if the byte sequence does not represent
|
115 |
|
|
* a valid encoding of an instance of this object.
|
116 |
|
|
*/
|
117 |
|
|
public static GnuDHPrivateKey valueOf(byte[] k)
|
118 |
|
|
{
|
119 |
|
|
// try RAW codec
|
120 |
|
|
if (k[0] == Registry.MAGIC_RAW_DH_PRIVATE_KEY[0])
|
121 |
|
|
try
|
122 |
|
|
{
|
123 |
|
|
return (GnuDHPrivateKey) new DHKeyPairRawCodec().decodePrivateKey(k);
|
124 |
|
|
}
|
125 |
|
|
catch (IllegalArgumentException ignored)
|
126 |
|
|
{
|
127 |
|
|
}
|
128 |
|
|
// try PKCS#8 codec
|
129 |
|
|
return (GnuDHPrivateKey) new DHKeyPairPKCS8Codec().decodePrivateKey(k);
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
public BigInteger getX()
|
133 |
|
|
{
|
134 |
|
|
return x;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
/**
|
138 |
|
|
* Returns the encoded form of this private key according to the designated
|
139 |
|
|
* format.
|
140 |
|
|
*
|
141 |
|
|
* @param format the desired format identifier of the resulting encoding.
|
142 |
|
|
* @return the byte sequence encoding this key according to the designated
|
143 |
|
|
* format.
|
144 |
|
|
* @exception IllegalArgumentException if the format is not supported.
|
145 |
|
|
* @see DHKeyPairRawCodec
|
146 |
|
|
*/
|
147 |
|
|
public byte[] getEncoded(int format)
|
148 |
|
|
{
|
149 |
|
|
byte[] result;
|
150 |
|
|
switch (format)
|
151 |
|
|
{
|
152 |
|
|
case IKeyPairCodec.RAW_FORMAT:
|
153 |
|
|
result = new DHKeyPairRawCodec().encodePrivateKey(this);
|
154 |
|
|
break;
|
155 |
|
|
case IKeyPairCodec.PKCS8_FORMAT:
|
156 |
|
|
result = new DHKeyPairPKCS8Codec().encodePrivateKey(this);
|
157 |
|
|
break;
|
158 |
|
|
default:
|
159 |
|
|
throw new IllegalArgumentException("Unsupported encoding format: "
|
160 |
|
|
+ format);
|
161 |
|
|
}
|
162 |
|
|
return result;
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
/**
|
166 |
|
|
* Returns <code>true</code> if the designated object is an instance of
|
167 |
|
|
* {@link DHPrivateKey} and has the same parameter values as this one.
|
168 |
|
|
*
|
169 |
|
|
* @param obj the other non-null DH key to compare to.
|
170 |
|
|
* @return <code>true</code> if the designated object is of the same type
|
171 |
|
|
* and value as this one.
|
172 |
|
|
*/
|
173 |
|
|
public boolean equals(Object obj)
|
174 |
|
|
{
|
175 |
|
|
if (obj == null)
|
176 |
|
|
return false;
|
177 |
|
|
|
178 |
|
|
if (! (obj instanceof DHPrivateKey))
|
179 |
|
|
return false;
|
180 |
|
|
|
181 |
|
|
DHPrivateKey that = (DHPrivateKey) obj;
|
182 |
|
|
return super.equals(that) && x.equals(that.getX());
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
public String toString()
|
186 |
|
|
{
|
187 |
|
|
if (str == null)
|
188 |
|
|
{
|
189 |
|
|
String ls = (String) AccessController.doPrivileged
|
190 |
|
|
(new GetPropertyAction("line.separator"));
|
191 |
|
|
str = new StringBuilder(this.getClass().getName()).append("(")
|
192 |
|
|
.append(super.toString()).append(",").append(ls)
|
193 |
|
|
.append("x=0x").append(Configuration.DEBUG ? x.toString(16)
|
194 |
|
|
: "**...*").append(ls)
|
195 |
|
|
.append(")")
|
196 |
|
|
.toString();
|
197 |
|
|
}
|
198 |
|
|
return str;
|
199 |
|
|
}
|
200 |
|
|
}
|