Search in sources :

Example 6 with UnencryptedEmailPacket

use of i2p.bote.packet.dht.UnencryptedEmailPacket in project i2p.i2p-bote by i2p.

the class IncompleteEmailFolderTest method testAddEmail.

private void testAddEmail(String mailContent, int expectedNumPackets) throws Exception {
    Email email = new Email(true);
    String recipient = "test@bote.i2p";
    email.addRecipient(RecipientType.TO, new InternetAddress(recipient));
    email.setText(mailContent);
    EmailIdentity identity = new EmailIdentity("DVkhqF6R9SHB5svViGtqRYZO7oI-0-omnIFtae29fNnNtTTH2j37Fr5fWp4t6rseTjiJ8gwg08DnbA4qP72aSQcDQPSErOELOMSU5BUTtsT8hnv1-DKdhIn~1qoIjxzIFHbxT3xnR3nFI7lKd6couscilzPBCjoFDUKb5ds2u23RO29K7~EKxU1O7Ltu6sT5etXkJkhAziOcuyfZyxJXqH1caYX5e2aWIhY3D2ESfy4nMK66r5KcDVQOPTzCkJq6d1FFOmnDGrlJjN~HgHmfUCtLbO~TLugWx9FCiDGfPkBb-3ODYTDaUR1zobOj1tiffV3Nm73PsYddRt84emLKzIRsC77JJpflw~h8UIRYJ29vJDf4VQ54BhZcelmN192sIrWr2nKN8n6PpSP4LI4RAuG2UvLytnDYzFM7O9WcnFP2-Qs3t1lD9aF72JVTYTpH5PZupnB1cglSsdRg8RmtRa41Fseyx8D3EdH~DCdpMGmfupaWp9~dKpFMleqk9scRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABTjDxn3wEOjCjJ4APg~2IGpqWwy2Hw728aZ3eCC5l0MP913BLdIfSUiXPbs6sN9A2");
    KeyUpdateHandler keyUpdateHandler = TestUtil.createDummyKeyUpdateHandler();
    Collection<UnencryptedEmailPacket> packets = email.createEmailPackets(identity, keyUpdateHandler, recipient, I2PBotePacket.MAX_DATAGRAM_SIZE);
    assertTrue("Expected " + expectedNumPackets + " email packets, got " + packets.size(), packets.size() == expectedNumPackets);
    assertTrue("The inbox should be empty at this point!", inbox.getElements().size() == 0);
    for (UnencryptedEmailPacket emailPacket : packets) incompleteFolder.addEmailPacket(emailPacket);
    assertTrue("The incomplete emails folder is not empty!", incompleteFolder.getElements().size() == 0);
    assertTrue("Expected: one email in the inbox, actual number = " + inbox.getElements().size(), inbox.getElements().size() == 1);
    // Verify that the original email and the email in the folder are the same except for the signature header
    Email storedEmail = inbox.getElements().iterator().next();
    storedEmail.removeHeader("X-I2PBote-Sig-Valid");
    TestUtil.assertEquals("Stored email differs from original email!", email, storedEmail);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Email(i2p.bote.email.Email) KeyUpdateHandler(i2p.bote.crypto.KeyUpdateHandler) EmailIdentity(i2p.bote.email.EmailIdentity) UnencryptedEmailPacket(i2p.bote.packet.dht.UnencryptedEmailPacket)

Example 7 with UnencryptedEmailPacket

use of i2p.bote.packet.dht.UnencryptedEmailPacket in project i2p.i2p-bote by i2p.

the class IndexPacketFolderTest method setUp.

@Before
public void setUp() throws Exception {
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    testDir = new File(tempDir, "IndexPacketFolderTest-" + System.currentTimeMillis());
    folderDir = new File(testDir, "dht_index_pkt");
    folder = new SettableStoreTimeIndexPacketFolder(folderDir);
    byte[] content1 = "test TEST test ABCDEFGH asdfsadfsadf 3487562384".getBytes();
    byte[] content2 = "fdlkhgjfljh test 123456".getBytes();
    byte[] messageIdBytes = new byte[] { -69, -24, -109, 1, 69, -122, -69, 113, -68, -90, 55, -28, 105, 97, 125, 70, 51, 58, 14, 2, -13, -53, 90, -29, 36, 67, 36, -94, -108, -125, 11, 123 };
    UniqueId messageId = new UniqueId(messageIdBytes, 0);
    int fragmentIndex = 0;
    unencryptedPacket1 = new UnencryptedEmailPacket(new ByteArrayInputStream(content1), messageId, fragmentIndex, I2PBotePacket.MAX_DATAGRAM_SIZE);
    unencryptedPacket1.setNumFragments(1);
    unencryptedPacket2 = new UnencryptedEmailPacket(new ByteArrayInputStream(content2), messageId, fragmentIndex, I2PBotePacket.MAX_DATAGRAM_SIZE);
    unencryptedPacket2.setNumFragments(1);
    destination1 = new EmailDestination("2XP9Ep3WWLk3-FTlMgUjgw4h8GYVBCvR6YrPyKdhP4xyQMSh8Da0VjZCmQGbD3PCeaGXAShBKbKjhJjQ7laekI");
    destination2 = new EmailDestination("m-5~1dZ0MrGdyAWu-C2ecNAB5LCCsHQpeSfjn-r~mqMfNvroR98~BRmReUDmb0la-r-pBHLMtflrJE7aTrGwDTBm5~AJFEm-9SJPZnyGs-ed5pOj4Db65yJml1y1n77qr1~mM4GITl6KuIoxg8YwvPrCIlXe2hiiDCoC-uY9-np9UY");
    destination3 = new EmailDestination("0XuJjhgp58aOhvHHgpaxoQYsCUfDS6BECMEoVxFGEFPdk3y8lbzIsq9eUyeizFleMacYwoscCir8nQLlW34lxfRmirkNpD9vU1XnmjnZ5hGdnor1qIDqz3KJ040dVQ617MwyG97xxYLT0FsH907vBXgdc4RCHwKd1~9siagA5CSMaA~wM8ymKXLypiZGYexENLmim7nMzJTQYoOM~fVS99UaGJleDBN3pgZ2EvRYDQV2VqKH7Gee07R3y7b~c0tAKVHS0IbPQfTVJigrIHjTl~ZczxpaeTM04T8IgxKnO~lSmR1w7Ik8TpEkETwT9PDwUqQsjmlSY8E~WwwGMRJVyIRZUkHeRZ0aFq7us8W9EKzYtjjiU1z0QFpZrTfJE8oqCbnH5Lqv5Q86UdTPpriJC1N99E77TpCTnNzcBnpp6ko2JCy2IJUveaigKxS6EmS9KarkkkBRsckOKZZ6UNTOqPZsBCsx0Q9WvDF-Uc3dtouXWyenxRptaQsdkZyYlEQv");
    emailPacket1 = new EncryptedEmailPacket(unencryptedPacket1, destination1);
    emailPacket2 = new EncryptedEmailPacket(unencryptedPacket2, destination2);
}
Also used : UniqueId(i2p.bote.UniqueId) ByteArrayInputStream(java.io.ByteArrayInputStream) EncryptedEmailPacket(i2p.bote.packet.dht.EncryptedEmailPacket) EmailDestination(i2p.bote.email.EmailDestination) UnencryptedEmailPacket(i2p.bote.packet.dht.UnencryptedEmailPacket) File(java.io.File) Before(org.junit.Before)

Example 8 with UnencryptedEmailPacket

use of i2p.bote.packet.dht.UnencryptedEmailPacket in project i2p.i2p-bote by i2p.

the class Email method createEmailPackets.

/**
 * Updates headers, signs the email, and converts it into one or more email packets.
 * If an error occurs, an empty <code>Collection</code> is returned.
 *
 * @param senderIdentity The sender's Email Identity, or <code>null</code> for anonymous emails
 * @param keyUpdateHandler Needed for updating the signature key after signing (see {@link CryptoImplementation#sign(byte[], PrivateKey, KeyUpdateHandler)})
 * @param bccToKeep All BCC fields in the header section of the email are removed, except this field. If this parameter is <code>null</code>, all BCC fields are written.
 * @param maxPacketSize The size limit in bytes
 * @throws MessagingException
 * @throws GeneralSecurityException If the email cannot be signed
 * @throws PasswordException If the private signing key cannot be updated
 */
public Collection<UnencryptedEmailPacket> createEmailPackets(EmailIdentity senderIdentity, KeyUpdateHandler keyUpdateHandler, String bccToKeep, int maxPacketSize) throws MessagingException, GeneralSecurityException, PasswordException {
    ArrayList<UnencryptedEmailPacket> packets = new ArrayList<UnencryptedEmailPacket>();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    String[] bccHeaders = null;
    try {
        bccHeaders = getHeader("BCC");
        saveChanges();
        if (bccToKeep != null && isBCC(bccToKeep))
            // set bccToKeep and remove any other existing BCC addresses
            setHeader("BCC", bccToKeep);
        else
            removeHeader("BCC");
        if (!isAnonymous())
            sign(senderIdentity, keyUpdateHandler);
        compressTo(outputStream);
    } catch (IOException e) {
        throw new MessagingException("Can't write the email to an OutputStream.", e);
    } catch (GeneralSecurityException e) {
        throw new GeneralSecurityException("Can't sign email.", e);
    } finally {
        // restore the BCC headers
        removeHeader("BCC");
        if (bccHeaders != null)
            for (String bccAddress : bccHeaders) addHeader("BCC", bccAddress);
    }
    InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    int packetIndex = 0;
    try {
        while (true) {
            UnencryptedEmailPacket packet = new UnencryptedEmailPacket(inputStream, messageId, packetIndex, maxPacketSize);
            packets.add(packet);
            packetIndex++;
            if (inputStream.available() <= 0)
                break;
        }
    } catch (IOException e) {
        log.error("Can't read from ByteArrayInputStream.", e);
    }
    // #packets has not been set yet, do it now
    int numPackets = packetIndex;
    for (UnencryptedEmailPacket packet : packets) packet.setNumFragments(numPackets);
    return packets;
}
Also used : MessagingException(javax.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) UnencryptedEmailPacket(i2p.bote.packet.dht.UnencryptedEmailPacket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 9 with UnencryptedEmailPacket

use of i2p.bote.packet.dht.UnencryptedEmailPacket in project i2p.i2p-bote by i2p.

the class RelayPacketFolderTest method setUp.

@Before
public void setUp() throws Exception {
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    testDir = new File(tempDir, "RelayPacketFolderTest-" + System.currentTimeMillis());
    folderDir = new File(testDir, "relay_pkt");
    folder = new RelayPacketFolder(folderDir);
    // make an EncryptedEmailPacket
    String base64Destination = "X3oKYQJ~1EAz7B1ZYGSrOTIMCW5Rnn2Svoc38dx5D9~zvz8vqiWcH-pCqQDwLgPWl9RTBzHtTmZcGRPXIv54i0XWeUfX6rTPDQGuZsnBMM0xrkH2FNLNFaJa0NgW3uKXWpNj9AI1AXUXzK-2MYTYoaZHx5SBoCaKfAGMcFJvTON1~kopxBxdBF9Q7T4~PJ3I2LeU-ycmUlehe9N9bIu7adUGyPGVl8Ka-UxwQromoJ~vSWHHl8HkwcDkW--v9Aj~wvFqxqriFkB1EeBiThi3V4XtVY~GUP4IkRj9YZGTsSBf3eS4xwXgnYWlB7IvxAGBfHY9MCg3lbAa1Dg~1IH6rhtXxsXUtGcXsz9yMZTxXHd~rGo~JrXeM1y~Vcenpr6tJcum6pxevkKzzT0qDegGPH3Zhqz7sSeeIaJEcPBUAkX89csqyFWFIjTMm6yZp2rW-QYUnVNLNTjf7vndYUAEICogAkq~btqpIzrGEpm3Pr9F23br3SpbOmdxQxg51AMmAAAA";
    Destination nextDestination = new Destination(base64Destination.substring(0, 516));
    long delayMilliseconds = TimeUnit.MILLISECONDS.convert(111, TimeUnit.MINUTES);
    String content = "Warum, warum, warum\n" + "Ist die Banane krumm?\n" + "Weil niemand in den Urwald zog\n" + "Und die Banane grade bog.\n";
    byte[] messageIdBytes = new byte[] { -69, -24, -109, 1, 69, -122, -69, 113, -68, -90, 55, -28, 105, 97, 125, 70, 51, 58, 14, 2, -13, -53, 90, -29, 36, 67, 36, -94, -108, -125, 11, 123 };
    UniqueId messageId = new UniqueId(messageIdBytes, 0);
    int fragmentIndex = 0;
    InputStream contentStream = new ByteArrayInputStream(content.getBytes());
    UnencryptedEmailPacket unencryptedPacket = new UnencryptedEmailPacket(contentStream, messageId, fragmentIndex, I2PBotePacket.MAX_DATAGRAM_SIZE);
    unencryptedPacket.setNumFragments(1);
    String base64EmailDest = "rIbyUukqtsacD-MDJJ8KbIP9d3WQQo~t~zysc3bNcF1mSwz9PcGJnvWCNhnG2nzbdUAIDouESZjLRnBr7-mxNS";
    EmailDestination recipient = new EmailDestination(base64EmailDest);
    emailPacket = new EncryptedEmailPacket(unencryptedPacket, recipient);
    // make a RelayRequest
    StoreRequest storeRequest = new StoreRequest(emailPacket);
    relayRequest = new RelayRequest(storeRequest, nextDestination, delayMilliseconds, 1000);
}
Also used : Destination(net.i2p.data.Destination) EmailDestination(i2p.bote.email.EmailDestination) UniqueId(i2p.bote.UniqueId) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EncryptedEmailPacket(i2p.bote.packet.dht.EncryptedEmailPacket) StoreRequest(i2p.bote.packet.dht.StoreRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) EmailDestination(i2p.bote.email.EmailDestination) UnencryptedEmailPacket(i2p.bote.packet.dht.UnencryptedEmailPacket) File(java.io.File) RelayRequest(i2p.bote.packet.relay.RelayRequest) Before(org.junit.Before)

Example 10 with UnencryptedEmailPacket

use of i2p.bote.packet.dht.UnencryptedEmailPacket in project i2p.i2p-bote by i2p.

the class EmailTest method testCompression.

@Test
public void testCompression() throws MessagingException, GeneralSecurityException, PasswordException {
    // create a 500,000-char string that should compress to one packet
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < 50000; i++) stringBuilder.append("0123456789");
    Email newEmail = new Email(true);
    newEmail.setText(stringBuilder.toString());
    KeyUpdateHandler keyUpdateHandler = TestUtil.createDummyKeyUpdateHandler();
    Collection<UnencryptedEmailPacket> packets = newEmail.createEmailPackets(bccIdentity, keyUpdateHandler, null, I2PBotePacket.MAX_DATAGRAM_SIZE);
    assertEquals("The email was not compressed into one email packet.", 1, packets.size());
}
Also used : KeyUpdateHandler(i2p.bote.crypto.KeyUpdateHandler) UnencryptedEmailPacket(i2p.bote.packet.dht.UnencryptedEmailPacket) Test(org.junit.Test)

Aggregations

UnencryptedEmailPacket (i2p.bote.packet.dht.UnencryptedEmailPacket)12 EncryptedEmailPacket (i2p.bote.packet.dht.EncryptedEmailPacket)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 UniqueId (i2p.bote.UniqueId)5 KeyUpdateHandler (i2p.bote.crypto.KeyUpdateHandler)5 EmailDestination (i2p.bote.email.EmailDestination)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Test (org.junit.Test)4 File (java.io.File)3 Before (org.junit.Before)3 EmailIdentity (i2p.bote.email.EmailIdentity)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 GeneralSecurityException (java.security.GeneralSecurityException)2 MessagingException (javax.mail.MessagingException)2 Destination (net.i2p.data.Destination)2 Email (i2p.bote.email.Email)1 EmailMetadata (i2p.bote.email.EmailMetadata)1 EmailStatus (i2p.bote.folder.Outbox.EmailStatus)1 DhtException (i2p.bote.network.DhtException)1