Skip to content

Commit

Permalink
ZCS-13176:Mail recall zimlet message verfication header
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamCS03 committed Mar 24, 2023
1 parent 6d5058e commit b577990
Showing 1 changed file with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions store/src/java/com/zimbra/cs/mailbox/MailSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import javax.mail.Address;
Expand Down Expand Up @@ -90,6 +84,8 @@ public class MailSender {

public static final String MSGTYPE_REPLY = String.valueOf(Flag.toChar(Flag.ID_REPLIED));
public static final String MSGTYPE_FORWARD = String.valueOf(Flag.toChar(Flag.ID_FORWARDED));
public static final String MSGVRFY_HEADER_PREFIX = "hash=SHA256;guid=";
public static final String MSGVRFY_ALGORITHM_NAME = "SHA-256";
private static Map<String, PreSendMailListener> mPreSendMailListeners = new ConcurrentHashMap<String, PreSendMailListener>();

private Boolean mSaveToSent;
Expand Down Expand Up @@ -1015,7 +1011,12 @@ void updateHeaders(MimeMessage mm, Account acct, Account authuser, OperationCont
mm.setFrom(from);
mm.setSender(sender);

mm.setSentDate(new Date());
Date date = new Date();
mm.setSentDate(date);

String value = getMessageVerificationHeaderValue(mm.getMessageID(), date, from.getAddress());
mm.addHeader("Message-Verification", value);

if (sender == null) {
Address[] existingReplyTos = mm.getReplyTo();
if (existingReplyTos == null || existingReplyTos.length == 0) {
Expand Down Expand Up @@ -1478,4 +1479,45 @@ public static void unregisterPreSendMailListener(PreSendMailListener listener) {
}
}
}

/**
* provide the Hash for Message-Verification header field.
* @param id
* @param date
* @param from
* @throws ServiceException
*
*/
private static String getMessageVerificationHeaderValue(String id, Date date, String from) throws MessagingException, ServiceException {
String guid = (id + date + from);
String guidHash = getHashForMessageVerification(guid, MSGVRFY_ALGORITHM_NAME);
String hash = MSGVRFY_HEADER_PREFIX + guidHash;
return hash;
}

/**
* Create a digest of the given input with the given algorithm.
* @param input
* @param algo
* @throws ServiceException
*/
private static String getHashForMessageVerification(String input, String algo) throws ServiceException {
try {
MessageDigest md1 = MessageDigest.getInstance(algo);
byte[] messageDigest1 = md1.digest(input.getBytes(StandardCharsets.UTF_8));
BigInteger number = new BigInteger(1, messageDigest1);
StringBuilder hexString = new StringBuilder(number.toString(16));

while (hexString.length() < 64) {
hexString.insert(0, '0');
}

String basicBase64format
= Base64.getEncoder()
.encodeToString(hexString.toString().getBytes());
return basicBase64format;
} catch (NoSuchAlgorithmException e) {
throw ServiceException.FAILURE("Unable to encrypt", e);
}
}
}

0 comments on commit b577990

Please sign in to comment.