Changeset 82e3440


Ignore:
Timestamp:
Dec 24, 2010 5:39:31 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
ff0c168
Parents:
63c6613
Message:
  • Certificate: Use a single static null cert in place of

tens of thousands of them

Files:
6 edited

Legend:

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

    r63c6613 r82e3440  
    2727 */
    2828public class Certificate extends DataStructureImpl {
    29     private int _type;
    30     private byte[] _payload;
     29    public final static Certificate NULL_CERT = new NullCert();
     30
     31    protected int _type;
     32    protected byte[] _payload;
    3133
    3234    /** Specifies a null certificate type with no payload */
     
    4143    /** Contains multiple certs */
    4244    public final static int CERTIFICATE_TYPE_MULTIPLE = 4;
     45
     46    /**
     47     * If null cert, return immutable static instance, else create new
     48     * @since 0.8.3
     49     */
     50    public static Certificate create(InputStream in) throws DataFormatException, IOException {
     51        int type = (int) DataHelper.readLong(in, 1);
     52        int length = (int) DataHelper.readLong(in, 2);
     53        if (type == 0 && length == 0)
     54            return NULL_CERT;
     55        // from here down roughly the same as readBytes() below
     56        if (length == 0)
     57            return new Certificate(type, null);
     58        byte[] payload = new byte[length];
     59        int read = DataHelper.read(in, payload);
     60        if (read != length)
     61            throw new DataFormatException("Not enough bytes for the payload (read: " + read + " length: " + length + ')');
     62        return new Certificate(type, payload);
     63    }
    4364
    4465    public Certificate() {
     
    141162        return _type == cert.getCertificateType() && DataHelper.eq(_payload, cert.getPayload());
    142163    }
     164
    143165    @Override
    144166    public int hashCode() {
    145167        return _type + DataHelper.hashCode(_payload);
    146168    }
     169
    147170    @Override
    148171    public String toString() {
     
    178201        return buf.toString();
    179202    }
     203
     204    /**
     205     *  An immutable null certificate.
     206     *  @since 0.8.3
     207     */
     208    private static final class NullCert extends Certificate {
     209        private static final int NULL_LENGTH = 1 + 2;
     210        private static final byte[] NULL_DATA = new byte[NULL_LENGTH];
     211
     212        public NullCert() {
     213            // zero already
     214            //_type = CERTIFICATE_TYPE_NULL;
     215        }
     216
     217        /** @throws RuntimeException always */
     218        @Override
     219        public void setCertificateType(int type) {
     220            throw new RuntimeException("Data already set");
     221        }
     222
     223        /** @throws RuntimeException always */
     224        @Override
     225        public void setPayload(byte[] payload) {
     226            throw new RuntimeException("Data already set");
     227        }
     228   
     229        /** @throws RuntimeException always */
     230        @Override
     231        public void readBytes(InputStream in) throws DataFormatException, IOException {
     232            throw new RuntimeException("Data already set");
     233        }
     234   
     235        /** Overridden for efficiency */
     236        @Override
     237        public void writeBytes(OutputStream out) throws IOException {
     238            out.write(NULL_DATA);
     239        }
     240   
     241        /** Overridden for efficiency */
     242        @Override
     243        public int writeBytes(byte target[], int offset) {
     244            System.arraycopy(NULL_DATA, 0, target, offset, NULL_LENGTH);
     245            return offset + NULL_LENGTH;
     246        }
     247   
     248        /** @throws RuntimeException always */
     249        @Override
     250        public int readBytes(byte source[], int offset) throws DataFormatException {
     251            throw new RuntimeException("Data already set");
     252        }
     253   
     254        /** Overridden for efficiency */
     255        @Override
     256        public int size() {
     257            return NULL_LENGTH;
     258        }
     259   
     260        /** Overridden for efficiency */
     261        @Override
     262        public int hashCode() {
     263            return 99999;
     264        }
     265    }
    180266}
  • core/java/src/net/i2p/data/KeysAndCert.java

    r63c6613 r82e3440  
    6666        _signingKey = new SigningPublicKey();
    6767        _signingKey.readBytes(in);
    68         _certificate = new Certificate();
    69         _certificate.readBytes(in);
     68        //_certificate = new Certificate();
     69        //_certificate.readBytes(in);
     70        _certificate = Certificate.create(in);
    7071        __calculatedHash = null;
    7172    }
  • router/java/src/net/i2p/data/i2np/GarlicClove.java

    r63c6613 r82e3440  
    6969        if (_log.shouldLog(Log.DEBUG))
    7070            _log.debug("CloveID read: " + _cloveId + " expiration read: " + _expiration);
    71         _certificate = new Certificate();
    72         _certificate.readBytes(in);
     71        //_certificate = new Certificate();
     72        //_certificate.readBytes(in);
     73        _certificate = Certificate.create(in);
    7374        if (_log.shouldLog(Log.DEBUG))
    7475            _log.debug("Read cert: " + _certificate);
  • router/java/src/net/i2p/router/message/OutboundClientMessageJobHelper.java

    r63c6613 r82e3440  
    116116        instructions.setTunnelId(null);
    117117       
    118         config.setCertificate(new Certificate(Certificate.CERTIFICATE_TYPE_NULL, null));
     118        config.setCertificate(Certificate.NULL_CERT);
    119119        config.setDeliveryInstructions(instructions);
    120120        config.setId(ctx.random().nextLong(I2NPMessage.MAX_ID_VALUE));
     
    166166            log.debug("Delivery status message key: " + replyToken + " arrival: " + msg.getArrival());
    167167       
    168         ackClove.setCertificate(new Certificate(Certificate.CERTIFICATE_TYPE_NULL, null));
     168        ackClove.setCertificate(Certificate.NULL_CERT);
    169169        ackClove.setDeliveryInstructions(ackInstructions);
    170170        ackClove.setExpiration(expiration);
     
    197197        instructions.setEncrypted(false);
    198198       
    199         clove.setCertificate(new Certificate(Certificate.CERTIFICATE_TYPE_NULL, null));
     199        clove.setCertificate(Certificate.NULL_CERT);
    200200        clove.setDeliveryInstructions(instructions);
    201201        clove.setExpiration(expiration);
     
    223223        instructions.setEncrypted(false);
    224224       
    225         clove.setCertificate(new Certificate(Certificate.CERTIFICATE_TYPE_NULL, null));
     225        clove.setCertificate(Certificate.NULL_CERT);
    226226        clove.setDeliveryInstructions(instructions);
    227227        clove.setExpiration(expiration);
  • router/java/src/net/i2p/router/message/OutboundClientMessageOneShotJob.java

    r63c6613 r82e3440  
    873873        instructions.setEncrypted(false);
    874874       
    875         clove.setCertificate(new Certificate(Certificate.CERTIFICATE_TYPE_NULL, null));
     875        clove.setCertificate(Certificate.NULL_CERT);
    876876        clove.setDeliveryInstructions(instructions);
    877877        clove.setExpiration(OVERALL_TIMEOUT_MS_DEFAULT+getContext().clock().now());
  • router/java/src/net/i2p/router/networkdb/kademlia/MessageWrapper.java

    r63c6613 r82e3440  
    4747
    4848        PayloadGarlicConfig payload = new PayloadGarlicConfig();
    49         payload.setCertificate(new Certificate(Certificate.CERTIFICATE_TYPE_NULL, null));
     49        payload.setCertificate(Certificate.NULL_CERT);
    5050        payload.setId(ctx.random().nextLong(I2NPMessage.MAX_ID_VALUE));
    5151        payload.setPayload(m);
Note: See TracChangeset for help on using the changeset viewer.