EJBCA使用之注册用户及创建证书

来源:互联网 发布:网络招嫖2016最大案件 编辑:程序博客网 时间:2024/06/07 23:48

研究ejbca源码快一个月了,从openipmp中的老版ejbca到最新的4.0.15,感觉看别人代码实在太痛苦了,而且之前自己对ejb并不是很熟悉,还必须自己重新学了一点基本的ejb知识。

我最开始是研究openipmp的,里面自带就有ejbca的jar包,所以一开始我看openipmp怎么调用ejbca就行,但是由于openipmp实在太老了,它使用的ejbca是遵守ejb2.1标准的尴尬,调用起来实在太复杂太麻烦了,所以经过一周后我彻底放弃它,重新看最新版的ejbca。

最新版的ejbca可以从官网下,官网下的包括java源码、jsp和一堆云里雾里的文档。但注意官网下的java源码并不是一个完整eclipse工程,我们需要修改源码的话,还是用svn检出eclipse工程的源码比较好。svn检出地址可以从http://ejbca.org/repository.html找到,是https://svn.cesecore.eu/svn/ejbca/branches/Branch_4_0/ejbca

检出完ejbca后,我们可以参考维基上关于ejbca的api http://wiki.ejbca.org/developers#toc6,我们首先导入工程,然后会发现有编译错误,因为工程使用了JBOSS5_HOME类变量,我们需要添加该变量,步骤是Window->Preferences->Java->Build Path->Classpath,指向jboss的根目录。设置完这个后还是不行,我们要使用ant命令编译得到一些必需的类,就在ejbca的根目录下使用ant,或者使用eclipse集成的ant。(注意,千万不要在windows X64上使用ejbca,因为windows X64的jdk缺少一个库,好像是sun的一个关于security的库,而ejbca必需这个库)。

做完以上步骤后,应该就没有问题了,我们现在就需要做最痛苦的事情——读代码。还好,ejbca的注释还是比较详尽的,而且都是javadoc注释,在eclipse查看很方便。好了,废话不多说,马上进入正题,使用ejbca注册新用户并且为用户导出证书。

注册新用户:ejbca使用方式有两种:web和命令行。web很简单,网上很多教程,这里主要介绍命令行方式。我们可以找到modules/ejbca-ejb-cli/src这个包里面都是使用命令行方式操作ejbca,而注册新用户是属于ra的操作,我们需要看org.ejbca.ui.cli.ra.RaAddUserCommand类,它的execute方法就是添加一个用户,那我们只需要调用该方法就添加用户,实在不明白这么好用的方法,ejbca的官方api为什么不给出,但是这个方法是不推荐的,因为注意到execute方法的参数是一个string数组,这就意味着我们可以出错的概率大大增加,他实际上用的是UserAdminSession的addUser方法,这个方法有很多重载方法,其中官方推荐的是使用一个userDataVo的对象作为注册参数,的确这比较好。然后下面是我写的一个junit测试用例(PS:舔一下Junit的菊花,junit确实是一个好东东)。

