Search in sources :

Example 36 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class SessionConfig method getBytes.

private byte[] getBytes() {
    if (_destination == null)
        return null;
    if (_options == null)
        return null;
    if (_creationDate == null)
        return null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        // _log.debug("PubKey size for destination: " + _destination.getPublicKey().getData().length);
        // _log.debug("SigningKey size for destination: " + _destination.getSigningPublicKey().getData().length);
        _destination.writeBytes(out);
        // UTF-8
        DataHelper.writeProperties(out, _options, true);
        DataHelper.writeDate(out, _creationDate);
    } catch (IOException ioe) {
        Log log = I2PAppContext.getGlobalContext().logManager().getLog(SessionConfig.class);
        log.error("IOError signing", ioe);
        return null;
    } catch (DataFormatException dfe) {
        Log log = I2PAppContext.getGlobalContext().logManager().getLog(SessionConfig.class);
        log.error("Error writing out the bytes for signing/verification", dfe);
        return null;
    }
    return out.toByteArray();
}
Also used : DataFormatException(net.i2p.data.DataFormatException) Log(net.i2p.util.Log) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 37 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class SessionConfig method signSessionConfig.

/**
 * Sign the structure using the supplied private key
 *
 * @param signingKey SigningPrivateKey to sign with
 * @throws DataFormatException
 */
public void signSessionConfig(SigningPrivateKey signingKey) throws DataFormatException {
    byte[] data = getBytes();
    if (data == null)
        throw new DataFormatException("Unable to retrieve bytes for signing");
    if (signingKey == null)
        throw new DataFormatException("No signing key");
    _signature = DSAEngine.getInstance().sign(data, signingKey);
    if (_signature == null)
        throw new DataFormatException("Signature failed with " + signingKey.getType() + " key");
}
Also used : DataFormatException(net.i2p.data.DataFormatException)

Example 38 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class RouterInfo method getBytes.

/**
 * Write out the raw payload of the routerInfo, excluding the signature.  This
 * caches the data in memory if possible.
 *
 * @throws DataFormatException if the data is somehow b0rked (missing props, etc)
 */
