⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webpublickeystore.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
字号:
/* *  @(#)WebPublicKeyStore.java	1.2 01/08/10 *  *  Copyright (c) 2001 Sun Microsystems, Inc., 901 San Antonio Road, *  Palo Alto, CA 94303, U.S.A.  All Rights Reserved. * *  Sun Microsystems, Inc. has intellectual property rights relating *  to the technology embodied in this software.  In particular, and *  without limitation, these intellectual property rights may include *  one or more U.S. patents, foreign patents, or pending *  applications.  Sun, Sun Microsystems, the Sun logo, Java, KJava, *  and all Sun-based and Java-based marks are trademarks or *  registered trademarks of Sun Microsystems, Inc.  in the United *  States and other countries. * *  This software is distributed under licenses restricting its use, *  copying, distribution, and decompilation.  No part of this *  software may be reproduced in any form by any means without prior *  written authorization of Sun and its licensors, if any. * *  FEDERAL ACQUISITIONS:  Commercial Software -- Government Users *  Subject to Standard License Terms and Conditions */package com.sun.midp.publickeystore;import java.io.*;import java.util.*;import com.sun.ksecurity.*;import com.sun.kssl.*;/** * A keystore that can be extended to include keys from another keystore * eliminating the need to build a composite keystore. This is useful for * combining a system keystore with application specific keystore for * use with KSSL. To work with KSSL this class implements the KSSL * {@link KeyStore} interface. */public class WebPublicKeyStore extends PublicKeyStore implements KeyStore {    /** keystore this package uses for verifying descriptors */    private static WebPublicKeyStore trustedKeyStore;    /** keystore to search if a key is not found in the main key list */    private PublicKeyStore extension;    /**     * 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()     */    public static void setTrustedKeyStore(WebPublicKeyStore keyStore) {        if (trustedKeyStore != null) {            return;        }        trustedKeyStore = keyStore;        SSLStreamConnection.setTrustedKeyStore(keyStore);        SSLStreamConnection.lockTrustedKeyStore();         }    /**     * 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(WebPublicKeyStore)     */    public static WebPublicKeyStore getTrustedKeyStore() {        return trustedKeyStore;    }    /**     * 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     */    public WebPublicKeyStore(InputStream in) throws IOException {        super(in);    }    /**     * Extends this store to include the keys of a given keystore.     * @param theExtension keystore to be searched if a given     *        owner's key is not found in this keystore.     */    public void ExtendKeyStore(PublicKeyStore theExtension) {        if (extension != null) {            return;        }        extension = theExtension;    }    /**     * Get's a WebAliasEnum enumeration object.     *     * @return WebAliasEnum enumeration type.     */    public Enumeration aliases() {        Enumeration extAliases = null;        if (extension != null) {            extAliases = extension.getOwners();        }	return new WebAliasEnum(getOwners(), extAliases);    }    /**     * Get's a certificate for an alias.     *     * @param alias a key store alias.     * @return a certificate for the alias.     */    public Certificate getCertificate(String alias) {        return createCertificate(findKey(alias));    }    /**     * Determines if there's an alais in the key store.     *     * @param alias a key store alias.     * @return a boolean value indicating whether or not alias found.     */    public boolean containsAlias(String alias) {        return (findKey(alias) != null);    }    /**     * Gets the number of elements present.     *     * @return the number of keys.     */    public int size() {        int size;	size = super.numberOfKeys();        if (extension == null) {            return size;        }        return size + extension.numberOfKeys();    }        /**     * Returns the type of keystore being used.     *     * @return a key store type.     */    public String getType() {	return "KSSLKS";    }    /**     * Finds a key based on the owners distinguished name.     * @param alias distinguished name of key's owner     * @return public key information     */    public synchronized PublicKeyInfo findKey(String alias) {        PublicKeyInfo keyInfo;        keyInfo = super.findKey(alias);        if (keyInfo != null) {            return keyInfo;        }        if (extension == null) {            return null;        }        return extension.findKey(alias);    }    /**     * Creates a KSSL {@link Certificate} using the given public key     * information.     * @param keyInfo key information     * @return KSSL Certificate     */    static X509Certificate createCertificate(PublicKeyInfo keyInfo) {        if (keyInfo == null) {            return null;        }	try {	    X509Certificate cert;	    cert = new X509Certificate((byte)1, // fixed at version 1                                keyInfo.getOwner(),                                keyInfo.getOwner(), // issuer same as subject                                keyInfo.getNotBefore(),                                keyInfo.getNotAfter(),                                keyInfo.getModulus(),                                keyInfo.getExponent(),                                null, // we don't use finger prints                                0); // chains are not allowed	    return cert;	} catch (Exception e) {	    return null;	}    }}/** * Implements the an enumeration of key owner names in the both the main and * extension key lists. */class WebAliasEnum implements Enumeration {    /** list of owners from the main keystore */    private Enumeration enumMain;    /** list of owners from the extension keystore */    private Enumeration enumExt;    /**     * Constructs a WebAliasEnum from a main and extension key list     * enumerations.     * @param main an enumeration of the main key list     * @param ext an enumeration of the extension key list     */    WebAliasEnum(Enumeration main, Enumeration ext) {	enumMain = main;	enumExt = ext;    }    /**     * Tests if this enumeration contains more elements.     *     * @return boolean if there's any more elements.     */    public boolean hasMoreElements() {        if (enumMain.hasMoreElements()) {            return true;        }        if (enumExt == null) {            return false;        }        return enumExt.hasMoreElements();    }    /**     * Returns the next element of this enumeration if this enumeration      * object has at least one more element to provide.     *     * @return Object the next element of this object     */    public Object nextElement() {        if (enumExt != null && !enumMain.hasMoreElements()) {            return enumExt.nextElement();        }        return enumMain.nextElement();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -