FileDocCategorySizeDatePackage
WebPublicKeyStore.javaAPI DocJ2ME MIDP 2.05615Thu Nov 07 12:02:26 GMT 2002com.sun.midp.publickeystore

WebPublicKeyStore

public class WebPublicKeyStore extends PublicKeyStore implements CertStore
A public keystore that can used with SSL. To work with SSL this class implements the SSL {@link PublicKeyStore} interface.

Fields Summary
private static SecurityToken
classSecurityToken
This class has a different security domain than the MIDlet suite
private static WebPublicKeyStore
trustedKeyStore
keystore this package uses for verifying descriptors
Constructors Summary
public WebPublicKeyStore(InputStream in)
Constructs an extendable keystore from a serialized keystore created by {@link PublicKeyStoreBuilder}.

param
in stream to read a keystore serialized by {@link PublicKeyStoreBuilder#serialize(OutputStream)} from
exception
IOException if the key storage was corrupted

        super(in);
    
Methods Summary
public static X509CertificatecreateCertificate(PublicKeyInfo keyInfo)
Creates an {@link X509Certificate} using the given public key information.

param
keyInfo key information
return
X509 certificate

        if (keyInfo == null) {
            return null;
        }

	try {
	    X509Certificate cert;

	    cert = new X509Certificate((byte)1, // fixed at version 1
                                new byte[0],
                                keyInfo.getOwner(),
                                keyInfo.getOwner(), // issuer same as subject
                                keyInfo.getNotBefore(),
                                keyInfo.getNotAfter(),
                                keyInfo.getModulus(),
                                keyInfo.getExponent(),
                                null, // we don't use finger prints
                                0);
	    return cert;
	} catch (Exception e) {
	    return null;
	}
    
public X509Certificate[]getCertificates(java.lang.String subjectName)
Returns the certificate(s) corresponding to a subject name string.

param
subjectName subject name of the certificate in printable form.
return
corresponding certificates or null (if not found)

        Vector keys;
        X509Certificate[] certs;

        keys = findKeys(subjectName);
        if (keys == null) {
            return null;
        }

        certs = new X509Certificate[keys.size()];
        for (int i = 0; i < keys.size(); i++) {
            certs[i] = createCertificate((PublicKeyInfo)keys.elementAt(i));
        }

        return certs;
    
public static com.sun.midp.publickeystore.WebPublicKeyStoregetTrustedKeyStore()
Provides the keystore of resident public keys for security domain owners and other CA's.

return
keystore of domain owner and CA keys
see
#setTrustedKeyStore

        return trustedKeyStore;
    
public static voidinitSecurityToken(SecurityToken token)
Initializes the security domain for this class, so it can perform actions that a normal MIDlet Suite cannot.

param
token security token for this class.

        if (classSecurityToken == null) {
            classSecurityToken = token;
        }
    
public static voidloadCertificateAuthorities()
Load the certificate authorities for the MIDP RI from storage into the SSL keystore.

        RandomAccessStream storage;
        InputStream tks;
        WebPublicKeyStore ks;

        if (trustedKeyStore != null) {
            return;
        }

        try {
            storage = new RandomAccessStream(classSecurityToken);
            storage.connect(File.getStorageRoot() + "_main.ks",
                            Connector.READ);
            tks = storage.openInputStream();
        } catch (Exception e) {
            System.out.println("Could not open the trusted key store, " +
                               "cannot authenticate HTTPS servers");
            return;
        }

        try {
            ks = new com.sun.midp.publickeystore.WebPublicKeyStore(tks);
        } catch (Exception e) {
            System.out.println("Corrupt key store file, " +
                               "cannot authenticate HTTPS servers");
            e.printStackTrace();
            return;
        } finally {
            try {
                storage.disconnect();
            } catch (Exception e) {
                // nothing we can do.
            }
        }

        WebPublicKeyStore.setTrustedKeyStore(ks);
    
private static voidsetTrustedKeyStore(com.sun.midp.publickeystore.WebPublicKeyStore keyStore)
Establish the given keystore as the system trusted keystore. This is a one-shot method, it will only set the trusted keystore it there is no keystore set. For security purposes only read-only PublicKeyStores should be set.

param
keyStore keystore to be the system trusted keystore
see
#getTrustedKeyStore

        if (trustedKeyStore != null) {
            return;
        }

        trustedKeyStore = keyStore;

        SSLStreamConnection.setTrustedCertStore(keyStore);
        SSLStreamConnection.lockTrustedCertStore();