protected byte[] getBytes() throws DataFormatException {
    if (_byteified != null)
        return _byteified;
    ByteArrayOutputStream out = new ByteArrayOutputStream(2 * 1024);
    try {
        writeDataBytes(out);
    } catch (IOException ioe) {
        throw new DataFormatException("IO Error getting bytes", ioe);
    }
    byte[] data = out.toByteArray();
    if (CACHE_ALL || _shouldCache)
        _byteified = data;
    return data;
}
Also used : DataFormatException(net.i2p.data.DataFormatException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 39 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class RouterPrivateKeyFile method getRouterIdentity.

/**
 *  Read it in from the file.
 *  Also sets the local privKey and signingPrivKey.
 */
public RouterIdentity getRouterIdentity() throws IOException, DataFormatException {
    InputStream in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(this.file));
        RouterIdentity ri = new RouterIdentity();
        ri.readBytes(in);
        privKey = new PrivateKey();
        privKey.readBytes(in);
        SigType type = ri.getSigningPublicKey().getType();
        if (type == null)
            throw new DataFormatException("Unknown sig type");
        signingPrivKey = new SigningPrivateKey(type);
        signingPrivKey.readBytes(in);
        // set it a Destination, so we may call validateKeyPairs()
        // or other methods
        dest = new Destination();
        dest.setPublicKey(ri.getPublicKey());
        dest.setSigningPublicKey(ri.getSigningPublicKey());
        dest.setCertificate(ri.getCertificate());
        dest.setPadding(ri.getPadding());
        return ri;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ioe) {
            }
        }
    }
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) Destination(net.i2p.data.Destination) SigningPrivateKey(net.i2p.data.SigningPrivateKey) PrivateKey(net.i2p.data.PrivateKey) DataFormatException(net.i2p.data.DataFormatException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SigType(net.i2p.crypto.SigType)

Example 40 with DataFormatException

use of net.i2p.data.DataFormatException in project i2p.i2p by i2p.

the class DatabaseStoreMessage method readMessage.

public void readMessage(byte[] data, int offset, int dataSize, int type) throws I2NPMessageException {
    if (type != MESSAGE_TYPE)
        throw new I2NPMessageException("Message type is incorrect for this message");
    int curIndex = offset;
    _key = Hash.create(data, curIndex);
    // Fast-fail here to save resources.
    if (_key.equals(Hash.FAKE_HASH)) {
        // createRateStat in KNDF
        _context.statManager().addRateData("netDb.DSMAllZeros", 1);
        throw new I2NPMessageException("DSM all zeros");
    }
    curIndex += Hash.HASH_LENGTH;
    // as of 0.9.18, ignore other 7 bits of the type byte, in preparation for future options
    int dbType = data[curIndex] & 0x01;
    curIndex++;
    _replyToken = DataHelper.fromLong(data, curIndex, 4);
    curIndex += 4;
    if (_replyToken > 0) {
        long tunnel = DataHelper.fromLong(data, curIndex, 4);
        if (tunnel > 0)
            _replyTunnel = new TunnelId(tunnel);
        curIndex += 4;
        _replyGateway = Hash.create(data, curIndex);
        curIndex += Hash.HASH_LENGTH;
    } else {
        _replyTunnel = null;
        _replyGateway = null;
    }
    if (dbType == DatabaseEntry.KEY_TYPE_LEASESET) {
        _dbEntry = new LeaseSet();
        try {
            _dbEntry.readBytes(new ByteArrayInputStream(data, curIndex, data.length - curIndex));
        } catch (DataFormatException dfe) {
            throw new I2NPMessageException("Error reading the leaseSet", dfe);
        } catch (IOException ioe) {
            throw new I2NPMessageException("Error reading the leaseSet", ioe);
        }
    } else {
        // dbType == DatabaseEntry.KEY_TYPE_ROUTERINFO
        _dbEntry = new RouterInfo();
        int compressedSize = (int) DataHelper.fromLong(data, curIndex, 2);
        curIndex += 2;
        if (compressedSize <= 0 || curIndex + compressedSize > data.length || curIndex + compressedSize > dataSize + offset)
            throw new I2NPMessageException("Compressed RI length: " + compressedSize + " but remaining bytes: " + Math.min(data.length - curIndex, dataSize + offset - curIndex));
        try {
            // TODO we could delay decompression, just copy to a new byte array and store in _byteCache
            // May not be necessary since the IBGW now uses UnknownI2NPMessage.
            // DSMs at the OBEP are generally garlic wrapped, so the OBEP won't see it.
            // If we do delay it, getEntry() will have to check if _dbEntry is null and _byteCache
            // is non-null, and then decompress.
            byte[] decompressed = DataHelper.decompress(data, curIndex, compressedSize);
            _dbEntry.readBytes(new ByteArrayInputStream(decompressed));
        } catch (DataFormatException dfe) {
            throw new I2NPMessageException("Error reading the routerInfo", dfe);
        } catch (IOException ioe) {
            throw new I2NPMessageException("Corrupt compressed routerInfo size = " + compressedSize, ioe);
        }
    }
// if (!key.equals(_dbEntry.getHash()))
// throw new I2NPMessageException("Hash mismatch in DSM");
}
Also used : LeaseSet(net.i2p.data.LeaseSet) DataFormatException(net.i2p.data.DataFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) RouterInfo(net.i2p.data.router.RouterInfo) IOException(java.io.IOException) TunnelId(net.i2p.data.TunnelId)

Aggregations

DataFormatException (net.i2p.data.DataFormatException)112 IOException (java.io.IOException)53 Destination (net.i2p.data.Destination)32 Properties (java.util.Properties)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 FileInputStream (java.io.FileInputStream)16 Hash (net.i2p.data.Hash)14 File (java.io.File)13 SigType (net.i2p.crypto.SigType)13 I2PSessionException (net.i2p.client.I2PSessionException)12 InputStream (java.io.InputStream)11 PrivateKey (net.i2p.data.PrivateKey)11 SigningPrivateKey (net.i2p.data.SigningPrivateKey)11 SigningPublicKey (net.i2p.data.SigningPublicKey)11 RouterInfo (net.i2p.data.router.RouterInfo)11 Signature (net.i2p.data.Signature)10 FileOutputStream (java.io.FileOutputStream)8 InterruptedIOException (java.io.InterruptedIOException)8 HashMap (java.util.HashMap)8 PublicKey (net.i2p.data.PublicKey)8