FileDocCategorySizeDatePackage
ReceiveObject.javaAPI DocExample4615Sun Oct 25 18:13:36 GMT 1998None

ReceiveObject

public class ReceiveObject extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.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();
		}
	
private static voidprocessCertificate(java.security.cert.X509Certificate x509)

		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");
		}
	
private static voidverifyCertificateSigner(java.security.cert.Certificate c, java.lang.String name)

		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());
		}