public class AddUser {RaAddUserCommand raCommand = null;@Beforepublic void setUp() throws Exception {raCommand = new RaAddUserCommand();}@Testpublic void test() {String[] args = { "", "xanxus1", "12345","CN=xanxus", "NULL", "AdminCA1","xanxusiou@163.com", "2", "P12" };try {raCommand.execute(args);} catch (ErrorAdminCommandException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


这里解释一下参数的含义,第一个是没意义的,所以为空字符串,接着是用户名,密码,dn(这个需要注意,必须要有cn,而且cn必须是唯一的),subjectAltName(这个没什么用,就NULL吧),ca名字,用户邮箱,用户类型(2是终端用户),最后的是证书类型(这里是pkcs12)。

为用户导出证书:这里就不能简单的使用junit测试了,因为我们需要使用ejb远程调用,所以我们需要创建一个web工程,放在jboss环境下。然后我们创建一个servlet,在dopost里面导出证书吧(注意,必须是post里),代码如下:

protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubresponse.setContentType("text/html;charset=UTF-8");String name = request.getParameter("name");String password = request.getParameter("password");try {Context jndiContext = new InitialContext();UserAdminSessionLocal userAdminSession = (UserAdminSessionLocal) jndiContext.lookup("ejbca/UserAdminSessionBean/local");AuthenticationSessionLocal authenticationSession = (AuthenticationSessionLocal) jndiContext.lookup("ejbca/AuthenticationSessionBean/local");CAAdminSessionLocal caAdminSession = (CAAdminSessionLocal) jndiContext.lookup("ejbca/CAAdminSessionBean/local");KeyRecoverySessionLocal keyRecoverySession = (KeyRecoverySessionLocal) jndiContext.lookup("ejbca/KeyRecoverySessionBean/local");SignSessionLocal signSession = (SignSessionLocal) jndiContext.lookup("ejbca/RSASignSessionBean/local");EndEntityProfileSessionLocal endEntityProfileSession = (EndEntityProfileSessionLocal) jndiContext.lookup("ejbca/EndEntityProfileSessionBean/local");Admin admin = new Admin(Admin.TYPE_PUBLIC_WEB_USER,request.getRemoteAddr());UserDataVO user = userAdminSession.findUser(new Admin(Admin.TYPE_PUBLIC_WEB_USER), name);GlobalConfigurationSessionLocal globalConfigurationSession = (GlobalConfigurationSessionLocal) jndiContext.lookup("ejbca/GlobalConfigurationSessionBean/local");RaAdminSessionLocal raAdminSession = (RaAdminSessionLocal) jndiContext.lookup("ejbca/RaAdminSessionBean/local");CertificateProfileSessionLocal certificateProfileSession = (CertificateProfileSessionLocal) jndiContext.lookup("ejbca/CertificateProfileSessionBean/local");boolean usekeyrecovery = false;usekeyrecovery = globalConfigurationSession.getCachedGlobalConfiguration(admin).getEnableKeyRecovery();// 生成证书boolean savekeys = user.getKeyRecoverable()&& usekeyrecovery&& (user.getStatus() != UserDataConstants.STATUS_KEYRECOVERY);boolean loadkeys = (user.getStatus() == UserDataConstants.STATUS_KEYRECOVERY)&& usekeyrecovery;int endEntityProfileId = user.getEndEntityProfileId();int certificateProfileId = user.getCertificateProfileId();EndEntityProfile endEntityProfile = endEntityProfileSession.getEndEntityProfile(admin, endEntityProfileId);boolean reusecertificate = endEntityProfile.getReUseKeyRecoveredCertificate();GenerateToken token = new GenerateToken(authenticationSession,userAdminSession, caAdminSession, keyRecoverySession,signSession);KeyStore keyStore = token.generateOrKeyRecoverToken(admin, name,password, user.getCAId(), "1024",AlgorithmConstants.KEYALGORITHM_RSA, false, loadkeys,savekeys, reusecertificate, endEntityProfileId);System.out.println("size:" + keyStore.size());sendP12Token(keyStore, name, password, response);} catch (Exception exception) {exception.printStackTrace();}}private void sendP12Token(KeyStore ks, String username, String kspassword,HttpServletResponse out) throws Exception {ByteArrayOutputStream buffer = new ByteArrayOutputStream();ks.store(buffer, kspassword.toCharArray());out.setContentType("application/x-pkcs12");out.setHeader("Content-disposition", "filename=" + username + ".p12");out.setContentLength(buffer.size());buffer.writeTo(out.getOutputStream());out.flushBuffer();buffer.close();}

首先,导出用户的前提是你必须已经在jboss里部署好ejbca,然后第一步是使用jndi找出远程对象,使用lookup方法,参数可以从jboss的jndiview里找出,注意这里都是local对象,接着就是使用GenerateToken对象生成keystore,有一个false参数代表生成的是p12证书,还能指定密码的长度和算法,最后就是使用response输出证书。

以上就是目前为止我看ejbca的成果,以后会继续更新,ejbca资料实在太少,希望大家一起努力,共同研究。

原创粉丝点击