gzz-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gzz-commits] storm/org/nongnu/storm/pointers PointerBlock.ja...


From: Benja Fallenstein
Subject: [Gzz-commits] storm/org/nongnu/storm/pointers PointerBlock.ja...
Date: Fri, 09 May 2003 12:42:23 -0400

CVSROOT:        /cvsroot/storm
Module name:    storm
Changes by:     Benja Fallenstein <address@hidden>      03/05/09 12:42:23

Modified files:
        org/nongnu/storm/pointers: PointerBlock.java PointerId.java 
                                   PointerIndex.java SetPointer.java 

Log message:
        Signature- and timestamp-based pointers work!!!

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/storm/storm/org/nongnu/storm/pointers/PointerBlock.java.diff?tr1=1.1&tr2=1.2&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/storm/storm/org/nongnu/storm/pointers/PointerId.java.diff?tr1=1.2&tr2=1.3&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/storm/storm/org/nongnu/storm/pointers/PointerIndex.java.diff?tr1=1.2&tr2=1.3&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/storm/storm/org/nongnu/storm/pointers/SetPointer.java.diff?tr1=1.2&tr2=1.3&r1=text&r2=text

Patches:
Index: storm/org/nongnu/storm/pointers/PointerBlock.java
diff -u storm/org/nongnu/storm/pointers/PointerBlock.java:1.1 
storm/org/nongnu/storm/pointers/PointerBlock.java:1.2
--- storm/org/nongnu/storm/pointers/PointerBlock.java:1.1       Fri May  9 
11:23:11 2003
+++ storm/org/nongnu/storm/pointers/PointerBlock.java   Fri May  9 12:42:23 2003
@@ -31,6 +31,7 @@
 import java.util.*;
 import java.io.*;
 import java.security.*;
+import java.security.spec.*;
 
 public final class PointerBlock {
     public static final String COOKIE = 
@@ -61,6 +62,11 @@
        byte[] signature = Base32.decode(r.readLine());
 
        pointer = new PointerId(r.readLine());
+
+       byte[] keyBytes = Base32.decode(r.readLine());
+       X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
+       PublicKey key = PointerId.keyFactory.generatePublic(keySpec);
+
        timestamp = Long.parseLong(r.readLine());
        target = new BlockId(r.readLine());
 
@@ -69,6 +75,10 @@
 
        r.close();
 
+       // Next, verify that the pubkey matches the pointer id.
+
+       pointer.verify(keyBytes);
+
        // Now, verify signature.
        // Everything *after* the signature itself is signed.
        is = block.getInputStream();
@@ -82,7 +92,7 @@
        }
 
        Signature s = Signature.getInstance("SHA1withDSA");
-       s.initVerify(pointer.getKey());
+       s.initVerify(key);
 
        int b;
        while((b = is.read()) >= 0)
Index: storm/org/nongnu/storm/pointers/PointerId.java
diff -u storm/org/nongnu/storm/pointers/PointerId.java:1.2 
storm/org/nongnu/storm/pointers/PointerId.java:1.3
--- storm/org/nongnu/storm/pointers/PointerId.java:1.2  Fri May  9 11:52:47 2003
+++ storm/org/nongnu/storm/pointers/PointerId.java      Fri May  9 12:42:23 2003
@@ -39,7 +39,7 @@
     public static String PREFIX = "urn:x-storm:pointer-0.1:";
     public static int PREFIX_LEN = PREFIX.length();
 
