Mercurial > 510Connectbot
comparison src/ch/ethz/ssh2/crypto/dh/DhExchange.java @ 309:cb179051f0f2 ganymed
add ecdsa key support everywhere
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 30 Jul 2014 14:29:39 -0700 |
parents | 071eccdff8ea |
children | 1d400fd78e4a |
comparison
equal
deleted
inserted
replaced
308:42b15aaa7ac7 | 309:cb179051f0f2 |
---|---|
1 /* | 1 /** |
2 * Copyright (c) 2006-2013 Christian Plattner. All rights reserved. | 2 * |
3 * Please refer to the LICENSE.txt for licensing details. | |
4 */ | 3 */ |
5 package ch.ethz.ssh2.crypto.dh; | 4 package ch.ethz.ssh2.crypto.dh; |
6 | 5 |
7 import java.io.IOException; | 6 import java.io.IOException; |
8 import java.math.BigInteger; | 7 import java.math.BigInteger; |
9 import java.security.SecureRandom; | 8 import java.security.InvalidAlgorithmParameterException; |
9 import java.security.InvalidKeyException; | |
10 import java.security.KeyFactory; | |
11 import java.security.KeyPair; | |
12 import java.security.KeyPairGenerator; | |
13 import java.security.NoSuchAlgorithmException; | |
14 import java.security.spec.InvalidKeySpecException; | |
10 | 15 |
11 import ch.ethz.ssh2.crypto.digest.HashForSSH2Types; | 16 import javax.crypto.KeyAgreement; |
12 import ch.ethz.ssh2.log.Logger; | 17 import javax.crypto.interfaces.DHPrivateKey; |
13 import ch.ethz.ssh2.util.StringEncoder; | 18 import javax.crypto.interfaces.DHPublicKey; |
19 import javax.crypto.spec.DHParameterSpec; | |
20 import javax.crypto.spec.DHPublicKeySpec; | |
14 | 21 |
15 /** | 22 /** |
16 * @author Christian Plattner | 23 * @author kenny |
17 * @version $Id: DhExchange.java 152 2014-04-28 11:02:23Z dkocher@sudo.ch $ | 24 * |
18 */ | 25 */ |
19 public class DhExchange { | 26 public class DhExchange extends GenericDhExchange { |
20 private static final Logger log = Logger.getLogger(DhExchange.class); | |
21 | 27 |
22 /* Given by the standard */ | 28 /* Given by the standard */ |
23 | 29 |
24 static final BigInteger p1, p14; | 30 private static final BigInteger P1 = new BigInteger( |
25 static final BigInteger g; | 31 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" |
32 + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" | |
33 + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" | |
34 + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" | |
35 + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" | |
36 + "FFFFFFFFFFFFFFFF", 16); | |
26 | 37 |
27 BigInteger p; | 38 private static final BigInteger P14 = new BigInteger( |
39 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" | |
40 + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" | |
41 + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" | |
42 + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" | |
43 + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" | |
44 + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" | |
45 + "83655D23DCA3AD961C62F356208552BB9ED529077096966D" | |
46 + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" | |
47 + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" | |
48 + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" | |
49 + "15728E5A8AACAA68FFFFFFFFFFFFFFFF", 16); | |
50 | |
51 private static final BigInteger G = BigInteger.valueOf(2); | |
28 | 52 |
29 /* Client public and private */ | 53 /* Client public and private */ |
30 | 54 |
31 BigInteger e; | 55 private DHPrivateKey clientPrivate; |
32 BigInteger x; | 56 private DHPublicKey clientPublic; |
33 | 57 |
34 /* Server public and private */ | 58 /* Server public */ |
35 | 59 |
36 BigInteger f; | 60 private DHPublicKey serverPublic; |
37 BigInteger y; | |
38 | 61 |
39 /* Shared secret */ | 62 @Override |
63 public void init(String name) throws IOException { | |
64 final DHParameterSpec spec; | |
40 | 65 |
41 BigInteger k; | 66 if ("diffie-hellman-group1-sha1".equals(name)) { |
67 spec = new DHParameterSpec(P1, G); | |
68 } | |
69 else if ("diffie-hellman-group14-sha1".equals(name)) { | |
70 spec = new DHParameterSpec(P14, G); | |
71 } | |
72 else { | |
73 throw new IllegalArgumentException("Unknown DH group " + name); | |
74 } | |
42 | 75 |
43 static { | 76 try { |
44 final String p1_string = "17976931348623159077083915679378745319786029604875" | 77 KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH"); |
45 + "60117064444236841971802161585193689478337958649255415021805654859805036464" | 78 kpg.initialize(spec); |
46 + "40548199239100050792877003355816639229553136239076508735759914822574862575" | 79 KeyPair pair = kpg.generateKeyPair(); |
47 + "00742530207744771258955095793777842444242661733472762929938766870920560605" | 80 clientPrivate = (DHPrivateKey) pair.getPrivate(); |
48 + "0270810842907692932019128194467627007"; | 81 clientPublic = (DHPublicKey) pair.getPublic(); |
49 final String p14_string = "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129" | 82 } |
50 + "024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0" | 83 catch (NoSuchAlgorithmException e) { |
51 + "A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB" | 84 throw(IOException) new IOException("No DH keypair generator").initCause(e); |
52 + "6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A" | 85 } |
53 + "163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208" | 86 catch (InvalidAlgorithmParameterException e) { |
54 + "552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36C" | 87 throw(IOException) new IOException("Invalid DH parameters").initCause(e); |
55 + "E3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF69558171" | 88 } |
56 + "83995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF"; | |
57 p1 = new BigInteger(p1_string); | |
58 p14 = new BigInteger(p14_string, 16); | |
59 g = new BigInteger("2"); | |
60 } | 89 } |
61 | 90 |
62 public DhExchange() { | 91 @Override |
92 public byte[] getE() { | |
93 if (clientPublic == null) | |
94 throw new IllegalStateException("DhExchange not initialized!"); | |
95 | |
96 return clientPublic.getY().toByteArray(); | |
63 } | 97 } |
64 | 98 |
65 public void clientInit(int group, SecureRandom rnd) { | 99 @Override |
66 k = null; | 100 protected byte[] getServerE() { |
101 if (serverPublic == null) | |
102 throw new IllegalStateException("DhExchange not initialized!"); | |
67 | 103 |
68 if (group == 1) { | 104 return serverPublic.getY().toByteArray(); |
69 p = p1; | 105 } |
106 | |
107 @Override | |
108 public void setF(byte[] f) throws IOException { | |
109 if (clientPublic == null) | |
110 throw new IllegalStateException("DhExchange not initialized!"); | |
111 | |
112 final KeyAgreement ka; | |
113 | |
114 try { | |
115 KeyFactory kf = KeyFactory.getInstance("DH"); | |
116 DHParameterSpec params = clientPublic.getParams(); | |
117 this.serverPublic = (DHPublicKey) kf.generatePublic(new DHPublicKeySpec( | |
118 new BigInteger(f), params.getP(), params.getG())); | |
119 ka = KeyAgreement.getInstance("DH"); | |
120 ka.init(clientPrivate); | |
121 ka.doPhase(serverPublic, true); | |
70 } | 122 } |
71 else if (group == 14) { | 123 catch (NoSuchAlgorithmException e) { |
72 p = p14; | 124 throw(IOException) new IOException("No DH key agreement method").initCause(e); |
73 } | 125 } |
74 else { | 126 catch (InvalidKeyException e) { |
75 throw new IllegalArgumentException("Unknown DH group " + group); | 127 throw(IOException) new IOException("Invalid DH key").initCause(e); |
128 } | |
129 catch (InvalidKeySpecException e) { | |
130 throw(IOException) new IOException("Invalid DH key").initCause(e); | |
76 } | 131 } |
77 | 132 |
78 while (true) { | 133 sharedSecret = new BigInteger(ka.generateSecret()); |
79 x = new BigInteger(p.bitLength() - 1, rnd); | |
80 | |
81 if (x.compareTo(BigInteger.ONE) > 0) { | |
82 break; | |
83 } | |
84 } | |
85 | |
86 e = g.modPow(x, p); | |
87 } | 134 } |
88 | 135 |
89 public void serverInit(int group, SecureRandom rnd) { | 136 @Override |
90 k = null; | 137 public String getHashAlgo() { |
91 | 138 return "SHA1"; |
92 if (group == 1) { | |
93 p = p1; | |
94 } | |
95 else if (group == 14) { | |
96 p = p14; | |
97 } | |
98 else { | |
99 throw new IllegalArgumentException("Unknown DH group " + group); | |
100 } | |
101 | |
102 y = new BigInteger(p.bitLength() - 1, rnd); | |
103 f = g.modPow(y, p); | |
104 } | |
105 | |
106 /** | |
107 * @return Returns the e. | |
108 * @throws IllegalStateException | |
109 */ | |
110 public BigInteger getE() { | |
111 if (e == null) { | |
112 throw new IllegalStateException("DhDsaExchange not initialized!"); | |
113 } | |
114 | |
115 return e; | |
116 } | |
117 | |
118 /** | |
119 * @return Returns the f. | |
120 * @throws IllegalStateException | |
121 */ | |
122 public BigInteger getF() { | |
123 if (f == null) { | |
124 throw new IllegalStateException("DhDsaExchange not initialized!"); | |
125 } | |
126 | |
127 return f; | |
128 } | |
129 | |
130 /** | |
131 * @return Returns the shared secret k. | |
132 * @throws IllegalStateException | |
133 */ | |
134 public BigInteger getK() { | |
135 if (k == null) { | |
136 throw new IllegalStateException("Shared secret not yet known, need f first!"); | |
137 } | |
138 | |
139 return k; | |
140 } | |
141 | |
142 /** | |
143 * @param f | |
144 */ | |
145 public void setF(BigInteger f) { | |
146 if (e == null) { | |
147 throw new IllegalStateException("DhDsaExchange not initialized!"); | |
148 } | |
149 | |
150 if (BigInteger.ZERO.compareTo(f) >= 0 || p.compareTo(f) <= 0) { | |
151 throw new IllegalArgumentException("Invalid f specified!"); | |
152 } | |
153 | |
154 this.f = f; | |
155 this.k = f.modPow(x, p); | |
156 } | |
157 | |
158 /** | |
159 * @param e | |
160 */ | |
161 public void setE(BigInteger e) { | |
162 if (f == null) { | |
163 throw new IllegalStateException("DhDsaExchange not initialized!"); | |
164 } | |
165 | |
166 if (BigInteger.ZERO.compareTo(e) >= 0 || p.compareTo(e) <= 0) { | |
167 throw new IllegalArgumentException("Invalid e specified!"); | |
168 } | |
169 | |
170 this.e = e; | |
171 this.k = e.modPow(y, p); | |
172 } | |
173 | |
174 public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload, | |
175 byte[] serverKexPayload, byte[] hostKey) throws IOException { | |
176 HashForSSH2Types hash = new HashForSSH2Types("SHA1"); | |
177 | |
178 if (log.isInfoEnabled()) { | |
179 log.info("Client: '" + StringEncoder.GetString(clientversion) + "'"); | |
180 log.info("Server: '" + StringEncoder.GetString(serverversion) + "'"); | |
181 } | |
182 | |
183 hash.updateByteString(clientversion); | |
184 hash.updateByteString(serverversion); | |
185 hash.updateByteString(clientKexPayload); | |
186 hash.updateByteString(serverKexPayload); | |
187 hash.updateByteString(hostKey); | |
188 hash.updateBigInt(e); | |
189 hash.updateBigInt(f); | |
190 hash.updateBigInt(k); | |
191 return hash.getDigest(); | |
192 } | 139 } |
193 } | 140 } |