OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [security/] [jce/] [sig/] [DSSKeyFactory.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* DSSKeyFactory.java -- JCE DSA key factory Adapter
2
   Copyright (C) 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 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.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;
46
 
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;
61
 
62
/**
63
 * DSA key factory.
64
 *
65
 * @author Casey Marshall (rsdio@metastatic.org)
66
 */
67
public class DSSKeyFactory
68
    extends KeyFactorySpi
69
{
70
  // implicit 0-arguments constructor
71
 
72
  protected PublicKey engineGeneratePublic(KeySpec keySpec)
73
      throws InvalidKeySpecException
74
  {
75
    if (keySpec instanceof DSAPublicKeySpec)
76
      {
77
        DSAPublicKeySpec spec = (DSAPublicKeySpec) keySpec;
78
        BigInteger p = spec.getP();
79
        BigInteger q = spec.getQ();
80
        BigInteger g = spec.getG();
81
        BigInteger y = spec.getY();
82
        return new DSSPublicKey(Registry.X509_ENCODING_ID, p, q, g, y);
83
      }
84
    if (keySpec instanceof X509EncodedKeySpec)
85
      {
86
        X509EncodedKeySpec spec = (X509EncodedKeySpec) keySpec;
87
        byte[] encoded = spec.getEncoded();
88
        PublicKey result;
89
        try
90
          {
91
            result = new DSSKeyPairX509Codec().decodePublicKey(encoded);
92
            return result;
93
          }
94
        catch (RuntimeException x)
95
          {
96
            throw new InvalidKeySpecException(x.getMessage(), x);
97
          }
98
      }
99
    throw new InvalidKeySpecException("Unsupported (public) key specification");
100
  }
101
 
102
  protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
103
      throws InvalidKeySpecException
104
  {
105
    if (keySpec instanceof DSAPrivateKeySpec)
106
      {
107
        DSAPrivateKeySpec spec = (DSAPrivateKeySpec) keySpec;
108
        BigInteger p = spec.getP();
109
        BigInteger q = spec.getQ();
110
        BigInteger g = spec.getG();
111
        BigInteger x = spec.getX();
112
        return new DSSPrivateKey(Registry.PKCS8_ENCODING_ID, p, q, g, x);
113
      }
114
    if (keySpec instanceof PKCS8EncodedKeySpec)
115
      {
116
        PKCS8EncodedKeySpec spec = (PKCS8EncodedKeySpec) keySpec;
117
        byte[] encoded = spec.getEncoded();
118
        PrivateKey result;
119
        try
120
          {
121
            result = new DSSKeyPairPKCS8Codec().decodePrivateKey(encoded);
122
            return result;
123
          }
124
        catch (RuntimeException x)
125
          {
126
            throw new InvalidKeySpecException(x.getMessage(), x);
127
          }
128
      }
129
    throw new InvalidKeySpecException("Unsupported (private) key specification");
130
  }
131
 
132
  protected KeySpec engineGetKeySpec(Key key, Class keySpec)
133
      throws InvalidKeySpecException
134
  {
135
    if (key instanceof DSAPublicKey)
136
      {
137
        if (keySpec.isAssignableFrom(DSAPublicKeySpec.class))
138
          {
139
            DSAPublicKey dsaKey = (DSAPublicKey) key;
140
            BigInteger p = dsaKey.getParams().getP();
141
            BigInteger q = dsaKey.getParams().getQ();
142
            BigInteger g = dsaKey.getParams().getG();
143
            BigInteger y = dsaKey.getY();
144
            return new DSAPublicKeySpec(y, p, q, g);
145
          }
146
        if (keySpec.isAssignableFrom(X509EncodedKeySpec.class))
147
          {
148
            if (key instanceof DSSPublicKey)
149
              {
150
                DSSPublicKey dssKey = (DSSPublicKey) key;
151
                byte[] encoded = dssKey.getEncoded(Registry.X509_ENCODING_ID);
152
                return new X509EncodedKeySpec(encoded);
153
              }
154
            if (Registry.X509_ENCODING_SORT_NAME.equalsIgnoreCase(key.getFormat()))
155
              {
156
                byte[] encoded = key.getEncoded();
157
                return new X509EncodedKeySpec(encoded);
158
              }
159
            throw new InvalidKeySpecException(
160
                "Wrong key type or unsupported (public) key specification");
161
          }
162
        throw new InvalidKeySpecException("Unsupported (public) key specification");
163
      }
164
    if (key instanceof DSAPrivateKey)
165
      {
166
        if (keySpec.isAssignableFrom(DSAPrivateKeySpec.class))
167
          {
168
            DSAPrivateKey dsaKey = (DSAPrivateKey) key;
169
            BigInteger p = dsaKey.getParams().getP();
170
            BigInteger q = dsaKey.getParams().getQ();
171
            BigInteger g = dsaKey.getParams().getG();
172
            BigInteger x = dsaKey.getX();
173
            return new DSAPrivateKeySpec(x, p, q, g);
174
          }
175
        if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class))
176
          {
177
            if (key instanceof DSSPrivateKey)
178
              {
179
                DSSPrivateKey dssKey = (DSSPrivateKey) key;
180
                byte[] encoded = dssKey.getEncoded(Registry.PKCS8_ENCODING_ID);
181
                return new PKCS8EncodedKeySpec(encoded);
182
              }
183
            if (Registry.PKCS8_ENCODING_SHORT_NAME.equalsIgnoreCase(key.getFormat()))
184
              {
185
                byte[] encoded = key.getEncoded();
186
                return new PKCS8EncodedKeySpec(encoded);
187
              }
188
            throw new InvalidKeySpecException(
189
                "Wrong key type or unsupported (private) key specification");
190
          }
191
        throw new InvalidKeySpecException("Unsupported (private) key specification");
192
      }
193
    throw new InvalidKeySpecException("Wrong key type or unsupported key specification");
194
  }
195
 
196
  protected Key engineTranslateKey(Key key) throws InvalidKeyException
197
  {
198
    if ((key instanceof DSSPublicKey) || (key instanceof DSSPrivateKey))
199
      return key;
200
 
201
    if (key instanceof DSAPublicKey)
202
      {
203
        DSAPublicKey dsaKey = (DSAPublicKey) key;
204
        BigInteger p = dsaKey.getParams().getP();
205
        BigInteger q = dsaKey.getParams().getQ();
206
        BigInteger g = dsaKey.getParams().getG();
207
        BigInteger y = dsaKey.getY();
208
        return new DSSPublicKey(Registry.X509_ENCODING_ID, p, q, g, y);
209
      }
210
    if (key instanceof DSAPrivateKey)
211
      {
212
        DSAPrivateKey dsaKey = (DSAPrivateKey) key;
213
        BigInteger p = dsaKey.getParams().getP();
214
        BigInteger q = dsaKey.getParams().getQ();
215
        BigInteger g = dsaKey.getParams().getG();
216
        BigInteger x = dsaKey.getX();
217
        return new DSSPrivateKey(Registry.PKCS8_ENCODING_ID, p, q, g, x);
218
      }
219
    throw new InvalidKeyException("Wrong key type");
220
  }
221
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.