Changeset e391992 for core/java


Ignore:
Timestamp:
Oct 13, 2018 12:46:45 PM (21 months ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
e9ec043
Parents:
02f5733
Message:

Data: MetaLeaseSet2 data structure, LS2 test enhancements

Location:
core/java/src/net/i2p/data
Files:
1 added
1 edited

Legend:

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

    r02f5733 re391992  
    2121 */
    2222public class LeaseSet2 extends LeaseSet {
    23     private int _flags;
     23    protected int _flags;
    2424    // stored as absolute ms
    25     private long _published;
     25    protected long _published;
    2626    // stored as absolute ms
    27     private long _expires;
     27    protected long _expires;
    2828    // stored as absolute ms
    29     private long _transientExpires;
     29    protected long _transientExpires;
    3030    // if non-null, type of this is type of _signature in super
    31     private SigningPublicKey _transientSigningPublicKey;
     31    protected SigningPublicKey _transientSigningPublicKey;
    3232    // if non-null, type of this is type of SPK in the dest
    33     private Signature _offlineSignature;
     33    protected Signature _offlineSignature;
    3434    // may be null
    35     private Properties _options;
     35    protected Properties _options;
    3636    // only used for unknown types; else use _encryptionKey.getType()
    3737    private int _encType;
     
    187187            throw new IllegalStateException();
    188188        // LS2 header
    189         _destination = Destination.create(in);
    190         _published = DataHelper.readLong(in, 4) * 1000;
    191         _expires = _published + (DataHelper.readLong(in, 2) * 1000);
    192         _flags = (int) DataHelper.readLong(in, 2);
    193         if (isOffline()) {
    194             _transientExpires = DataHelper.readLong(in, 4) * 1000;
    195             int itype = (int) DataHelper.readLong(in, 2);
    196             SigType type = SigType.getByCode(itype);
    197             if (type == null)
    198                 throw new DataFormatException("Unknown sig type " + itype);
    199             _transientSigningPublicKey = new SigningPublicKey(type);
    200             _transientSigningPublicKey.readBytes(in);
    201             SigType stype = _destination.getSigningPublicKey().getType();
    202             _offlineSignature = new Signature(stype);
    203             _offlineSignature.readBytes(in);
    204         }
     189        readHeader(in);
    205190        // LS2 part
    206191        _options = DataHelper.readProperties(in);
     
    232217        _signature.readBytes(in);
    233218    }
    234    
     219
    235220    /**
    236221     *  Including sig. This does NOT validate the signature
     
    247232     *  Without sig. This does NOT validate the signature
    248233     */
    249     private void writeBytesWithoutSig(OutputStream out) throws DataFormatException, IOException {
     234    protected void writeBytesWithoutSig(OutputStream out) throws DataFormatException, IOException {
    250235        if (_destination == null || _encryptionKey == null)
    251236            throw new DataFormatException("Not enough data to write out a LeaseSet");
    252237        // LS2 header
     238        writeHeader(out);
     239        // LS2 part
     240        if (_options != null && !_options.isEmpty()) {
     241            DataHelper.writeProperties(out, _options);
     242        } else {
     243            DataHelper.writeLong(out, 2, 0);
     244        }
     245        EncType type = _encryptionKey.getType();
     246        if (type != null) {
     247            DataHelper.writeLong(out, 2, type.getCode());
     248        } else {
     249            DataHelper.writeLong(out, 2, _encType);
     250        }
     251        DataHelper.writeLong(out, 2, _encryptionKey.length());
     252        _encryptionKey.writeBytes(out);
     253        out.write((byte) _leases.size());
     254        for (Lease lease : _leases) {
     255            lease.writeBytes(out);
     256        }
     257    }
     258   
     259    protected void readHeader(InputStream in) throws DataFormatException, IOException {
     260        _destination = Destination.create(in);
     261        _published = DataHelper.readLong(in, 4) * 1000;
     262        _expires = _published + (DataHelper.readLong(in, 2) * 1000);
     263        _flags = (int) DataHelper.readLong(in, 2);
     264        if (isOffline())
     265            readOfflineBytes(in);
     266    }
     267
     268    protected void writeHeader(OutputStream out) throws DataFormatException, IOException {
    253269        _destination.writeBytes(out);
    254270        if (_published <= 0)
     
    257273        DataHelper.writeLong(out, 2, (_expires - _published) / 1000);
    258274        DataHelper.writeLong(out, 2, _flags);
    259         if (isOffline()) {
    260             if (_transientSigningPublicKey == null || _offlineSignature == null)
    261                 throw new DataFormatException("No offline key/sig");
    262             DataHelper.writeLong(out, 4, _transientExpires / 1000);
    263             DataHelper.writeLong(out, 2, _transientSigningPublicKey.getType().getCode());
    264             _transientSigningPublicKey.writeBytes(out);
    265             _offlineSignature.writeBytes(out);
    266         }
    267         // LS2 part
    268         if (_options != null && !_options.isEmpty()) {
    269             DataHelper.writeProperties(out, _options);
    270         } else {
    271             DataHelper.writeLong(out, 2, 0);
    272         }
    273         EncType type = _encryptionKey.getType();
    274         if (type != null) {
    275             DataHelper.writeLong(out, 2, type.getCode());
    276         } else {
    277             DataHelper.writeLong(out, 2, _encType);
    278         }
    279         DataHelper.writeLong(out, 2, _encryptionKey.length());
    280         _encryptionKey.writeBytes(out);
    281         out.write((byte) _leases.size());
    282         for (Lease lease : _leases) {
    283             lease.writeBytes(out);
    284         }
     275        if (isOffline())
     276            writeOfflineBytes(out);
     277    }
     278
     279    private void readOfflineBytes(InputStream in) throws DataFormatException, IOException {
     280        _transientExpires = DataHelper.readLong(in, 4) * 1000;
     281        int itype = (int) DataHelper.readLong(in, 2);
     282        SigType type = SigType.getByCode(itype);
     283        if (type == null)
     284            throw new DataFormatException("Unknown sig type " + itype);
     285        _transientSigningPublicKey = new SigningPublicKey(type);
     286        _transientSigningPublicKey.readBytes(in);
     287        SigType stype = _destination.getSigningPublicKey().getType();
     288        _offlineSignature = new Signature(stype);
     289        _offlineSignature.readBytes(in);
     290    }
     291
     292    private void writeOfflineBytes(OutputStream out) throws DataFormatException, IOException {
     293        if (_transientSigningPublicKey == null || _offlineSignature == null)
     294            throw new DataFormatException("No offline key/sig");
     295        DataHelper.writeLong(out, 4, _transientExpires / 1000);
     296        DataHelper.writeLong(out, 2, _transientSigningPublicKey.getType().getCode());
     297        _transientSigningPublicKey.writeBytes(out);
     298        _offlineSignature.writeBytes(out);
    285299    }
    286300   
     
    296310        if (isOffline())
    297311            rv += 2 + _transientSigningPublicKey.length() + _offlineSignature.length();
    298         if (_options != null && !_options.isEmpty())
    299             rv += 99; // TODO FIXME
    300         else
     312        if (_options != null && !_options.isEmpty()) {
     313            try {
     314                rv += DataHelper.toProperties(_options).length;
     315            } catch (DataFormatException dfe) {
     316                throw new IllegalStateException("bad options", dfe);
     317            }
     318        } else {
    301319            rv += 2;
     320        }
    302321        return rv;
    303322    }
     
    372391        if (isOffline()) {
    373392            buf.append("\n\tTransient Key: ").append(_transientSigningPublicKey);
    374             buf.append("\n\tExpires: ").append(new java.util.Date(_transientExpires));
     393            buf.append("\n\tTransient Expires: ").append(new java.util.Date(_transientExpires));
    375394            buf.append("\n\tOffline Signature: ").append(_offlineSignature);
    376395        }
     396        buf.append("\n\tOptions: ").append((_options != null) ? _options.size() : 0);
    377397        buf.append("\n\tSignature: ").append(_signature);
     398        buf.append("\n\tPublished: ").append(new java.util.Date(_published));
     399        buf.append("\n\tExpires: ").append(new java.util.Date(_expires));
    378400        buf.append("\n\tLeases: #").append(getLeaseCount());
    379401        for (int i = 0; i < getLeaseCount(); i++)
     
    398420        pkf.createIfAbsent(SigType.EdDSA_SHA512_Ed25519);
    399421        System.out.println("Online test");
    400         test(pkf, false);
     422        java.io.File f2 = new java.io.File("online-leaseset.dat");
     423        test(pkf, f2, false);
    401424        System.out.println("Offline test");
    402         test(pkf, true);
    403     }
    404 
    405     private static void test(PrivateKeyFile pkf, boolean offline) throws Exception {
     425        f2 = new java.io.File("offline-leaseset.dat");
     426        test(pkf, f2, true);
     427    }
     428
     429    private static void test(PrivateKeyFile pkf, java.io.File outfile, boolean offline) throws Exception {
    406430        net.i2p.util.RandomSource rand = net.i2p.util.RandomSource.getInstance();
    407431        long now = System.currentTimeMillis() + 5*60*1000;
     
    418442            ls2.addLease(l2);
    419443        }
     444        Properties opts = new Properties();
     445        opts.setProperty("foo", "bar");
     446        opts.setProperty("test", "bazzle");
     447        ls2.setOptions(opts);
    420448        ls2.setDestination(pkf.getDestination());
    421449        SimpleDataStructure encKeys[] = net.i2p.crypto.KeyGenerator.getInstance().generatePKIKeys();
     
    439467        ByteArrayOutputStream out = new ByteArrayOutputStream();
    440468        ls2.writeBytes(out);
     469        java.io.OutputStream out2 = new java.io.FileOutputStream(outfile);
     470        ls2.writeBytes(out2);
     471        out2.close();
    441472        java.io.ByteArrayInputStream in = new java.io.ByteArrayInputStream(out.toByteArray());
    442473        LeaseSet2 ls3 = new LeaseSet2();
Note: See TracChangeset for help on using the changeset viewer.