001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.security.provider.crypto;
019:
020: import java.math.BigInteger;
021: import java.security.InvalidKeyException;
022: import java.security.Key;
023: import java.security.KeyFactorySpi;
024: import java.security.PrivateKey;
025: import java.security.PublicKey;
026: import java.security.interfaces.DSAParams;
027: import java.security.interfaces.DSAPrivateKey;
028: import java.security.interfaces.DSAPublicKey;
029: import java.security.spec.DSAPrivateKeySpec;
030: import java.security.spec.DSAPublicKeySpec;
031: import java.security.spec.InvalidKeySpecException;
032: import java.security.spec.KeySpec;
033: import java.security.spec.PKCS8EncodedKeySpec;
034: import java.security.spec.X509EncodedKeySpec;
035:
036: import org.apache.harmony.security.internal.nls.Messages;
037:
038: public class DSAKeyFactoryImpl extends KeyFactorySpi {
039:
040: /**
041: * This method generates a DSAPrivateKey object from the provided key specification.
042: *
043: * @param
044: * keySpec - the specification (key material) for the DSAPrivateKey.
045: *
046: * @return
047: * a DSAPrivateKey object
048: *
049: * @throws InvalidKeySpecException
050: * if "keySpec" is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec
051: */
052: protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
053: throws InvalidKeySpecException {
054:
055: if (keySpec != null) {
056: if (keySpec instanceof DSAPrivateKeySpec) {
057:
058: return new DSAPrivateKeyImpl(
059: (DSAPrivateKeySpec) keySpec);
060: }
061: if (keySpec instanceof PKCS8EncodedKeySpec) {
062:
063: return new DSAPrivateKeyImpl(
064: (PKCS8EncodedKeySpec) keySpec);
065: }
066: }
067: throw new InvalidKeySpecException(Messages
068: .getString("security.19C")); //$NON-NLS-1$
069: }
070:
071: /**
072: * This method generates a DSAPublicKey object from the provided key specification.
073: *
074: * @param
075: * keySpec - the specification (key material) for the DSAPublicKey.
076: *
077: * @return
078: * a DSAPublicKey object
079: *
080: * @throws InvalidKeySpecException
081: * if "keySpec" is neither DSAPublicKeySpec nor X509EncodedKeySpec
082: */
083: protected PublicKey engineGeneratePublic(KeySpec keySpec)
084: throws InvalidKeySpecException {
085:
086: if (keySpec != null) {
087: if (keySpec instanceof DSAPublicKeySpec) {
088:
089: return new DSAPublicKeyImpl((DSAPublicKeySpec) keySpec);
090: }
091: if (keySpec instanceof X509EncodedKeySpec) {
092:
093: return new DSAPublicKeyImpl(
094: (X509EncodedKeySpec) keySpec);
095: }
096: }
097: throw new InvalidKeySpecException(Messages
098: .getString("security.19D")); //$NON-NLS-1$
099: }
100:
101: /**
102: * This method returns a specification for the supplied key.
103: *
104: * The specification will be returned in the form of an object of the type
105: * specified by keySpec.
106: *
107: * @param key -
108: * either DSAPrivateKey or DSAPublicKey
109: * @param keySpec -
110: * either DSAPrivateKeySpec.class or DSAPublicKeySpec.class
111: *
112: * @return either a DSAPrivateKeySpec or a DSAPublicKeySpec
113: *
114: * @throws InvalidKeySpecException
115: * if "keySpec" is not a specification for DSAPublicKey or
116: * DSAPrivateKey
117: */
118: protected <T extends KeySpec> T engineGetKeySpec(Key key,
119: Class<T> keySpec) throws InvalidKeySpecException {
120:
121: BigInteger p, q, g, x, y;
122:
123: if (key != null) {
124: if (keySpec == null) {
125: throw new NullPointerException(Messages
126: .getString("security.19E")); //$NON-NLS-1$
127: }
128: if (key instanceof DSAPrivateKey) {
129: DSAPrivateKey privateKey = (DSAPrivateKey) key;
130:
131: if (keySpec.equals(DSAPrivateKeySpec.class)) {
132:
133: x = privateKey.getX();
134:
135: DSAParams params = privateKey.getParams();
136:
137: p = params.getP();
138: q = params.getQ();
139: g = params.getG();
140:
141: return (T) (new DSAPrivateKeySpec(x, p, q, g));
142: }
143:
144: if (keySpec.equals(PKCS8EncodedKeySpec.class)) {
145: return (T) (new PKCS8EncodedKeySpec(key
146: .getEncoded()));
147: }
148:
149: throw new InvalidKeySpecException(Messages
150: .getString("security.19C")); //$NON-NLS-1$
151: }
152:
153: if (key instanceof DSAPublicKey) {
154: DSAPublicKey publicKey = (DSAPublicKey) key;
155:
156: if (keySpec.equals(DSAPublicKeySpec.class)) {
157:
158: y = publicKey.getY();
159:
160: DSAParams params = publicKey.getParams();
161:
162: p = params.getP();
163: q = params.getQ();
164: g = params.getG();
165:
166: return (T) (new DSAPublicKeySpec(y, p, q, g));
167: }
168:
169: if (keySpec.equals(X509EncodedKeySpec.class)) {
170: return (T) (new X509EncodedKeySpec(key.getEncoded()));
171: }
172:
173: throw new InvalidKeySpecException(Messages
174: .getString("security.19D")); //$NON-NLS-1$
175: }
176: }
177: throw new InvalidKeySpecException(Messages
178: .getString("security.19F")); //$NON-NLS-1$
179: }
180:
181: /**
182: * The method generates a DSAPublicKey object from the provided key.
183: *
184: * @param
185: * key - a DSAPublicKey object or DSAPrivateKey object.
186: *
187: * @return
188: * object of the same type as the "key" argument
189: *
190: * @throws InvalidKeyException
191: * if "key" is neither DSAPublicKey nor DSAPrivateKey
192: */
193: protected Key engineTranslateKey(Key key)
194: throws InvalidKeyException {
195:
196: if (key != null) {
197: if (key instanceof DSAPrivateKey) {
198:
199: DSAPrivateKey privateKey = (DSAPrivateKey) key;
200: DSAParams params = privateKey.getParams();
201:
202: try {
203: return engineGeneratePrivate(new DSAPrivateKeySpec(
204: privateKey.getX(), params.getP(), params
205: .getQ(), params.getG()));
206: } catch (InvalidKeySpecException e) {
207: // Actually this exception shouldn't be thrown
208: throw new InvalidKeyException(Messages.getString(
209: "security.1A0", e)); //$NON-NLS-1$
210: }
211: }
212:
213: if (key instanceof DSAPublicKey) {
214:
215: DSAPublicKey publicKey = (DSAPublicKey) key;
216: DSAParams params = publicKey.getParams();
217:
218: try {
219: return engineGeneratePublic(new DSAPublicKeySpec(
220: publicKey.getY(), params.getP(), params
221: .getQ(), params.getG()));
222: } catch (InvalidKeySpecException e) {
223: // Actually this exception shouldn't be thrown
224: throw new InvalidKeyException(Messages.getString(
225: "security.1A1", e)); //$NON-NLS-1$
226: }
227: }
228: }
229: throw new InvalidKeyException(Messages
230: .getString("security.19F")); //$NON-NLS-1$
231: }
232:
233: }
|