Changeset cc271de


Ignore:
Timestamp:
Jul 22, 2013 2:25:30 PM (7 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
dff357a
Parents:
a7485ab
Message:
Location:
router/java/src/net/i2p/router/tunnel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/tunnel/BuildReplyHandler.java

    ra7485ab rcc271de  
    1111import net.i2p.data.i2np.TunnelBuildReplyMessage;
    1212import net.i2p.util.Log;
     13import net.i2p.util.SimpleByteCache;
    1314
    1415/**
     
    1718 *
    1819 */
    19 public abstract class BuildReplyHandler {
     20public class BuildReplyHandler {
     21
     22    private final I2PAppContext ctx;
     23    private final Log log;
    2024
    2125    /**
    22      * Decrypt the tunnel build reply records.  This overwrites the contents of the reply
     26     *  @since 0.9.8 (methods were static before)
     27     */
     28    public BuildReplyHandler(I2PAppContext context) {
     29        ctx = context;
     30        log = ctx.logManager().getLog(BuildReplyHandler.class);
     31    }
     32
     33    /**
     34     * Decrypt the tunnel build reply records.  This overwrites the contents of the reply.
     35     * Thread safe (no state).
    2336     *
    2437     * @return status for the records (in record order), or null if the replies were not valid.  Fake records
    2538     *         always have 0 as their value
    2639     */
    27     public static int[] decrypt(I2PAppContext ctx, TunnelBuildReplyMessage reply, TunnelCreatorConfig cfg, List<Integer> recordOrder) {
    28         Log log = ctx.logManager().getLog(BuildReplyHandler.class);
     40    public int[] decrypt(TunnelBuildReplyMessage reply, TunnelCreatorConfig cfg, List<Integer> recordOrder) {
    2941        if (reply.getRecordCount() != recordOrder.size()) {
    3042            // somebody messed with us
     
    4153                rv[i] = 0;
    4254            } else {
    43                 int ok = decryptRecord(ctx, reply, cfg, i, hop);
     55                int ok = decryptRecord(reply, cfg, i, hop);
    4456                if (ok == -1) {
    4557                    if (log.shouldLog(Log.WARN))
     
    6173     * @return -1 on decrypt failure
    6274     */
    63     private static int decryptRecord(I2PAppContext ctx, TunnelBuildReplyMessage reply, TunnelCreatorConfig cfg, int recordNum, int hop) {
    64         Log log = ctx.logManager().getLog(BuildReplyHandler.class);
     75    private int decryptRecord(TunnelBuildReplyMessage reply, TunnelCreatorConfig cfg, int recordNum, int hop) {
    6576        if (BuildMessageGenerator.isBlank(cfg, hop)) {
    6677            if (log.shouldLog(Log.DEBUG))
     
    6980        }
    7081        ByteArray rec = reply.getRecord(recordNum);
     82        byte[] data = rec.getData();
    7183        int off = rec.getOffset();
    7284        int start = cfg.getLength() - 1;
     
    7991            byte replyIV[] = hopConfig.getReplyIV().getData();
    8092            int replyIVOff = hopConfig.getReplyIV().getOffset();
    81             if (log.shouldLog(Log.DEBUG))
     93            if (log.shouldLog(Log.DEBUG)) {
    8294                log.debug(reply.getUniqueId() + ": Decrypting record " + recordNum + "/" + hop + "/" + j + " with replyKey "
    8395                          + replyKey.toBase64() + "/" + Base64.encode(replyIV, replyIVOff, 16) + ": " + cfg);
     96                log.debug(reply.getUniqueId() + ": before decrypt("+ off + "-"+(off+rec.getValid())+"): " + Base64.encode(data, off, rec.getValid()));
     97                log.debug(reply.getUniqueId() + ": Full reply rec: offset=" + off + ", sz=" + data.length + "/" + rec.getValid() + ", data=" + Base64.encode(data, off, TunnelBuildReplyMessage.RECORD_SIZE));
     98            }
     99            ctx.aes().decrypt(data, off, data, off, replyKey, replyIV, replyIVOff, TunnelBuildReplyMessage.RECORD_SIZE);
    84100            if (log.shouldLog(Log.DEBUG))
    85                 log.debug(reply.getUniqueId() + ": before decrypt("+ off + "-"+(off+rec.getValid())+"): " + Base64.encode(rec.getData(), off, rec.getValid()));
    86            
    87             if (log.shouldLog(Log.DEBUG))
    88                 log.debug(reply.getUniqueId() + ": Full reply rec: offset=" + off + ", sz=" + rec.getData().length + "/" + rec.getValid() + ", data=" + Base64.encode(rec.getData(), off, TunnelBuildReplyMessage.RECORD_SIZE));
    89             ctx.aes().decrypt(rec.getData(), off, rec.getData(), off, replyKey, replyIV, replyIVOff, TunnelBuildReplyMessage.RECORD_SIZE);
    90             if (log.shouldLog(Log.DEBUG))
    91                 log.debug(reply.getUniqueId() + ": after decrypt: " + Base64.encode(rec.getData(), off, rec.getValid()));
     101                log.debug(reply.getUniqueId() + ": after decrypt: " + Base64.encode(data, off, rec.getValid()));
    92102        }
    93103        // ok, all of the layered encryption is stripped, so lets verify it
    94104        // (formatted per BuildResponseRecord.create)
    95         Hash h = ctx.sha().calculateHash(rec.getData(), off + Hash.HASH_LENGTH, TunnelBuildReplyMessage.RECORD_SIZE-Hash.HASH_LENGTH);
    96         if (!DataHelper.eq(h.getData(), 0, rec.getData(), off, Hash.HASH_LENGTH)) {
     105        // don't cache the result
     106        //Hash h = ctx.sha().calculateHash(data, off + Hash.HASH_LENGTH, TunnelBuildReplyMessage.RECORD_SIZE-Hash.HASH_LENGTH);
     107        byte[] h = SimpleByteCache.acquire(Hash.HASH_LENGTH);
     108        ctx.sha().calculateHash(data, off + Hash.HASH_LENGTH, TunnelBuildReplyMessage.RECORD_SIZE-Hash.HASH_LENGTH, h, 0);
     109        boolean ok = DataHelper.eq(h, 0, data, off, Hash.HASH_LENGTH);
     110        if (!ok) {
    97111            if (log.shouldLog(Log.DEBUG))
    98                 log.debug(reply.getUniqueId() + ": Failed verification on " + recordNum + "/" + hop + ": " + h.toBase64() + " calculated, " +
    99                           Base64.encode(rec.getData(), off, Hash.HASH_LENGTH) + " expected\n" +
    100                           "Record: " + Base64.encode(rec.getData(), off+Hash.HASH_LENGTH, TunnelBuildReplyMessage.RECORD_SIZE-Hash.HASH_LENGTH));
     112                log.debug(reply.getUniqueId() + ": Failed verification on " + recordNum + "/" + hop + ": " + Base64.encode(h) + " calculated, " +
     113                          Base64.encode(data, off, Hash.HASH_LENGTH) + " expected\n" +
     114                          "Record: " + Base64.encode(data, off+Hash.HASH_LENGTH, TunnelBuildReplyMessage.RECORD_SIZE-Hash.HASH_LENGTH));
     115            SimpleByteCache.release(h);
    101116            return -1;
    102117        } else {
    103             int rv = (int)DataHelper.fromLong(rec.getData(), off + TunnelBuildReplyMessage.RECORD_SIZE - 1, 1);
     118            SimpleByteCache.release(h);
     119            int rv = (int)DataHelper.fromLong(data, off + TunnelBuildReplyMessage.RECORD_SIZE - 1, 1);
    104120            if (log.shouldLog(Log.DEBUG))
    105121                log.debug(reply.getUniqueId() + ": Verified: " + rv + " for record " + recordNum + "/" + hop);
  • router/java/src/net/i2p/router/tunnel/pool/BuildHandler.java

    ra7485ab rcc271de  
    6060    private final RequestThrottler _requestThrottler;
    6161    private final ParticipatingThrottler _throttler;
     62    private final BuildReplyHandler _buildReplyHandler;
    6263    private final AtomicInteger _currentLookups = new AtomicInteger();
    6364    private volatile boolean _isRunning;
     
    135136        // used for previous and next hops, for successful builds only
    136137        _throttler = new ParticipatingThrottler(ctx);
     138        _buildReplyHandler = new BuildReplyHandler(ctx);
    137139        _buildMessageHandlerJob = new TunnelBuildMessageHandlerJob(ctx);
    138140        _buildReplyMessageHandlerJob = new TunnelBuildReplyMessageHandlerJob(ctx);
     
    247249       
    248250        List<Integer> order = cfg.getReplyOrder();
    249         int statuses[] = BuildReplyHandler.decrypt(_context, msg, cfg, order);
     251        int statuses[] = _buildReplyHandler.decrypt(msg, cfg, order);
    250252        if (statuses != null) {
    251253            boolean allAgree = true;
Note: See TracChangeset for help on using the changeset viewer.