cfca采用加密组件Bouncy Castle。
cfca证书工具包包含如下文件(服务器端,以jdk1.4为例):
1、bcprov-jdk14-125.jar:security provider lib
2、bcmail-jdk14-125.jar:貌似邮件用的
3、cfcaDigitalKitTest.jar:cfca自己开发的工具包,就一个类
4、entbase.jar、entuser.jar:cfcaDigitalKitTest.jar要应用的2个jar包
5、cacerts:cfca的根证书链文件,是一个keystore文件,可以用keytool -list查看
6、castle.ini:cfca工具包的配置文件,内容如下
[CRL校验部分]==========貌似可以不配
/*ldap服务器端口号*/
ldapServerPort=389
/*ldap服务器地址*/
ldapServerName=210.74.41.60
/*CFCA CRL cache存放目录*/
cachedCRLDirPath=G:\\testCerts
[用户证书部分]
/*用户证书路径*/==========这里可以配置keystore(扩展名必须是keysotre,写死在cfca工具包中)或者pfx文件
userCertFilePath=D:\\java\\stf\\cfca\\cfcakeystore_server.keystore
/*访问用户证书口令*/
userCertPassword=Passw0rds
/*用户证书别名,可选*/===========如果前面配的是keystore必须指定alias
userCertAlias=cfca_server
[可信CA库口令]=============根证书链文件cacerts的默认文件
trustedCAStorePass=changeit
两种方式使用cfca的证书工具包:
参考:
http://danielzzu.blog.163.com/blog/static/11851530420101194345324/
一、配置方式(以jdk1.4为例)
1. 添加安全库
前置条件:获得/usr/java14目录的写权限
动作:
要添加两个安全jar文件:bcmail-jdk14-125.jar和bcprov-jdk14-125.jar
把这两个jar放到/usr/java14/jre/lib/ext目录下
修改这两个jar包的权限:
chmod 755 bcmail-jdk14-125.jar
chmod 755 bcprov-jdk14-125.jar
2. 配置安全文件java.security
该文件在/usr/java14/jre/lib/security/ 目录下
在其中添加一行:
security.provider.N=org.bouncycastle.jce.provider.BouncyCastleProvider
其中N用具体的数字替换,如果文件中原有的security provider编号最大是4,则这里的N就用5,注意千万不要与原有的security provider编号重复!
3. 更新cacerts文件
该文件在/usr/java14/jre/lib/security/ 目录下
首先对该目录下原cacerts进行备份:mv cacerts cacerts.old
然后把提供的cacerts复制到这个目录下
修改文件权限:chmod 755 cacerts
4. 将cfcaDigitalKitTest.jar、entbase.jar、entuser.jar、castle.ini放到classpath中,就可以调用cfca工具包:
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import com.cfca.toolkit.*;
public class TestSign {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
TestSign ts=new TestSign();;
String msg="test string !!!!!!";
System.out.println("签名前数据:"+msg);
String signedMsg=ts.signData(msg);
System.out.println("签名后数据:"+signedMsg);
String checkedMsg=ts.checkSign(signedMsg);
System.out.println("验证签名后数据:"+checkedMsg);
}catch(Exception e){
e.printStackTrace();
}
}
public String signData(String msg) throws Exception{
Castle.iniFilePath = "D:\\java\\stf\\castle_client.ini";
Castle castle=new Castle();
castle.initCertAppContext();
return castle.signData(msg);
}
public String checkSign(String msg) throws Exception{
Castle.iniFilePath = "D:\\java\\stf\\castle.ini";
Castle castle=new Castle();
// castle.initCertAppContext();
System.out.println("签名者DN:"+castle.getDN(castle.getCertificate(msg)));
return castle.verifySignedData(msg);
}
}
二、编码方式
配置方式稍显麻烦,编码方式无非在程序中指定provider
悲剧:cfca证书工具包中指定trustkeystore只能是上述配置的位于jdk目录的cacerts,不能放在其他地方
//指定truststore(但经过测试,仅用客户端证书签名,用服务器端证书验证签名不需要配置truststore)
//验证签名不需要配置trustCA,但调用cfca工具包的验证证书命令:verifyCertificate,就需要配置信任CA
// System.setProperty("javax.net.ssl.trustStore","D:\\java\\stf\\cacerts");
// System.setProperty("javax.net.ssl.trustStorePassword","changeit");
//指定provider
Security.addProvider(new BouncyCastleProvider());
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import com.cfca.toolkit.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class TestSign {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
TestSign ts=new TestSign();
String msg="test string!!!!!!!!!";
System.out.println("签名前数据:"+msg);
String signedMsg=ts.signData(msg);
System.out.println("签名后数据:"+signedMsg);
String checkedMsg=ts.checkSign(signedMsg);
System.out.println("验证签名后数据:"+checkedMsg);
}catch(Exception e){
e.printStackTrace();
}
}
public TestSign(){
//指定truststore(但经过测试,仅用客户端证书签名,用服务器端证书验证签名不需要配置truststore)
//验证签名不需要配置trustCA,但调用cfca工具包的验证证书命令:verifyCertificate,就需要配置信任CA
// System.setProperty("javax.net.ssl.trustStore","D:\\java\\stf\\cacerts");
// System.setProperty("javax.net.ssl.trustStorePassword","changeit");
//指定provider
Security.addProvider(new BouncyCastleProvider());
}
public String signData(String msg) throws Exception{
Castle.iniFilePath = "D:\\java\\stf\\castle.ini";
Castle castle=new Castle();
castle.initCertAppContext();
return castle.signData(msg);
}
public String checkSign(String msg) throws Exception{
Castle.iniFilePath = "D:\\java\\stf\\castle_client.ini";
Castle castle=new Castle();
// castle.initCertAppContext();
System.out.println("签名者DN:"+castle.getDN(castle.getCertificate(msg)));
return castle.verifySignedData(msg);
}
}