comparison src/com/trilead/ssh2/signature/RSASHA1Verify.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
1
2 package com.trilead.ssh2.signature;
3
4 import java.io.IOException;
5 import java.math.BigInteger;
6 import java.security.InvalidKeyException;
7 import java.security.KeyFactory;
8 import java.security.NoSuchAlgorithmException;
9 import java.security.Signature;
10 import java.security.SignatureException;
11 import java.security.interfaces.RSAPrivateKey;
12 import java.security.interfaces.RSAPublicKey;
13 import java.security.spec.InvalidKeySpecException;
14 import java.security.spec.KeySpec;
15 import java.security.spec.RSAPublicKeySpec;
16
17 import com.trilead.ssh2.log.Logger;
18 import com.trilead.ssh2.packets.TypesReader;
19 import com.trilead.ssh2.packets.TypesWriter;
20
21
22 /**
23 * RSASHA1Verify.
24 *
25 * @author Christian Plattner, plattner@trilead.com
26 * @version $Id: RSASHA1Verify.java,v 1.1 2007/10/15 12:49:57 cplattne Exp $
27 */
28 public class RSASHA1Verify {
29 private static final Logger log = Logger.getLogger(RSASHA1Verify.class);
30
31 public static RSAPublicKey decodeSSHRSAPublicKey(byte[] key) throws IOException {
32 TypesReader tr = new TypesReader(key);
33 String key_format = tr.readString();
34
35 if (key_format.equals("ssh-rsa") == false)
36 throw new IllegalArgumentException("This is not a ssh-rsa public key");
37
38 BigInteger e = tr.readMPINT();
39 BigInteger n = tr.readMPINT();
40
41 if (tr.remain() != 0)
42 throw new IOException("Padding in RSA public key!");
43
44 KeySpec keySpec = new RSAPublicKeySpec(n, e);
45
46 try {
47 KeyFactory kf = KeyFactory.getInstance("RSA");
48 return (RSAPublicKey) kf.generatePublic(keySpec);
49 }
50 catch (NoSuchAlgorithmException nsae) {
51 IOException ioe = new IOException("No RSA KeyFactory available");
52 ioe.initCause(nsae);
53 throw ioe;
54 }
55 catch (InvalidKeySpecException ikse) {
56 IOException ioe = new IOException("No RSA KeyFactory available");
57 ioe.initCause(ikse);
58 throw ioe;
59 }
60 }
61
62 public static byte[] encodeSSHRSAPublicKey(RSAPublicKey pk) throws IOException {
63 TypesWriter tw = new TypesWriter();
64 tw.writeString("ssh-rsa");
65 tw.writeMPInt(pk.getPublicExponent());
66 tw.writeMPInt(pk.getModulus());
67 return tw.getBytes();
68 }
69
70 public static byte[] decodeSSHRSASignature(byte[] sig) throws IOException {
71 TypesReader tr = new TypesReader(sig);
72 String sig_format = tr.readString();
73
74 if (sig_format.equals("ssh-rsa") == false)
75 throw new IOException("Peer sent wrong signature format");
76
77 /* S is NOT an MPINT. "The value for 'rsa_signature_blob' is encoded as a string
78 * containing s (which is an integer, without lengths or padding, unsigned and in
79 * network byte order)." See also below.
80 */
81 byte[] s = tr.readByteString();
82
83 if (s.length == 0)
84 throw new IOException("Error in RSA signature, S is empty.");
85
86 if (log.isEnabled()) {
87 log.log(80, "Decoding ssh-rsa signature string (length: " + s.length + ")");
88 }
89
90 if (tr.remain() != 0)
91 throw new IOException("Padding in RSA signature!");
92
93 if (s[0] == 0 && s[1] == 0 && s[2] == 0) {
94 int i = 0;
95 int j = ((s[i++] << 24) & 0xff000000) | ((s[i++] << 16) & 0x00ff0000)
96 | ((s[i++] << 8) & 0x0000ff00) | ((s[i++]) & 0x000000ff);
97 i += j;
98 j = ((s[i++] << 24) & 0xff000000) | ((s[i++] << 16) & 0x00ff0000)
99 | ((s[i++] << 8) & 0x0000ff00) | ((s[i++]) & 0x000000ff);
100 byte[] tmp = new byte[j];
101 System.arraycopy(s, i, tmp, 0, j);
102 sig = tmp;
103 }
104
105 return s;
106 }
107
108 public static byte[] encodeSSHRSASignature(byte[] s) throws IOException {
109 TypesWriter tw = new TypesWriter();
110 tw.writeString("ssh-rsa");
111
112 /* S is NOT an MPINT. "The value for 'rsa_signature_blob' is encoded as a string
113 * containing s (which is an integer, without lengths or padding, unsigned and in
114 * network byte order)."
115 */
116
117 /* Remove first zero sign byte, if present */
118
119 if ((s.length > 1) && (s[0] == 0x00))
120 tw.writeString(s, 1, s.length - 1);
121 else
122 tw.writeString(s, 0, s.length);
123
124 return tw.getBytes();
125 }
126
127 public static byte[] generateSignature(byte[] message, RSAPrivateKey pk) throws IOException {
128 try {
129 Signature s = Signature.getInstance("SHA1withRSA");
130 s.initSign(pk);
131 s.update(message);
132 return s.sign();
133 }
134 catch (NoSuchAlgorithmException e) {
135 IOException ex = new IOException();
136 ex.initCause(e);
137 throw ex;
138 }
139 catch (InvalidKeyException e) {
140 IOException ex = new IOException();
141 ex.initCause(e);
142 throw ex;
143 }
144 catch (SignatureException e) {
145 IOException ex = new IOException();
146 ex.initCause(e);
147 throw ex;
148 }
149 }
150
151 public static boolean verifySignature(byte[] message, byte[] ds, RSAPublicKey dpk) throws IOException {
152 try {
153 Signature s = Signature.getInstance("SHA1withRSA");
154 s.initVerify(dpk);
155 s.update(message);
156 return s.verify(ds);
157 }
158 catch (NoSuchAlgorithmException e) {
159 IOException ex = new IOException();
160 ex.initCause(e);
161 throw ex;
162 }
163 catch (InvalidKeyException e) {
164 IOException ex = new IOException();
165 ex.initCause(e);
166 throw ex;
167 }
168 catch (SignatureException e) {
169 IOException ex = new IOException();
170 ex.initCause(e);
171 throw ex;
172 }
173 }
174 }