1 /* SignatureAdapter.java --
2    Copyright 2001, 2002, 2006, 2010 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 = Configuration.DEBUG ?
79                 Logger.getLogger(SignatureAdapter.class.getName()) : null;
80 
81   /** Our underlying signature instance. */
82   private ISignature adaptee;
83 
84   /** Our underlying signature encoder/decoder engine. */
85   private ISignatureCodec codec;
86 
87   /**
88    * Trivial protected constructor.
89    *
90    * @param sigName the canonical name of the signature scheme.
91    * @param codec the signature codec engine to use with this scheme.
92    */
SignatureAdapter(String sigName, ISignatureCodec codec)93   protected SignatureAdapter(String sigName, ISignatureCodec codec)
94   {
95     this(SignatureFactory.getInstance(sigName), codec);
96   }
97 
98   /**
99    * Private constructor for cloning purposes.
100    *
101    * @param adaptee a clone of the underlying signature scheme instance.
102    * @param codec the signature codec engine to use with this scheme.
103    */
SignatureAdapter(ISignature adaptee, ISignatureCodec codec)104   private SignatureAdapter(ISignature adaptee, ISignatureCodec codec)
105   {
106     super();
107 
108     this.adaptee = adaptee;
109     this.codec = codec;
110   }
111 
clone()112   public Object clone()
113   {
114     return new SignatureAdapter((ISignature) adaptee.clone(), codec);
115   }
116 
engineInitVerify(PublicKey publicKey)117   public void engineInitVerify(PublicKey publicKey) throws InvalidKeyException
118   {
119     HashMap attributes = new HashMap();
120     attributes.put(BaseSignature.VERIFIER_KEY, publicKey);
121     try
122       {
123         adaptee.setupVerify(attributes);
124       }
125     catch (IllegalArgumentException x)
126       {
127         throw new InvalidKeyException(x.getMessage(), x);
128       }
129   }
130 
engineInitSign(PrivateKey privateKey)131   public void engineInitSign(PrivateKey privateKey) throws InvalidKeyException
132   {
133     HashMap attributes = new HashMap();
134     attributes.put(BaseSignature.SIGNER_KEY, privateKey);
135     try
136       {
137         adaptee.setupSign(attributes);
138       }
139     catch (IllegalArgumentException x)
140       {
141         throw new InvalidKeyException(x.getMessage(), x);
142       }
143   }
144 
engineInitSign(PrivateKey privateKey, SecureRandom random)145   public void engineInitSign(PrivateKey privateKey, SecureRandom random)
146       throws InvalidKeyException
147   {
148     HashMap attributes = new HashMap();
149     attributes.put(BaseSignature.SIGNER_KEY, privateKey);
150     attributes.put(BaseSignature.SOURCE_OF_RANDOMNESS, random);
151     try
152       {
153         adaptee.setupSign(attributes);
154       }
155     catch (IllegalArgumentException x)
156       {
157         throw new InvalidKeyException(x.getMessage(), x);
158       }
159   }
160 
engineUpdate(byte b)161   public void engineUpdate(byte b) throws SignatureException
162   {
163     try
164       {
165         adaptee.update(b);
166       }
167     catch (IllegalStateException x)
168       {
169         throw new SignatureException(x.getMessage(), x);
170       }
171   }
172 
engineUpdate(byte[] b, int off, int len)173   public void engineUpdate(byte[] b, int off, int len)
174       throws SignatureException
175   {
176     try
177       {
178         adaptee.update(b, off, len);
179       }
180     catch (IllegalStateException x)
181       {
182         throw new SignatureException(x.getMessage(), x);
183       }
184   }
185 
engineSign()186   public byte[] engineSign() throws SignatureException
187   {
188     Object signature = null;
189     try
190       {
191         signature = adaptee.sign();
192       }
193     catch (IllegalStateException x)
194       {
195         throw new SignatureException(x.getMessage(), x);
196       }
197     byte[] result = codec.encodeSignature(signature);
198     return result;
199   }
200 
engineSign(byte[] outbuf, int offset, int len)201   public int engineSign(byte[] outbuf, int offset, int len)
202       throws SignatureException
203   {
204     byte[] signature = this.engineSign();
205     int result = signature.length;
206     if (result > len)
207       throw new SignatureException("Not enough room to store signature");
208 
209     System.arraycopy(signature, 0, outbuf, offset, result);
210     return result;
211   }
212 
engineVerify(byte[] sigBytes)213   public boolean engineVerify(byte[] sigBytes) throws SignatureException
214   {
215     if (Configuration.DEBUG)
216       log.entering(this.getClass().getName(), "engineVerify");
217     Object signature = codec.decodeSignature(sigBytes);
218     boolean result = false;
219     try
220       {
221         result = adaptee.verify(signature);
222       }
223     catch (IllegalStateException x)
224       {
225         throw new SignatureException(x.getMessage(), x);
226       }
227     if (Configuration.DEBUG)
228       log.exiting(this.getClass().getName(), "engineVerify",
229                   Boolean.valueOf(result));
230     return result;
231   }
232 
233   // Deprecated. Replaced by engineSetParameter.
engineSetParameter(String param, Object value)234   public void engineSetParameter(String param, Object value)
235       throws InvalidParameterException
236   {
237     throw new InvalidParameterException("deprecated");
238   }
239 
engineSetParameter(AlgorithmParameterSpec params)240   public void engineSetParameter(AlgorithmParameterSpec params)
241       throws InvalidAlgorithmParameterException
242   {
243   }
244 
245   // Deprecated
engineGetParameter(String param)246   public Object engineGetParameter(String param)
247       throws InvalidParameterException
248   {
249     throw new InvalidParameterException("deprecated");
250   }
251 }
252