0
|
1
|
|
2 package com.trilead.ssh2.crypto.dh;
|
|
3
|
|
4 import java.io.IOException;
|
|
5 import java.io.UnsupportedEncodingException;
|
|
6 import java.math.BigInteger;
|
|
7
|
|
8 import com.trilead.ssh2.crypto.digest.HashForSSH2Types;
|
|
9 import com.trilead.ssh2.log.Logger;
|
|
10
|
|
11
|
|
12 /**
|
|
13 * DhExchange.
|
|
14 *
|
|
15 * @author Christian Plattner, plattner@trilead.com
|
|
16 * @version $Id: DhExchange.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $
|
|
17 */
|
|
18 public abstract class GenericDhExchange {
|
|
19 private static final Logger log = Logger.getLogger(GenericDhExchange.class);
|
|
20
|
|
21 /* Shared secret */
|
|
22
|
|
23 BigInteger sharedSecret;
|
|
24
|
|
25 protected GenericDhExchange() {
|
|
26 }
|
|
27
|
|
28 public static GenericDhExchange getInstance(String algo) {
|
|
29 if (algo.startsWith("ecdh-sha2-")) {
|
|
30 return new EcDhExchange();
|
|
31 }
|
|
32 else {
|
|
33 return new DhExchange();
|
|
34 }
|
|
35 }
|
|
36
|
|
37 public abstract void init(String name) throws IOException;
|
|
38
|
|
39 /**
|
|
40 * @return Returns the e (public value)
|
|
41 * @throws IllegalStateException
|
|
42 */
|
|
43 public abstract byte[] getE();
|
|
44
|
|
45 /**
|
|
46 * @return Returns the server's e (public value)
|
|
47 * @throws IllegalStateException
|
|
48 */
|
|
49 protected abstract byte[] getServerE();
|
|
50
|
|
51 /**
|
|
52 * @return Returns the shared secret k.
|
|
53 * @throws IllegalStateException
|
|
54 */
|
|
55 public BigInteger getK() {
|
|
56 if (sharedSecret == null)
|
|
57 throw new IllegalStateException("Shared secret not yet known, need f first!");
|
|
58
|
|
59 return sharedSecret;
|
|
60 }
|
|
61
|
|
62 /**
|
|
63 * @param f
|
|
64 */
|
|
65 public abstract void setF(byte[] f) throws IOException;
|
|
66
|
|
67 public byte[] calculateH(byte[] clientversion, byte[] serverversion, byte[] clientKexPayload,
|
|
68 byte[] serverKexPayload, byte[] hostKey) throws UnsupportedEncodingException {
|
|
69 HashForSSH2Types hash = new HashForSSH2Types(getHashAlgo());
|
|
70
|
|
71 if (log.isEnabled()) {
|
|
72 log.log(90, "Client: '" + new String(clientversion, "ISO-8859-1") + "'");
|
|
73 log.log(90, "Server: '" + new String(serverversion, "ISO-8859-1") + "'");
|
|
74 }
|
|
75
|
|
76 hash.updateByteString(clientversion);
|
|
77 hash.updateByteString(serverversion);
|
|
78 hash.updateByteString(clientKexPayload);
|
|
79 hash.updateByteString(serverKexPayload);
|
|
80 hash.updateByteString(hostKey);
|
|
81 hash.updateByteString(getE());
|
|
82 hash.updateByteString(getServerE());
|
|
83 hash.updateBigInt(sharedSecret);
|
|
84 return hash.getDigest();
|
|
85 }
|
|
86
|
|
87 public abstract String getHashAlgo();
|
|
88 }
|