Changeset 79440f84


Ignore:
Timestamp:
Dec 1, 2018 11:40:10 AM (19 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
026ddb3
Parents:
922515df
Message:

I2CP: Add preliminary support for LS2 (proposal 123)

Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/client/impl/I2CPMessageProducer.java

    r922515df r79440f84  
    1818import net.i2p.client.I2PSessionException;
    1919import net.i2p.client.SendMessageOptions;
     20import net.i2p.data.DatabaseEntry;
    2021import net.i2p.data.DataFormatException;
    2122import net.i2p.data.Destination;
     
    2930import net.i2p.data.i2cp.AbuseSeverity;
    3031import net.i2p.data.i2cp.CreateLeaseSetMessage;
     32import net.i2p.data.i2cp.CreateLeaseSet2Message;
    3133import net.i2p.data.i2cp.CreateSessionMessage;
    3234import net.i2p.data.i2cp.DestroySessionMessage;
     
    329331
    330332    /**
    331      * Create a new signed leaseSet in response to a request to do so and send it
    332      * to the router
    333      *
     333     * In response to a RequestLeaseSet Message from the router, send a
     334     * CreateLeaseset Message back to the router.
     335     * This method is misnamed, it does not create the LeaseSet,
     336     * the caller does that.
     337     *
    334338     */
    335339    public void createLeaseSet(I2PSessionImpl session, LeaseSet leaseSet, SigningPrivateKey signingPriv,
    336340                               PrivateKey priv) throws I2PSessionException {
    337         CreateLeaseSetMessage msg = new CreateLeaseSetMessage();
     341        CreateLeaseSetMessage msg;
     342        int type = leaseSet.getType();
     343        if (type == DatabaseEntry.KEY_TYPE_LEASESET)
     344            msg = new CreateLeaseSetMessage();
     345        else
     346            msg = new CreateLeaseSet2Message();
    338347        msg.setLeaseSet(leaseSet);
    339348        msg.setPrivateKey(priv);
  • core/java/src/net/i2p/client/impl/I2PSessionImpl.java

    r922515df r79440f84  
    172172    private volatile boolean _routerSupportsFastReceive;
    173173    private volatile boolean _routerSupportsHostLookup;
     174    private volatile boolean _routerSupportsLS2;
    174175
    175176    protected static final int CACHE_MAX_SIZE = SystemVersion.isAndroid() ? 32 : 128;
     
    198199
    199200    private static final String MIN_FAST_VERSION = "0.9.4";
     201////// TESTING, change to 38 before release
     202    private static final String MIN_LS2_VERSION = "0.9.37";
    200203
    201204    /** @param routerVersion as rcvd in the SetDateMessage, may be null for very old routers */
    202205    void dateUpdated(String routerVersion) {
    203         _routerSupportsFastReceive = _context.isRouterContext() ||
     206        boolean isrc = _context.isRouterContext();
     207        _routerSupportsFastReceive = isrc ||
    204208                                     (routerVersion != null && routerVersion.length() > 0 &&
    205209                                      VersionComparator.comp(routerVersion, MIN_FAST_VERSION) >= 0);
    206         _routerSupportsHostLookup = _context.isRouterContext() ||
     210        _routerSupportsHostLookup = isrc ||
    207211                                    TEST_LOOKUP ||
    208212                                     (routerVersion != null && routerVersion.length() > 0 &&
    209213                                      VersionComparator.comp(routerVersion, MIN_HOST_LOOKUP_VERSION) >= 0);
    210         _routerSupportsSubsessions = _context.isRouterContext() ||
     214        _routerSupportsSubsessions = isrc ||
    211215                                     (routerVersion != null && routerVersion.length() > 0 &&
    212216                                      VersionComparator.comp(routerVersion, MIN_SUBSESSION_VERSION) >= 0);
     217        _routerSupportsLS2 = isrc ||
     218                                     (routerVersion != null && routerVersion.length() > 0 &&
     219                                      VersionComparator.comp(routerVersion, MIN_LS2_VERSION) >= 0);
    213220        synchronized (_stateLock) {
    214221            if (_state == State.OPENING) {
     
    282289            _signingPrivateKey = null;
    283290        }
    284         _routerSupportsFastReceive = _context.isRouterContext();
    285         _routerSupportsHostLookup = _context.isRouterContext();
    286         _routerSupportsSubsessions = _context.isRouterContext();
     291        boolean isrc = _context.isRouterContext();
     292        _routerSupportsFastReceive = isrc;
     293        _routerSupportsHostLookup = isrc;
     294        _routerSupportsSubsessions = isrc;
     295        _routerSupportsLS2 = isrc;
    287296    }
    288297
     
    508517    public boolean getFastReceive() {
    509518        return _fastReceive && _routerSupportsFastReceive;
     519    }
     520
     521    /**
     522     *  @since 0.9.38
     523     */
     524    public boolean supportsLS2() {
     525        return _routerSupportsLS2;
    510526    }
    511527
  • core/java/src/net/i2p/client/impl/RequestLeaseSetMessageHandler.java

    r922515df r79440f84  
    1818import net.i2p.I2PAppContext;
    1919import net.i2p.client.I2PSessionException;
     20import net.i2p.crypto.EncType;
    2021import net.i2p.crypto.KeyGenerator;
     22import net.i2p.crypto.KeyPair;
    2123import net.i2p.crypto.SigType;
     24import net.i2p.data.DatabaseEntry;
    2225import net.i2p.data.DataFormatException;
    2326import net.i2p.data.DataHelper;
     
    2528import net.i2p.data.Hash;
    2629import net.i2p.data.Lease;
     30import net.i2p.data.Lease2;
    2731import net.i2p.data.LeaseSet;
     32import net.i2p.data.LeaseSet2;
    2833import net.i2p.data.PrivateKey;
    2934import net.i2p.data.PublicKey;
     
    5964    }
    6065   
     66    /**
     67     *  Do we send a LeaseSet or a LeaseSet2?
     68     *  @since 0.9.38
     69     */
     70    protected static boolean requiresLS2(I2PSessionImpl session) {
     71        if (!session.supportsLS2())
     72            return false;
     73        String s = session.getOptions().getProperty("crypto.encType");
     74        if (s != null) {
     75            EncType type = EncType.parseEncType(s);
     76            if (type != null && type != EncType.ELGAMAL_2048 && type.isAvailable())
     77                return true;
     78        }
     79        s = session.getOptions().getProperty("i2cp.leaseSetType");
     80        if (s != null) {
     81            try {
     82                int type = Integer.parseInt(s);
     83                if (type != DatabaseEntry.KEY_TYPE_LEASESET)
     84                    return true;
     85            } catch (NumberFormatException nfe) {}
     86        }
     87        return false;
     88    }
     89
    6190    public void handleMessage(I2CPMessage message, I2PSessionImpl session) {
    6291        if (_log.shouldLog(Log.DEBUG))
    6392            _log.debug("Handle message " + message);
    6493        RequestLeaseSetMessage msg = (RequestLeaseSetMessage) message;
    65         LeaseSet leaseSet = new LeaseSet();
     94        boolean isLS2 = requiresLS2(session);
     95        LeaseSet leaseSet = isLS2 ? new LeaseSet2() : new LeaseSet();
    6696        for (int i = 0; i < msg.getEndpoints(); i++) {
    67             Lease lease = new Lease();
     97            Lease lease = isLS2 ? new Lease2() : new Lease();
    6898            lease.setGateway(msg.getRouter(i));
    6999            lease.setTunnelId(msg.getTunnelId(i));
     
    148178                }
    149179            } else {
    150                 li = new LeaseInfo(dest);
     180                EncType type = EncType.ELGAMAL_2048;
     181                String senc = session.getOptions().getProperty("crypto.encType");
     182                if (senc != null) {
     183                    EncType newtype = EncType.parseEncType(senc);
     184                    if (newtype != null) {
     185                        if (newtype.isAvailable()) {
     186                            type = newtype;
     187                            if (_log.shouldDebug())
     188                                _log.debug("Using crypto type: " + type);
     189                        } else {
     190                            _log.error("Unsupported crypto.encType: " + newtype);
     191                        }
     192                    } else {
     193                        _log.error("Bad crypto.encType: " + senc);
     194                    }
     195                } else {
     196                    if (_log.shouldDebug())
     197                        _log.debug("Using default crypto type");
     198                }
     199                li = new LeaseInfo(dest, type);
    151200                if (_log.shouldLog(Log.DEBUG))
    152201                    _log.debug("Creating new leaseInfo keys for " + dest + " without configured private keys");
     
    188237            // Send him a dummy DSA_SHA1 private key since it's unused anyway
    189238            // See CreateLeaseSetMessage.doReadMessage()
     239            // For LS1 only
    190240            SigningPrivateKey spk = li.getSigningPrivateKey();
    191             if (!_context.isRouterContext() && spk.getType() != SigType.DSA_SHA1) {
     241            if (!_context.isRouterContext() && spk.getType() != SigType.DSA_SHA1 &&
     242                !(leaseSet instanceof LeaseSet2)) {
    192243                byte[] dummy = new byte[SigningPrivateKey.KEYSIZE_BYTES];
    193244                _context.random().nextBytes(dummy);
     
    196247            session.getProducer().createLeaseSet(session, leaseSet, spk, li.getPrivateKey());
    197248            session.setLeaseSet(leaseSet);
     249            if (_log.shouldDebug())
     250                _log.debug("Created and signed LeaseSet: " + leaseSet);
    198251        } catch (DataFormatException dfe) {
    199252            session.propogateError("Error signing the leaseSet", dfe);
     
    221274         *  New keys
    222275         */
    223         public LeaseInfo(Destination dest) {
    224             SimpleDataStructure encKeys[] = KeyGenerator.getInstance().generatePKIKeys();
     276        public LeaseInfo(Destination dest, EncType type) {
     277            KeyPair encKeys = KeyGenerator.getInstance().generatePKIKeys(type);
    225278            // must be same type as the Destination's signing key
    226279            SimpleDataStructure signKeys[];
     
    230283                throw new IllegalStateException(gse);
    231284            }
    232             _pubKey = (PublicKey) encKeys[0];
    233             _privKey = (PrivateKey) encKeys[1];
     285            _pubKey = encKeys.getPublic();
     286            _privKey = encKeys.getPrivate();
    234287            _signingPubKey = (SigningPublicKey) signKeys[0];
    235288            _signingPrivKey = (SigningPrivateKey) signKeys[1];
  • core/java/src/net/i2p/client/impl/RequestVariableLeaseSetMessageHandler.java

    r922515df r79440f84  
    1111
    1212import net.i2p.I2PAppContext;
     13import net.i2p.data.Lease;
     14import net.i2p.data.Lease2;
    1315import net.i2p.data.LeaseSet;
     16import net.i2p.data.LeaseSet2;
    1417import net.i2p.data.i2cp.I2CPMessage;
    1518import net.i2p.data.i2cp.RequestVariableLeaseSetMessage;
     
    3336            _log.debug("Handle message " + message);
    3437        RequestVariableLeaseSetMessage msg = (RequestVariableLeaseSetMessage) message;
    35         LeaseSet leaseSet = new LeaseSet();
     38        boolean isLS2 = requiresLS2(session);
     39        LeaseSet leaseSet = isLS2 ? new LeaseSet2() : new LeaseSet();
    3640        for (int i = 0; i < msg.getEndpoints(); i++) {
    37             leaseSet.addLease(msg.getEndpoint(i));
     41            Lease lease;
     42            if (isLS2) {
     43                // convert Lease to Lease2
     44                Lease old = msg.getEndpoint(i);
     45                lease = new Lease2();
     46                lease.setGateway(old.getGateway());
     47                lease.setTunnelId(old.getTunnelId());
     48                lease.setEndDate(old.getEndDate());
     49            } else {
     50                lease = msg.getEndpoint(i);
     51            }
     52            leaseSet.addLease(lease);
    3853        }
    3954        signLeaseSet(leaseSet, session);
  • core/java/src/net/i2p/data/i2cp/CreateLeaseSetMessage.java

    r922515df r79440f84  
    2727public class CreateLeaseSetMessage extends I2CPMessageImpl {
    2828    public final static int MESSAGE_TYPE = 4;
    29     private SessionId _sessionId;
    30     private LeaseSet _leaseSet;
    31     private SigningPrivateKey _signingPrivateKey;
    32     private PrivateKey _privateKey;
     29    protected SessionId _sessionId;
     30    protected LeaseSet _leaseSet;
     31    protected SigningPrivateKey _signingPrivateKey;
     32    protected PrivateKey _privateKey;
    3333
    3434    public CreateLeaseSetMessage() {
  • core/java/src/net/i2p/data/i2cp/I2CPMessageHandler.java

    r922515df r79440f84  
    113113        case HostReplyMessage.MESSAGE_TYPE:
    114114            return new HostReplyMessage();
     115        case CreateLeaseSet2Message.MESSAGE_TYPE:
     116            return new CreateLeaseSet2Message();
    115117        default:
    116118            throw new I2CPMessageException("The type " + type + " is an unknown I2CP message");
  • router/java/src/net/i2p/router/client/ClientMessageEventListener.java

    r922515df r79440f84  
    2121import net.i2p.data.i2cp.BandwidthLimitsMessage;
    2222import net.i2p.data.i2cp.CreateLeaseSetMessage;
     23import net.i2p.data.i2cp.CreateLeaseSet2Message;
    2324import net.i2p.data.i2cp.CreateSessionMessage;
    2425import net.i2p.data.i2cp.DestLookupMessage;
     
    127128                break;
    128129            case CreateLeaseSetMessage.MESSAGE_TYPE:
     130            case CreateLeaseSet2Message.MESSAGE_TYPE:
    129131                handleCreateLeaseSet((CreateLeaseSetMessage)message);
    130132                break;
Note: See TracChangeset for help on using the changeset viewer.