/** * Create a new payload. * No more end-to-end encryption, just set the "encrypted" data to the payload. */ private static Payload createPayload(byte[] payload) throws I2PSessionException { if (payload == null) throw new I2PSessionException("No payload specified"); Payload data = new Payload(); data.setEncryptedData(payload); return data; }
@Override public boolean equals(Object object) { if (object == this) return true; if ((object == null) || !(object instanceof Payload)) return false; Payload p = (Payload) object; return Arrays.equals(_unencryptedData, p.getUnencryptedData()) && Arrays.equals(_encryptedData, p.getEncryptedData()); }
public void readBytes(InputStream in) throws DataFormatException, IOException { int size = (int) DataHelper.readLong(in, 4); if (size < 0 || size > MAX_LENGTH) throw new DataFormatException("payload size out of range (" + size + ")"); _encryptedData = new byte[size]; int read = read(in, _encryptedData); if (read != size) throw new DataFormatException("Incorrect number of bytes read in the payload structure"); //if (_log.shouldLog(Log.DEBUG)) // _log.debug("read payload: " + read + " bytes"); }
/** * Decrypt the payload * * We don't really decrypt (no more end-to-end crypto) * If we do, we need to use the correct key manager in the decrypt() call below */ private Payload decryptPayload(MessagePayloadMessage msg, I2PSessionImpl session) throws DataFormatException { Payload payload = msg.getPayload(); //if (!I2CPMessageProducer.END_TO_END_CRYPTO) { payload.setUnencryptedData(payload.getEncryptedData()); return payload; //} //byte[] data = _context.elGamalAESEngine().decrypt(payload.getEncryptedData(), session.getDecryptionKey()); //if (data == null) { // if (_log.shouldLog(Log.WARN)) // _log.warn("Error decrypting the payload"); // throw new DataFormatException("Unable to decrypt the payload"); //} //payload.setUnencryptedData(data); //return payload; } }
@Override protected void doReadMessage(InputStream in, int size) throws I2CPMessageException, IOException { try { _sessionId = (int) DataHelper.readLong(in, 2); _messageId = DataHelper.readLong(in, 4); _payload = new Payload(); _payload.readBytes(in); } catch (DataFormatException dfe) { throw new I2CPMessageException("Unable to load the message data", dfe); } }
/** * Write out the full message to the stream, including the 4 byte size and 1 * byte type header. * * @throws IOException */ @Override public void writeMessage(OutputStream out) throws I2CPMessageException, IOException { if (_sessionId <= 0) throw new I2CPMessageException("Unable to write out the message, as the session ID has not been defined"); if (_messageId < 0) throw new I2CPMessageException("Unable to write out the message, as the message ID has not been defined"); if (_payload == null) throw new I2CPMessageException("Unable to write out the message, as the payload has not been defined"); int size = 2 + 4 + 4 + _payload.getSize(); try { DataHelper.writeLong(out, 4, size); out.write((byte) MESSAGE_TYPE); DataHelper.writeLong(out, 2, _sessionId); DataHelper.writeLong(out, 4, _messageId); DataHelper.writeLong(out, 4, _payload.getSize()); out.write(_payload.getEncryptedData()); } catch (DataFormatException dfe) { throw new I2CPMessageException("Unable to write the message length or type", dfe); } }
/** * Write out the full message to the stream, including the 4 byte size and 1 * byte type header. Override the parent so we can be more mem efficient * * @throws IOException */ @Override public void writeMessage(OutputStream out) throws I2CPMessageException, IOException { if (_sessionId == null) throw new I2CPMessageException("No session ID"); if (_destination == null) throw new I2CPMessageException("No dest"); if (_payload == null) throw new I2CPMessageException("No payload"); if (_nonce < 0) throw new I2CPMessageException("No nonce"); int len = 2 + _destination.size() + _payload.getSize() + 4 + 4; try { DataHelper.writeLong(out, 4, len); out.write((byte) getType()); _sessionId.writeBytes(out); _destination.writeBytes(out); _payload.writeBytes(out); DataHelper.writeLong(out, 4, _nonce); } catch (DataFormatException dfe) { throw new I2CPMessageException("Error writing the msg", dfe); } }
public DataStructure createStructureToRead() { return new Payload(); }
id = new MessageId(nextID); _runner.setPayload(id, _payload); messageAvailable(id, _payload.getSize());
/** * Build a clove that sends the payload to the destination */ private static PayloadGarlicConfig buildDataClove(RouterContext ctx, Payload data, Destination dest, long expiration) { DeliveryInstructions instructions = new DeliveryInstructions(); instructions.setDeliveryMode(DeliveryInstructions.DELIVERY_MODE_DESTINATION); instructions.setDestination(dest.calculateHash()); // defaults //instructions.setDelayRequested(false); //instructions.setDelaySeconds(0); //instructions.setEncrypted(false); PayloadGarlicConfig clove = new PayloadGarlicConfig(); clove.setCertificate(Certificate.NULL_CERT); clove.setDeliveryInstructions(instructions); clove.setExpiration(expiration); clove.setId(ctx.random().nextLong(I2NPMessage.MAX_ID_VALUE)); DataMessage msg = new DataMessage(ctx); msg.setData(data.getEncryptedData()); clove.setPayload(msg); // defaults //clove.setRecipientPublicKey(null); //clove.setRequestAck(false); return clove; }
private static int getByte(MessagePayloadMessage msg, int i) { return msg.getPayload().getUnencryptedData()[i] & 0xff; }
/** * Read the body into the data structures * * @throws IOException */ @Override public void readMessage(InputStream in, int length, int type) throws I2CPMessageException, IOException { if (type != getType()) throw new I2CPMessageException("Invalid message type (found: " + type + " supported: " + getType() + " class: " + getClass().getName() + ")"); if (length < 0) throw new IOException("Negative payload size"); try { _sessionId = new SessionId(); _sessionId.readBytes(in); _destination = Destination.create(in); _payload = new Payload(); _payload.readBytes(in); _nonce = DataHelper.readLong(in, 4); } catch (DataFormatException dfe) { throw new I2CPMessageException("Unable to load the message data", dfe); } }
if (_nonce < 0) throw new I2CPMessageException("No nonce"); int len = 2 + _destination.size() + _payload.getSize() + 4 + 4 + DataHelper.DATE_LENGTH; _sessionId.writeBytes(out); _destination.writeBytes(out); _payload.writeBytes(out); DataHelper.writeLong(out, 4, _nonce); _daf.writeBytes(out);
+ payload.getSize() + " for session " + message.getSessionId());
if (p == null) return null; byte d[] = p.getEncryptedData(); if (d == null) return null;
/** * Pull the unencrypted data from the message that we've already prefetched and * notified the user that its available. * */ public byte[] receiveMessage(int msgId) throws I2PSessionException { MessagePayloadMessage msg = _availableMessages.remove(Long.valueOf(msgId)); if (msg == null) { _log.error("Receive message " + msgId + " had no matches"); return null; } updateActivity(); return msg.getPayload().getUnencryptedData(); }
public DataStructure createDataStructure() throws DataFormatException { Payload payload = new Payload(); SessionKey key = (SessionKey)(new SessionKeyTest()).createDataStructure(); byte data[] = DataHelper.getASCII("Hello, I2P"); // This causes equals() to fail unless we override the test // to set the unencrypted data after reading. // Unencrypted data is deprecated, just use encrypted data for the test. //payload.setUnencryptedData(data); Hash hash = (Hash)(new HashTest()).createDataStructure(); Destination target = (Destination)(new DestinationTest()).createDataStructure(); payload.setEncryptedData(data); return payload; } public DataStructure createStructureToRead() { return new Payload(); }
_clientMessage = msg; _clientMessageId = msg.getMessageId(); _clientMessageSize = msg.getPayload().getSize(); _from = msg.getFromDestination(); _to = msg.getDestination();
/** * Recieve a payload message and let the app know its available */ public void addNewMessage(MessagePayloadMessage msg) { Long mid = Long.valueOf(msg.getMessageId()); _availableMessages.put(mid, msg); long id = msg.getMessageId(); byte data[] = msg.getPayload().getUnencryptedData(); if ((data == null) || (data.length <= 0)) { if (_log.shouldLog(Log.CRIT)) _log.log(Log.CRIT, getPrefix() + "addNewMessage of a message with no unencrypted data", new Exception("Empty message")); } else { int size = data.length; _availabilityNotifier.available(id, size); if (_log.shouldLog(Log.INFO)) _log.info(getPrefix() + "Notified availability for session " + _sessionId + ", message " + id); } }
_log.debug("data message came down a tunnel for " + _client); DataMessage dm = (DataMessage)data; Payload payload = new Payload(); payload.setEncryptedData(dm.getData()); ClientMessage m = new ClientMessage(_client, payload); _context.clientManager().messageReceived(m); + _client + " targeting shared " + to); DataMessage dm = (DataMessage)data; Payload payload = new Payload(); payload.setEncryptedData(dm.getData()); ClientMessage m = new ClientMessage(to, payload); _context.clientManager().messageReceived(m);