[Webfunds-commits] java/java/security/cert Certificate.java CertificateEncodingException.java CertificateException.java CertificateFactory.java CertificateFactorySpi.java CertificateFactorySpiX509.java SimpleX509Certificate.java

Jeroen C. van Gelderen gelderen@cypherpunks.ai
Fri, 21 Jul 2000 17:29:58 -0400 (AST)


gelderen    00/07/21 17:29:58

  Added:       java/security/cert Certificate.java
                        CertificateEncodingException.java
                        CertificateException.java CertificateFactory.java
                        CertificateFactorySpi.java
                        CertificateFactorySpiX509.java
                        SimpleX509Certificate.java
  Log:
  Initial version, just enough to support WebFunds.

Revision  Changes    Path
1.1                  java/java/security/cert/Certificate.java

Index: Certificate.java
===================================================================
/* $Id: Certificate.java,v 1.1 2000/07/21 21:29:57 gelderen Exp $
 *
 * Copyright (C) 2000 The Cryptix Foundation Limited. All rights reserved.
 *
 * Use, modification, copying and distribution of this software is subject
 * the terms and conditions of the Cryptix General Licence. You should have
 * received a copy of the Cryptix General Licence along with this library;
 * if not, you can download a copy from http://www.cryptix.org/ .
 */
package java.security.cert;


import java.io.ObjectStreamException;
import java.io.Serializable;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.SignatureException;


/**
 * @version $Revision: 1.1 $
 * @author  Jeroen C. van Gelderen (gelderen@cryptix.org)
 */
public abstract class Certificate implements Serializable {

    private final String type;


    protected Certificate(String type) {
        this.type = type;
    }


    public boolean equals(Object other) {
        throw new RuntimeException("NYI");
    }


    public abstract byte[] getEncoded() throws CertificateEncodingException;


    public abstract PublicKey getPublicKey();


    public final String getType() {
        return this.type;
    }


    public int hashCode() {
        throw new RuntimeException("NYI");
    }


    public abstract String toString();


    public abstract void verify(PublicKey key)
        throws CertificateException, NoSuchAlgorithmException,
               InvalidKeyException, NoSuchProviderException, SignatureException;


    public abstract void verify(PublicKey key, String sigProvider)
        throws CertificateException, NoSuchAlgorithmException,
               InvalidKeyException, NoSuchProviderException, SignatureException;


    protected Object writeReplace() throws ObjectStreamException {
        throw new RuntimeException("NYI");
    }
}



1.1                  java/java/security/cert/CertificateEncodingException.java

Index: CertificateEncodingException.java
===================================================================
/* $Id: CertificateEncodingException.java,v 1.1 2000/07/21 21:29:57 gelderen Exp $
 *
 * Copyright (C) 2000 The Cryptix Foundation Limited. All rights reserved.
 *
 * Use, modification, copying and distribution of this software is subject
 * the terms and conditions of the Cryptix General Licence. You should have
 * received a copy of the Cryptix General Licence along with this library;
 * if not, you can download a copy from http://www.cryptix.org/ .
 */
package java.security.cert;


/**
 * Thrown during certificate encoding.
 *
 * @version $Revision: 1.1 $
 * @author  Jeroen C. van Gelderen (gelderen@cryptix.org)
 */
public class CertificateEncodingException extends CertificateException {

    public CertificateEncodingException() {
        super();
    }


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



1.1                  java/java/security/cert/CertificateException.java

Index: CertificateException.java
===================================================================
/* $Id: CertificateException.java,v 1.1 2000/07/21 21:29:57 gelderen Exp $
 *
 * Copyright (C) 2000 The Cryptix Foundation Limited. All rights reserved.
 *
 * Use, modification, copying and distribution of this software is subject
 * the terms and conditions of the Cryptix General Licence. You should have
 * received a copy of the Cryptix General Licence along with this library;
 * if not, you can download a copy from http://www.cryptix.org/ .
 */
package java.security.cert;


import java.security.GeneralSecurityException;


/**
 * Base class of Certificate related exceptions.
 *
 * @version $Revision: 1.1 $
 * @author  Jeroen C. van Gelderen (gelderen@cryptix.org)
 */
public class CertificateException extends GeneralSecurityException {

    public CertificateException() {
        super();
    }


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



1.1                  java/java/security/cert/CertificateFactory.java

Index: CertificateFactory.java
===================================================================
/* $Id; $
 *
 * Copyright (c) 2000 The Cryptix Foundation Limited. All rights reserved.
 */

package java.security.cert;


import java.io.InputStream;
import java.security.Provider;


/**
 * Stripped down version of the JDK 1.2 CertificateFactory. The methods that
 * are present have the same signature as Sun's. The methods that were omitted
 * depend on JDK 1.2 features. This implementation is just sufficient to run
 * WebFunds on JDK 1.1.
 *
 * @version $Revision: 1.1 $
 * @author  Jeroen C. van Gelderen (gelderen@cryptix.org)
 */
public class CertificateFactory
{
    /** Delegate. */
    private final CertificateFactorySpi spi;


    protected CertificateFactory(CertificateFactorySpi certFacSpi,
                                 Provider provider, String type)
    {
        this.spi = certFacSpi;
    }


