/**
* 生成自签名证书
* @param alias
* @param pass
* @param dn
* @param keysize
* @param validity
* @param jksfile
* @throws Exception
*/
public void generateJKS(String alias,String pass,String dn,String keysize,String validity,String jksfile)throws Exception{
CommandRunner runner = null;
Vector<String> v = new Vector<String>();
v.add(KEY_TOOL);
v.add("-genkey");
v.add("-alias");
v.add(alias);
v.add("-keyalg");
v.add(KEYALG_RSA);
v.add("-keysize");
v.add(keysize);
v.add("-keystore");
v.add(jksfile);
v.add("-dname");
v.add(dn);
v.add("-storetype");
v.add("jks");
v.add("-storepass");
v.add(pass);
v.add("-keypass");
v.add(pass);
v.add("-validity");
v.add(validity);
try{
runner = new CommandRunner(v);
runner.runCommand();
}catch(Exception e){
throw new CertificateException("generateJKS", e);
}
}
/**
* 产生证书请求
* @param alias
* @param pass
* @param dn
* @param keysize
* @param validity
* @param csrfile
* @param csrkeystore
* @return String
* @throws Exception
*/
public String generateCSR(String alias,String pass,String dn,String keysize,String validity,String csrfile,String csrkeystore)throws Exception{
generateJKS(alias, pass, dn, keysize, validity, csrkeystore);
InputStream in = null;
CommandRunner runner = null;
Vector<String> v = new Vector<String>();
v.add(KEY_TOOL);
v.add("-certreq");
v.add("-alias");
v.add(alias);
v.add("-keyalg");
v.add(KEYALG_RSA);
v.add("-dname");
v.add(dn);
v.add("-keystore");
v.add(csrkeystore);
v.add("-file");
v.add(csrfile);
v.add("-storepass");
v.add(pass);
v.add("-keypass");
v.add(pass);
v.add("-validity");
v.add(validity);
try{
runner = new CommandRunner(v);
runner.runCommand();
in = new FileInputStream(csrfile);
return Util.loadStreamToString(in, null);
}catch(Exception e){
throw new CertificateException("generateCSR", e);
}finally{
Util.closeStream(in);
}
}
/**
* 导入第三方签发者CA证书到临时密钥库中
* @param alias
* @param pass
* @param cafile
* @param csrkeystore
* @throws Exception
*/
public void importSignedCACert(String alias,String pass,String cafile,String csrkeystore)throws Exception{
CommandRunner runner = null;
Vector<String> v = new Vector<String>();
v.add(KEY_TOOL);
v.add("-import");
v.add("-alias");
v.add(alias);
v.add("-keystore");
v.add(csrkeystore);
v.add("-file");
v.add(cafile);
v.add("-storepass");
v.add(pass);
v.add("-keypass");
v.add(pass);
v.add("-noprompt");
try{
runner = new CommandRunner(v);
runner.runCommand();
}catch(Exception e){
throw new CertificateException("importSignedCert cafile", e);
}
}
/**
* 导入第三方签发证书到临时密钥库中
* @param alias
* @param pass
* @param certfile
* @param csrkeystore
* @throws Exception
*/
public void importSignedCert(String alias,String pass,String certfile,String csrkeystore)throws Exception{
CommandRunner runner = null;
Vector<String> v = new Vector<String>();
v.add(KEY_TOOL);
v.add("-import");
v.add("-trustcacerts");
v.add("-alias");
v.add(alias);
v.add("-keystore");
v.add(csrkeystore);
v.add("-file");
v.add(certfile);
v.add("-storepass");
v.add(pass);
v.add("-keypass");
v.add(pass);
try{
runner = new CommandRunner(v);
runner.runCommand();
}catch(Exception e){
throw new CertificateException("importSignedCert certfile", e);
}
}
/**
* 删除签发者证书
* @param alias
* @param pass
* @param csrkeystore
* @throws Exception
*/
public void deleteSignedCACert(String alias, String pass, String csrkeystore)throws Exception{
CommandRunner runner = null;
Vector<String> v = new Vector<String>();
v.add(KEY_TOOL);
v.add("-delete");
v.add("-alias");
v.add("root");
v.add("-keystore");
v.add(csrkeystore);
v.add("-storepass");
v.add(pass);
try{
runner = new CommandRunner(v);
runner.runCommand();
}catch(Exception e){
throw new CertificateException("delete cafile", e);
}
}