29.07.2013 Views

Master of Science thesis Fighting Spam

Master of Science thesis Fighting Spam

Master of Science thesis Fighting Spam

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

{<br />

java.security.cert.Certificate cert2 = FileHandler.importCertificate(new<br />

File("d:\\CA\\cacert.pem"));<br />

KeyFactory kf = KeyFactory.getInstance("RSA");<br />

PKCS8EncodedKeySpec pkcs = new PKCS8EncodedKeySpec(FileHandler.readBytes(new<br />

File("d:\\CA\\cakey.key")));<br />

System.out.println(pkcs.getFormat());<br />

PrivateKey pk = kf.generatePrivate(pkcs);<br />

KeyStore ks = KeyStore.getInstance("JKS");<br />

//ks.load(new FileInputStream(new File(Config.OpenSSLpath + Config.name +<br />

".keystore")),<br />

// "spammail".toCharArray());<br />

ks.load(null,"spammail".toCharArray());<br />

ks.setKeyEntry("RS", pk, "spammail".toCharArray(), new<br />

java.security.cert.Certificate[]<br />

{cert2});<br />

ks.store(new FileOutputStream(new File(Config.OpenSSLpath + Config.name +<br />

".keystore")), "spammail".toCharArray());<br />

FileHandler.writeObject((RSAPrivateKey) pk, new File(Config.OpenSSLpath +<br />

"RS_private.key"));<br />

}<br />

private static void generateCertificate(String alias, int keySize) throws Exception<br />

{<br />

generateKeyPair(alias, Config.OpenSSLpath + Config.name + ".keystore", "rsa",<br />

keySize, "spammail",<br />

alias + "_password",<br />

new String[]<br />

{alias, "", "", "", "", "", ""});<br />

PrivateKey privKey = getKey(Config.OpenSSLpath + Config.name + ".keystore",<br />

alias, "spammail",<br />

alias + "_password");<br />

FileHandler.writeObject(privKey, new File(Config.OpenSSLpath + alias +<br />

"_private.key"));<br />

byte[] data = generateCertRequest(alias, Config.OpenSSLpath + Config.name +<br />

".keystore", "spammail",<br />

alias + "_password");<br />

System.out.println(data.length);<br />

String filesep = System.getProperty("file.separator");<br />

FileOutputStream fos = new FileOutputStream(new File(Config.OpenSSLpath + alias +<br />

"_certificaterequest.csr"));<br />

fos.write(data);<br />

fos.close();<br />

Runtime rt = Runtime.getRuntime();<br />

String command = "openssl ca -in " + Config.OpenSSLpath + alias +<br />

"_certificaterequest.csr -out "<br />

+ Config.OpenSSLpath<br />

+ alias + "_certificate.crt -notext -batch -key spammail";<br />

Process proc = rt.exec(command);<br />

File generatedCert = new File(Config.OpenSSLpath + alias + "_certificate.crt");<br />

int checks = 0;<br />

while(!generatedCert.exists() || generatedCert.length()==0 ||<br />

!generatedCert.canRead())<br />

{<br />

if(checks >= 10)<br />

throw new Exception("Error generating certificate!");<br />

Thread.sleep(300);<br />

Debugger.debug(Cryptotools.class, 3, "Waiting for certificate to be<br />

generated");<br />

checks++;<br />

}<br />

395

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!