/*
*
* Copyright (c) 1998 Scott Oaks. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and
* without fee is hereby granted.
*
* This sample source code is provided for example only,
* on an unsupported, as-is basis.
*
* AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
* CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
* PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
* NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
* SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
* PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). AUTHOR
* SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
* HIGH RISK ACTIVITIES.
*/
import java.io.*;
import java.security.cert.CertificateException;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;
import java.security.*;
import java.util.*;
public class ReceiveObject {
private static void verifyCertificateSigner(java.security.cert.Certificate c,
String name) throws CertificateException {
java.security.cert.Certificate issuerCert = null;
X509Certificate sCert = null;
KeyStore ks = null;
try {
// In 1.2 beta 4, we need an argument for the following method
ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(new FileInputStream(System.getProperty("user.home") + File.separator + ".keystore"), null);
} catch (Exception e) {
throw new CertificateException("Invalid keystore");
}
// In 1.2 beta 4, we must handle a KeyStoreException
try {
String signer = ks.getCertificateAlias(c);
if (signer != null) {
System.out.println("We know the signer as " + signer);
return;
}
for (Enumeration alias = ks.aliases(); alias.hasMoreElements(); ) {
String s = (String) alias.nextElement();
try {
sCert = (X509Certificate) ks.getCertificate(s);
} catch (Exception e) {
continue;
}
if (name.equals(sCert.getSubjectDN().getName())) {
issuerCert = sCert;
break;
}
}
} catch (KeyStoreException kse) {
throw new CertificateException("Invalid keystore");
}
if (issuerCert == null) {
throw new CertificateException("No such certificate");
}
try {
c.verify(issuerCert.getPublicKey());
} catch (Exception e) {
System.out.println("EX" + e);
throw new CertificateException(e.toString());
}
}
private static void processCertificate(X509Certificate x509) throws CertificateParsingException {
Principal p;
p = x509.getSubjectDN();
System.out.println("This message was signed by " + p.getName());
p = x509.getIssuerDN();
System.out.println("This certificate was provided by " + p.getName());
try {
verifyCertificateSigner(x509, p.getName());
} catch (CertificateException ce) {
System.out.println("We do not recognize the authority who signed the certificate");
}
try {
x509.checkValidity();
} catch (CertificateExpiredException cee) {
System.out.println("That certificate is no longer valid");
} catch (CertificateNotYetValidException cnyve) {
System.out.println("That certificate is not yet valid");
}
}
public static void main(String args[]) {
try {
FileInputStream fis = new FileInputStream("test.obj");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
if (o instanceof Message) {
Message m = (Message) o;
System.out.println("Received message");
processCertificate((X509Certificate) m.certificate);
PublicKey pk = m.certificate.getPublicKey();
if (m.object.verify(pk, Signature.getInstance("DSA"))) {
System.out.println("Message is valid");
System.out.println(m.object.getObject());
}
else System.out.println("Message signature is invalid");
}
else System.out.println("Message is correupted");
} catch (Exception e) {
e.printStackTrace();
}
}
}
|