Skip to content

Commit

Permalink
Merge pull request #2 from TpmKranz/feature-pgpencryption
Browse files Browse the repository at this point in the history
Add PGP encryption abilities
  • Loading branch information
Tom committed Nov 21, 2015
2 parents bc565e2 + 9a0ce5a commit 61d6224
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ An Android app to forward SMS to an email account

Implemented thus far are:
* [x] Basic functionality (accepting SMTP credentials and actually catching and forwarding SMS via SMTP)
* [x] End-to-end encryption via PGP

Planned features:
* [ ] End-to-end encryption via PGP
* [ ] Direct access to javax.mail props
2 changes: 1 addition & 1 deletion app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="recyclerview-v7-23.1.1" level="project" />
<orderEntry type="library" exported="" name="mail" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" />
<orderEntry type="library" exported="" name="pkix-1.51.0.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.1" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.1.1" level="project" />
<orderEntry type="library" exported="" name="design-23.1.1" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.1.1" level="project" />
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "org.tpmkranz.smsforward"
minSdkVersion 15
targetSdkVersion 23
versionCode 20151120
versionName "0.9.0.1"
versionCode 20151122
versionName "0.9.1"
}
buildTypes {
release {
Expand Down
18 changes: 12 additions & 6 deletions app/src/main/java/org/tpmkranz/smsforward/EmailSenderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
Expand All @@ -25,8 +24,9 @@ public class EmailSenderService extends Service {
Service mContext;
SharedPreferences settings;
int mNotificationOffset;
public static String INTENTEXTRABODY = "emailBody";
public static String INTENTEXTRASUBJECT = "emailSubject";
protected static String LOGCATTAG = "EmailSenderService";
protected static String INTENTEXTRABODY = "emailBody";
protected static String INTENTEXTRASUBJECT = "emailSubject";

public EmailSenderService() {
}
Expand Down Expand Up @@ -107,10 +107,16 @@ protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
message.setFrom(new InternetAddress(username));
message.setRecipient(Message.RecipientType.TO, InternetAddress.parse(target)[0]);
message.setSubject(params[0]);
message.setText(params[1]);
String text = params[1]+"\n";
PGPPubkeyEncryptionUtil encrypter = new PGPPubkeyEncryptionUtil(settings.getString(SetupActivity.SHAREDPREFSPUBKEY, ""));
if (encrypter.hasKey())
text = encrypter.encrypt(text);
else
encrypter = null;
message.setText(text);
Transport.send(message);
}catch (MessagingException e){
Log.e(mContext.getResources().getString(R.string.app_name), e.toString());
}catch (Exception e){
Log.e(LOGCATTAG, e.toString());
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.tpmkranz.smsforward;

import android.util.Log;

import org.spongycastle.bcpg.ArmoredOutputStream;
import org.spongycastle.openpgp.PGPCompressedData;
import org.spongycastle.openpgp.PGPCompressedDataGenerator;
import org.spongycastle.openpgp.PGPEncryptedData;
import org.spongycastle.openpgp.PGPEncryptedDataGenerator;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPLiteralData;
import org.spongycastle.openpgp.PGPLiteralDataGenerator;
import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPUtil;
import org.spongycastle.openpgp.jcajce.JcaPGPPublicKeyRingCollection;
import org.spongycastle.openpgp.operator.bc.BcPGPDataEncryptorBuilder;
import org.spongycastle.openpgp.operator.bc.BcPublicKeyKeyEncryptionMethodGenerator;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import java.util.Iterator;

/**
* Created by tpm on 11/20/15.
*/
public class PGPPubkeyEncryptionUtil {

protected static String LOGCATTAG = "PGPPubkeyEncryptionUtil";
private PGPPublicKey publicKey = null;

public PGPPubkeyEncryptionUtil(String pubkeyBlock){
InputStream in = new ByteArrayInputStream(pubkeyBlock.getBytes());
publicKey = null;
try {
in = PGPUtil.getDecoderStream(in);
JcaPGPPublicKeyRingCollection pgpPub = new JcaPGPPublicKeyRingCollection(in);
in.close();
Iterator<PGPPublicKeyRing> rIt = pgpPub.getKeyRings();
while (publicKey == null && rIt.hasNext()) {
PGPPublicKeyRing keyRing = rIt.next();
Iterator<PGPPublicKey> kIt = keyRing.getPublicKeys();
while (publicKey == null && kIt.hasNext()) {
PGPPublicKey k = kIt.next();
if (k.isEncryptionKey()) {
publicKey = k;
}
}
}
} catch (Exception e){
Log.e(LOGCATTAG, e.toString());
}
}

public boolean hasKey(){
return publicKey != null;
}


public String encrypt(String plainText) throws NoSuchAlgorithmException, IOException, PGPException {
byte[] rawText = plainText.getBytes();
//This needs, like, three metric fucktons of explanation and/or cleaning up
ByteArrayOutputStream encOut = new ByteArrayOutputStream();
OutputStream out = new ArmoredOutputStream(encOut);
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
OutputStream cos = comData.open(bOut);
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
OutputStream pOut = lData.open(cos, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, rawText.length, new Date());
pOut.write(rawText);
lData.close();
comData.close();
BcPGPDataEncryptorBuilder builder = new BcPGPDataEncryptorBuilder(PGPEncryptedData.AES_256);
builder.setWithIntegrityPacket(true);
builder.setSecureRandom(new SecureRandom());
PGPEncryptedDataGenerator cpk = new PGPEncryptedDataGenerator(builder);
cpk.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
byte[] bytes = bOut.toByteArray();
OutputStream cOut = cpk.open(out, bytes.length);
cOut.write(bytes);
cOut.close();
out.close();
return new String(encOut.toByteArray(), Charset.forName("UTF-8"));
}
}
9 changes: 0 additions & 9 deletions app/src/main/java/org/tpmkranz/smsforward/SMSListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;

import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SMSListener extends BroadcastReceiver{
@Override
Expand Down
25 changes: 21 additions & 4 deletions app/src/main/java/org/tpmkranz/smsforward/SetupActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.security.Security;
Expand All @@ -29,6 +29,7 @@


public class SetupActivity extends AppCompatActivity {
protected static String LOGCATTAG = "SetupActivity";
protected static String SHAREDPREFSNAME = "org.tpmkranz.smsforward.SETTINGS";
protected static String SHAREDPREFSEMAIL = "email";
protected static String SHAREDPREFSPASSWORD = "pass";
Expand All @@ -43,6 +44,7 @@ public class SetupActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword, inputServer, inputPort, inputTarget, inputPubkey;
private SharedPreferences settings;
private HashMap<String,String> currentSettings;
private PGPPubkeyEncryptionUtil encrypter;


static {
Expand Down Expand Up @@ -125,9 +127,24 @@ private List<EditText> invalidSettings(){
invalidSettings.add(inputPort);
if (!getInputText(inputTarget).contains("@"))
invalidSettings.add(inputTarget);
if (!(getInputText(inputPubkey).contains("-----BEGIN PGP PUBLIC KEY BLOCK-----")
|| getInputText(inputPubkey).isEmpty()))
invalidSettings.add(inputPubkey);
if (!getInputText(inputPubkey).isEmpty()){
encrypter = new PGPPubkeyEncryptionUtil(getInputText(inputPubkey));
if (!encrypter.hasKey()) {
encrypter = null;
invalidSettings.add(inputPubkey);
}else{
String testEncryption = null;
try{
testEncryption = encrypter.encrypt("Test");
}catch (Exception e){
Log.e(LOGCATTAG, e.toString());
encrypter = null;
invalidSettings.add(inputPubkey);
}
if (testEncryption == null)
invalidSettings.add(inputPubkey);
}
}
return invalidSettings;
}

Expand Down

0 comments on commit 61d6224

Please sign in to comment.