KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > pki > dsa > SshDssPrivateKey


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security.pki.dsa;
21
22 import java.io.IOException JavaDoc;
23 import java.math.BigInteger JavaDoc;
24 import java.security.KeyFactory JavaDoc;
25 import java.security.PrivateKey JavaDoc;
26 import java.security.Signature JavaDoc;
27 import java.security.interfaces.DSAPrivateKey JavaDoc;
28 import java.security.interfaces.DSAPublicKey JavaDoc;
29 import java.security.spec.DSAPrivateKeySpec JavaDoc;
30 import java.security.spec.DSAPublicKeySpec JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import com.maverick.util.ByteArrayReader;
36 import com.maverick.util.ByteArrayWriter;
37 import com.sslexplorer.security.pki.InvalidKeyException;
38 import com.sslexplorer.security.pki.InvalidSignatureException;
39 import com.sslexplorer.security.pki.SimpleASNReader;
40 import com.sslexplorer.security.pki.SshPrivateKey;
41 import com.sslexplorer.security.pki.SshPublicKey;
42
43
44 class SshDssPrivateKey extends SshPrivateKey {
45     private static Log log = LogFactory.getLog(SshDssPrivateKey.class);
46     DSAPrivateKey JavaDoc prvkey;
47
48     /**
49      * Creates a new SshDssPrivateKey object.
50      *
51      * @param prvkey
52      */

53     public SshDssPrivateKey(DSAPrivateKey JavaDoc prvkey) {
54         this.prvkey = prvkey;
55     }
56     
57     public PrivateKey JavaDoc getPrivateKey() {
58         return prvkey;
59     }
60
61     /**
62      * Creates a new SshDssPrivateKey object.
63      *
64      * @param key
65      *
66      * @throws InvalidKeyException
67      */

68     public SshDssPrivateKey(byte[] key) throws InvalidKeyException {
69         try {
70             DSAPrivateKeySpec JavaDoc dsaKey;
71
72             // Extract the key information
73
ByteArrayReader bar = new ByteArrayReader(key);
74             String JavaDoc header = bar.readString();
75
76             if (!header.equals(getAlgorithmName())) {
77                 throw new InvalidKeyException();
78             }
79
80             BigInteger JavaDoc p = bar.readBigInteger();
81             BigInteger JavaDoc q = bar.readBigInteger();
82             BigInteger JavaDoc g = bar.readBigInteger();
83             BigInteger JavaDoc x = bar.readBigInteger();
84             dsaKey = new DSAPrivateKeySpec JavaDoc(x, p, q, g);
85
86             KeyFactory JavaDoc kf = KeyFactory.getInstance("DSA");
87             prvkey = (DSAPrivateKey JavaDoc) kf.generatePrivate(dsaKey);
88         } catch (Exception JavaDoc e) {
89             throw new InvalidKeyException();
90         }
91     }
92
93     /**
94      *
95      *
96      * @param obj
97      *
98      * @return
99      */

100     public boolean equals(Object JavaDoc obj) {
101         if (obj instanceof SshDssPrivateKey) {
102             return prvkey.equals(((SshDssPrivateKey) obj).prvkey);
103         }
104
105         return false;
106     }
107
108     /**
109      *
110      *
111      * @return
112      */

113     public int hashCode() {
114         return prvkey.hashCode();
115     }
116
117     /**
118      *
119      *
120      * @return
121      */

122     public String JavaDoc getAlgorithmName() {
123         return "ssh-dss";
124     }
125
126     /**
127      *
128      *
129      * @return
130      */

131     public int getBitLength() {
132         return prvkey.getX().bitLength();
133     }
134
135     /**
136      *
137      *
138      * @return
139      */

140     public byte[] getEncoded() {
141         try {
142             ByteArrayWriter baw = new ByteArrayWriter();
143             baw.writeString("ssh-dss");
144             baw.writeBigInteger(prvkey.getParams().getP());
145             baw.writeBigInteger(prvkey.getParams().getQ());
146             baw.writeBigInteger(prvkey.getParams().getG());
147             baw.writeBigInteger(prvkey.getX());
148
149             return baw.toByteArray();
150         } catch (IOException JavaDoc ioe) {
151             return null;
152         }
153     }
154
155     /**
156      *
157      *
158      * @return
159      */

160     public SshPublicKey getPublicKey() {
161         try {
162             DSAPublicKeySpec JavaDoc spec = new DSAPublicKeySpec JavaDoc(getY(),
163                     prvkey.getParams().getP(), prvkey.getParams().getQ(),
164                     prvkey.getParams().getG());
165             KeyFactory JavaDoc kf = KeyFactory.getInstance("DSA");
166
167             return new SshDssPublicKey((DSAPublicKey JavaDoc) kf.generatePublic(spec));
168         } catch (Exception JavaDoc e) {
169             return null;
170         }
171     }
172
173     /**
174      *
175      *
176      * @param data
177      *
178      * @return
179      *
180      * @throws InvalidSshKeySignatureException
181      */

182     public byte[] generateSignature(byte[] data)
183         throws InvalidSignatureException {
184         try {
185             Signature JavaDoc sig = Signature.getInstance("SHA1withDSA");
186             sig.initSign(prvkey);
187
188             sig.update(data);
189
190             byte[] signature = sig.sign();
191             byte[] decoded = new byte[40];
192             SimpleASNReader asn = new SimpleASNReader(signature);
193             asn.getByte();
194             asn.getLength();
195             asn.getByte();
196
197             byte[] r = asn.getData();
198             asn.getByte();
199
200             byte[] s = asn.getData();
201
202             if (r.length >= 20) {
203                 System.arraycopy(r, r.length - 20, decoded, 0, 20);
204             } else {
205                 System.arraycopy(r, 0, decoded, 20 - r.length, r.length);
206             }
207
208             if (s.length >= 20) {
209                 System.arraycopy(s, s.length - 20, decoded, 20, 20);
210             } else {
211                 System.arraycopy(s, 0, decoded, 20 + (20 - s.length), s.length);
212             }
213
214             if (log.isDebugEnabled()) {
215                 log.debug("s length is " + String.valueOf(s.length));
216                 log.debug("r length is " + String.valueOf(r.length));
217
218                 String JavaDoc str = "";
219
220                 for (int i = 0; i < signature.length; i++) {
221                     str += (Integer.toHexString(signature[i] & 0xFF) + " ");
222                 }
223
224                 log.debug("Java signature is " + str);
225                 str = "";
226
227                 for (int i = 0; i < decoded.length; i++) {
228                     str += (Integer.toHexString(decoded[i] & 0xFF) + " ");
229                 }
230
231                 log.debug("SSH signature is " + str);
232             }
233
234             ByteArrayWriter baw = new ByteArrayWriter();
235             baw.writeString(getAlgorithmName());
236             baw.writeBinaryString(decoded);
237
238             return baw.toByteArray();
239         } catch (Exception JavaDoc e) {
240             throw new InvalidSignatureException(e);
241         }
242     }
243
244     private BigInteger JavaDoc getY() {
245         return prvkey.getParams().getG().modPow(prvkey.getX(),
246             prvkey.getParams().getP());
247     }
248 }
249
Popular Tags