Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / security / jce / sig / DSSKeyFactory.java
blobbb4d85c899d8784416531399ba1ce52399d356cd
1 /* DSSKeyFactory.java -- JCE DSA key factory Adapter
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)
9 any later version.
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
19 02110-1301 USA.
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.
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.jce.sig;
41 import gnu.java.security.Registry;
42 import gnu.java.security.key.dss.DSSKeyPairPKCS8Codec;
43 import gnu.java.security.key.dss.DSSKeyPairX509Codec;
44 import gnu.java.security.key.dss.DSSPrivateKey;
45 import gnu.java.security.key.dss.DSSPublicKey;
47 import java.math.BigInteger;
48 import java.security.InvalidKeyException;
49 import java.security.Key;
50 import java.security.KeyFactorySpi;
51 import java.security.PrivateKey;
52 import java.security.PublicKey;
53 import java.security.interfaces.DSAPrivateKey;
54 import java.security.interfaces.DSAPublicKey;
55 import java.security.spec.DSAPrivateKeySpec;
56 import java.security.spec.DSAPublicKeySpec;
57 import java.security.spec.InvalidKeySpecException;
58 import java.security.spec.KeySpec;
59 import java.security.spec.PKCS8EncodedKeySpec;
60 import java.security.spec.X509EncodedKeySpec;
62 /**
63 * DSA key factory.
65 * @author Casey Marshall (rsdio@metastatic.org)
67 public class DSSKeyFactory extends KeyFactorySpi
69 // implicit 0-arguments constructor
71 protected PublicKey engineGeneratePublic(KeySpec keySpec)
72 throws InvalidKeySpecException
74 if (keySpec instanceof DSAPublicKeySpec)
76 DSAPublicKeySpec spec = (DSAPublicKeySpec) keySpec;
77 BigInteger p = spec.getP();
78 BigInteger q = spec.getQ();
79 BigInteger g = spec.getG();
80 BigInteger y = spec.getY();
81 return new DSSPublicKey(Registry.X509_ENCODING_ID, p, q, g, y);
84 if (keySpec instanceof X509EncodedKeySpec)
86 X509EncodedKeySpec spec = (X509EncodedKeySpec) keySpec;
87 byte[] encoded = spec.getEncoded();
88 PublicKey result;
89 try
91 result = new DSSKeyPairX509Codec().decodePublicKey(encoded);
92 return result;
94 catch (RuntimeException x)
96 InvalidKeySpecException y = new InvalidKeySpecException();
97 y.initCause(x);
98 throw y;
102 throw new InvalidKeySpecException("Unsupported (public) key specification");
105 protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
106 throws InvalidKeySpecException
108 if (keySpec instanceof DSAPrivateKeySpec)
110 DSAPrivateKeySpec spec = (DSAPrivateKeySpec) keySpec;
111 BigInteger p = spec.getP();
112 BigInteger q = spec.getQ();
113 BigInteger g = spec.getG();
114 BigInteger x = spec.getX();
115 return new DSSPrivateKey(Registry.PKCS8_ENCODING_ID, p, q, g, x);
118 if (keySpec instanceof PKCS8EncodedKeySpec)
120 PKCS8EncodedKeySpec spec = (PKCS8EncodedKeySpec) keySpec;
121 byte[] encoded = spec.getEncoded();
122 PrivateKey result;
125 result = new DSSKeyPairPKCS8Codec().decodePrivateKey(encoded);
126 return result;
128 catch (RuntimeException x)
130 InvalidKeySpecException y = new InvalidKeySpecException();
131 y.initCause(x);
132 throw y;
136 throw new InvalidKeySpecException("Unsupported (private) key specification");
139 protected KeySpec engineGetKeySpec(Key key, Class keySpec)
140 throws InvalidKeySpecException
142 if (key instanceof DSAPublicKey)
144 if (keySpec.isAssignableFrom(DSAPublicKeySpec.class))
146 DSAPublicKey dsaKey = (DSAPublicKey) key;
147 BigInteger p = dsaKey.getParams().getP();
148 BigInteger q = dsaKey.getParams().getQ();
149 BigInteger g = dsaKey.getParams().getG();
150 BigInteger y = dsaKey.getY();
151 return new DSAPublicKeySpec(y, p, q, g);
154 if (keySpec.isAssignableFrom(X509EncodedKeySpec.class))
156 if (key instanceof DSSPublicKey)
158 DSSPublicKey dssKey = (DSSPublicKey) key;
159 byte[] encoded = dssKey.getEncoded(Registry.X509_ENCODING_ID);
160 return new X509EncodedKeySpec(encoded);
163 if (Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat()))
165 byte[] encoded = key.getEncoded();
166 return new X509EncodedKeySpec(encoded);
169 throw new InvalidKeySpecException("Wrong key type or unsupported (public) key specification");
172 throw new InvalidKeySpecException("Unsupported (public) key specification");
175 if (key instanceof DSAPrivateKey)
177 if (keySpec.isAssignableFrom(DSAPrivateKeySpec.class))
179 DSAPrivateKey dsaKey = (DSAPrivateKey) key;
180 BigInteger p = dsaKey.getParams().getP();
181 BigInteger q = dsaKey.getParams().getQ();
182 BigInteger g = dsaKey.getParams().getG();
183 BigInteger x = dsaKey.getX();
184 return new DSAPrivateKeySpec(x, p, q, g);
187 if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class))
189 if (key instanceof DSSPrivateKey)
191 DSSPrivateKey dssKey = (DSSPrivateKey) key;
192 byte[] encoded = dssKey.getEncoded(Registry.PKCS8_ENCODING_ID);
193 return new PKCS8EncodedKeySpec(encoded);
196 if (Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat()))
198 byte[] encoded = key.getEncoded();
199 return new PKCS8EncodedKeySpec(encoded);
202 throw new InvalidKeySpecException("Wrong key type or unsupported (private) key specification");
205 throw new InvalidKeySpecException("Unsupported (private) key specification");
208 throw new InvalidKeySpecException("Wrong key type or unsupported key specification");
211 protected Key engineTranslateKey(Key key) throws InvalidKeyException
213 if ((key instanceof DSSPublicKey) || (key instanceof DSSPrivateKey))
214 return key;
216 if (key instanceof DSAPublicKey)
218 DSAPublicKey dsaKey = (DSAPublicKey) key;
219 BigInteger p = dsaKey.getParams().getP();
220 BigInteger q = dsaKey.getParams().getQ();
221 BigInteger g = dsaKey.getParams().getG();
222 BigInteger y = dsaKey.getY();
223 return new DSSPublicKey(Registry.X509_ENCODING_ID, p, q, g, y);
226 if (key instanceof DSAPrivateKey)
228 DSAPrivateKey dsaKey = (DSAPrivateKey) key;
229 BigInteger p = dsaKey.getParams().getP();
230 BigInteger q = dsaKey.getParams().getQ();
231 BigInteger g = dsaKey.getParams().getG();
232 BigInteger x = dsaKey.getX();
233 return new DSSPrivateKey(Registry.PKCS8_ENCODING_ID, p, q, g, x);
236 throw new InvalidKeyException("Wrong key type");