|
| 1 | +package net.schmizz.sshj.transport.kex; |
| 2 | + |
| 3 | +import net.schmizz.sshj.common.*; |
| 4 | +import net.schmizz.sshj.signature.Signature; |
| 5 | +import net.schmizz.sshj.transport.Transport; |
| 6 | +import net.schmizz.sshj.transport.TransportException; |
| 7 | +import net.schmizz.sshj.transport.digest.Digest; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.math.BigInteger; |
| 12 | +import java.security.GeneralSecurityException; |
| 13 | +import java.security.PublicKey; |
| 14 | +import java.util.Arrays; |
| 15 | + |
| 16 | +public abstract class AbstractDHGex extends KeyExchangeBase { |
| 17 | + private final Logger log = LoggerFactory.getLogger(getClass()); |
| 18 | + |
| 19 | + private Digest digest; |
| 20 | + |
| 21 | + private int minBits = 1024; |
| 22 | + private int maxBits = 8192; |
| 23 | + private int preferredBits = 2048; |
| 24 | + |
| 25 | + private DH dh; |
| 26 | + private PublicKey hostKey; |
| 27 | + private byte[] H; |
| 28 | + |
| 29 | + public AbstractDHGex(Digest digest) { |
| 30 | + this.digest = digest; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void init(Transport trans, String V_S, String V_C, byte[] I_S, byte[] I_C) throws GeneralSecurityException, TransportException { |
| 35 | + super.init(trans, V_S, V_C, I_S, I_C); |
| 36 | + dh = new DH(); |
| 37 | + digest.init(); |
| 38 | + |
| 39 | + log.debug("Sending {}", Message.KEX_DH_GEX_REQUEST); |
| 40 | + trans.write(new SSHPacket(Message.KEX_DH_GEX_REQUEST).putUInt32(minBits).putUInt32(preferredBits).putUInt32(maxBits)); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public byte[] getH() { |
| 45 | + return Arrays.copyOf(H, H.length); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public BigInteger getK() { |
| 50 | + return dh.getK(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Digest getHash() { |
| 55 | + return digest; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public PublicKey getHostKey() { |
| 60 | + return hostKey; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean next(Message msg, SSHPacket buffer) throws GeneralSecurityException, TransportException { |
| 65 | + log.debug("Got message {}", msg); |
| 66 | + try { |
| 67 | + switch (msg) { |
| 68 | + case KEXDH_31: |
| 69 | + return parseGexGroup(buffer); |
| 70 | + case KEX_DH_GEX_REPLY: |
| 71 | + return parseGexReply(buffer); |
| 72 | + } |
| 73 | + } catch (Buffer.BufferException be) { |
| 74 | + throw new TransportException(be); |
| 75 | + } |
| 76 | + throw new TransportException("Unexpected message " + msg); |
| 77 | + } |
| 78 | + |
| 79 | + private boolean parseGexReply(SSHPacket buffer) throws Buffer.BufferException, GeneralSecurityException, TransportException { |
| 80 | + byte[] K_S = buffer.readBytes(); |
| 81 | + BigInteger f = buffer.readMPInt(); |
| 82 | + byte[] sig = buffer.readBytes(); |
| 83 | + hostKey = new Buffer.PlainBuffer(K_S).readPublicKey(); |
| 84 | + |
| 85 | + dh.computeK(f); |
| 86 | + BigInteger k = dh.getK(); |
| 87 | + |
| 88 | + final Buffer.PlainBuffer buf = initializedBuffer() |
| 89 | + .putString(K_S) |
| 90 | + .putUInt32(minBits) |
| 91 | + .putUInt32(preferredBits) |
| 92 | + .putUInt32(maxBits) |
| 93 | + .putMPInt(dh.getP()) |
| 94 | + .putMPInt(dh.getG()) |
| 95 | + .putMPInt(dh.getE()) |
| 96 | + .putMPInt(f) |
| 97 | + .putMPInt(k); |
| 98 | + digest.update(buf.array(), buf.rpos(), buf.available()); |
| 99 | + H = digest.digest(); |
| 100 | + Signature signature = Factory.Named.Util.create(trans.getConfig().getSignatureFactories(), |
| 101 | + KeyType.fromKey(hostKey).toString()); |
| 102 | + signature.init(hostKey, null); |
| 103 | + signature.update(H, 0, H.length); |
| 104 | + if (!signature.verify(sig)) |
| 105 | + throw new TransportException(DisconnectReason.KEY_EXCHANGE_FAILED, |
| 106 | + "KeyExchange signature verification failed"); |
| 107 | + return true; |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + private boolean parseGexGroup(SSHPacket buffer) throws Buffer.BufferException, GeneralSecurityException, TransportException { |
| 112 | + BigInteger p = buffer.readMPInt(); |
| 113 | + BigInteger g = buffer.readMPInt(); |
| 114 | + int bitLength = p.bitLength(); |
| 115 | + if (bitLength < minBits || bitLength > maxBits) { |
| 116 | + throw new GeneralSecurityException("Server generated gex p is out of range (" + bitLength + " bits)"); |
| 117 | + } |
| 118 | + log.debug("Received server p bitlength {}", bitLength); |
| 119 | + dh.init(p, g); |
| 120 | + log.debug("Sending {}", Message.KEX_DH_GEX_INIT); |
| 121 | + trans.write(new SSHPacket(Message.KEX_DH_GEX_INIT).putMPInt(dh.getE())); |
| 122 | + return false; |
| 123 | + } |
| 124 | +} |
0 commit comments