KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > pki > rsa > SshRsaPublicKey


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.rsa;
21
22
23 import java.io.IOException JavaDoc;
24 import java.math.BigInteger JavaDoc;
25 import java.security.KeyFactory JavaDoc;
26 import java.security.NoSuchAlgorithmException JavaDoc;
27 import java.security.PublicKey JavaDoc;
28 import java.security.Signature JavaDoc;
29 import java.security.SignatureException JavaDoc;
30 import java.security.interfaces.RSAPublicKey JavaDoc;
31 import java.security.spec.InvalidKeySpecException JavaDoc;
32 import java.security.spec.RSAPublicKeySpec JavaDoc;
33
34 import com.maverick.util.ByteArrayReader;
35 import com.maverick.util.ByteArrayWriter;
36 import com.sslexplorer.security.pki.InvalidKeyException;
37 import com.sslexplorer.security.pki.InvalidSignatureException;
38 import com.sslexplorer.security.pki.SshPublicKey;
39
40
41 /**
42  *
43  *
44  * @author $author$
45  */

46 public class SshRsaPublicKey extends SshPublicKey {
47     RSAPublicKey JavaDoc pubKey;
48
49     /**
50      * Creates a new SshRsaPublicKey object.
51      *
52      * @param key
53      */

54     public SshRsaPublicKey(RSAPublicKey JavaDoc key) {
55         pubKey = key;
56     }
57     
58     
59     public PublicKey JavaDoc getPublicKey() {
60         return pubKey;
61     }
62
63     /**
64      * Creates a new SshRsaPublicKey object.
65      *
66      * @param encoded
67      *
68      * @throws InvalidKeyException
69      */

70     public SshRsaPublicKey(byte[] encoded) throws InvalidKeyException {
71         try {
72             //this.hostKey = hostKey;
73
RSAPublicKeySpec JavaDoc rsaKey;
74
75             // Extract the key information
76
ByteArrayReader bar = new ByteArrayReader(encoded);
77             String JavaDoc header = bar.readString();
78
79             if (!header.equals(getAlgorithmName())) {
80                 throw new InvalidKeyException();
81             }
82
83             BigInteger JavaDoc e = bar.readBigInteger();
84             BigInteger JavaDoc n = bar.readBigInteger();
85             rsaKey = new RSAPublicKeySpec JavaDoc(n, e);
86
87             try {
88                 KeyFactory JavaDoc kf = KeyFactory.getInstance("RSA");
89                 pubKey = (RSAPublicKey JavaDoc) kf.generatePublic(rsaKey);
90             } catch (NoSuchAlgorithmException JavaDoc nsae) {
91                 throw new InvalidKeyException();
92             } catch (InvalidKeySpecException JavaDoc ikpe) {
93                 throw new InvalidKeyException();
94             }
95         } catch (IOException JavaDoc ioe) {
96             throw new InvalidKeyException();
97         }
98     }
99
100     /**
101      *
102      *
103      * @return String
104      */

105     public String JavaDoc getAlgorithmName() {
106         return "ssh-rsa";
107     }
108
109     /**
110      *
111      *
112      * @return int
113      */

114     public int getBitLength() {
115         return pubKey.getModulus().bitLength();
116     }
117
118     /**
119      *
120      *
121      * @return byte[]
122      */

123     public byte[] getEncoded() {
124         try {
125             ByteArrayWriter baw = new ByteArrayWriter();
126             baw.writeString(getAlgorithmName());
127             baw.writeBigInteger(pubKey.getPublicExponent());
128             baw.writeBigInteger(pubKey.getModulus());
129
130             return baw.toByteArray();
131         } catch (IOException JavaDoc ioe) {
132             return null;
133         }
134     }
135
136     /**
137      *
138      *
139      * @param signature
140      * @param data
141      *
142      * @return boolean
143      *
144      * @throws InvalidSignatureException
145      */

146     public boolean verifySignature(byte[] signature, byte[] data)
147         throws InvalidSignatureException {
148         try {
149             // Check for older versions of the transport protocol
150
if (signature.length != 128) {
151                 ByteArrayReader bar = new ByteArrayReader(signature);
152                 byte[] sig = bar.readBinaryString();
153                 String JavaDoc header = new String JavaDoc(sig);
154
155                 if (!header.equals(getAlgorithmName())) {
156                     throw new InvalidSignatureException();
157                 }
158
159                 signature = bar.readBinaryString();
160             }
161
162             Signature JavaDoc s = Signature.getInstance("SHA1withRSA");
163             s.initVerify(pubKey);
164             s.update(data);
165
166             return s.verify(signature);
167         } catch (NoSuchAlgorithmException JavaDoc nsae) {
168             throw new InvalidSignatureException();
169         } catch (IOException JavaDoc ioe) {
170             throw new InvalidSignatureException();
171         } catch (java.security.InvalidKeyException JavaDoc ike) {
172             throw new InvalidSignatureException();
173         } catch (SignatureException JavaDoc se) {
174             throw new InvalidSignatureException();
175         }
176     }
177 }
178
Popular Tags