[Webfunds-commits] java/webfunds/ricardian KeyUtil.java StripKeyException.java

Jeroen C. van Gelderen gelderen@cypherpunks.ai
Mon, 14 Aug 2000 21:52:41 -0400 (AST)


gelderen    00/08/14 21:52:41

  Added:       webfunds/ricardian KeyUtil.java StripKeyException.java
  Log:
  Initial version.

Revision  Changes    Path
1.1                  java/webfunds/ricardian/KeyUtil.java

Index: KeyUtil.java
===================================================================

package webfunds.ricardian;


import java.io.*;
import java.util.Vector;
import cryptix.openpgp.*;


public final class KeyUtil {

    /** Static methods only. */
    private KeyUtil() {}


    /**
     * Strip the given key of all superfluous data. The key being returned
     * will have at most one userId (containing the string userIdTag) which 
     * is both self-signed and (optionally) signed by 'userIdSigner' (if 
     * 'userIdSigner' != null).
     *
     * @throws IllegalArgumentException
     *         If key==null || userIdTag==null.
     *
     * @throws StripKeyException
     *         If the requested userId doesn't exist or is not self-signed or
     *         has multiple self-sigs or isn't signed by userIdSigner or has
     *         multiple sigs by userIdSigner (dunno if that's possible).
     */
    public static PGPPublicKey 
    stripAndVerifyKey(PGPPublicKey key, 
                      String userIdTag, PGPPublicKey userIdSigner)
    throws StripKeyException
    {
        if( key==null || userIdTag==null )
            throw new IllegalArgumentException();

        // prevent duplication of sigs
        if (key.equals(userIdSigner))
            userIdSigner = null;

        try {

            // find the userId we want
            PGPUserID uid = findUserId(key, userIdTag);
            if (uid == null) 
                throw new StripKeyException(
                    "UserId with tag (" + userIdTag + ") doesn't exist.");

            // strip sigs we don't want
            Vector oldSigs = uid.getSignatures();
            Vector newSigs = new Vector(2);

            // find self-sig
            for(int i=0; i<oldSigs.size(); i++) {
                PGPIDSignature sig = (PGPIDSignature)oldSigs.elementAt(i);
                if (sig.verify(key)) newSigs.addElement(sig);
            }

            if (newSigs.size()!=1)
                throw new StripKeyException(
                    "Key is not self-signed or has multiple self-signatures.");

            // (optional) retain userIdSigner sig
            if (userIdSigner!=null) {
                for(int i=0; i<oldSigs.size(); i++) {
                    PGPIDSignature sig = (PGPIDSignature)oldSigs.elementAt(i);
                    if (sig.verify(userIdSigner)) newSigs.addElement(sig);
                }

                if (newSigs.size()!=2)
                    throw new StripKeyException(
                        "Key is not signed or has multiple sigs.");
            }

            newSigs.trimToSize();

            // replace sigs
            uid.setSignatures(newSigs);

            // replace the existing userIds with the one we want
            Vector v = new Vector(1);
            v.addElement(uid);
            key.setUserIDs(v);

            return key;

        } catch(Exception e) {
            // something went wrong, dunno what
            e.printStackTrace();
            throw new StripKeyException(e.getMessage());
        }
    }


    /**
     * Return the first UserId on the key that matches 'userIdTag' or return
     * null.
     */
    private static PGPUserID findUserId(PGPPublicKey key, String userIdTag) {
        Vector uids = key.getUserIDs();
        for(int i=0; i<uids.size(); i++) {
            PGPUserID uid = (PGPUserID)uids.elementAt(i);
            String s = uid.getValue();
            if( isMatch(s, userIdTag) ) return uid;
        }
        return null;
    }



    private static boolean isMatch(String str, String substr) {
        return (str.indexOf(substr) != -1);
    }




    public static void main(String[] argv) throws Exception {

        FileInputStream fis = new FileInputStream(argv[0]);
        byte[] key1bytes = new byte[fis.available()];
        fis.read(key1bytes);
        fis.close();

        PGPKeyFactory factory = new PGPKeyFactory();

        Vector key1vector = factory.decodeKeys(key1bytes);

        PGPPublicKey key1 = (PGPPublicKey)key1vector.elementAt(0);

        System.out.println(key1.toString());

        key1 = stripAndVerifyKey(key1, "mediaport", key1);
        key1.writeKey("tootsie.pgp");
    }
}



1.1                  java/webfunds/ricardian/StripKeyException.java

Index: StripKeyException.java
===================================================================

package webfunds.ricardian;


public final class StripKeyException extends Exception {

    public StripKeyException(String msg) {
        super(msg);
    }
}