PluginVerifier.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.plugin |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.plugin;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.security.interfaces.DSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.apache.log4j.Logger;
import org.apache.xerces.utils.Base64;
import org.xnap.XNap;
import org.xnap.loader.XNapClassLoader;
/**
* This class verifies the signature of the given Jar and provides
* informations about its signers
*
* @version $Id: PluginVerifier.java,v 1.7 2003/11/04 13:51:06 vanto Exp $
*/
public class PluginVerifier {
private static Logger logger = Logger.getLogger(PluginVerifier.class);
private PluginInfo info;
private List certs = new ArrayList();
private boolean valid;
private boolean signed;
private List unsignedJars = new ArrayList();
private List signedJars = new ArrayList();
/**
* Initialized a new PluginVerifier
* It verfies all jars used by the plugin and provides information
* about its signers.
*/
public PluginVerifier(PluginInfo info) throws IOException {
this.info = info;
this.valid = true;
this.signed = true;
try {
String[] classpath = info.getClassPath();
for (int i = 0; i < classpath.length; i++) {
if (XNapClassLoader.isLoaded(new File(classpath[i]).toURL())) break;
boolean s = validateJar(classpath[i]);
if (!s) {
unsignedJars.add(classpath[i]);
} else {
signedJars.add(classpath[i]);
}
signed = signed && s;
}
} catch (SecurityException e) {
this.valid = false;
}
}
/**
* Returns true if all jars are consistent and if signed, the signature
* must be valid. */
public boolean isValid() {
return valid;
}
/**
* Return true, (only) if all jars of the plugin are signed. */
public boolean isSigned() {
return valid && signed;
}
/**
* Returns true, if all jars are signed and trusted by XNap */
public boolean isTrustedByXNap() {
return isTrustedBy(XNap.TRUSTED_KEYS);
}
/**
* Returns true, if all jars are signed and trusted by the given keyset
* @param encodedPKs StringArray of Base64-encoded X509-PublicKeys */
public boolean isTrustedBy(String[] encodedPKs) {
boolean trusted = false;
if (isSigned()) {
trusted = true;
try {
KeyFactory kf = KeyFactory.getInstance("DSA");
for (int i = 0; i < getCertificates().length; i++) {
if (getCertificates()[i] instanceof X509Certificate) {
X509Certificate cert = (X509Certificate)getCertificates()[i];
DSAPublicKey pk = (DSAPublicKey)cert.getPublicKey();
for (int j = 0; j < encodedPKs.length; j++) {
KeySpec ks = new X509EncodedKeySpec(Base64.decode(encodedPKs[j].getBytes()));
PublicKey trustedKey = kf.generatePublic(ks);
trusted = trusted && trustedKey.equals(pk);
}
}
}
} catch (NoSuchAlgorithmException e) {
logger.debug(e);
} catch (InvalidKeySpecException e) {
logger.debug(e);
}
}
return trusted;
}
/**
* Returns a list of unsigned jars */
public String[] getUnsignedJars() {
return (String[])unsignedJars.toArray(new String[0]);
}
/**
* Returns a list of signed jars */
public String[] getSignedJars() {
return (String[])signedJars.toArray(new String[0]);
}
/**
* Returns the list of certificates (mostly X509) */
public Certificate[] getCertificates() {
return (Certificate[])certs.toArray(new Certificate[0]);
}
/**
* Validates the given jar and updates this' fields */
private boolean validateJar(String filename) throws IOException
{
logger.debug(new File(filename.trim()).getAbsolutePath().toString());
JarFile jar = new JarFile(new File(filename.trim()).getAbsolutePath(), true);
Enumeration entries = jar.entries();
boolean signed = false;
while (entries.hasMoreElements()) {
JarEntry entry = (JarEntry) entries.nextElement();
InputStream in = jar.getInputStream(entry);
// read whole stream to make certificates available
while (in.read() != -1) {}
if (entry.getCertificates() != null) {
for (int i = 0; i < entry.getCertificates().length; i++) {
signed = true;
if (!certs.contains(entry.getCertificates()[i])) {
certs.add(entry.getCertificates()[i]);
}
}
}
}
return signed;
}
/**
* Formats the Principal to make it more human readable */
public static String readablePrincipal(Principal prin)
{
// FIX: replaceAll() is only available on JDK 1.4+
// return prin.getName().replaceAll("CN=", "")
// .replaceAll("OU=", "").replaceAll("O=", "\n")
// .replaceAll("L=", "\n").replaceAll("ST=", "\n")
// .replaceAll("C=", "\n").replaceAll("DE", XNap.tr("Germany"))
// .replaceAll("US", XNap.tr("United States"));
return prin.getName();
}
/**
* Returns a String with all signers. */
public String getSigners()
{
StringBuffer sig = new StringBuffer();
for (int i = 0; i < getCertificates().length; i++) {
if (getCertificates()[i] instanceof X509Certificate) {
X509Certificate cert = (X509Certificate)getCertificates()[i];
sig.append(cert.getIssuerDN());
if (i < getCertificates().length) {
sig.append(", ");
}
}
}
return sig.toString();
}
}
The table below shows all metrics for PluginVerifier.java.



