You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2012/08/13 12:23:45 UTC

svn commit: r1372354 - in /camel/trunk/components/camel-crypto/src: main/java/org/apache/camel/converter/crypto/ test/java/org/apache/camel/converter/crypto/ test/resources/org/apache/camel/component/crypto/

Author: ningjiang
Date: Mon Aug 13 10:23:45 2012
New Revision: 1372354

URL: http://svn.apache.org/viewvc?rev=1372354&view=rev
Log:
CAMEL-5475 Fix the issue that camel can't unmarshal pgp messages encrypted with ElGamal.

Added:
    camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatElGamalTest.java
    camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/pubring-ElGamal.gpg
    camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/secring-ElGamal.gpg
Modified:
    camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java
    camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java
    camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java

Modified: camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java?rev=1372354&r1=1372353&r2=1372354&view=diff
==============================================================================
--- camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java (original)
+++ camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormat.java Mon Aug 13 10:23:45 2012
@@ -91,7 +91,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/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java?rev=1372354&r1=1372353&r2=1372354&view=diff
==============================================================================
--- camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java (original)
+++ camel/trunk/components/camel-crypto/src/main/java/org/apache/camel/converter/crypto/PGPDataFormatUtil.java Mon Aug 13 10:23:45 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 = pgpSec.getKeyRings();
-        while (keyRingIter.hasNext()) {
-            PGPSecretKeyRing keyRing = keyRingIter.next();
-
-            Iterator<PGPSecretKey> keyIter = keyRing.getSecretKeys();
-            while (keyIter.hasNext()) {
-                PGPSecretKey key = keyIter.next();
-                for (Iterator<String> iterator = 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 {

Added: camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatElGamalTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatElGamalTest.java?rev=1372354&view=auto
==============================================================================
--- camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatElGamalTest.java (added)
+++ camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatElGamalTest.java Mon Aug 13 10:23:45 2012
@@ -0,0 +1,28 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.converter.crypto;
+
+public class PGPDataFormatElGamalTest extends PGPDataFormatTest {
+    protected String getKeyFileName() {
+        return "org/apache/camel/component/crypto/pubring-ElGamal.gpg";
+    }
+    
+    protected String getKeyFileNameSec() {
+        return "org/apache/camel/component/crypto/secring-ElGamal.gpg";
+    }
+
+}

Modified: camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java?rev=1372354&r1=1372353&r2=1372354&view=diff
==============================================================================
--- camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java (original)
+++ camel/trunk/components/camel-crypto/src/test/java/org/apache/camel/converter/crypto/PGPDataFormatTest.java Mon Aug 13 10:23:45 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 = "sdude@nowhere.net";
                 // Private key password

Added: camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/pubring-ElGamal.gpg
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/pubring-ElGamal.gpg?rev=1372354&view=auto
==============================================================================
Files camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/pubring-ElGamal.gpg (added) and camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/pubring-ElGamal.gpg Mon Aug 13 10:23:45 2012 differ

Added: camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/secring-ElGamal.gpg
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/secring-ElGamal.gpg?rev=1372354&view=auto
==============================================================================
Files camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/secring-ElGamal.gpg (added) and camel/trunk/components/camel-crypto/src/test/resources/org/apache/camel/component/crypto/secring-ElGamal.gpg Mon Aug 13 10:23:45 2012 differ