Author: ningjiang Date: Mon Aug 13 11:05:23 2012 New Revision: 1372364 URL: http://svn.apache.org/viewvc?rev=1372364&view=rev Log: Merged revisions 1372356 via svnmerge from https://svn.apache.org/repos/asf/camel/branches/camel-2.10.x
................ r1372356 | ningjiang | 2012-08-13 18:37:18 +0800 (Mon, 13 Aug 2012) | 9 lines Merged revisions 1372354 via svnmerge from https://svn.apache.org/repos/asf/camel/trunk ........ r1372354 | ningjiang | 2012-08-13 18:23:45 +0800 (Mon, 13 Aug 2012) | 1 line CAMEL-5475 Fix the issue that camel can't unmarshal pgp messages encrypted with ElGamal. ........ ................ Added: camel/branches/camel-2.9.x/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatElGamalTest.java - copied unchanged from r1372356, camel/branches/camel-2.10.x/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatElGamalTest.java camel/branches/camel-2.9.x/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/pubring-ElGamal.gpg - copied unchanged from r1372356, camel/branches/camel-2.10.x/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/pubring-ElGamal.gpg camel/branches/camel-2.9.x/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/secring-ElGamal.gpg - copied unchanged from r1372356, camel/branches/camel-2.10.x/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/secring-ElGamal.gpg Modified: camel/branches/camel-2.9.x/ (props changed) camel/branches/camel-2.9.x/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java camel/branches/camel-2.9.x/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java camel/branches/camel-2.9.x/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java Propchange: camel/branches/camel-2.9.x/ ------------------------------------------------------------------------------ Merged /camel/trunk:r1372354 Merged /camel/branches/camel-2.10.x:r1372356 Propchange: camel/branches/camel-2.9.x/ ------------------------------------------------------------------------------ Binary property 'svnmerge-integrated' - no diff available. Modified: camel/branches/camel-2.9.x/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java?rev=1372364&r1=1372363&r2=1372364&view=diff ============================================================================== --- camel/branches/camel-2.9.x/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java (original) +++ camel/branches/camel-2.9.x/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java Mon Aug 13 11:05:23 2012 @@ -92,7 +92,7 @@ public class PGPDataFormat implements Da return null; } - PGPPrivateKey key = PGPDataFormatUtil.findPrivateKey(exchange.getContext(), keyFileName, keyUserid, password); + PGPPrivateKey key = PGPDataFormatUtil.findPrivateKey(exchange.getContext(), keyFileName, encryptedStream, password); if (key == null) { throw new IllegalArgumentException("Private key is null, cannot proceed"); } Modified: camel/branches/camel-2.9.x/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java?rev=1372364&r1=1372363&r2=1372364&view=diff ============================================================================== --- camel/branches/camel-2.9.x/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java (original) +++ camel/branches/camel-2.9.x/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java Mon Aug 13 11:05:23 2012 @@ -36,9 +36,11 @@ import org.bouncycastle.openpgp.PGPPubli import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.openpgp.PGPSecretKey; -import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.bouncycastle.openpgp.PGPUtil; +import org.bouncycastle.openpgp.PGPObjectFactory; +import org.bouncycastle.openpgp.PGPEncryptedDataList; +import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData; public final class PGPDataFormatUtil { @@ -83,42 +85,42 @@ public final class PGPDataFormatUtil { return null; } - public static PGPPrivateKey findPrivateKey(CamelContext context, String filename, String userid, String passphrase) throws IOException, - PGPException, NoSuchProviderException { + public static PGPPrivateKey findPrivateKey(CamelContext context, String keychainFilename, InputStream encryptedInput, String passphrase) + throws IOException, PGPException, NoSuchProviderException { - InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(), filename); + InputStream keyChainInputStream = ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(), keychainFilename); - PGPPrivateKey privKey; + PGPPrivateKey privKey = null; try { - privKey = findPrivateKey(context, is, userid, passphrase); + privKey = findPrivateKey(context, keyChainInputStream, encryptedInput, passphrase); } finally { - IOHelper.close(is); + IOHelper.close(keyChainInputStream); } return privKey; } @SuppressWarnings("unchecked") - public static PGPPrivateKey findPrivateKey(CamelContext context, InputStream input, String userid, String passphrase) throws IOException, + public static PGPPrivateKey findPrivateKey(CamelContext context, InputStream keyringInput, InputStream encryptedInput, String passphrase) throws IOException, PGPException, NoSuchProviderException { - PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input)); - - Iterator<PGPSecretKeyRing> keyRingIter = (Iterator<PGPSecretKeyRing>) pgpSec.getKeyRings(); - while (keyRingIter.hasNext()) { - PGPSecretKeyRing keyRing = keyRingIter.next(); - - Iterator<PGPSecretKey> keyIter = (Iterator<PGPSecretKey>) keyRing.getSecretKeys(); - while (keyIter.hasNext()) { - PGPSecretKey key = keyIter.next(); - for (Iterator<String> iterator = (Iterator<String>) key.getUserIDs(); iterator.hasNext();) { - String userId = iterator.next(); - if (key.isSigningKey() && userId.contains(userid)) { - return key.extractPrivateKey(passphrase.toCharArray(), "BC"); - } - } - } + PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyringInput)); + PGPObjectFactory factory = new PGPObjectFactory(PGPUtil.getDecoderStream(encryptedInput)); + PGPEncryptedDataList enc; + Object o = factory.nextObject(); + if (o instanceof PGPEncryptedDataList) { + enc = (PGPEncryptedDataList) o; + } else { + enc = (PGPEncryptedDataList) factory.nextObject(); } - - return null; + encryptedInput.reset(); // nextObject() method reads from the InputStream, so rewind it! + Iterator encryptedDataObjects = enc.getEncryptedDataObjects(); + PGPPrivateKey privateKey = null; + PGPPublicKeyEncryptedData encryptedData; + while (privateKey == null && encryptedDataObjects.hasNext()) { + encryptedData = (PGPPublicKeyEncryptedData) encryptedDataObjects.next(); + PGPSecretKey pgpSecKey = pgpSec.getSecretKey(encryptedData.getKeyID()); + privateKey = pgpSecKey.extractPrivateKey(passphrase.toCharArray(), "BC"); + } + return privateKey; } public static byte[] compress(byte[] clearData, String fileName, int algorithm) throws IOException { Modified: camel/branches/camel-2.9.x/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java?rev=1372364&r1=1372363&r2=1372364&view=diff ============================================================================== --- camel/branches/camel-2.9.x/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java (original) +++ camel/branches/camel-2.9.x/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java Mon Aug 13 11:05:23 2012 @@ -22,6 +22,14 @@ import org.apache.camel.builder.RouteBui import org.junit.Test; public class PGPDataFormatTest extends AbstractPGPDataFormatTest { + + protected String getKeyFileName() { + return "org/apache/camel/component/crypto/pubring.gpg"; + } + + protected String getKeyFileNameSec() { + return "org/apache/camel/component/crypto/secring.gpg"; + } @Test public void testEncryption() throws Exception { @@ -43,9 +51,9 @@ public class PGPDataFormatTest extends A public void configure() throws Exception { // START SNIPPET: pgp-format // Public Key FileName - String keyFileName = "org/apache/camel/component/crypto/pubring.gpg"; + String keyFileName = getKeyFileName(); // Private Key FileName - String keyFileNameSec = "org/apache/camel/component/crypto/secring.gpg"; + String keyFileNameSec = getKeyFileNameSec(); // Keyring Userid Used to Encrypt String keyUserid = "[email protected]"; // Private key password
