Add test to verify a signed PGP/MIME message

pgp_mime_preparations_view
cketti 2014-12-09 00:33:38 +01:00
parent 987b8b17b1
commit f89b0548a6
2 changed files with 274 additions and 3 deletions

View File

@ -43,12 +43,14 @@ dependencies {
}
androidTestCompile("com.icegreen:greenmail:1.3.1b") {
// Use a better, later version
exclude group: "javax.mail"
}
// Use a better, later version
exclude group: "javax.mail"
}
// this version avoids some "Ignoring InnerClasses attribute for an anonymous inner class" warnings
androidTestCompile "javax.mail:javax.mail-api:1.5.2"
androidTestCompile "com.madgag.spongycastle:pg:1.51.0.0"
}
project.ext.preDexLibs = !project.hasProperty('disablePreDex')

View File

@ -0,0 +1,269 @@
package com.fsck.k9.mail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.test.AndroidTestCase;
import com.fsck.k9.mail.internet.BinaryTempFileBody;
import com.fsck.k9.mail.internet.MimeMessage;
import org.spongycastle.openpgp.PGPCompressedData;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPObjectFactory;
import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.PGPPublicKeyRingCollection;
import org.spongycastle.openpgp.PGPSignature;
import org.spongycastle.openpgp.PGPSignatureList;
import org.spongycastle.openpgp.PGPUtil;
import org.spongycastle.openpgp.bc.BcPGPObjectFactory;
import org.spongycastle.openpgp.bc.BcPGPPublicKeyRingCollection;
import org.spongycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
public class PgpMimeMessageTest extends AndroidTestCase {
private static final String PUBLIC_KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
"Version: GnuPG v1\n" +
"\n" +
"mQINBE49+OsBEADIu2zVIYllkqLYaCZq2d8r80titzegJiXTaW8fRS0FKGE7KmNt\n" +
"tWvWdiyLqvWlP4Py9OZPmEBdz8AaPxqCFmVZfJimf28CW0wz2sRCYmmbQqaHFfpD\n" +
"rK+EJofckOu2j81coaFVLbvkvUNh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\n" +
"MWm/3YfACVVWNkG9PanhroNcVr925k/y58WRKdJOOgMGGBYyIAvtWb6m0Qn978AE\n" +
"53r7msHwZq06sPXIZJpCl6CTeyMrqU90G+JJY3BfP9rFsU9OLkDRrsAELleI9iXP\n" +
"QGw6Ixezdi93CqY+Y4weCjtYxm/5vKxwssg/ALVkM/VftWgWRSnZmnZwubgBzgwy\n" +
"wBwGHxPHz7CV3lBKZfw8U3L4Md3u1bMUu6Y+jW+322D+7+ZaLdJejmmJcEvLaItd\n" +
"c60IHTM/GbtV7TDiqQaRmyLY5KxnwGLthcYUsGI7HYDNqEa1+cRctB8lEWpgTjHK\n" +
"nwemvB5c1fPxao7w15O0tvSCX2kD5UMoAbvWJJvxcUTPTPBEHTYWrAk+Ny7CbdMA\n" +
"+71r942RXo9Xdm4hqjfMcDXdQmfjftfFB1rsBd5Qui8ideQP7ypllsWC8fJUkWN6\n" +
"3leW5gysLx9Mj6bu6XB4rYS1zH2keGtZe4Qqlxss7JPVsJzD9xSotg+G/Wb7F3HL\n" +
"HzpeeqkwzVU=\n" +
"=3yEX\n" +
"-----END PGP PUBLIC KEY BLOCK-----\n";
public void testSignedMessage() throws IOException, MessagingException, PGPException {
String messageSource = "Date: Mon, 08 Dec 2014 17:44:18 +0100\r\n" +
"From: cketti <cketti@googlemail.com>\r\n" +
"MIME-Version: 1.0\r\n" +
"To: test@example.com\r\n" +
"Subject: OpenPGP signature test\r\n" +
"Content-Type: multipart/signed; micalg=pgp-sha1;\r\n" +
" protocol=\"application/pgp-signature\";\r\n" +
" boundary=\"24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM\"\r\n" +
"\r\n" +
"This is an OpenPGP/MIME signed message (RFC 4880 and 3156)\r\n" +
"--24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM\r\n" +
"Content-Type: multipart/mixed;\r\n" +
" boundary=\"------------030308060900040601010501\"\r\n" +
"\r\n" +
"This is a multi-part message in MIME format.\r\n" +
"--------------030308060900040601010501\r\n" +
"Content-Type: text/plain; charset=utf-8\r\n" +
"Content-Transfer-Encoding: quoted-printable\r\n" +
"\r\n" +
"Message body\r\n" +
"goes here\r\n" +
"\r\n" +
"\r\n" +
"--------------030308060900040601010501\r\n" +
"Content-Type: text/plain; charset=UTF-8;\r\n" +
" name=\"attachment.txt\"\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"Content-Disposition: attachment;\r\n" +
" filename=\"attachment.txt\"\r\n" +
"\r\n" +
"VGV4dCBhdHRhY2htZW50Cg==\r\n" +
"--------------030308060900040601010501--\r\n" +
"\r\n" +
"--24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM\r\n" +
"Content-Type: application/pgp-signature; name=\"signature.asc\"\r\n" +
"Content-Description: OpenPGP digital signature\r\n" +
"Content-Disposition: attachment; filename=\"signature.asc\"\r\n" +
"\r\n" +
"-----BEGIN PGP SIGNATURE-----\r\n" +
"Version: GnuPG v1\r\n" +
"\r\n" +
"iQIcBAEBAgAGBQJUhdVqAAoJEO4v7zp9qOKJ8DQP/1+JE8UF7UmirnN1ZO+25hFC\r\n" +
"jAfFMxRWMWXN0gGB+6ySy6ah0bCwmRwHpRBsW/tNcsmOPKb2XBf9zwF06uk/lLp4\r\n" +
"ZmGXxSdQ9XJrlaHk8Sitn9Gi/1L+MNWgrsrLROAZv2jfc9wqN3FOrhN9NC1QXQvO\r\n" +
"+D7sMorSr3l94majoIDrzvxEnfJVfrZWNTUaulJofOJ55GBZ3UJNob1WKjrnculL\r\n" +
"IwmSERmVUoFBUfe/MBqqZH0WDJq9nt//NZFHLunj6nGsrpush1dQRcbR3zzQfXkk\r\n" +
"s7zDLDa8VUv6OxcefjsVN/O7EenoWWgNg6GfW6tY2+oUsLSP2OS3JXvYsylQP4hR\r\n" +
"iU1V9vvsu2Ax6bVb0+uTqw3jNiqVFy3o4mBigVUqp1EFIwBYmyNbe5wj4ACs9Avj\r\n" +
"9t2reFSfXobWQFUS4s71JeMefNAHHJWZI63wNTxE6LOw01YxdJiDaPWGTOyM75MK\r\n" +
"yqn7r5uIfeSv8NypGJaUv4firxKbrcZKk7Wpeh/rZuUSgoPcf3I1IzXfGKKIBHjU\r\n" +
"WUMhTF5SoC5kIZyeXvHrhTM8HszcS8EoG2XcmcYArwgCUlOunFwZNqLPsfdMTRL6\r\n" +
"9rcioaohEtroqoJiGAToJtIz8kqCaamnP/ASBkp9qqJizRd6fqt+tE8BsmJbuPLS\r\n" +
"6lBpS8j0TqmaZMYfB9u4\r\n" +
"=QvET\r\n" +
"-----END PGP SIGNATURE-----\r\n" +
"\r\n" +
"--24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM--\r\n";
BinaryTempFileBody.setTempDirectory(getContext().getCacheDir());
InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes());
MimeMessage message;
try {
message = new MimeMessage(messageInputStream, true);
} finally {
messageInputStream.close();
}
Multipart multipartSigned = (Multipart) message.getBody();
BodyPart signedPart = multipartSigned.getBodyPart(0);
ByteArrayOutputStream signedPartOutputStream = new ByteArrayOutputStream();
signedPart.writeTo(signedPartOutputStream);
byte[] signedData = signedPartOutputStream.toByteArray();
Body signatureBody = multipartSigned.getBodyPart(1).getBody();
ByteArrayOutputStream signatureBodyOutputStream = new ByteArrayOutputStream();
signatureBody.writeTo(signatureBodyOutputStream);
byte[] signatureData = signatureBodyOutputStream.toByteArray();
assertTrue(verifySignature(signedData, signatureData));
}
private boolean verifySignature(byte[] signedData, byte[] signatureData) throws IOException, PGPException {
InputStream signatureInputStream = PGPUtil.getDecoderStream(new ByteArrayInputStream(signatureData));
PGPObjectFactory pgpObjectFactory = new BcPGPObjectFactory(signatureInputStream);
Object pgpObject = pgpObjectFactory.nextObject();
PGPSignatureList pgpSignatureList;
if (pgpObject instanceof PGPCompressedData) {
PGPCompressedData compressedData = (PGPCompressedData) pgpObject;
pgpObjectFactory = new BcPGPObjectFactory(compressedData.getDataStream());
pgpSignatureList = (PGPSignatureList) pgpObjectFactory.nextObject();
} else {
pgpSignatureList = (PGPSignatureList) pgpObject;
}
PGPSignature signature = pgpSignatureList.get(0);
InputStream keyInputStream = PGPUtil.getDecoderStream(new ByteArrayInputStream(PUBLIC_KEY.getBytes()));
PGPPublicKeyRingCollection pgpPublicKeyRingCollection = new BcPGPPublicKeyRingCollection(keyInputStream);
PGPPublicKey publicKey = pgpPublicKeyRingCollection.getPublicKey(signature.getKeyID());
signature.init(new BcPGPContentVerifierBuilderProvider(), publicKey);
InputStream signedDataInputStream = new ByteArrayInputStream(signedData);
int ch;
while ((ch = signedDataInputStream.read()) >= 0) {
signature.update((byte) ch);
}
signedDataInputStream.close();
keyInputStream.close();
signatureInputStream.close();
return signature.verify();
}
}