1 19 20 package com.maverick.ssl; 21 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.Hashtable ; 26 import java.util.Random ; 27 import java.util.Vector ; 28 29 import com.maverick.crypto.asn1.ASN1Sequence; 30 import com.maverick.crypto.asn1.DERInputStream; 31 import com.maverick.crypto.asn1.x509.X509Certificate; 32 import com.maverick.crypto.asn1.x509.X509CertificateStructure; 33 import com.maverick.crypto.security.SecureRandom; 34 35 40 public class SSLContext { 41 42 Hashtable cipherClassesByID = new Hashtable (); 43 Hashtable cipherIDsByName = new Hashtable (); 44 Vector cipherIDs = new Vector (); 45 SecureRandom rnd = SecureRandom.getInstance(); 46 47 boolean allowUntrustedCertificates = false; 48 boolean allowInvalidCertificates = false; 49 50 TrustedCACertStore cacerts; 51 52 public SSLContext() throws IOException { 53 54 cacerts = new TrustedCACertStore(); 55 56 addCipherSuite(0x00, 0x04, "SSL_RSA_WITH_RC4_128_MD5", SSL_RSA_WITH_RC4_128_MD5.class); 58 59 try { 60 allowUntrustedCertificates = Boolean.valueOf(System.getProperty("com.maverick.ssl.allowUntrustedCertificates", "false")).booleanValue(); } catch (Exception ex) { 62 } 63 64 try { 65 allowInvalidCertificates = Boolean.valueOf(System.getProperty("com.maverick.ssl.allowInvalidCertificates", "false")).booleanValue(); } catch (Exception ex1) { 67 } 68 69 } 70 71 public void addCipherSuite(int id1, int id2, String name, Class suite) { 72 73 SSLCipherSuiteID id = new SSLCipherSuiteID(id1, id2); 74 cipherClassesByID.put(id, suite); 75 cipherIDsByName.put(name, id); 76 cipherIDs.addElement(id); 77 } 78 79 public TrustedCACertStore getTrustedCACerts() { 80 return cacerts; 81 } 82 83 public Class getCipherSuiteClass(SSLCipherSuiteID id) { 84 85 Enumeration e = cipherClassesByID.keys(); 86 while (e.hasMoreElements()) { 87 SSLCipherSuiteID i = (SSLCipherSuiteID) e.nextElement(); 88 if (i.equals(id)) { 89 return (Class ) cipherClassesByID.get(i); 90 } 91 } 92 93 return null; 94 } 95 96 public SSLCipherSuiteID[] getCipherSuiteIDs() { 97 SSLCipherSuiteID[] ids = new SSLCipherSuiteID[cipherIDs.size()]; 98 cipherIDs.copyInto(ids); 99 return ids; 100 } 101 102 public Random getRND() { 103 return rnd; 104 } 105 106 public boolean isUntrustedCertificateAllowed() { 107 return allowUntrustedCertificates; 108 } 109 110 public boolean isInvalidCertificateAllowed() { 111 return allowInvalidCertificates; 112 } 113 114 public static void main(String [] args) { 115 116 try { 117 SSLContext ssl = new SSLContext(); 118 DERInputStream der = new DERInputStream(new FileInputStream ("c:/exported.cer")); 121 ASN1Sequence certificate = (ASN1Sequence) der.readObject(); 122 123 X509Certificate x509 = new X509Certificate(X509CertificateStructure.getInstance(certificate)); 125 126 System.out.println(x509.getIssuerDN()); 127 System.out.println(x509.getSubjectDN()); 128 ssl.getTrustedCACerts().isTrustedCertificate(x509, true, true); 129 } catch (Exception ex) { 130 ex.printStackTrace(); 131 } 132 133 } 134 135 } 136 | Popular Tags |