public static void checkRecipient(Address address) throws AddressException { String addr = address.toString(); try { new EmailDestination(addr); } catch (GeneralSecurityException e) { // check for external address // InternetAddress accepts addresses without a domain, so check that there is a '.' after the '@' if (addr.indexOf('@') >= addr.indexOf('.')) throw new AddressException("Address doesn't contain an Email Destination or an external address", addr); } } /*
/** * @param emailDestination Determines the DHT key of this Index Packet */ public IndexPacket(EmailDestination emailDestination) { this(emailDestination.getHash()); }
public String getBase64Dest() { return destination.toBase64(); }
/** * Verifies the signature and the fingerprint. * @return * @throws GeneralSecurityException */ public boolean verify() throws GeneralSecurityException { if (signature==null || fingerprint==null) return false; CryptoImplementation cryptoImpl = destination.getCryptoImpl(); PublicKey key = destination.getPublicSigningKey(); boolean valid = cryptoImpl.verify(getDataToSign(), signature, key); valid &= fingerprint.isValid(); return valid; }
/** * Looks up an {@link EmailIdentity} that has the same public encryption key and the * same public signing key as a given {@link EmailDestination}.<br/> * Returns <code>null</code> if nothing is found. * @param destination * @throws PasswordException * @throws GeneralSecurityException * @throws IOException */ public EmailIdentity get(EmailDestination destination) throws PasswordException, IOException, GeneralSecurityException { initializeIfNeeded(); if (identities == null) return null; for (EmailIdentity identity: identities) if (identity.getPublicEncryptionKey().equals(destination.getPublicEncryptionKey()) && identity.getPublicSigningKey().equals(destination.getPublicSigningKey())) return identity; return null; }
EmailDestination senderDestination = new EmailDestination(getOneFromAddress()); return cryptoImpl.verify(toByteArray(), signature, senderDestination.getPublicSigningKey()); } catch (Exception e) { log.error("Cannot verify email signature. Email: [" + this + "]", e);
/** * Looks for an Email Destination in a sender address. If no Email Destination * is found, </code>null</code> is returned. * @param address */ public static String extractEmailDestination(String address) { return EmailDestination.extractBase64Dest(address); }
/** * 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(); }
public Hash getHash() { // TODO cache the hash value? return SHA256Generator.getInstance().calculateHash(toByteArray()); }
@Override public String toString() { return getKey(); } }
/** * Same as {@link #getNameAndDestination(String)} but base64 destinations are shortened to 4 characters * if they are in the address book or refer to an email identity. */ public String getNameAndShortDestination(String address) throws PasswordException, IOException, GeneralSecurityException { String nameAndDest = getNameAndDestination(address); String base64dest = EmailDestination.extractBase64Dest(address); if (base64dest == null) return nameAndDest; if (nameAndDest.contains(base64dest) && nameAndDest.length()>base64dest.length()) nameAndDest = nameAndDest.replace(base64dest, base64dest.substring(0, 4)); return nameAndDest; }
byte[] destBytes = destination.toByteArray(); dataStream.writeShort(destBytes.length); dataStream.write(destBytes);
public String toBase64() { return getKey(); }
byte[] emailDestBytes = new byte[emailDestLength]; buffer.get(emailDestBytes); destination = new EmailDestination(emailDestBytes);
private int getDestinationIndex(EmailDestination destination) { int destIndex = 0; while (true) { String value = getProperty(PROPERTY_DESTINATION + destIndex); if (value==null || destination.toBase64().equals(value)) return destIndex; destIndex++; } }
Hash dest1Hash = destination1.getHash(); IndexPacketDeleteRequest delRequest = new IndexPacketDeleteRequest(dest1Hash); Hash dhtKey1 = emailPacket1.getDhtKey();
private void removeRecipientName(InternetAddress address) { String addr = address.getAddress(); String dest = EmailDestination.extractBase64Dest(addr); if (dest != null) try { address.setPersonal(null); } catch (UnsupportedEncodingException e) {} // If there is no email destination, assume it is an external address and don't change it }
/** * Returns a byte array of length <code>NUM_FINGERPRINT_BYTES</code>. * @throws GeneralSecurityException */ private byte[] getRawFingerprint() throws GeneralSecurityException { byte[] input = Util.concat(nameHash.toByteArray(), destination.toByteArray()); byte[] fingerprint = SCrypt.scrypt(input, salt, SCRYPT_PARAMETERS.N, SCRYPT_PARAMETERS.r, SCRYPT_PARAMETERS.p, NUM_FINGERPRINT_BYTES); return fingerprint; }
@Override public int compare(Contact contact1, Contact contact2) { int nameComparison = String.CASE_INSENSITIVE_ORDER.compare(contact1.getName(), contact2.getName()); if (nameComparison == 0) { // if the names are the same, compare destination keys String key1 = contact1.getDestination().getKey(); String key2 = contact2.getDestination().getKey(); return key1.compareTo(key2); } else return nameComparison; } }
continue; try { EmailDestination destination = new EmailDestination(destBase64); String pictureBase64 = properties.getProperty(prefix + "picture"); String text = properties.getProperty(prefix + "text");