You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2008/07/17 09:06:49 UTC

svn commit: r677515 - in /commons/sandbox/openpgp/trunk/src: main/java/org/apache/commons/openpgp/ test/java/org/apache/commons/openpgp/ test/resources/

Author: brett
Date: Thu Jul 17 00:06:49 2008
New Revision: 677515

URL: http://svn.apache.org/viewvc?rev=677515&view=rev
Log:
add test for key ring - to load a full key ring file you actually need to process multiple key rings, so revert to previous behaviour but store directly to reduce the amount of iteration needed

Added:
    commons/sandbox/openpgp/trunk/src/test/java/org/apache/commons/openpgp/BouncyCastleKeyRingTest.java   (with props)
Modified:
    commons/sandbox/openpgp/trunk/src/main/java/org/apache/commons/openpgp/BouncyCastleKeyRing.java
    commons/sandbox/openpgp/trunk/src/main/java/org/apache/commons/openpgp/KeyRing.java
    commons/sandbox/openpgp/trunk/src/test/resources/pubring.gpg
    commons/sandbox/openpgp/trunk/src/test/resources/secring.gpg

Modified: commons/sandbox/openpgp/trunk/src/main/java/org/apache/commons/openpgp/BouncyCastleKeyRing.java
URL: http://svn.apache.org/viewvc/commons/sandbox/openpgp/trunk/src/main/java/org/apache/commons/openpgp/BouncyCastleKeyRing.java?rev=677515&r1=677514&r2=677515&view=diff
==============================================================================
--- commons/sandbox/openpgp/trunk/src/main/java/org/apache/commons/openpgp/BouncyCastleKeyRing.java (original)
+++ commons/sandbox/openpgp/trunk/src/main/java/org/apache/commons/openpgp/BouncyCastleKeyRing.java Thu Jul 17 00:06:49 2008
@@ -17,96 +17,112 @@
  * limitations under the License.
  */
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
 import org.bouncycastle.openpgp.PGPException;
+import org.bouncycastle.openpgp.PGPObjectFactory;
 import org.bouncycastle.openpgp.PGPPublicKey;
 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 java.io.IOException;
-import java.io.InputStream;
-import java.util.Iterator;
-
 /**
  * Bouncy Castle implementation of the OpenPGP key ring.
- *
+ * 
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  * @todo password is not secure
  */
 public class BouncyCastleKeyRing
     implements KeyRing
 {
-    private final PGPSecretKeyRing pgpSec;
+    private final Map pgpSec = new HashMap();
 
-    private final char[] password;
+    private char[] password;
 
-    private final PGPPublicKeyRing pgpPub;
+    private final Map pgpPub = new HashMap();
 
     private static final long MASK = 0xFFFFFFFFL;
 
+    public BouncyCastleKeyRing()
+    {
+    }
+    
     public BouncyCastleKeyRing( InputStream secretKeyRingStream, InputStream publicKeyRingStream, char[] password )
         throws IOException, PGPException
     {
-        pgpSec = new PGPSecretKeyRing( PGPUtil.getDecoderStream( secretKeyRingStream ) );
+        addSecretKeyRing( secretKeyRingStream, password );
 
-        pgpPub = new PGPPublicKeyRing( PGPUtil.getDecoderStream( publicKeyRingStream ) );
-
-        this.password = password;
+        addPublicKeyRing( publicKeyRingStream );
     }
 
-    public char[] getPassword()
-    {
-        return password;
-    }
-    
-    public PGPSecretKey getSecretKey( String keyId )
+    public void addPublicKeyRing( InputStream publicKeyRingStream )
+        throws IOException, PGPException
     {
-        Iterator kIt = pgpSec.getSecretKeys();
+        PGPObjectFactory pgpFact = new PGPObjectFactory( PGPUtil.getDecoderStream( publicKeyRingStream ) );
+        Object obj;
 
-        while ( kIt.hasNext() )
+        while ( ( obj = pgpFact.nextObject() ) != null )
         {
-            PGPSecretKey k = (PGPSecretKey) kIt.next();
-
-            // TODO: do long conversion in other direction
-            if ( k.isSigningKey() && Long.toHexString( k.getKeyID() & MASK ).equals( keyId.toLowerCase() ) )
+            if ( !( obj instanceof PGPPublicKeyRing ) )
             {
-                return k;
+                throw new PGPException( obj.getClass().getName() + " found where PGPPublicKeyRing expected" );
             }
-        }
 
-        return null;
+            PGPPublicKeyRing keyRing = (PGPPublicKeyRing) obj;
+            Long key = new Long( keyRing.getPublicKey().getKeyID() & MASK );
+
+            pgpPub.put( key, keyRing.getPublicKey() );
+        }
     }
 