-    private static KeyFactory keyFactory;
+    static final KeyFactory keyFactory;
     static {
        try {
            keyFactory = KeyFactory.getInstance("DSA");
@@ -51,10 +51,8 @@
     private static SecureRandom random = new SecureRandom();
 
     private String uri;
-
+    private byte[] bytes;
     private String randomPart;
-    private byte[] keyBytes;
-    private PublicKey key;
 
     public PointerId(String uri) 
        throws IllegalArgumentException, InvalidKeyException,
@@ -62,32 +60,31 @@
        uri = uri.toLowerCase().intern();
        this.uri = uri;
        
-       int colon = uri.indexOf(':', PREFIX_LEN);
+       int colon = uri.lastIndexOf(':');
 
        if(!uri.startsWith(PREFIX))
            throw new IllegalArgumentException("Storm URN must start "+PREFIX+" 
[[ was "+uri+" ]]");
-       if(colon < 0)
-            throw new IllegalArgumentException("URN must contain random part");
-           
-       
+       if(colon < PREFIX_LEN)
+           throw new IllegalArgumentException("Illegal pointer URN (colon 
missing)");
+       bytes = Base32.decode(uri.substring(PREFIX_LEN, colon));
        randomPart = uri.substring(colon+1);
-       keyBytes = Base32.decode(uri.substring(PREFIX_LEN, colon));
-       X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
-       key = keyFactory.generatePublic(keySpec);
     }
 
     public PointerId(PublicKey key, String randomPart) 
        throws InvalidKeyException, InvalidKeySpecException {
 
-       key = (PublicKey)keyFactory.translateKey(key);
-       this.key = key;
-       EncodedKeySpec keySpec = 
-           (EncodedKeySpec)keyFactory.getKeySpec(key, 
X509EncodedKeySpec.class);
-       keyBytes = keySpec.getEncoded();
+       MessageDigest d;
+       try {
+           d = MessageDigest.getInstance("SHA-1");
+       } catch(NoSuchAlgorithmException _) {
+           throw new Error("Need SHA-1 algorithm support in Storm");
+       }
 
-       this.randomPart = randomPart;
+       d.update(getKeyBytes(key));
 
-       String uri = PREFIX + Base32.encode(keyBytes) + ":" + randomPart;
+       this.bytes = d.digest();
+       this.randomPart = randomPart;
+       String uri = PREFIX + Base32.encode(bytes) + ":" + randomPart;
        this.uri = uri.toLowerCase().intern();
     }
 
@@ -97,9 +94,6 @@
        this(key, Base32.encode(randomBytes()));
     }
 
-    public PublicKey getKey() { return key; }
-    public String getRandomPart() { return randomPart; }
-
     public String getURI() { return uri; }
     public String toString() { return uri; }
 
@@ -110,9 +104,31 @@
 
     public int hashCode() { return uri.hashCode(); }
 
+    public void verify(byte[] keyBytes) {
+       MessageDigest d;
+       try {
+           d = MessageDigest.getInstance("SHA-1");
+       } catch(NoSuchAlgorithmException _) {
+           throw new Error("Need SHA-1 algorithm support in Storm");
+       }
+
+       d.update(keyBytes);
+
+       if(!d.isEqual(d.digest(), this.bytes))
+           throw new IllegalArgumentException("Pointer doesn't match: "+this);
+    }
+
     private static byte[] randomBytes() { 
        byte[] b = new byte[20];
        random.nextBytes(b);
        return b;
+    }
+
+    public static byte[] getKeyBytes(PublicKey key)    
+       throws InvalidKeyException, InvalidKeySpecException {
+       key = (PublicKey)keyFactory.translateKey(key);
+       EncodedKeySpec keySpec = 
+           (EncodedKeySpec)keyFactory.getKeySpec(key, 
X509EncodedKeySpec.class);
+       return keySpec.getEncoded();
     }
 }
Index: storm/org/nongnu/storm/pointers/PointerIndex.java
diff -u storm/org/nongnu/storm/pointers/PointerIndex.java:1.2 
storm/org/nongnu/storm/pointers/PointerIndex.java:1.3
--- storm/org/nongnu/storm/pointers/PointerIndex.java:1.2       Fri May  9 
11:52:47 2003
+++ storm/org/nongnu/storm/pointers/PointerIndex.java   Fri May  9 12:42:23 2003
@@ -33,7 +33,7 @@
 import java.security.*;
 
 public class PointerIndex {
-    public static boolean dbg = true;
+    public static boolean dbg = false;
     private static void p(String s) { System.out.println("PointerIndex:: "+s); 
}
 
     public static final String uri =
@@ -82,7 +82,7 @@
        return result;
     }
 
-    public void set(PointerId id, BlockId target, PrivateKey key) 
+    public void set(PointerId id, BlockId target, KeyPair keyPair) 
        throws IOException, GeneralSecurityException {
        // XXX this assumes that the computer clock
        // is always set correctly: if there is an existing
@@ -91,13 +91,17 @@
        // actually change the pointer...
        long timestamp = System.currentTimeMillis();
 
+       byte[] keyBytes = 
+           PointerId.getKeyBytes(keyPair.getPublic());
+
        String data =
            id.toString() + "\n" +
+           Base32.encode(keyBytes) + "\n" +
            timestamp + "\n" +
            target.toString();
 
        Signature s = Signature.getInstance("SHA1withDSA");
-       s.initSign(key);
+       s.initSign(keyPair.getPrivate());
        s.update(data.getBytes("US-ASCII"));
        byte[] signature = s.sign();
 
Index: storm/org/nongnu/storm/pointers/SetPointer.java
diff -u storm/org/nongnu/storm/pointers/SetPointer.java:1.2 
storm/org/nongnu/storm/pointers/SetPointer.java:1.3
--- storm/org/nongnu/storm/pointers/SetPointer.java:1.2 Fri May  9 11:52:47 2003
+++ storm/org/nongnu/storm/pointers/SetPointer.java     Fri May  9 12:42:23 2003
@@ -75,7 +75,7 @@
        }
 
        PointerIndex idx = (PointerIndex)pool.getIndex(PointerIndex.uri);
-       idx.set(pointer, target, keys.getPrivate());
+       idx.set(pointer, target, keys);
 
        System.out.println("Set pointer");
        System.out.println(pointer);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]