[Webfunds-commits] java/webfunds/openpgp/cert SimpleOpenPGPCertificate.java SimpleOpenPGPCertificateFactorySpi.java
    Edwin Woudt 
    edwin@cypherpunks.ai
    Mon,  7 Aug 2000 14:38:01 -0400 (AST)
    
    
  
edwin       00/08/07 14:38:01
  Added:       webfunds/openpgp/cert SimpleOpenPGPCertificate.java
                        SimpleOpenPGPCertificateFactorySpi.java
  Log:
  Initial implementation of OpenPGP certificate classes.
Revision  Changes    Path
1.1                  java/webfunds/openpgp/cert/SimpleOpenPGPCertificate.java
Index: SimpleOpenPGPCertificate.java
===================================================================
/* $Id: SimpleOpenPGPCertificate.java,v 1.1 2000/08/07 18:38:00 edwin Exp $
 *
 * Copyright (c) Systemics Ltd 2000 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.openpgp.cert;
// cryptix openpgp classes and exceptions
import cryptix.openpgp.PGPFatalDataFormatException;
import cryptix.openpgp.PGPPublicKey;
import cryptix.openpgp.PGPKeyFactory;
import cryptix.openpgp.PGPUserID;
// java.security interfaces 
import java.security.PublicKey;
import java.security.cert.Certificate;
// java.security exceptions
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateEncodingException;
// other java.* classes and exceptions
import java.io.InputStream;
import java.io.IOException;
import java.util.Vector;
/**
 * Simple OpenPGP Certificate class.
 * 
 * <p>Warning: this class is a hack, which will most likely disappear in favour
 * of a proper implementation.</p>
 */
