[Webfunds-commits] java/webfunds/x509/cert SimpleX509Certificate.java SimpleX509CertificateFactorySpi.java

Jeroen C. van Gelderen gelderen@cypherpunks.ai
Thu, 17 Aug 2000 20:19:01 -0400 (AST)


gelderen    00/08/17 20:19:00

  Added:       webfunds/x509/cert SimpleX509Certificate.java
                        SimpleX509CertificateFactorySpi.java
  Log:
  Initial version.

Revision  Changes    Path
1.1                  java/webfunds/x509/cert/SimpleX509Certificate.java

Index: SimpleX509Certificate.java
===================================================================
/* $Id: SimpleX509Certificate.java,v 1.1 2000/08/18 00:19:00 gelderen Exp $
 *
 * Copyright (c) 2000 The Cryptix Foundation Limited. All rights reserved.
 */

package webfunds.x509.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 java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;

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";
        }
    }
}



1.1                  java/webfunds/x509/cert/SimpleX509CertificateFactorySpi.java

Index: SimpleX509CertificateFactorySpi.java
===================================================================
/* $Id: SimpleX509CertificateFactorySpi.java,v 1.1 2000/08/18 00:19:00 gelderen Exp $
 *
 * Copyright (c) 2000 The Cryptix Foundation Limited. All rights reserved.
 */

package webfunds.x509.cert;


import java.io.InputStream;
import java.io.IOException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactorySpi;
import java.security.cert.CRL;
import java.security.cert.CRLException;
import java.util.Collection;

import webfunds.x509.*;


public final class SimpleX509CertificateFactorySpi 
    extends CertificateFactorySpi
{

    public SimpleX509CertificateFactorySpi() {}


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


    public Collection engineGenerateCertificates(InputStream inStream)
        throws CertificateException
    {
        throw new InternalError("Method not implemented.");
    }


    public CRL engineGenerateCRL(InputStream inStream)
        throws CRLException
    {
        throw new InternalError("Method not implemented.");
    }


    public Collection engineGenerateCRLs(InputStream inStream)
        throws CRLException
    {
        throw new InternalError("Method not implemented.");
    }
}