Skip to content

Commit

Permalink
open pgp encryption added
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwechner committed Jan 31, 2014
1 parent 4899217 commit 9a658e2
Showing 1 changed file with 95 additions and 3 deletions.
98 changes: 95 additions & 3 deletions src/core/java/org/wyona/yanel/core/util/MailUtil.java
Expand Up @@ -8,13 +8,25 @@
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
* SMTP mail utility class
*/
public class MailUtil {
private static Logger log = Logger.getLogger(MailUtil.class);
private static Logger log = LogManager.getLogger(MailUtil.class);

public static final String STATUS = "status";
public static final String MESSAGE = "message";
Expand All @@ -40,6 +52,86 @@ public static void send(String from, String replyTo, String to, String subject,
send(null, -1, from, replyTo, to, subject, content);
}

/**
* @param from From address
* @param replyTo email address (if null, then no reply-to will be set)
* @param recipient Recipient (to which email will be sent)
* @param subject Subject of email
* @param content Body of email
*/
public static void send(String from, String replyTo, Recipient recipient, String subject, String content) throws AddressException, MessagingException {
if (recipient.getPGPPublicKey() != null) {
try {
// INFO: See http://fastpicket.com/blog/2012/05/14/easy-pgp-in-java-bouncy-castle/
//log.debug("Encrypt email with PGP public key: " + recipient.getPGPPublicKey());
// INFO: Add the bouncy castle security provider or have it installed in $JAVA_HOME/jre/lib/ext
java.security.Security.addProvider(new BouncyCastleProvider());

PGPPublicKeyRing keyRing = getKeyring(new java.io.ByteArrayInputStream(recipient.getPGPPublicKey().getBytes()));
PGPPublicKey publicKey = getEncryptionKey(keyRing);
//log.debug("Public Key: " + publicKey);
//log.debug("ID: " + publicKey.getKeyID());
// make an output stream connected to a file this also works with output streams in servlets
File tmpFile = new File(System.getProperty("java.io.tmpdir"), "/openpgp-encrypted-email-" + new java.util.Date().getTime() + ".asc");
java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream(tmpFile);
// make one of our encryption utilities
PGPEncryptionUtil util = new PGPEncryptionUtil(publicKey, "secrets.txt", fileOutputStream);
// finally write something
java.io.PrintWriter pw = new java.io.PrintWriter(util.getPayloadOutputStream());
pw.println(content);
// flush the stream and close up everything
pw.flush();
util.close();
send(null, -1, from, replyTo, recipient.getEmail(), subject, org.apache.commons.io.IOUtils.toString(new java.io.FileInputStream(tmpFile)));
tmpFile.delete();
} catch(Exception e) {
log.error(e, e);
throw new MessagingException(e.getMessage());
}
} else {
send(null, -1, from, replyTo, recipient.getEmail(), subject, content);
}
}

/**
* Decode a PGP public key block and return the keyring it represents.
* @param keyBlockStream Piblic key block as stream
* @return TODO
*/
private static PGPPublicKeyRing getKeyring(InputStream keyBlockStream) throws IOException {
// PGPUtil.getDecoderStream() will detect ASCII-armor automatically and decode it,
// the PGPObject factory then knows how to read all the data in the encoded stream
PGPObjectFactory factory = new PGPObjectFactory(PGPUtil.getDecoderStream(keyBlockStream));

// these files should really just have one object in them,
// and that object should be a PGPPublicKeyRing.
Object o = factory.nextObject();
if (o instanceof PGPPublicKeyRing) {
return (PGPPublicKeyRing)o;
}
throw new IllegalArgumentException("Input text does not contain a PGP Public Key");
}

/**
* Get the first encyption key off the given keyring.
*/
private static PGPPublicKey getEncryptionKey(PGPPublicKeyRing keyRing) {
if (keyRing == null)
return null;

// iterate over the keys on the ring, look for one
// which is suitable for encryption.
Iterator keys = keyRing.getPublicKeys();
PGPPublicKey key = null;
while (keys.hasNext()) {
key = (PGPPublicKey)keys.next();
if (key.isEncryptionKey()) {
return key;
}
}
return null;
}

/**
* @param fromAddress From address, e.g. contact@wyona.org
* @param fromName From name, e.g. Wyona
Expand Down Expand Up @@ -138,7 +230,7 @@ public static void send(String smtpHost, int smtpPort, String fromEmailAddress,
msg.setSubject(subject);
msg.setText(content, charset, mimeSubType);

// Send the message
// INFO: Send the message
Transport.send(msg);
}
}
Expand Down

0 comments on commit 9a658e2

Please sign in to comment.