public class SimpleOpenPGPCertificate extends Certificate { 
// Instance variables
//.............................................................................
    /** The public key contained in this certificate */
    private PGPPublicKey key;
// Constructor
//.............................................................................
    /**
     * Constructor that reads a certificate from an InputStream
     *
     * @param in the inputstream that contains an OpenPGP key
     *
     * @throws IOException if the inputstream throws it
     * @throws CertificateException if multiple keys are found or if a fatal
     *         problem is found in the data (a PGPFatalDataFormatException).
     */
    /* package */ SimpleOpenPGPCertificate(InputStream in)
        throws IOException, CertificateException
    {
        
        super("OpenPGP"); // Certificate type
        
        PGPKeyFactory factory = new PGPKeyFactory();
        Vector keys;
        try {
            keys = factory.decodeKeys(in);
        } catch (PGPFatalDataFormatException fdfe) {
            throw new CertificateException("Invalid OpenPGP key - "+fdfe);
        }
        
        if (keys.size() > 1) {
            throw new CertificateException("Multiple keys found.");
        }
        
        if (keys.size() == 0) {
            throw new CertificateException("No key found.");
        }
        
        key = (PGPPublicKey)keys.elementAt(0);
        
    }
    
    
// Implemented abstract methods
//.............................................................................
    /**
     * Verify that this certificate was signed with the given key
     *
     * <p>In case of multiple userID's, this method only succeeds if all
     * userID's are signed using the given key.</p>
     *
     * @param key the key to use for verification
     *
     * @throws CertificateException if the certificate contains an invalid
     *         signature.
     * @throws NoSuchAlgorithmException this method does currently not throw
     *         this exception (a runtime exception is thrown instead if an
     *         algorithm is not found), but it may do so in the future.
     * @throws InvalidKeyException is the given public key is not a 
     *         cryptix.openpgp.PGPPublicKey
     * @throws NoSuchProviderException this method does not throw this 
     *         exception.
     * @throws SignatureException if the verification fails.
     */
    public void verify(PublicKey key)
        throws CertificateException, NoSuchAlgorithmException,
               InvalidKeyException, NoSuchProviderException, SignatureException
    {
    
        if (! (key instanceof PGPPublicKey)) {
            throw new InvalidKeyException("Key not of type: PGPPublicKey.");
        }
        
        boolean result = true;
        Vector userids = ((PGPPublicKey)key).getUserIDs();
        
        for (int i=0; i<userids.size(); i++) {
            PGPUserID userid = (PGPUserID)userids.elementAt(i);
            try {
                result &= userid.isSignedBy((PGPPublicKey)key);
            } catch (PGPFatalDataFormatException fdfe) {
                throw new CertificateException("Invalid signature - "+fdfe);
            }
        }
        
        if (! result) {
            throw new SignatureException("Not all userIDs are signed with "+
                                         "the given key.");
        }
        
    }
    /**
     * Braindamaged unsupported method
     *
     * <p>Multiple OpenPGP providers, yeah right!</p>
     */
    public void verify(PublicKey key, String sigProvider)
        throws CertificateException, NoSuchAlgorithmException,
               InvalidKeyException, NoSuchProviderException, SignatureException
    {
    
        throw new RuntimeException(
            "Brain damaged method signature. Not implemented.");
        
    }
    /**
     * Returns the encoded publickey
     *
     * <p>This class uses the binary OpenPGP representation of it's public
     * key</p>
     *
     * @return the binary encoded representation of the contained OpenPGP
     *         public key.
     *
     * @throws CertificateEncodingException this method does not throw this
     *         exception.
     */
    public byte[] getEncoded() throws CertificateEncodingException {
    
        return key.getEncoded();
        
    }
    
    
    /**
     * Return the public key that is contained in this certificate
     *
     * <p>This always returns a cryptix.openpgp.PGPPublicKey</p>
     */
    public PublicKey getPublicKey() {
        return key;
    }
    /**
     * Returns a human-readable string that is descriptive for this certificate.
     *
     * <p>For a certificate with one userID, this simply returns the userID.
     * For multiple userID's, all userID's are concatenated in a special way.
     * </p>
     */
    public String toString() {
        String result = "";
        Vector userids = key.getUserIDs();
        for (int i=0; i<userids.size(); i++) {
            PGPUserID userid = (PGPUserID)userids.elementAt(i);
            if (i>0) result += " + ";
            result += userid.getValue();
        }
        
        return result;
    }
}
1.1                  java/webfunds/openpgp/cert/SimpleOpenPGPCertificateFactorySpi.java
Index: SimpleOpenPGPCertificateFactorySpi.java
===================================================================
/* $Id: SimpleOpenPGPCertificateFactorySpi.java,v 1.1 2000/08/07 18:38:00 edwin Exp $
 *
 * Copyright (c) Systemics Ltd 2000 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.openpgp.cert;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactorySpi;
import java.security.cert.CRL;
import java.security.cert.CRLException;
import java.io.InputStream;
import java.io.IOException;
import java.util.Collection;
/**
 * Simple OpenPGP Certificate class.
 * 
 * <p>Warning: this class is a hack, which will most likely disappear in favour
 * of a proper implementation.</p>
 */
public class SimpleOpenPGPCertificateFactorySpi extends CertificateFactorySpi { 
// Constructor
//.............................................................................
    /**
     * Empty constructor
     */
    public SimpleOpenPGPCertificateFactorySpi() {}
    
    
// Implemented abstract methods
//.............................................................................
    /**
     * Decode an OpenPGP certificate
     *
     * <p>Note that this method only works if exactly one certificate is 
     * present.</p>
     * <p>The certificate has to be binary encoded. If an application wants to
     * parse a base64 armoured key then the data has to be unarmoured first.</p>
     *
     * @param inStream the stream to read the certificate from
     * @return the certificate read
     * @throws CertificateException if an error occured while parsing the
     *         key or if an IOException is thrown while reading from inStream.
     */
    public Certificate engineGenerateCertificate(InputStream inStream)
        throws CertificateException
    {
        try {
            return new SimpleOpenPGPCertificate(inStream);
        } catch (IOException ioe) {
            throw new CertificateException("IOException on parsing key - "+ioe);
        }
    }
    /** Unsupported method */
    public Collection engineGenerateCertificates(InputStream inStream)
        throws CertificateException
    {
        throw new RuntimeException("Not implemented, bugger off");
    }
    /** Unsupported method */
    public CRL engineGenerateCRL(InputStream inStream)
        throws CRLException
    {
        throw new RuntimeException("Not implemented, bugger off");
    }
    
    
    /** Unsupported method */
    public Collection engineGenerateCRLs(InputStream inStream)
        throws CRLException
    {
        throw new RuntimeException("Not implemented, bugger off");
    }                                  
    
}