/** * * @param messageIdString Must be a 44-character Base64-encoded string. */ public void setMessageID(String messageIdString) { this.messageId = new UniqueId(messageIdString); }
/** * Writes the Prefix, Version, Type, and Packet Id fields of a Communication Packet to * an {@link OutputStream}. * @param outputStream */ protected void writeHeader(OutputStream outputStream) throws IOException { outputStream.write(PACKET_PREFIX); outputStream.write((byte)getPacketTypeCode()); outputStream.write(getProtocolVersion()); outputStream.write(packetId.toByteArray()); }
/** * Returns a <code>FindClosePeersPacket</code> that matches a given {@link UniqueId} * from a {@link Collection} of packets, or <code>null</code> if no match. * @param packets * @param packetId * @return */ private FindClosePeersPacket getPacketById(Collection<FindClosePeersPacket> packets, UniqueId packetId) { for (FindClosePeersPacket packet: packets) if (packetId.equals(packet.getPacketId())) return packet; return null; } };
/** Tests processing of one valid and one invalid {@link EmailPacketDeleteRequest}. */ @Test public void testPacketReceived() throws PasswordException { packetFolder.store(emailPacket); assertEquals(1, packetFolder.getElements().size()); EmailPacketDeleteRequest delRequest; // send an invalid delete request byte[] delAuthBytes = unencryptedPacket.getDeleteAuthorization().toByteArray().clone(); delAuthBytes[5] ^= 1; UniqueId invalidAuthorization = new UniqueId(delAuthBytes, 0); delRequest = new EmailPacketDeleteRequest(emailPacket.getDhtKey(), invalidAuthorization); packetFolder.packetReceived(delRequest, sender, System.currentTimeMillis()); assertEquals(1, packetFolder.getElements().size()); // send a valid delete request delRequest = new EmailPacketDeleteRequest(emailPacket.getDhtKey(), unencryptedPacket.getDeleteAuthorization()); packetFolder.packetReceived(delRequest, sender, System.currentTimeMillis()); assertEquals(0, packetFolder.getElements().size()); }
@Override public String getMessageID() { return messageId.toBase64(); }
@Override public String toString() { return "Type=" + getClass().getSimpleName() + ", Id=" + (packetId==null?"<null>":packetId.toString().substring(0, 8)) + "..."; } }
@Override public byte[] toByteArray() { ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(byteArrayStream); try { writeHeader(dataStream); messageId.writeTo(dataStream); delAuthorization.writeTo(dataStream); dataStream.writeShort(fragmentIndex); dataStream.writeShort(numFragments); dataStream.writeShort(content.length); dataStream.write(content); } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream.", e); } return byteArrayStream.toByteArray(); }
/** * Returns all filenames that match a given message ID. Not to be confused with * the {@link #retrieve(net.i2p.data.Hash)} method, which takes a DHT key. * @param messageId */ private File[] getAllMatchingFiles(UniqueId messageId) { final String base64Id = messageId.toBase64(); return storageDir.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.startsWith(base64Id); } }); }
protected CommunicationPacket() { this(new UniqueId()); }
@Override public byte[] toByteArray() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { writeHeader(outputStream); outputStream.write(dhtKey.toByteArray()); outputStream.write(authorization.toByteArray()); } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream.", e); } return outputStream.toByteArray(); }
private void write(File cacheFile) { log.debug("Writing message ID cache file: <" + cacheFile.getAbsolutePath() + ">"); String newLine = System.getProperty("line.separator"); Writer writer = null; try { writer = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(cacheFile.getAbsolutePath()))); for (UniqueId id: idList) writer.write(id.toBase64() + newLine); } catch (IOException e) { log.error("Can't write message ID cache file.", e); } finally { if (writer != null) try { writer.close(); } catch (IOException e) { log.error("Error closing Writer.", e); } } }
@Override public void packetReceived(CommunicationPacket packet, Destination sender, long receiveTime) { // synchronize access to lastSentPacket (which can be null, so synchronize on "this") synchronized(this) { if (lastSentPacket!=null && packet instanceof ResponsePacket) { ResponsePacket responsePacket = (ResponsePacket)packet; boolean packetIdMatches = lastSentPacket.getPacketId().equals(responsePacket.getPacketId()); boolean statusOk = StatusCode.OK == responsePacket.getStatusCode(); if (packetIdMatches && statusOk) confirmationReceived.countDown(); } } } }
/** * Creates an <code>UnencryptedEmailPacket</code> using data from an <code>InputStream</code> * for the content. The number of bytes read is limited so the byte array representation of the * packet does not exceed <code>maxPacketSize</code>. * @throws IOException */ public UnencryptedEmailPacket(InputStream inputStream, UniqueId messageId, int fragmentIndex, int maxPacketSize) throws IOException { this.messageId = messageId; delAuthorization = new UniqueId(); this.fragmentIndex = fragmentIndex; // read content maxPacketSize -= OVERHEAD; content = new byte[maxPacketSize]; int bytesRead = inputStream.read(content); if (bytesRead < maxPacketSize) content = Arrays.copyOf(content, bytesRead); }
@Override public byte[] toByteArray() { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(byteStream); try { writeHeader(dataStream); dataStream.writeInt(entries.size()); for (DeletionRecord entry: entries) { dataStream.write(entry.dhtKey.toByteArray()); dataStream.write(entry.delAuthorization.toByteArray()); dataStream.writeInt((int)(entry.storeTime/1000L)); // store as seconds } } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream/DataOutputStream.", e); } return byteStream.toByteArray(); }
/** * Creates an <code>Email</code> from a byte array containing a <strong>compressed</strong> MIME email. * @param bytes * @throws MessagingException * @throws IOException */ public Email(byte[] bytes) throws MessagingException, IOException { this(new ByteArrayInputStream(bytes), true); messageId = new UniqueId(); metadata = new EmailMetadata(); }
@Override public byte[] toByteArray() { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(byteStream); try { writeHeader(dataStream); emailDestHash.writeBytes(dataStream); dataStream.writeShort(entries.size()); for (Entry<Hash, UniqueId> entry: entries.entrySet()) { dataStream.write(entry.getKey().toByteArray()); dataStream.write(entry.getValue().toByteArray()); } } catch (DataFormatException e) { log.error("Invalid format for email destination.", e); } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream.", e); } return byteStream.toByteArray(); }
/** * @param includeSendTime Whether to add a "Date" header. */ public Email(boolean includeSendTime) { super(Session.getDefaultInstance(new Properties())); messageId = new UniqueId(); metadata = new EmailMetadata(); this.includeSendTime = includeSendTime; }
public static boolean isDeleteAuthorizationValid(Hash verificationHash, UniqueId delAuthorization) { Hash expectedVerificationHash = SHA256Generator.getInstance().calculateHash(delAuthorization.toByteArray()); boolean valid = expectedVerificationHash.equals(verificationHash); return valid; }
/** * Creates an <code>UnencryptedEmailPacket</code> from a <code>byte</code> array that contains MIME data. * @param data */ public UnencryptedEmailPacket(byte[] data) { super(data); ByteBuffer buffer = ByteBuffer.wrap(data, HEADER_LENGTH, data.length-HEADER_LENGTH); messageId = new UniqueId(buffer); delAuthorization = new UniqueId(buffer); fragmentIndex = buffer.getShort() & 0xFFFF; numFragments = buffer.getShort() & 0xFFFF; int contentLength = buffer.getShort() & 0xFFFF; content = new byte[contentLength]; buffer.get(content); verify(); }
/** * Creates an <code>EncryptedEmailPacket</code> from an <code>UnencryptedEmailPacket</code>. * The public key of <code>emailDestination</code> is used for encryption. * The store time is set to <code>0</code>. * @param unencryptedPacket * @param emailDestination * @throws GeneralSecurityException If an error occurred during encryption */ public EncryptedEmailPacket(UnencryptedEmailPacket unencryptedPacket, EmailDestination emailDestination) throws GeneralSecurityException { storeTime = 0; byte[] delAuthorizationBytes = unencryptedPacket.getDeleteAuthorization().toByteArray(); delVerificationHash = SHA256Generator.getInstance().calculateHash(delAuthorizationBytes); cryptoImpl = emailDestination.getCryptoImpl(); encryptedData = cryptoImpl.encrypt(unencryptedPacket.toByteArray(), emailDestination.getPublicEncryptionKey()); dhtKey = getDhtKey(); }