forked from I2P_Developers/i2p.i2p
NTCP: More NTCP 1 removal cleanup
This commit is contained in:
@@ -7,54 +7,13 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import net.i2p.router.Router;
|
||||
import net.i2p.router.RouterContext;
|
||||
import net.i2p.router.transport.crypto.DHSessionKeyBuilder;
|
||||
import net.i2p.util.Log;
|
||||
import net.i2p.util.SimpleByteCache;
|
||||
|
||||
/**
|
||||
* Inbound NTCP 1 or 2. Outbound NTCP 1 only.
|
||||
* Inbound NTCP 2 only.
|
||||
* OutboundNTCP2State does not extend this.
|
||||
*
|
||||
* NTCP 1 establishement overview:
|
||||
*
|
||||
* Handle the 4-phase establishment, which is as follows:
|
||||
*
|
||||
* <pre>
|
||||
*
|
||||
* Alice contacts Bob
|
||||
* =========================================================
|
||||
*
|
||||
* Message 1 (Session Request):
|
||||
* X+(H(X) xor Bob.identHash)----------------------------->
|
||||
*
|
||||
* Message 2 (Session Created):
|
||||
* <----------------------------------------Y+E(H(X+Y)+tsB, sk, Y[239:255])
|
||||
*
|
||||
* Message 3 (Session Confirm A):
|
||||
* E(sz+Alice.identity+tsA+padding+S(X+Y+Bob.identHash+tsA+tsB), sk, hX_xor_Bob.identHash[16:31])--->
|
||||
*
|
||||
* Message 4 (Session Confirm B):
|
||||
* <----------------------E(S(X+Y+Alice.identHash+tsA+tsB)+padding, sk, prev)
|
||||
*
|
||||
* Key:
|
||||
*
|
||||
* X, Y: 256 byte DH keys
|
||||
* H(): 32 byte SHA256 Hash
|
||||
* E(data, session key, IV): AES256 Encrypt
|
||||
* S(): 40 byte DSA Signature, or length as implied by sig type
|
||||
* tsA, tsB: timestamps (4 bytes, seconds since epoch)
|
||||
* sk: 32 byte Session key
|
||||
* sz: 2 byte size of Alice identity to follow
|
||||
*
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* Alternately, when Bob receives a connection, it could be a
|
||||
* check connection (perhaps prompted by Bob asking for someone
|
||||
* to verify his listener). check connections are formatted per
|
||||
* isCheckInfo()
|
||||
* NOTE: Check info is unused.
|
||||
*
|
||||
* @since 0.9.35 pulled out of EstablishState
|
||||
*/
|
||||
abstract class EstablishBase implements EstablishState {
|
||||
@@ -94,8 +53,6 @@ abstract class EstablishBase implements EstablishState {
|
||||
/** bytes received so far */
|
||||
protected int _received;
|
||||
|
||||
protected final DHSessionKeyBuilder _dh;
|
||||
|
||||
protected final NTCPTransport _transport;
|
||||
protected final NTCPConnection _con;
|
||||
|
||||
@@ -114,28 +71,7 @@ abstract class EstablishBase implements EstablishState {
|
||||
|
||||
protected enum State {
|
||||
OB_INIT,
|
||||
/** sent 1 */
|
||||
OB_SENT_X,
|
||||
/** sent 1, got 2 partial */
|
||||
OB_GOT_Y,
|
||||
/** sent 1, got 2 */
|
||||
OB_GOT_HXY,
|
||||
/** sent 1, got 2, sent 3 */
|
||||
OB_SENT_RI,
|
||||
/** sent 1, got 2, sent 3, got 4 */
|
||||
OB_GOT_SIG,
|
||||
|
||||
IB_INIT,
|
||||
/** got 1 partial */
|
||||
IB_GOT_X,
|
||||
/** got 1 */
|
||||
IB_GOT_HX,
|
||||
/** got 1, sent 2 */
|
||||
IB_SENT_Y,
|
||||
/** got 1, sent 2, got partial 3 */
|
||||
IB_GOT_RI_SIZE,
|
||||
/** got 1, sent 2, got 3 */
|
||||
IB_GOT_RI,
|
||||
|
||||
/**
|
||||
* Next state IB_NTCP2_GOT_X
|
||||
@@ -187,7 +123,6 @@ abstract class EstablishBase implements EstablishState {
|
||||
_Y = null;
|
||||
_hX_xor_bobIdentHash = null;
|
||||
_curDecrypted = null;
|
||||
_dh = null;
|
||||
_transport = null;
|
||||
_con = null;
|
||||
_e_hXY_tsB = null;
|
||||
@@ -198,19 +133,13 @@ abstract class EstablishBase implements EstablishState {
|
||||
_log = ctx.logManager().getLog(getClass());
|
||||
_transport = transport;
|
||||
_con = con;
|
||||
// null if NTCP1 disabled
|
||||
_dh = _transport.getDHBuilder();
|
||||
_hX_xor_bobIdentHash = SimpleByteCache.acquire(HXY_SIZE);
|
||||
if (_con.isInbound()) {
|
||||
_X = SimpleByteCache.acquire(XY_SIZE);
|
||||
_Y = (_dh != null) ?_dh.getMyPublicValueBytes() : null;
|
||||
_Y = null;
|
||||
} else {
|
||||
// OutboundNTCP2State does not extend this,
|
||||
// can't get here with NTCP1 disabled
|
||||
if (_dh == null)
|
||||
throw new IllegalStateException();
|
||||
_X = _dh.getMyPublicValueBytes();
|
||||
_Y = SimpleByteCache.acquire(XY_SIZE);
|
||||
}
|
||||
|
||||
_e_hXY_tsB = new byte[HXY_TSB_PAD_SIZE];
|
||||
@@ -314,8 +243,6 @@ abstract class EstablishBase implements EstablishState {
|
||||
SimpleByteCache.release(_prevEncrypted);
|
||||
SimpleByteCache.release(_curDecrypted);
|
||||
SimpleByteCache.release(_hX_xor_bobIdentHash);
|
||||
if (_dh != null && _dh.getPeerPublicValue() == null)
|
||||
_transport.returnUnused(_dh);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,7 +40,7 @@ import net.i2p.util.SimpleByteCache;
|
||||
|
||||
/**
|
||||
*
|
||||
* NTCP 1 or 2. We are Bob.
|
||||
* NTCP 2. We are Bob.
|
||||
*
|
||||
* @since 0.9.35 pulled out of EstablishState
|
||||
*/
|
||||
@@ -74,7 +74,6 @@ class InboundEstablishState extends EstablishBase implements NTCP2Payload.Payloa
|
||||
private static final int MAX_DATA_READ_BUFS = 32;
|
||||
private static final ByteCache _dataReadBufs = ByteCache.getInstance(MAX_DATA_READ_BUFS, BUFFER_SIZE);
|
||||
|
||||
private static final int NTCP1_MSG1_SIZE = XY_SIZE + HXY_SIZE;
|
||||
// 287 - 64 = 223
|
||||
private static final int PADDING1_MAX = TOTAL1_MAX - MSG1_SIZE;
|
||||
private static final int PADDING1_FAIL_MAX = 128;
|
||||
@@ -89,7 +88,6 @@ class InboundEstablishState extends EstablishBase implements NTCP2Payload.Payloa
|
||||
private static final Set<State> STATES_NTCP2 =
|
||||
EnumSet.of(State.IB_NTCP2_INIT, State.IB_NTCP2_GOT_X, State.IB_NTCP2_GOT_PADDING,
|
||||
State.IB_NTCP2_SENT_Y, State.IB_NTCP2_GOT_RI, State.IB_NTCP2_READ_RANDOM);
|
||||
private static final Set<State> STATES_MSG3 = EnumSet.of(State.IB_SENT_Y, State.IB_GOT_RI_SIZE, State.IB_GOT_RI);
|
||||
|
||||
|
||||
public InboundEstablishState(RouterContext ctx, NTCPTransport transport, NTCPConnection con) {
|
||||
@@ -123,17 +121,7 @@ class InboundEstablishState extends EstablishBase implements NTCP2Payload.Payloa
|
||||
* @since 0.9.35
|
||||
*/
|
||||
public int getVersion() {
|
||||
if (!_transport.isNTCP2Enabled())
|
||||
return 1;
|
||||
if (!_transport.isNTCP1Enabled())
|
||||
return 2;
|
||||
synchronized (_stateLock) {
|
||||
if (_state == State.IB_INIT)
|
||||
return 0;
|
||||
if (STATES_NTCP2.contains(_state))
|
||||
return 2;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -391,11 +379,6 @@ class InboundEstablishState extends EstablishBase implements NTCP2Payload.Payloa
|
||||
fail("Clock Skew: " + _peerSkew, null, true);
|
||||
return;
|
||||
}
|
||||
// If NTCP1 disabled, we allow longer padding
|
||||
if (_padlen1 > PADDING1_MAX && _transport.isNTCP1Enabled()) {
|
||||
fail("bad msg 1 padlen: " + _padlen1);
|
||||
return;
|
||||
}
|
||||
if (_msg3p2len < MSG3P2_MIN || _msg3p2len > MSG3P2_MAX) {
|
||||
fail("bad msg3p2 len: " + _msg3p2len);
|
||||
return;
|
||||
|
||||
@@ -18,7 +18,6 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.zip.Adler32;
|
||||
|
||||
import com.southernstorm.noise.protocol.CipherState;
|
||||
|
||||
@@ -144,8 +143,6 @@ public class NTCPConnection implements Closeable {
|
||||
static final int BUFFER_SIZE = 16*1024;
|
||||
private static final int MAX_DATA_READ_BUFS = 16;
|
||||
private static final ByteCache _dataReadBufs = ByteCache.getInstance(MAX_DATA_READ_BUFS, BUFFER_SIZE);
|
||||
/** 2 bytes for length and 4 for CRC */
|
||||
static final int NTCP1_MAX_MSG_SIZE = BUFFER_SIZE - (2 + 4);
|
||||
|
||||
private static final int INFO_PRIORITY = OutNetMessage.PRIORITY_MY_NETDB_STORE_LOW;
|
||||
private static final String FIXED_RI_VERSION = "0.9.12";
|
||||
@@ -620,18 +617,15 @@ public class NTCPConnection implements Closeable {
|
||||
static class PrepBuffer {
|
||||
final byte unencrypted[];
|
||||
int unencryptedLength;
|
||||
final Adler32 crc;
|
||||
byte encrypted[];
|
||||
|
||||
public PrepBuffer() {
|
||||
unencrypted = new byte[BUFFER_SIZE];
|
||||
crc = new Adler32();
|
||||
}
|
||||
|
||||
public void init() {
|
||||
unencryptedLength = 0;
|
||||
encrypted = null;
|
||||
crc.reset();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -416,7 +416,8 @@ public class NTCPTransport extends TransportImpl {
|
||||
// Queue the message, and our RI
|
||||
// doesn't do anything yet, just enqueues it
|
||||
con.send(msg);
|
||||
con.enqueueInfoMessage();
|
||||
// does nothing for outbound NTCP2
|
||||
//con.enqueueInfoMessage();
|
||||
} else if (shouldFlood || newVersion == 1) {
|
||||
// Queue the message, which is a DSM of our RI
|
||||
con.send(msg);
|
||||
@@ -501,9 +502,8 @@ public class NTCPTransport extends TransportImpl {
|
||||
return null;
|
||||
// passed in dataSize assumes 16 byte header, if NTCP2 then
|
||||
// we have a 9-byte header so there's 7 to spare
|
||||
if (dataSize > NTCPConnection.NTCP2_MAX_MSG_SIZE + 7 ||
|
||||
(!_enableNTCP2 && dataSize > NTCPConnection.NTCP1_MAX_MSG_SIZE)) {
|
||||
// Too big for NTCP2, or NTCP2 disabled and too big for NTCP1
|
||||
if (dataSize > NTCPConnection.NTCP2_MAX_MSG_SIZE + 7) {
|
||||
// Too big for NTCP2
|
||||
// Let SSU deal with it
|
||||
_context.statManager().addRateData("ntcp.noBidTooLargeI2NP", dataSize);
|
||||
return null;
|
||||
@@ -521,14 +521,6 @@ public class NTCPTransport extends TransportImpl {
|
||||
|
||||
boolean established = isEstablished(peer);
|
||||
if (established) { // should we check the queue size? nah, if its valid, use it
|
||||
if (dataSize > NTCPConnection.NTCP1_MAX_MSG_SIZE) {
|
||||
// Must be version 2 to send a big message
|
||||
NTCPConnection con = _conByIdent.get(peer);
|
||||
if (con == null || con.getVersion() < NTCP2_INT_VERSION) {
|
||||
_context.statManager().addRateData("ntcp.noBidTooLargeI2NP", dataSize);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return _fastBid;
|
||||
}
|
||||
if (toAddress.getNetworkId() != _networkID) {
|
||||
@@ -538,12 +530,6 @@ public class NTCPTransport extends TransportImpl {
|
||||
markUnreachable(peer);
|
||||
return null;
|
||||
}
|
||||
if (dataSize > NTCPConnection.NTCP1_MAX_MSG_SIZE) {
|
||||
// Not established, too big for NTCP 1, let SSU deal with it
|
||||
// TODO look at his addresses to see if NTCP2 supported?
|
||||
_context.statManager().addRateData("ntcp.noBidTooLargeI2NP", dataSize);
|
||||
return null;
|
||||
}
|
||||
|
||||
RouterAddress addr = getTargetAddress(toAddress);
|
||||
if (addr == null) {
|
||||
|
||||
Reference in New Issue
Block a user