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: /*
019: * TODO
020: * 1. The class extends the PrivateKeyImpl class in "org.apache.harmony.security" package.
021: *
022: * 2. See a compatibility with RI comments
023: * in the below "DSAPrivateKeyImpl(PKCS8EncodedKeySpec keySpec)" constructor.
024: */
025:
026: package org.apache.harmony.security.provider.crypto;
027:
028: import java.io.IOException;
029: import java.io.NotActiveException;
030: import java.math.BigInteger;
031: import java.security.interfaces.DSAParams;
032: import java.security.interfaces.DSAPrivateKey;
033: import java.security.spec.DSAParameterSpec;
034: import java.security.spec.DSAPrivateKeySpec;
035: import java.security.spec.InvalidKeySpecException;
036: import java.security.spec.PKCS8EncodedKeySpec;
037:
038: import org.apache.harmony.security.PrivateKeyImpl;
039: import org.apache.harmony.security.asn1.ASN1Integer;
040: import org.apache.harmony.security.internal.nls.Messages;
041: import org.apache.harmony.security.pkcs8.PrivateKeyInfo;
042: import org.apache.harmony.security.utils.AlgNameMapper;
043: import org.apache.harmony.security.x509.AlgorithmIdentifier;
044:
045: /**
046: * The class provides DSAPrivateKey functionality by extending a class implementing PrivateKey
047: * and implementing methods defined in both interfaces, DSAKey and DSAPrivateKey
048: */
049: public class DSAPrivateKeyImpl extends PrivateKeyImpl implements
050: DSAPrivateKey {
051:
052: /**
053: * @serial
054: */
055: private static final long serialVersionUID = -4716227614104950081L;
056:
057: private BigInteger x, g, p, q;
058:
059: private transient DSAParams params;
060:
061: /**
062: * Creates object from DSAPrivateKeySpec.
063: *
064: * @param keySpec - a DSAPrivateKeySpec object
065: */
066: public DSAPrivateKeyImpl(DSAPrivateKeySpec keySpec) {
067:
068: super ("DSA"); //$NON-NLS-1$
069:
070: PrivateKeyInfo pki;
071:
072: g = keySpec.getG();
073: p = keySpec.getP();
074: q = keySpec.getQ();
075:
076: ThreeIntegerSequence threeInts = new ThreeIntegerSequence(p
077: .toByteArray(), q.toByteArray(), g.toByteArray());
078:
079: AlgorithmIdentifier ai = new AlgorithmIdentifier(AlgNameMapper
080: .map2OID("DSA"), //$NON-NLS-1$
081: threeInts.getEncoded());
082: x = keySpec.getX();
083:
084: pki = new PrivateKeyInfo(0, ai, ASN1Integer.getInstance()
085: .encode(x.toByteArray()), null);
086:
087: setEncoding(pki.getEncoded());
088:
089: params = new DSAParameterSpec(p, q, g);
090: }
091:
092: /**
093: * Creates object from PKCS8EncodedKeySpec.
094: *
095: * @param keySpec - a XPKCS8EncodedKeySpec object
096: *
097: * @throws InvalidKeySpecException - if key data cannot be obtain from encoded format
098: */
099: public DSAPrivateKeyImpl(PKCS8EncodedKeySpec keySpec)
100: throws InvalidKeySpecException {
101:
102: super ("DSA"); //$NON-NLS-1$
103:
104: AlgorithmIdentifier ai;
105: ThreeIntegerSequence threeInts = null;
106:
107: String alg, algName;
108:
109: byte encoding[] = keySpec.getEncoded();
110:
111: PrivateKeyInfo privateKeyInfo = null;
112:
113: try {
114: privateKeyInfo = (PrivateKeyInfo) PrivateKeyInfo.ASN1
115: .decode(encoding);
116: } catch (IOException e) {
117: throw new InvalidKeySpecException(Messages.getString(
118: "security.19A", e)); //$NON-NLS-1$
119: }
120:
121: try {
122: x = new BigInteger((byte[]) ASN1Integer.getInstance()
123: .decode(privateKeyInfo.getPrivateKey()));
124: } catch (IOException e) {
125: throw new InvalidKeySpecException(Messages.getString(
126: "security.19B", e)); //$NON-NLS-1$
127: }
128:
129: ai = privateKeyInfo.getAlgorithmIdentifier();
130: try {
131: threeInts = (ThreeIntegerSequence) ThreeIntegerSequence.ASN1
132: .decode(ai.getParameters());
133: } catch (IOException e) {
134: throw new InvalidKeySpecException(Messages.getString(
135: "security.19B", e)); //$NON-NLS-1$
136: }
137: p = new BigInteger(threeInts.p);
138: q = new BigInteger(threeInts.q);
139: g = new BigInteger(threeInts.g);
140: params = new DSAParameterSpec(p, q, g);
141: setEncoding(encoding);
142:
143: /*
144: * the following code implements RI behavior
145: */
146: alg = ai.getAlgorithm();
147: algName = AlgNameMapper.map2AlgName(alg);
148: setAlgorithm(algName == null ? alg : algName);
149: }
150:
151: public BigInteger getX() {
152: return x;
153: }
154:
155: public DSAParams getParams() {
156: return params;
157: }
158:
159: private void readObject(java.io.ObjectInputStream in)
160: throws NotActiveException, IOException,
161: ClassNotFoundException {
162: in.defaultReadObject();
163: params = new DSAParameterSpec(p, q, g);
164: }
165:
166: }
|