    public final Certificate generateCertificate(InputStream inStream)
        throws CertificateException
    {
        return this.spi.engineGenerateCertificate(inStream);
    }


    public static final CertificateFactory getInstance(String type)
        throws CertificateException
    {
        if( type.equals("X.509") )
            return new CertificateFactory(
                new CertificateFactorySpiX509(), null, "X.509" );
        else
            throw new CertificateException("Unknown type: " + type);
    }
}



1.1                  java/java/security/cert/CertificateFactorySpi.java

Index: CertificateFactorySpi.java
===================================================================
/* $Id: CertificateFactorySpi.java,v 1.1 2000/07/21 21:29:57 gelderen Exp $
 *
 * Copyright (c) 2000 The Cryptix Foundation Limited. All rights reserved.
 */

package java.security.cert;


import java.io.InputStream;


public abstract class CertificateFactorySpi {

    public CertificateFactorySpi() {
    }


    public abstract Certificate engineGenerateCertificate(InputStream inStream)
        throws CertificateException;
}



1.1                  java/java/security/cert/CertificateFactorySpiX509.java

Index: CertificateFactorySpiX509.java
===================================================================
/* $Id: CertificateFactorySpiX509.java,v 1.1 2000/07/21 21:29:57 gelderen Exp $
 *
 * Copyright (c) 2000 The Cryptix Foundation Limited. All rights reserved.
 */

package java.security.cert;


import java.io.InputStream;
import java.io.IOException;

import webfunds.x509.*;


public final class CertificateFactorySpiX509 extends CertificateFactorySpi {

    public CertificateFactorySpiX509() {
    }


    public Certificate engineGenerateCertificate(InputStream inStream)
        throws CertificateException
    {
        return new SimpleX509Certificate(inStream);
    }
}



1.1                  java/java/security/cert/SimpleX509Certificate.java

Index: SimpleX509Certificate.java
===================================================================
/* $Id: SimpleX509Certificate.java,v 1.1 2000/07/21 21:29:57 gelderen Exp $
 *
 * Copyright (c) 2000 The Cryptix Foundation Limited. All rights reserved.
 */

package java.security.cert;


import java.io.InputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.SignatureException;

import webfunds.x509.*;


public class SimpleX509Certificate extends Certificate {

    private final AsnSequence certSeq;


    private final PublicKey pubKey;


    /*package*/ SimpleX509Certificate(InputStream inStream) 
        throws CertificateException
    {
        super("X.509");

        try {
            AsnInputStream is = new AsnInputStream(inStream);
            this.certSeq = (AsnSequence)is.read();
            this.pubKey = extractPublickKey(this.certSeq);
        } catch(ClassCastException e) {
            // this catches unexpected ASN.1 types in the stream
            throw new CertificateException(
                "Unexpected ASN.1 type detected.");
        } catch(IOException e) {
            throw new CertificateException(
                "Unknown error parsing certificate.");
        }
    }


    public byte[] getEncoded() throws CertificateEncodingException {
        try {
            AsnOutputStream os = new AsnOutputStream();
            os.write(this.certSeq);
            return os.toByteArray();
        } catch(IOException e) {
            throw new CertificateEncodingException("Unknown error.");
        }
    }


    public PublicKey getPublicKey() {
        return this.pubKey;
    }


    public String toString() {
        return this.certSeq.toString();
    }


    public void verify(PublicKey key)
        throws CertificateException, NoSuchAlgorithmException,
               InvalidKeyException, NoSuchProviderException, SignatureException
    {
        // XXX: always accept now
    }


    public void verify(PublicKey key, String sigProvider)
        throws CertificateException, NoSuchAlgorithmException,
               InvalidKeyException, NoSuchProviderException, SignatureException
    {
        throw new RuntimeException(
            "Brain damaged method signature. Not implemented.");
    }


    private PublicKey extractPublickKey(AsnSequence seq1)
        throws IOException
    {
        if(seq1.size() != 3)
            throw new IOException("seq1 != 3");
        
        AsnSequence seq2 = (AsnSequence)seq1.get(0);
        if(seq2.size() != 6)
            throw new IOException("seq2 != 6");

        AsnSequence seq3 = (AsnSequence)seq2.get(5);

        AsnOutputStream os = new AsnOutputStream();
        os.write(seq3);
        return new PK(os.toByteArray());
    }


    private PublicKey publicKeyFromBitString(AsnBitString bs)
        throws IOException
    {
        AsnInputStream is = new AsnInputStream(bs.toByteArray());
        AsnSequence seq = (AsnSequence)is.read();
        if(seq.size() != 2)
            throw new IOException("seq != 2");
        
        AsnInteger n = (AsnInteger)seq.get(0);
        AsnInteger e = (AsnInteger)seq.get(1);
        return null;
    }


    private class PK implements PublicKey {

        private final byte[] data;


        public PK(byte[] data) {
            this.data = (byte[])data.clone();
        }


        public String getAlgorithm() {
            return "RSA";
        }


        public byte[] getEncoded() {
            return (byte[])this.data.clone();
        }


        public String getFormat() {
            return "X.509";
        }
    }
}