FileDocCategorySizeDatePackage
CertificateMessage.javaAPI DocAndroid 1.5 API5642Wed May 06 22:41:06 BST 2009org.apache.harmony.xnet.provider.jsse

CertificateMessage

public class CertificateMessage extends org.apache.harmony.xnet.provider.jsse.Message
Represents server/client certificate message
see
TLS 1.0 spec., 7.4.2. Server certificate; 7.4.6. Client certificate

Fields Summary
X509Certificate[]
certs
Certificates
byte[]
encoded_certs
Certificates in encoded form
Constructors Summary
public CertificateMessage(org.apache.harmony.xnet.provider.jsse.HandshakeIODataStream in, int length)
Creates inbound message

param
in
param
length
throws
IOException

        int l = in.readUint24(); // total_length
        if (l == 0) {  // message contais no certificates
            if (length != 3) { // no more bytes after total_length
                fatalAlert(AlertProtocol.DECODE_ERROR,
                        "DECODE ERROR: incorrect CertificateMessage");
            }
            certs = new X509Certificate[0];
            encoded_certs = new byte[0][0];
            this.length = 3;
            return;
        }
        CertificateFactory cf;
        try {
            cf = CertificateFactory.getInstance("X509");
        } catch (CertificateException e) {
            fatalAlert(AlertProtocol.INTERNAL_ERROR, "INTERNAL ERROR", e);
            return;
        }
        Vector certs_vector = new Vector();
        int size = 0;
        int enc_size = 0;
        while (l > 0) {
            size = in.readUint24();
            l -= 3;
            try {
                certs_vector.add(cf.generateCertificate(in));
            } catch (CertificateException e) {
                fatalAlert(AlertProtocol.DECODE_ERROR, "DECODE ERROR", e);
            }
            l -= size;
            enc_size += size;
        }
        certs = new X509Certificate[certs_vector.size()];
        for (int i = 0; i < certs.length; i++) {
            certs[i] = (X509Certificate) certs_vector.elementAt(i);
        }
        this.length = 3 + 3 * certs.length + enc_size;
        if (this.length != length) {
            fatalAlert(AlertProtocol.DECODE_ERROR,
                    "DECODE ERROR: incorrect CertificateMessage");
        }

    
public CertificateMessage(X509Certificate[] certs)
Creates outbound message

param
certs

        if (certs == null) {
            this.certs = new X509Certificate[0];
            encoded_certs = new byte[0][0];
            length = 3;
            return;
        }
        this.certs = certs;
        if (encoded_certs == null) {
            encoded_certs = new byte[certs.length][];
            for (int i = 0; i < certs.length; i++) {
                try {
                    encoded_certs[i] = certs[i].getEncoded();
                } catch (CertificateEncodingException e) {
                    fatalAlert(AlertProtocol.INTERNAL_ERROR, "INTERNAL ERROR",
                            e);
                }
            }
        }
        length = 3 + 3 * encoded_certs.length;
        for (int i = 0; i < encoded_certs.length; i++) {
            length += encoded_certs[i].length;
        }
    
Methods Summary
public intgetType()
Returns message type

return

        return Handshake.CERTIFICATE;
    
public voidsend(org.apache.harmony.xnet.provider.jsse.HandshakeIODataStream out)
Sends message

param
out


        int total_length = 0;
        if (encoded_certs == null) {
            encoded_certs = new byte[certs.length][];
            for (int i = 0; i < certs.length; i++) {
                try {
                    encoded_certs[i] = certs[i].getEncoded();
                } catch (CertificateEncodingException e) {
                    fatalAlert(AlertProtocol.INTERNAL_ERROR, "INTERNAL ERROR",
                            e);
                }
            }
        }
        total_length = 3 * encoded_certs.length;
        for (int i = 0; i < encoded_certs.length; i++) {
            total_length += encoded_certs[i].length;
        }
        out.writeUint24(total_length);
        for (int i = 0; i < encoded_certs.length; i++) {
            out.writeUint24(encoded_certs[i].length);
            out.write(encoded_certs[i]);
        }