Search in sources :

Example 41 with DataFormatException

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

the class EstablishState method readAliceRouterIdentity.

/**
 * We are Bob. We have received enough of message #3 from Alice
 * to get Alice's RouterIdentity.
 *
 * _aliceIdentSize must be set.
 * _sz_aliceIdent_tsA_padding_aliceSig must contain at least 2 + _aliceIdentSize bytes.
 *
 * Sets _aliceIdent so that we
 * may determine the signature and padding sizes.
 *
 * After all of message #3 is received including the signature and
 * padding, verifyIdentity() must be called.
 *
 *  State must be IB_GOT_RI_SIZE.
 *  Caller must synch.
 *
 * @since 0.9.16 pulled out of verifyInbound()
 */
private void readAliceRouterIdentity() {
    byte[] b = _sz_aliceIdent_tsA_padding_aliceSig.toByteArray();
    try {
        int sz = _aliceIdentSize;
        if (sz < MIN_RI_SIZE || sz > MAX_RI_SIZE || sz > b.length - 2) {
            _context.statManager().addRateData("ntcp.invalidInboundSize", sz);
            fail("size is invalid", new Exception("size is " + sz));
            return;
        }
        RouterIdentity alice = new RouterIdentity();
        ByteArrayInputStream bais = new ByteArrayInputStream(b, 2, sz);
        alice.readBytes(bais);
        _aliceIdent = alice;
    } catch (IOException ioe) {
        _context.statManager().addRateData("ntcp.invalidInboundIOE", 1);
        fail("Error verifying peer", ioe);
    } catch (DataFormatException dfe) {
        _context.statManager().addRateData("ntcp.invalidInboundDFE", 1);
        fail("Error verifying peer", dfe);
    }
}
Also used : DataFormatException(net.i2p.data.DataFormatException) ByteArrayInputStream(java.io.ByteArrayInputStream) RouterIdentity(net.i2p.data.router.RouterIdentity) IOException(java.io.IOException) DataFormatException(net.i2p.data.DataFormatException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 42 with DataFormatException

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

the class I2NPMessageImpl method writeBytes.

/**
 *  Don't do this if you need a byte array - use toByteArray()
 *
 *  @deprecated unused
 */
@Deprecated
public void writeBytes(OutputStream out) throws DataFormatException, IOException {
    int size = getMessageSize();
    if (size < 15 + CHECKSUM_LENGTH)
        throw new DataFormatException("Unable to build the message");
    byte[] buf = new byte[size];
    int read = toByteArray(buf);
    if (read < 0)
        throw new DataFormatException("Unable to build the message");
    out.write(buf, 0, read);
}
Also used : DataFormatException(net.i2p.data.DataFormatException)

Example 43 with DataFormatException

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

the class RouterAddress method writeBytes.

/**
 *  As of 0.9.3, expiration MUST be all zeros as it is ignored on
 *  readin and the signature will fail.
 */
public void writeBytes(OutputStream out) throws DataFormatException, IOException {
    if (_transportStyle == null)
        throw new DataFormatException("uninitialized");
    out.write((byte) _cost);
    DataHelper.writeLong(out, 8, _expiration);
    DataHelper.writeString(out, _transportStyle);
    DataHelper.writeProperties(out, _options);
}
Also used : DataFormatException(net.i2p.data.DataFormatException)

Example 44 with DataFormatException

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

the class GarlicMessageBuilder method buildClove.

/**
 *  UNUSED
 *
 *  The Garlic Message we are building contains another garlic message,
 *  as specified by a GarlicConfig (NOT a PayloadGarlicConfig).
 *
 *  So this calls back to the top, to buildMessage(ctx, config),
 *  which uses the router's SKM, i.e. the wrong one.
 *  Unfortunately we've lost the reference to the SessionKeyManager way down here,
 *  so we can't call buildMessage(ctx, config, key, tags, skm).
 *
 *  If we do ever end up constructing a garlic message that contains a garlic message,
 *  we'll have to fix this by passing the skm through the last buildMessage,
 *  through buildCloveSet, to here.
 */
private static byte[] buildClove(RouterContext ctx, GarlicConfig config) throws DataFormatException, IOException {
    GarlicClove clove = new GarlicClove(ctx);
    GarlicMessage msg = buildMessage(ctx, config);
    if (msg == null)
        throw new DataFormatException("Unable to build message from clove config");
    clove.setData(msg);
    return buildCommonClove(clove, config);
}
Also used : DataFormatException(net.i2p.data.DataFormatException) GarlicMessage(net.i2p.data.i2np.GarlicMessage) GarlicClove(net.i2p.data.i2np.GarlicClove)

Example 45 with DataFormatException

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

the class GarlicMessageBuilder method buildCloveSet.

/**
 **
 *    private static void noteWrap(RouterContext ctx, GarlicMessage wrapper, GarlicConfig contained) {
 *        for (int i = 0; i < contained.getCloveCount(); i++) {
 *            GarlicConfig config = contained.getClove(i);
 *            if (config instanceof PayloadGarlicConfig) {
 *                I2NPMessage msg = ((PayloadGarlicConfig)config).getPayload();
 *                String bodyType = msg.getClass().getName();
 *                ctx.messageHistory().wrap(bodyType, msg.getUniqueId(), GarlicMessage.class.getName(), wrapper.getUniqueId());
 *            }
 *        }
 *    }
 ***
 */
/**
 * Build the unencrypted GarlicMessage specified by the config.
 * It contains the number of cloves, followed by each clove,
 * followed by a certificate, ID, and expiration date.
 *
 * @throws IllegalArgumentException on error
 */
private static byte[] buildCloveSet(RouterContext ctx, GarlicConfig config) {
    ByteArrayOutputStream baos = null;
    Log log = ctx.logManager().getLog(GarlicMessageBuilder.class);
    try {
        if (config instanceof PayloadGarlicConfig) {
            byte[] clove = buildClove(ctx, (PayloadGarlicConfig) config);
            baos = new ByteArrayOutputStream(clove.length + 16);
            baos.write((byte) 1);
            baos.write(clove);
        } else {
            byte[][] cloves = new byte[config.getCloveCount()][];
            for (int i = 0; i < config.getCloveCount(); i++) {
                GarlicConfig c = config.getClove(i);
                if (c instanceof PayloadGarlicConfig) {
                    // log.debug("Subclove IS a payload garlic clove");
                    cloves[i] = buildClove(ctx, (PayloadGarlicConfig) c);
                } else {
                    log.debug("Subclove IS NOT a payload garlic clove");
                    // See notes below
                    cloves[i] = buildClove(ctx, c);
                }
            }
            int len = 1;
            for (int i = 0; i < cloves.length; i++) len += cloves[i].length;
            baos = new ByteArrayOutputStream(len + 16);
            baos.write((byte) cloves.length);
            for (int i = 0; i < cloves.length; i++) baos.write(cloves[i]);
        }
        config.getCertificate().writeBytes(baos);
        DataHelper.writeLong(baos, 4, config.getId());
        DataHelper.writeLong(baos, DataHelper.DATE_LENGTH, config.getExpiration());
    } catch (IOException ioe) {
        log.error("Error building the clove set", ioe);
        throw new IllegalArgumentException("Error building the clove set", ioe);
    } catch (DataFormatException dfe) {
        log.error("Error building the clove set", dfe);
        throw new IllegalArgumentException("Error building the clove set", dfe);
    }
    return baos.toByteArray();
}
Also used : DataFormatException(net.i2p.data.DataFormatException) Log(net.i2p.util.Log) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

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