1 |
769 |
jeremybenn |
/* SignatureAdapter.java --
|
2 |
|
|
Copyright 2001, 2002, 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.java.security.jce.sig;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.Configuration;
|
42 |
|
|
import gnu.java.security.sig.BaseSignature;
|
43 |
|
|
import gnu.java.security.sig.ISignature;
|
44 |
|
|
import gnu.java.security.sig.ISignatureCodec;
|
45 |
|
|
import gnu.java.security.sig.SignatureFactory;
|
46 |
|
|
|
47 |
|
|
import java.security.InvalidAlgorithmParameterException;
|
48 |
|
|
import java.security.InvalidKeyException;
|
49 |
|
|
import java.security.InvalidParameterException;
|
50 |
|
|
import java.security.PrivateKey;
|
51 |
|
|
import java.security.PublicKey;
|
52 |
|
|
import java.security.SecureRandom;
|
53 |
|
|
import java.security.SignatureException;
|
54 |
|
|
import java.security.SignatureSpi;
|
55 |
|
|
import java.security.spec.AlgorithmParameterSpec;
|
56 |
|
|
import java.util.HashMap;
|
57 |
|
|
import java.util.logging.Logger;
|
58 |
|
|
|
59 |
|
|
/**
|
60 |
|
|
* The implementation of a generic {@link java.security.Signature} adapter class
|
61 |
|
|
* to wrap GNU signature instances.
|
62 |
|
|
* <p>
|
63 |
|
|
* This class defines the <i>Service Provider Interface</i> (<b>SPI</b>) for
|
64 |
|
|
* the {@link java.security.Signature} class, which provides the functionality
|
65 |
|
|
* of a digital signature algorithm. Digital signatures are used for
|
66 |
|
|
* authentication and integrity assurance of digital data.
|
67 |
|
|
* <p>
|
68 |
|
|
* All the abstract methods in the {@link SignatureSpi} class are implemented by
|
69 |
|
|
* this class and all its sub-classes.
|
70 |
|
|
* <p>
|
71 |
|
|
* All the implementations which subclass this object, and which are serviced by
|
72 |
|
|
* the GNU provider implement the {@link Cloneable} interface.
|
73 |
|
|
*/
|
74 |
|
|
class SignatureAdapter
|
75 |
|
|
extends SignatureSpi
|
76 |
|
|
implements Cloneable
|
77 |
|
|
{
|
78 |
|
|
private static final Logger log = Logger.getLogger(SignatureAdapter.class.getName());
|
79 |
|
|
|
80 |
|
|
/** Our underlying signature instance. */
|
81 |
|
|
private ISignature adaptee;
|
82 |
|
|
|
83 |
|
|
/** Our underlying signature encoder/decoder engine. */
|
84 |
|
|
private ISignatureCodec codec;
|
85 |
|
|
|
86 |
|
|
/**
|
87 |
|
|
* Trivial protected constructor.
|
88 |
|
|
*
|
89 |
|
|
* @param sigName the canonical name of the signature scheme.
|
90 |
|
|
* @param codec the signature codec engine to use with this scheme.
|
91 |
|
|
*/
|
92 |
|
|
protected SignatureAdapter(String sigName, ISignatureCodec codec)
|
93 |
|
|
{
|
94 |
|
|
this(SignatureFactory.getInstance(sigName), codec);
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
/**
|
98 |
|
|
* Private constructor for cloning purposes.
|
99 |
|
|
*
|
100 |
|
|
* @param adaptee a clone of the underlying signature scheme instance.
|
101 |
|
|
* @param codec the signature codec engine to use with this scheme.
|
102 |
|
|
*/
|
103 |
|
|
private SignatureAdapter(ISignature adaptee, ISignatureCodec codec)
|
104 |
|
|
{
|
105 |
|
|
super();
|
106 |
|
|
|
107 |
|
|
this.adaptee = adaptee;
|
108 |
|
|
this.codec = codec;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
public Object clone()
|
112 |
|
|
{
|
113 |
|
|
return new SignatureAdapter((ISignature) adaptee.clone(), codec);
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
public void engineInitVerify(PublicKey publicKey) throws InvalidKeyException
|
117 |
|
|
{
|
118 |
|
|
HashMap attributes = new HashMap();
|
119 |
|
|
attributes.put(BaseSignature.VERIFIER_KEY, publicKey);
|
120 |
|
|
try
|
121 |
|
|
{
|
122 |
|
|
adaptee.setupVerify(attributes);
|
123 |
|
|
}
|
124 |
|
|
catch (IllegalArgumentException x)
|
125 |
|
|
{
|
126 |
|
|
throw new InvalidKeyException(x.getMessage(), x);
|
127 |
|
|
}
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
public void engineInitSign(PrivateKey privateKey) throws InvalidKeyException
|
131 |
|
|
{
|
132 |
|
|
HashMap attributes = new HashMap();
|
133 |
|
|
attributes.put(BaseSignature.SIGNER_KEY, privateKey);
|
134 |
|
|
try
|
135 |
|
|
{
|
136 |
|
|
adaptee.setupSign(attributes);
|
137 |
|
|
}
|
138 |
|
|
catch (IllegalArgumentException x)
|
139 |
|
|
{
|
140 |
|
|
throw new InvalidKeyException(x.getMessage(), x);
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
public void engineInitSign(PrivateKey privateKey, SecureRandom random)
|
145 |
|
|
throws InvalidKeyException
|
146 |
|
|
{
|
147 |
|
|
HashMap attributes = new HashMap();
|
148 |
|
|
attributes.put(BaseSignature.SIGNER_KEY, privateKey);
|
149 |
|
|
attributes.put(BaseSignature.SOURCE_OF_RANDOMNESS, random);
|
150 |
|
|
try
|
151 |
|
|
{
|
152 |
|
|
adaptee.setupSign(attributes);
|
153 |
|
|
}
|
154 |
|
|
catch (IllegalArgumentException x)
|
155 |
|
|
{
|
156 |
|
|
throw new InvalidKeyException(x.getMessage(), x);
|
157 |
|
|
}
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
public void engineUpdate(byte b) throws SignatureException
|
161 |
|
|
{
|
162 |
|
|
try
|
163 |
|
|
{
|
164 |
|
|
adaptee.update(b);
|
165 |
|
|
}
|
166 |
|
|
catch (IllegalStateException x)
|
167 |
|
|
{
|
168 |
|
|
throw new SignatureException(x.getMessage(), x);
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
public void engineUpdate(byte[] b, int off, int len)
|
173 |
|
|
throws SignatureException
|
174 |
|
|
{
|
175 |
|
|
try
|
176 |
|
|
{
|
177 |
|
|
adaptee.update(b, off, len);
|
178 |
|
|
}
|
179 |
|
|
catch (IllegalStateException x)
|
180 |
|
|
{
|
181 |
|
|
throw new SignatureException(x.getMessage(), x);
|
182 |
|
|
}
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
public byte[] engineSign() throws SignatureException
|
186 |
|
|
{
|
187 |
|
|
Object signature = null;
|
188 |
|
|
try
|
189 |
|
|
{
|
190 |
|
|
signature = adaptee.sign();
|
191 |
|
|
}
|
192 |
|
|
catch (IllegalStateException x)
|
193 |
|
|
{
|
194 |
|
|
throw new SignatureException(x.getMessage(), x);
|
195 |
|
|
}
|
196 |
|
|
byte[] result = codec.encodeSignature(signature);
|
197 |
|
|
return result;
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
public int engineSign(byte[] outbuf, int offset, int len)
|
201 |
|
|
throws SignatureException
|
202 |
|
|
{
|
203 |
|
|
byte[] signature = this.engineSign();
|
204 |
|
|
int result = signature.length;
|
205 |
|
|
if (result > len)
|
206 |
|
|
throw new SignatureException("Not enough room to store signature");
|
207 |
|
|
|
208 |
|
|
System.arraycopy(signature, 0, outbuf, offset, result);
|
209 |
|
|
return result;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
public boolean engineVerify(byte[] sigBytes) throws SignatureException
|
213 |
|
|
{
|
214 |
|
|
if (Configuration.DEBUG)
|
215 |
|
|
log.entering(this.getClass().getName(), "engineVerify");
|
216 |
|
|
Object signature = codec.decodeSignature(sigBytes);
|
217 |
|
|
boolean result = false;
|
218 |
|
|
try
|
219 |
|
|
{
|
220 |
|
|
result = adaptee.verify(signature);
|
221 |
|
|
}
|
222 |
|
|
catch (IllegalStateException x)
|
223 |
|
|
{
|
224 |
|
|
throw new SignatureException(x.getMessage(), x);
|
225 |
|
|
}
|
226 |
|
|
if (Configuration.DEBUG)
|
227 |
|
|
log.exiting(this.getClass().getName(), "engineVerify",
|
228 |
|
|
Boolean.valueOf(result));
|
229 |
|
|
return result;
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
// Deprecated. Replaced by engineSetParameter.
|
233 |
|
|
public void engineSetParameter(String param, Object value)
|
234 |
|
|
throws InvalidParameterException
|
235 |
|
|
{
|
236 |
|
|
throw new InvalidParameterException("deprecated");
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
public void engineSetParameter(AlgorithmParameterSpec params)
|
240 |
|
|
throws InvalidAlgorithmParameterException
|
241 |
|
|
{
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
// Deprecated
|
245 |
|
|
public Object engineGetParameter(String param)
|
246 |
|
|
throws InvalidParameterException
|
247 |
|
|
{
|
248 |
|
|
throw new InvalidParameterException("deprecated");
|
249 |
|
|
}
|
250 |
|
|
}
|