1 /* DSAKeyFactory.java -- DSA key factory.
2    Copyright (C) 2003 Free Software Foundation, Inc.
3 
4 This file is 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, or (at your option)
9 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; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.provider;
40 
41 import java.security.InvalidKeyException;
42 import java.security.Key;
43 import java.security.KeyFactorySpi;
44 import java.security.PrivateKey;
45 import java.security.PublicKey;
46 import java.security.interfaces.DSAPrivateKey;
47 import java.security.interfaces.DSAPublicKey;
48 import java.security.spec.DSAPrivateKeySpec;
49 import java.security.spec.DSAPublicKeySpec;
50 import java.security.spec.InvalidKeySpecException;
51 import java.security.spec.KeySpec;
52 
53 /**
54  * DSA key factory.
55  *
56  * @author Casey Marshall (rsdio@metastatic.org)
57  */
58 public class DSAKeyFactory extends KeyFactorySpi
59 {
60 
61   // Constructor.
62   // ------------------------------------------------------------------------
63 
DSAKeyFactory()64   public DSAKeyFactory()
65   {
66     super();
67   }
68 
69   // Instance methods.
70   // ------------------------------------------------------------------------
71 
engineGeneratePrivate(KeySpec keySpec)72   protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
73     throws InvalidKeySpecException
74   {
75     if (!(keySpec instanceof DSAPrivateKeySpec))
76       throw new InvalidKeySpecException();
77     return new GnuDSAPrivateKey(
78       ((DSAPrivateKeySpec) keySpec).getX(),
79       ((DSAPrivateKeySpec) keySpec).getP(),
80       ((DSAPrivateKeySpec) keySpec).getQ(),
81       ((DSAPrivateKeySpec) keySpec).getG());
82   }
83 
engineGeneratePublic(KeySpec keySpec)84   protected PublicKey engineGeneratePublic(KeySpec keySpec)
85     throws InvalidKeySpecException
86   {
87     if (!(keySpec instanceof DSAPublicKeySpec))
88       throw new InvalidKeySpecException();
89     return new GnuDSAPublicKey(
90       ((DSAPublicKeySpec) keySpec).getY(),
91       ((DSAPublicKeySpec) keySpec).getP(),
92       ((DSAPublicKeySpec) keySpec).getQ(),
93       ((DSAPublicKeySpec) keySpec).getG());
94   }
95 
engineGetKeySpec(Key key, Class keySpec)96   protected KeySpec engineGetKeySpec(Key key, Class keySpec)
97     throws InvalidKeySpecException
98   {
99     if ((key instanceof DSAPublicKey) &&
100          keySpec.isAssignableFrom(DSAPublicKeySpec.class))
101       {
102         return new DSAPublicKeySpec(((DSAPublicKey) key).getY(),
103           ((DSAPublicKey) key).getParams().getP(),
104           ((DSAPublicKey) key).getParams().getQ(),
105           ((DSAPublicKey) key).getParams().getG());
106       }
107     if ((key instanceof DSAPrivateKey) &&
108          keySpec.isAssignableFrom(DSAPrivateKeySpec.class))
109       {
110         return new DSAPrivateKeySpec(((DSAPrivateKey) key).getX(),
111           ((DSAPrivateKey) key).getParams().getP(),
112           ((DSAPrivateKey) key).getParams().getQ(),
113           ((DSAPrivateKey) key).getParams().getG());
114       }
115     throw new InvalidKeySpecException();
116   }
117 
engineTranslateKey(Key key)118   protected Key engineTranslateKey(Key key) throws InvalidKeyException
119   {
120     if ((key instanceof GnuDSAPublicKey) || (key instanceof GnuDSAPrivateKey))
121       return key;
122     if (key instanceof DSAPublicKey)
123       return new GnuDSAPublicKey(((DSAPublicKey) key).getY(),
124         ((DSAPublicKey) key).getParams().getP(),
125         ((DSAPublicKey) key).getParams().getQ(),
126         ((DSAPublicKey) key).getParams().getG());
127     if (key instanceof DSAPrivateKey)
128       return new GnuDSAPrivateKey(((DSAPrivateKey) key).getX(),
129         ((DSAPrivateKey) key).getParams().getP(),
130         ((DSAPrivateKey) key).getParams().getQ(),
131         ((DSAPrivateKey) key).getParams().getG());
132     throw new InvalidKeyException();
133   }
134 }
135