public void removeSignatureFlag() { try { removeHeader(SIGNATURE_VALID_HEADER); } catch (MessagingException e) { log.error("Cannot remove header field: " + SIGNATURE_VALID_HEADER, e); } }
/** * Verifies the signature and sets the <code>SIGNATURE_VALID_HEADER</code> * header field accordingly. */ public void setSignatureFlag() { try { removeHeader(SIGNATURE_VALID_HEADER); // remove the signature validity flag before verifying boolean valid = verifySignature(); setHeader(SIGNATURE_VALID_HEADER, String.valueOf(valid)); } catch (MessagingException e) { log.error("Cannot get header field: " + SIGNATURE_VALID_HEADER, e); } }
/** * Removes all mail headers except the ones in <code>HEADER_WHITELIST</code>. * @throws MessagingException */ private void scrubHeaders() throws MessagingException { @SuppressWarnings("unchecked") List<Header> nonMatchingHeaders = Collections.list(getNonMatchingHeaders(HEADER_WHITELIST)); for (Header header: nonMatchingHeaders) if (header != null) { log.debug("Removing all instances of non-whitelisted header <" + header.getName() + ">"); removeHeader(header.getName()); } }
/** * Creates a digital signature of the email and stores it in the * <code>SIGNATURE_HEADER</code> header field. It also removes the * <code>SIGNATURE_VALID_HEADER</code> header. If there is a signature * already, it is replaced.<br/> * The signature is computed over the stream representation of the * email, minus the signature header if it is present.<br/> * The signature includes the ID number of the {@link CryptoImplementation} * used (signature lengths can be different for the same algorithm). * @param senderIdentity * @param keyUpdateHandler Needed for updating the signature key after signing (see {@link CryptoImplementation#sign(byte[], PrivateKey, KeyUpdateHandler)}) * @throws MessagingException * @throws GeneralSecurityException * @throws PasswordException */ public void sign(EmailIdentity senderIdentity, KeyUpdateHandler keyUpdateHandler) throws MessagingException, GeneralSecurityException, PasswordException { removeHeader(SIGNATURE_HEADER); // make sure there is no existing signature which would make the new signature invalid removeHeader(SIGNATURE_VALID_HEADER); // remove the signature validity flag before signing CryptoImplementation cryptoImpl = senderIdentity.getCryptoImpl(); PrivateKey privateSigningKey = senderIdentity.getPrivateSigningKey(); byte[] signature = cryptoImpl.sign(toByteArray(), privateSigningKey, keyUpdateHandler); String foldedSignature = foldSignature(cryptoImpl.getId() + "_" + Base64.encode(signature)); setHeader(SIGNATURE_HEADER, foldedSignature); }
setHeader("BCC", bccToKeep); // set bccToKeep and remove any other existing BCC addresses else removeHeader("BCC"); if (!isAnonymous()) sign(senderIdentity, keyUpdateHandler); } finally { removeHeader("BCC"); if (bccHeaders != null) for (String bccAddress: bccHeaders)
/** * Removes all headers that are not on the whitelist, and initializes some * basic header fields.<br/> * Called by {@link #saveChanges()}, see JavaMail JavaDoc. * @throws MessagingException */ @Override public void updateHeaders() throws MessagingException { super.updateHeaders(); scrubHeaders(); removeRecipientNames(); // Depending on includeSendTime, set the send time or remove the send time field if (includeSendTime) { // Ensure the "Date" field is set in UTC time, using the English locale. MailDateFormat formatter = new MailDateFormat(); formatter.setTimeZone(TimeZone.getTimeZone("GMT")); // always use UTC for outgoing mail if (getSentDate() == null) setHeader("Date", formatter.format(new Date())); else setHeader("Date", formatter.format(getSentDate())); } else removeHeader("Date"); }
removeHeader(SIGNATURE_HEADER); // remove the signature before verifying byte[] signature = Base64.decode(base64Signature); EmailDestination senderDestination = new EmailDestination(getOneFromAddress());
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); } }