Changeset fbd230f


Ignore:
Timestamp:
Mar 27, 2012 8:25:26 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
bed548d
Parents:
dc402ac
Message:
  • RouterInfo?: Add method to validate sig during read-in for speed
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/data/RouterInfo.java

    rdc402ac rfbd230f  
    1414import java.io.InputStream;
    1515import java.io.OutputStream;
     16import java.security.DigestInputStream;
     17import java.security.MessageDigest;
    1618import java.util.ArrayList;
    1719import java.util.Collection;
     
    2628
    2729import net.i2p.I2PAppContext;
     30import net.i2p.crypto.DSAEngine;
     31import net.i2p.crypto.SHA1;
     32import net.i2p.crypto.SHA1Hash;
    2833import net.i2p.crypto.SHA256Generator;
    2934import net.i2p.util.Clock;
     
    333338
    334339    /**
     340     * Same as isValid()
     341     * @since 0.9
     342     */
     343    @Override
     344    public boolean verifySignature() {
     345        return isValid();
     346    }
     347
     348    /**
    335349     * which network is this routerInfo a part of.  configured through the property
    336350     * PROP_NETWORK_ID
     
    489503     */
    490504    public void readBytes(InputStream in) throws DataFormatException, IOException {
     505        readBytes(in, false);
     506    }
     507
     508    /**
     509     *  If verifySig is true,
     510     *  this validates the signature while reading in,
     511     *  and throws a DataFormatException if the sig is invalid.
     512     *  This is faster than reserializing to validate later.
     513     *
     514     *  @throws IllegalStateException if RouterInfo was already read in
     515     *  @since 0.9
     516     */
     517    public void readBytes(InputStream in, boolean verifySig) throws DataFormatException, IOException {
    491518        if (_signature != null)
    492519            throw new IllegalStateException();
     520        InputStream din;
     521        MessageDigest digest;
     522        if (verifySig) {
     523            digest = SHA1.getInstance();
     524            din = new DigestInputStream(in, digest);
     525        } else {
     526            digest = null;
     527            din = in;
     528        }
    493529        _identity = new RouterIdentity();
    494         _identity.readBytes(in);
     530        _identity.readBytes(din);
    495531        // avoid thrashing objects
    496532        //Date when = DataHelper.readDate(in);
     
    499535        //else
    500536        //    _published = when.getTime();
    501         _published = DataHelper.readLong(in, 8);
    502         int numAddresses = (int) DataHelper.readLong(in, 1);
     537        _published = DataHelper.readLong(din, 8);
     538        int numAddresses = (int) DataHelper.readLong(din, 1);
    503539        for (int i = 0; i < numAddresses; i++) {
    504540            RouterAddress address = new RouterAddress();
    505             address.readBytes(in);
     541            address.readBytes(din);
    506542            _addresses.add(address);
    507543        }
    508         int numPeers = (int) DataHelper.readLong(in, 1);
     544        int numPeers = (int) DataHelper.readLong(din, 1);
    509545        if (numPeers == 0) {
    510546            _peers = null;
     
    513549            for (int i = 0; i < numPeers; i++) {
    514550                Hash peerIdentityHash = new Hash();
    515                 peerIdentityHash.readBytes(in);
     551                peerIdentityHash.readBytes(din);
    516552                _peers.add(peerIdentityHash);
    517553            }
    518554        }
    519         DataHelper.readProperties(in, _options);
     555        DataHelper.readProperties(din, _options);
    520556        _signature = new Signature();
    521557        _signature.readBytes(in);
    522558
     559        SHA1Hash hash = new SHA1Hash(digest.digest());
     560        if (verifySig) {
     561            _isValid = DSAEngine.getInstance().verifySignature(_signature, hash, _identity.getSigningPublicKey());
     562            _validated = true;
     563            if (!_isValid) {
     564                throw new DataFormatException("Bad sig");
     565            }
     566        }
    523567
    524568        //_log.debug("Read routerInfo: " + toString());
     
    611655                     System.out.println(ri.toString());
    612656                 else
    613                      System.err.println("Router info " + args[i] + "is invalid");
     657                     System.err.println("Router info " + args[i] + " is invalid");
    614658             } catch (Exception e) {
    615659                 System.err.println("Error reading " + args[i] + ": " + e);
  • router/java/src/net/i2p/router/networkdb/kademlia/PersistentDataStore.java

    rdc402ac rfbd230f  
    425425                    fis = new BufferedInputStream(fis);
    426426                    RouterInfo ri = new RouterInfo();
    427                     ri.readBytes(fis);
     427                    ri.readBytes(fis, true);  // true = verify sig on read
    428428                    if (ri.getNetworkId() != Router.NETWORK_ID) {
    429429                        corrupt = true;
Note: See TracChangeset for help on using the changeset viewer.