1
/* SignatureCodecFactory.java -- Factory to instantiate Signature codecs
2
Copyright (C) 2006 Free Software Foundation, Inc.
4
This file is part of GNU Classpath.
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, or (at your option)
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.
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING. If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
39
package gnu.java.security.sig;
41
import gnu.java.security.Registry;
42
import gnu.java.security.hash.HashFactory;
43
import gnu.java.security.sig.dss.DSSSignatureRawCodec;
44
import gnu.java.security.sig.dss.DSSSignatureX509Codec;
45
import gnu.java.security.sig.rsa.RSAPKCS1V1_5SignatureRawCodec;
46
import gnu.java.security.sig.rsa.RSAPKCS1V1_5SignatureX509Codec;
47
import gnu.java.security.sig.rsa.RSAPSSSignatureRawCodec;
48
import gnu.java.security.util.FormatUtil;
50
import java.util.Collections;
51
import java.util.HashSet;
52
import java.util.Iterator;
56
* A <i>Factory</i> class to instantiate Signature codecs.
58
public class SignatureCodecFactory
60
private static Set names;
62
/** Trivial constructor to enforce Singleton pattern. */
63
private SignatureCodecFactory()
69
* Returns the appropriate codec given a composed signature algorithm and an
70
* encoding format. A composed name is formed by the concatenation of the
71
* canonical signature algorithm name, the forward slash character
72
* <code>/</code> and the canonical name of the encoding format.
74
* When the encoding format name is missing, the Raw encoding format is
75
* assumed. When this is the case the trailing forward slash is discarded from
78
* @param name the case-insensitive, possibly composed, signature codec name.
79
* @return an instance of the signaturecodec, or <code>null</code> if none
82
public static ISignatureCodec getInstance(String name)
88
if (name.length() == 0)
91
if (name.startsWith("/"))
94
if (name.endsWith("/"))
95
return getInstance(name.substring(0, name.length() - 1),
96
Registry.RAW_ENCODING_ID);
98
int i = name.indexOf("/");
100
return getInstance(name, Registry.RAW_ENCODING_ID);
102
String sigName = name.substring(0, i);
103
String formatName = name.substring(i + 1);
104
return getInstance(sigName, formatName);
108
* Returns an instance of a signature codec given the canonical name of the
109
* signature algorithm, and that of the encoding format.
111
* @param name the case-insensitive signature algorithm name.
112
* @param format the name of the format to use when encodigng/decoding
113
* signatures generated by the named algorithm.
114
* @return an instance of the signature codec, or <code>null</code> if none
117
public static ISignatureCodec getInstance(String name, String format)
119
int formatID = FormatUtil.getFormatID(format);
123
return getInstance(name, formatID);
127
* Returns an instance of a signature codec given the canonical name of the
128
* signature algorithm, and the identifier of the format to use when
129
* encoding/decoding signatures generated by that algorithm.
131
* @param name the case-insensitive signature algorithm name.
132
* @param formatID the identifier of the format to use when encoding /
133
* decoding signatures generated by the designated algorithm.
134
* @return an instance of the signature codec, or <code>null</code> if none
137
public static ISignatureCodec getInstance(String name, int formatID)
145
case Registry.RAW_ENCODING_ID:
146
return getRawCodec(name);
147
case Registry.X509_ENCODING_ID:
148
return getX509Codec(name);
155
* Returns a {@link Set} of supported signature codec names.
157
* @return a {@link Set} of the names of supported signature codec (Strings).
159
public static synchronized final Set getNames()
163
HashSet hs = new HashSet();
164
hs.add(Registry.DSS_SIG + "/" + Registry.RAW_ENCODING_SHORT_NAME);
165
hs.add(Registry.DSS_SIG + "/" + Registry.X509_ENCODING_SORT_NAME);
166
Set hashNames = HashFactory.getNames();
167
for (Iterator it = hashNames.iterator(); it.hasNext();)
169
String mdName = (String) it.next();
170
String name = Registry.RSA_PKCS1_V1_5_SIG + "-" + mdName;
171
hs.add(name + "/" + Registry.RAW_ENCODING_SHORT_NAME);
172
hs.add(name + "/" + Registry.X509_ENCODING_SORT_NAME);
173
name = Registry.RSA_PSS_SIG + "-" + mdName;
174
hs.add(name + "/" + Registry.RAW_ENCODING_SHORT_NAME);
177
names = Collections.unmodifiableSet(hs);
184
* @param name the trimmed name of a signature algorithm.
185
* @return a Raw format codec for the designated signature algorithm, or
186
* <code>null</code> if none exists.
188
private static ISignatureCodec getRawCodec(String name)
190
ISignatureCodec result = null;
191
if (name.equalsIgnoreCase(Registry.DSA_SIG)
192
|| name.equalsIgnoreCase(Registry.DSS_SIG))
193
result = new DSSSignatureRawCodec();
196
name = name.toLowerCase();
197
if (name.startsWith(Registry.RSA_PKCS1_V1_5_SIG))
198
result = new RSAPKCS1V1_5SignatureRawCodec();
199
else if (name.startsWith(Registry.RSA_PSS_SIG))
200
result = new RSAPSSSignatureRawCodec();
207
* @param name the trimmed name of a signature algorithm.
208
* @return a X.509 format codec for the designated signature algorithm, or
209
* <code>null</code> if none exists.
211
private static ISignatureCodec getX509Codec(String name)
213
ISignatureCodec result = null;
214
if (name.equalsIgnoreCase(Registry.DSA_SIG)
215
|| name.equalsIgnoreCase(Registry.DSS_SIG))
216
result = new DSSSignatureX509Codec();
219
name = name.toLowerCase();
220
if (name.startsWith(Registry.RSA_PKCS1_V1_5_SIG))
221
result = new RSAPKCS1V1_5SignatureX509Codec();