verifierimpl.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 481 行 · 第 1/2 页

JAVA
481
字号
/* * * * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package com.sun.midp.installer;import java.io.InputStream;import java.io.IOException;import java.util.Vector;import javax.microedition.io.Connector;import javax.microedition.pki.CertificateException;import com.sun.midp.pki.X509Certificate;import com.sun.midp.pki.AuthorityInfoAccessEntry;import com.sun.midp.pki.ocsp.OCSPValidator;import com.sun.midp.pki.ocsp.CertStatus;import com.sun.midp.pki.ocsp.OCSPValidatorImpl;import com.sun.midp.pki.ocsp.OCSPException;import com.sun.midp.publickeystore.WebPublicKeyStore;import com.sun.midp.publickeystore.PublicKeyInfo;import com.sun.midp.crypto.PublicKey;import com.sun.midp.crypto.Signature;import com.sun.midp.crypto.GeneralSecurityException;import com.sun.midp.security.Permissions;import com.sun.midp.io.j2me.storage.RandomAccessStream;import com.sun.midp.io.Base64;/** * Verifier that is able to verify midlet suite's signature. * It is used when the crypto code is present in the build. */public class VerifierImpl implements Verifier {    /**     * Current installation state.     */    InstallState state;    /**     * Authorization Path: A list of authority names from the verification,     * begining with the most trusted.     */    private String[] authPath;    /** Authenticated content provider certificate. */    private X509Certificate cpCert;    /** Online Certificate Status Protocol validator. */    private OCSPValidator certValidator;    /**     * True if the certificates used to sign the midlet suite being     * installed must be checked using Online Certificate Status     * Protocol (if enabled at the build-time), false otherwise.     */    private boolean isOCSPEnabled;    /**     * Constructor.     *     * @param installState current state of the installation     */    public VerifierImpl(InstallState installState) {        state = installState;    }    /**     * Checks to see if the JAD has a signature, but does not verify the     * signature.     *     * @return true if the JAD has a signature     */    public boolean isJadSigned() {        return state.getAppProperty(SIG_PROP) != null;    }    /**     * Looks up the domain of a MIDlet suite.     *     * @param ca CA of an installed suite     *     * @return security domain of the MIDlet suite     */    public String getSecurityDomainName(String ca) {        Vector keys;        String domain;        /*         * look up the domain owner, then get the domain from the         * trusted key store and set the security domain         */        try {            keys = WebPublicKeyStore.getTrustedKeyStore().                         findKeys(ca);            domain = ((PublicKeyInfo)keys.elementAt(0)).getDomain();        } catch (Exception e) {            domain = Permissions.UNIDENTIFIED_DOMAIN_BINDING;        }        return domain;    }    /**     * Verifies a Jar. On success set the name of the domain owner in the     * install state. Post any error back to the server.     *     * @param jarStorage System store for applications     * @param jarFilename name of the jar to read.     *     * @return authorization path: a list of authority names begining with     *         the most trusted, or null if jar is not signed     *     * @exception IOException if any error prevents the reading     *   of the JAR     * @exception InvalidJadException if the JAR is not valid or the     *   provider certificate is missing     */    public String[] verifyJar(RandomAccessStream jarStorage,            String jarFilename) throws IOException, InvalidJadException {        InputStream jarStream;        String jarSig;        jarSig = state.getAppProperty(SIG_PROP);        if (jarSig == null) {            // no signature to verify            return null;        }        authPath = null;        // This will fill in the cpCert and authPath fields        findProviderCert();        jarStorage.connect(jarFilename, Connector.READ);        try {            jarStream = jarStorage.openInputStream();            try {                verifyStream(jarStream, jarSig);            } finally {                jarStream.close();            }        } finally {            jarStorage.disconnect();        }        return authPath;    }    /**     * Enables or disables certificate revocation checking using OCSP.     *     * @param enable true to enable OCSP checking, false - to disable it     */    public void enableOCSPCheck(boolean enable) {        isOCSPEnabled = enable;    }    /**     * Returns true if OCSP certificate revocation checking is enabled,     * false if it is disabled.     *     * @return true if OCSP checking is enabled, false otherwise     */    public boolean isOCSPCheckEnabled() {        return isOCSPEnabled;    }    /**     * Find the first provider certificate that is signed by a known CA.     * Set the lastCA field to name of the CA. Set the cpCert field to the     * provider certificate.     *     * IMPL_NOTE: in the case of erroneous certificate chains the first     *            chain error will be thrown.     *     * @exception InvalidJadException if the JAR is not valid or the     *   provider certificate is missing or a general certificate error     */    private void findProviderCert() throws InvalidJadException {        int chain;        int result;        InvalidJadException pendingException = null;        for (chain = 1; ; chain++) {            // sets the authPath and cpCert            try {                result = checkCertChain(chain);            } catch (InvalidJadException ije) {                // According to the spec, if some chain is invalid and                // the next chain exists, it should also be verified;                // the first valid chain should be used for the jar                // verification.                if (pendingException == null) {                    pendingException = ije;                }                continue;            }            if (result == 1) {                // we found the good chain                return;            }            if (result == -1) {                // chain not found, done                break;            }        }        if (pendingException != null) {            throw pendingException;

⌨️ 快捷键说明

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