-    public PGPPublicKey getPublicKey( String keyId )
+    public void addSecretKeyRing( InputStream secretKeyRingStream, char[] password )
+        throws IOException, PGPException
     {
-        Iterator kIt = pgpPub.getPublicKeys();
+        PGPObjectFactory pgpFact = new PGPObjectFactory( PGPUtil.getDecoderStream( secretKeyRingStream ) );
+        Object obj;
 
-        while ( kIt.hasNext() )
+        while ( ( obj = pgpFact.nextObject() ) != null )
         {
-            PGPPublicKey k = (PGPPublicKey) kIt.next();
-
-            // TODO: do long conversion in other direction
-            if ( Long.toHexString( k.getKeyID() & MASK ).equals( keyId.toLowerCase() ) )
+            if ( !( obj instanceof PGPSecretKeyRing ) )
             {
-                return k;
+                throw new PGPException( obj.getClass().getName() + " found where PGPSecretKeyRing expected" );
             }
+
+            PGPSecretKeyRing pgpSecret = (PGPSecretKeyRing) obj;
+            Long key = new Long( pgpSecret.getSecretKey().getKeyID() & MASK );
+
+            pgpSec.put( key, pgpSecret.getSecretKey() );
         }
 
-        return null;
+        this.password = password;
+    }
+
+    public char[] getPassword()
+    {
+        return password;
+    }
+
+    public PGPSecretKey getSecretKey( String keyId )
+    {
+        return (PGPSecretKey) pgpSec.get( Long.valueOf( keyId, 16 ) );
+    }
+
+    public PGPPublicKey getPublicKey( String keyId )
+    {
+        return (PGPPublicKey) pgpPub.get( Long.valueOf( keyId, 16 ) );
     }
 
     public PGPSecretKey getSecretKey( long keyId )
     {
-        // TODO: switch with above
-        return getSecretKey( Long.toHexString( keyId & MASK ) );
+        return (PGPSecretKey) pgpSec.get( new Long( keyId & MASK ) );
     }
 
     public PGPPublicKey getPublicKey( long keyId )
     {
-        // TODO: switch with above
-        return getPublicKey( Long.toHexString( keyId & MASK ) );
+        return (PGPPublicKey) pgpPub.get( new Long( keyId & MASK ) );
     }
 }

Modified: commons/sandbox/openpgp/trunk/src/main/java/org/apache/commons/openpgp/KeyRing.java
URL: http://svn.apache.org/viewvc/commons/sandbox/openpgp/trunk/src/main/java/org/apache/commons/openpgp/KeyRing.java?rev=677515&r1=677514&r2=677515&view=diff
==============================================================================
--- commons/sandbox/openpgp/trunk/src/main/java/org/apache/commons/openpgp/KeyRing.java (original)
+++ commons/sandbox/openpgp/trunk/src/main/java/org/apache/commons/openpgp/KeyRing.java Thu Jul 17 00:06:49 2008
@@ -17,11 +17,17 @@
  * limitations under the License.
  */
 
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.bouncycastle.openpgp.PGPException;
 import org.bouncycastle.openpgp.PGPPublicKey;
 import org.bouncycastle.openpgp.PGPSecretKey;
 
 /**
  * Interface describing a key ring for use in signing or verifying data.
+ * 
+ * @todo separate pub/priv and better error handling
  *
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  */
@@ -62,4 +68,7 @@
      * @todo remove BC specifics
      */
     PGPPublicKey getPublicKey( long keyId );
+
+    void addPublicKeyRing( InputStream inputStream )
+        throws IOException, PGPException;
 }

Added: commons/sandbox/openpgp/trunk/src/test/java/org/apache/commons/openpgp/BouncyCastleKeyRingTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/openpgp/trunk/src/test/java/org/apache/commons/openpgp/BouncyCastleKeyRingTest.java?rev=677515&view=auto
==============================================================================
--- commons/sandbox/openpgp/trunk/src/test/java/org/apache/commons/openpgp/BouncyCastleKeyRingTest.java (added)
+++ commons/sandbox/openpgp/trunk/src/test/java/org/apache/commons/openpgp/BouncyCastleKeyRingTest.java Thu Jul 17 00:06:49 2008
@@ -0,0 +1,69 @@
+package org.apache.commons.openpgp;
+
+/*
+ * 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.
+ */
+
+import junit.framework.TestCase;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+/**
+ * Test the open pgp key ring.
+ * 
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ */
+public class BouncyCastleKeyRingTest
+    extends TestCase
+{
+    private String[] pubKeyId = { "A7D16BD4", "84C9113", "E6C2AB68" };
+
+    private String[] secKeyId = { "A7D16BD4", "E6C2AB68" };
+
+    private KeyRing keyRing;
+
+    private static final String PASSWORD = "cop";
+
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+
+        keyRing =
+            new BouncyCastleKeyRing( getClass().getResourceAsStream( "/secring.gpg" ),
+                                     getClass().getResourceAsStream( "/pubring.gpg" ), PASSWORD.toCharArray() );
+    }
+
+    public void testPublicKeys()
+        throws OpenPgpException, IOException
+    {
+        for ( int i = 0; i < pubKeyId.length; i++ )
+        {
+            assertNotNull( "Unable to find key " + pubKeyId[i], keyRing.getPublicKey( pubKeyId[i] ) );
+        }
+    }
+
+    public void testSecretKeys()
+        throws OpenPgpException, IOException
+    {
+        for ( int i = 0; i < secKeyId.length; i++ )
+        {
+            assertNotNull( "Unable to find key " + secKeyId[i], keyRing.getSecretKey( secKeyId[i] ) );
+        }
+    }
+}

Propchange: commons/sandbox/openpgp/trunk/src/test/java/org/apache/commons/openpgp/BouncyCastleKeyRingTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/openpgp/trunk/src/test/java/org/apache/commons/openpgp/BouncyCastleKeyRingTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/sandbox/openpgp/trunk/src/test/resources/pubring.gpg
URL: http://svn.apache.org/viewvc/commons/sandbox/openpgp/trunk/src/test/resources/pubring.gpg?rev=677515&r1=677514&r2=677515&view=diff
==============================================================================
Binary files - no diff available.

Modified: commons/sandbox/openpgp/trunk/src/test/resources/secring.gpg
URL: http://svn.apache.org/viewvc/commons/sandbox/openpgp/trunk/src/test/resources/secring.gpg?rev=677515&r1=677514&r2=677515&view=diff
==============================================================================
Binary files - no diff available.