无法通过JSP生成根证书

时间:2013-04-21 21:16:42

标签: java security jsp certificate

我现在正致力于使用充气城堡库创建PKCS10根证书。

因此,我导入了具有(RootCertGenerator.java)的充气城堡库,它正确地在纯Java环境中工作(我完成的所有RootCertGenerator.java正在修改main()方法来执行(), ,重命名方法)

但是,执行generateRooteCert.jsp后,它会显示tomcat错误,如页面底部所示。有人说它可以通过在构建路径上包含mail.jar来解决,但它不起作用。

--- generateRooteCert.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="myPackage.Utils" %>
<%@ page import="myPackage.RootCertGenerator" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Make root Certificate</title>
</head>
<% 
RootCertGenerator.execute();
%>
<body>

</body>
</html>

--- RootCertGenerator.java     打包myPackage;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.X509Certificate;
import java.util.Date;
import javax.security.auth.x500.X500Principal;
import org.bouncycastle.openssl.PEMWriter;
import org.bouncycastle.x509.X509V1CertificateGenerator;

public class RootCertGenerator
{
public static X509Certificate generateV1Certificate(KeyPair pair)
throws InvalidKeyException, NoSuchProviderException, SignatureException
{
 // generate the certificate
X509V1CertificateGenerator  certGen = new X509V1CertificateGenerator();
certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
certGen.setIssuerDN(new X500Principal("CN=Test Certificate"));
certGen.setNotBefore(new Date(System.currentTimeMillis() - (7 * 24 * 60 * 60 * 1000))); 
certGen.setNotAfter(new Date(System.currentTimeMillis() + (7 * 24 * 60 * 60 * 1000)));
certGen.setSubjectDN(new X500Principal("CN=Test Certificate"));
certGen.setPublicKey(pair.getPublic());
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
return certGen.generateX509Certificate(pair.getPrivate(), "BC");
}

//////////added by jeon
public static void pemEncodeToFile(String filename, Object obj, char[] password) throws
Exception{
PEMWriter pw = new PEMWriter(new FileWriter(filename));
if (password != null && password.length > 0) {
pw.writeObject(obj, "DESEDE", password, new SecureRandom());
} else {
pw.writeObject(obj);
}
pw.flush();
pw.close();
}
//////////added by jeon

public static void execute() throws Exception
{
// create the keys
KeyPair          pair = Utils.generateRSAKeyPair();

////////////////////writing root certificate
PEMWriter pemWrt = new PEMWriter(new OutputStreamWriter(System.out));
pemWrt.writeObject(cert);
pemEncodeToFile("rootCertificate.cer", cert, null);
pemWrt.flush();
pemWrt.close();
/////////////////writing private key
PEMWriter pemWrt1 = new PEMWriter(new OutputStreamWriter(System.out));
pemWrt1.writeObject(pair.getPrivate());
pemEncodeToFile("rootPrivate.key", pair.getPrivate(), null);
pemWrt1.flush();
pemWrt1.close();

/////////////////writing public key
PEMWriter pemWrt2 = new PEMWriter(new OutputStreamWriter(System.out));
pemWrt2.writeObject(pair.getPublic());
pemEncodeToFile("rootPublic.key", pair.getPublic(), null);
pemWrt2.flush();
pemWrt2.close();
// show some basic validation
cert.checkValidity(new Date());
cert.verify(cert.getPublicKey());        
System.out.println("valid certificate generated");
}
}

错误信息是

org.apache.jasper.JasperException:
javax.servlet.ServletException:
java.security.NoSuchProviderException: no such provider: BC

我觉得即使我的系统

,TOMCAT也无法加载bouncycastle提供商

/jre/lib/security/java.security包含bouncycaslte信息。

我该怎么办?

1 个答案:

答案 0 :(得分:1)

查看网页

http://www.bouncycastle.org/wiki/display/JA1/Provider+Installation

指示如何将其添加到

$ JAVA_HOME / JRE / LIB /安全/ java.security

当然要确保这是tomcat正在使用的JRE。

同样基于个人经验将jar放入tomcat的lib中可以避免它被多次加载。