Search in sources :

Example 1 with CertUtlis

use of com.itrus.portal.utils.CertUtlis in project portal by ixinportal.

the class CertUtilsTest method testSetUserInfoVal.

@Test
public void testSetUserInfoVal() {
    CertUtlis cu = new CertUtlis();
    UserInfo userInfo = new UserInfo();
    cu.setUserInfoVal(userInfo, "userName", "test");
}
Also used : UserInfo(cn.topca.tca.ra.service.UserInfo) CertUtlis(com.itrus.portal.utils.CertUtlis) Test(org.junit.Test)

Example 2 with CertUtlis

use of com.itrus.portal.utils.CertUtlis in project portal by ixinportal.

the class CertificateController method update.

/**
 * 修改处理
 *
 * @param configuration
 * @param photoCodeImg
 * @param type
 *            新建类型 3是新建签章 2是新建证书1修改
 * @param uiModel
 * @return
 */
@RequestMapping(method = RequestMethod.POST, value = "/{type}", produces = "text/html")
public String update(@Valid EvidenceCertificate configuration, @RequestParam(value = "photoCodeImg", required = false) String photoCodeImg, @PathVariable("type") int type, Model uiModel) {
    if (type == 1) {
        // System.out.println("新建签章" + configuration.getSignatureType());
        // 获取证书信息
        EvidenceCertificate econfiguration = certificateService.selectById(configuration.getId());
        EvidenceSecretKey secretkey = new EvidenceSecretKey();
        try {
            if (StringUtils.isNotEmpty(photoCodeImg)) {
                // 获取密钥信息
                secretkey = sqlSession.selectOne("com.itrus.portal.db.EvidenceSecretKeyMapper.selectByPrimaryKey", econfiguration.getEvidenceSecretKey());
                File imgDir = filePathUtils.getEnterpriseFile(secretkey.getAlias());
                File frontImg = filePathUtils.saveImg(imgDir, null, photoCodeImg, IMG_DEFAULT_TYPE, IMG_NAME_VERIFY);
                if (frontImg != null && frontImg.isFile()) {
                    configuration.setPhotoCodeImg(frontImg.getName());
                    configuration.setPhotoCodeImgHash(HMACSHA1.genSha1HashOfFile(frontImg));
                }
            }
            if (configuration.getId() == null) {
                certificateService.insert(configuration);
            } else {
                String s = String.valueOf(econfiguration.getEvidenceSecretKey());
                Long keyid = Long.valueOf(s);
                EvidenceSecretKey secret = secrtkey.selectById(keyid);
                boolean is = certificateService.setSeal(secret.getAlias(), photoCodeImg);
                if (is) {
                    certificateService.update(configuration);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            String oper = "修改签章失败";
            String info = "错误原因:" + e.toString();
            LogUtil.evidencelog(sqlSession, null, oper, info);
            error = "修改签章失败";
            return "redirect:/certificate/" + configuration.getId() + "/" + 3;
        }
        if (econfiguration.getIsCertificate() == null || econfiguration.getIsCertificate() != 1) {
            JSONObject ret_data = null;
            ret_data = JSONObject.parseObject(configuration.getSubject());
            String cn = ret_data.getString("userName");
            String ou = ret_data.getString("userOrgunit");
            // String emali = ret_data.getString("EMAILADDRESS");
            String O = ret_data.getString("userOrganization");
            String dn = "CN=" + cn + "OU=" + ou + "O=" + O;
            String csr = certificateService.genCsr(secretkey.getAlias(), configuration.getArithmetic(), dn);
            if (csr != null) {
                RaAccount ra = sqlSession.selectOne("com.itrus.portal.db.RaAccountMapper.selectByPrimaryKey", configuration.getRaAccount());
                CertUtlis certutil = new CertUtlis();
                // UserInfo rauserinfo = new UserInfo();
                // Weibo类在下边定义
                UserInfo rauserinfo = JSON.parseObject(configuration.getSubject(), UserInfo.class);
                // System.out.println(rauserinfo.getUserAdditionalField1()+rauserinfo.getUserName());
                // UserInfo = (UserInfo)JSONObject.toBean(ret_data,UserInfo.class);//将建json对象转换为Person对象
                // System.out.println(configuration.getSubject());
                /*	rauserinfo.setUserName(cn);
					rauserinfo.setUserEmail(emali);
					rauserinfo.setUserAdditionalField1(ou);
					rauserinfo.setUserOrganization(O); */
                // 证书有效期
                Integer certValidity = 7;
                if ("0".equals(configuration.getPeriod())) {
                    certValidity = null;
                } else if (1 == configuration.getPeriod()) {
                    certValidity = 365 * 1 + 1;
                } else if (3 == configuration.getPeriod()) {
                    certValidity = 365 * 3 + 1;
                } else if (5 == configuration.getPeriod()) {
                    certValidity = 365 * 5 + 2;
                } else if (10 == configuration.getPeriod()) {
                    certValidity = 365 * 10 + 2;
                }
                // 调用RA
                CertInfo racertinfo = null;
                try {
                    if (ra.getAaPassword() != null) {
                        ra.setAaPassword(AESencrp.decrypt(ra.getAaPassword(), dbEncKey));
                    }
                    racertinfo = certutil.enrollCertByWS(csr, ra, rauserinfo, certValidity);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    String oper = "RA调用失败";
                    String info = "错误原因:" + e.toString();
                    LogUtil.evidencelog(sqlSession, null, oper, info);
                    error = "RA调用失败";
                    return "redirect:/certificate/" + configuration.getId() + "/" + 3;
                }
                if (racertinfo != null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
                    EvidenceCert cert = new EvidenceCert();
                    cert.setCertBase64(racertinfo.getCertSignBuf());
                    cert.setCreateTime(new Date());
                    cert.setCertSerialnumber(racertinfo.getCertSerialNumber());
                    cert.setIssuerdn(racertinfo.getCertIssuerDn());
                    cert.setSubjectdn(racertinfo.getCertSubjectDn());
                    try {
                        configuration.setStartTime(sdf.parse(racertinfo.getCertNotBefore()));
                        configuration.setEndTime(sdf.parse(racertinfo.getCertNotAfter()));
                        cert.setStartTime(sdf.parse(racertinfo.getCertNotBefore()));
                        cert.setEndTime(sdf.parse(racertinfo.getCertNotAfter()));
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    cert.setCreateTime(new Date());
                    sqlSession.insert("com.itrus.portal.db.EvidenceCertMapper.insert", cert);
                    configuration.setCert(cert.getId().toString());
                    configuration.setCertp7(racertinfo.getCertSignBufP7());
                    configuration.setCertSerialnumber(racertinfo.getCertSerialNumber());
                    boolean is = certificateService.setCertitifate(secretkey.getAlias(), racertinfo.getCertSignBuf(), racertinfo.getCertSignBufP7());
                    if (is) {
                        if ("0".equals(configuration.getArithmetic())) {
                            configuration.setArithmetic(null);
                        }
                        if (StringUtils.isNotEmpty(configuration.getSubject())) {
                            configuration.setSubject(configuration.getSubject());
                        }
                        String s = String.valueOf(secretkey.getId());
                        int keyid = Integer.parseInt(s);
                        configuration.setEvidenceSecretKey(keyid);
                        configuration.setCreateTime(new Date());
                        configuration.setCreator(getAdmin().getName());
                        certificateService.update(configuration);
                        configuration.setSignatureType(2);
                        String oper = "证书产生成功";
                        String info = "证书名称:" + configuration.getCertificateName();
                        LogUtil.evidencelog(sqlSession, null, oper, info);
                    }
                }
            } else {
                error = "csr产生失败";
                return "redirect:/certificate/" + configuration.getId() + "/" + 3;
            }
        }
    } else if (type == 3) {
        EvidenceCertificate econfiguration = certificateService.selectById(configuration.getId());
        EvidenceSecretKey secretkey = new EvidenceSecretKey();
        try {
            if (StringUtils.isNotEmpty(photoCodeImg)) {
                // 获取密钥信息
                secretkey = sqlSession.selectOne("com.itrus.portal.db.EvidenceSecretKeyMapper.selectByPrimaryKey", econfiguration.getEvidenceSecretKey());
                File imgDir = filePathUtils.getEnterpriseFile(secretkey.getAlias());
                File frontImg = filePathUtils.saveImg(imgDir, null, photoCodeImg, IMG_DEFAULT_TYPE, IMG_NAME_VERIFY);
                if (frontImg != null && frontImg.isFile()) {
                    configuration.setPhotoCodeImg(frontImg.getName());
                    configuration.setPhotoCodeImgHash(HMACSHA1.genSha1HashOfFile(frontImg));
                }
            }
            if (configuration.getId() == null) {
                certificateService.insert(configuration);
            } else {
                String s = String.valueOf(econfiguration.getEvidenceSecretKey());
                Long keyid = Long.valueOf(s);
                EvidenceSecretKey secret = secrtkey.selectById(keyid);
                boolean is = certificateService.setSeal(secret.getAlias(), photoCodeImg);
                if (is) {
                    certificateService.update(configuration);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            String oper = "新建签章失败";
            String info = "错误原因:" + e.toString();
            LogUtil.evidencelog(sqlSession, null, oper, info);
            error = "新建签章失败";
            return "redirect:/certificate/" + configuration.getId() + "/" + 2;
        }
    } else {
        // System.out.println("新建证书");
        EvidenceSecretKey secret = new EvidenceSecretKey();
        // List<EvidenceSecretKey> list = secrtkey.selecttype(configuration.getKeyType());
        // log.error("list="+list.size());
        // if(econfiguration.getIsCertificate()!=null && econfiguration.getIsCertificate()==1){
        /*if (list != null && list.size() != 0) {
					for (EvidenceSecretKey e : list) {
						EvidenceCertificate certificate = certificateService.selectone(e.getId());
						if (certificate == null) {
							secret.setAlias(e.getAlias());
							secret.setId(e.getId());
							break;
						}
					}
					if (StringUtils.isEmpty(secret.getAlias())) {
						List<EvidenceSecretKey> secter = secrtkey.certufucateInterface(getAdmin().getName());
						//List<EvidenceSecretKey> lists = secrtkey.selecttype(configuration.getKeyType());
						if (secter != null) {
							for (EvidenceSecretKey e : secter) {
								EvidenceCertificate certificate = certificateService.selectone(e.getId());
								if (certificate == null) {
									secret.setAlias(e.getAlias());
									secret.setId(e.getId());
									break;
								}
							}
						}
					}
				} else */
        {
            /*EvidenceSecretKey*/
            secret = secrtkey.certufucateInterface(getAdmin().getName(), configuration.getKeyType());
        // List<EvidenceSecretKey> lists = secrtkey.selecttype(configuration.getKeyType());
        /*if (secter != null) {
						for (EvidenceSecretKey e : secter) {
							EvidenceCertificate certificate = certificateService.selectone(e.getId());
							if (certificate == null) {
								secret.setAlias(e.getAlias());
								secret.setId(e.getId());
								break;
							}
						}
					}*/
        }
        /*	}else{
				 secret = sqlSession.selectOne(
						"com.itrus.portal.db.EvidenceSecretKeyMapper.selectByPrimaryKey",
						econfiguration.getEvidenceSecretKey());
			}*/
        JSONObject ret_data = null;
        ret_data = JSONObject.parseObject(configuration.getSubject());
        String cn = ret_data.getString("userName");
        String ou = ret_data.getString("userOrgunit");
        // String emali = ret_data.getString("EMAILADDRESS");
        String O = ret_data.getString("userOrganization");
        String dn = "CN=" + cn + "OU=" + ou + "O=" + O;
        log.error("Alias=" + secret.getAlias() + ",Arithmetic=" + configuration.getArithmetic());
        String csr = certificateService.genCsr(secret.getAlias(), configuration.getArithmetic(), dn);
        log.error("csr=" + csr);
        if (csr != null) {
            RaAccount ra = sqlSession.selectOne("com.itrus.portal.db.RaAccountMapper.selectByPrimaryKey", configuration.getRaAccount());
            CertUtlis certutil = new CertUtlis();
            // UserInfo rauserinfo = new UserInfo();
            // System.out.println(configuration.getSubject());
            // Weibo类在下边定义
            UserInfo rauserinfo = JSON.parseObject(configuration.getSubject(), UserInfo.class);
            // System.out.println(rauserinfo.getUserAdditionalField1()+rauserinfo.getUserName());
            /*rauserinfo.setUserName(cn);
				rauserinfo.setUserEmail(emali);
				rauserinfo.setUserAdditionalField1(ou);
				rauserinfo.setUserOrganization(O);*/
            // 证书有效期
            Integer certValidity = 7;
            if ("0".equals(configuration.getPeriod())) {
                certValidity = null;
            } else if (1 == configuration.getPeriod()) {
                certValidity = 365 * 1 + 1;
            } else if (3 == configuration.getPeriod()) {
                certValidity = 365 * 3 + 1;
            } else if (5 == configuration.getPeriod()) {
                certValidity = 365 * 5 + 2;
            } else if (10 == configuration.getPeriod()) {
                certValidity = 365 * 10 + 2;
            }
            // 调用RA
            CertInfo racertinfo = null;
            try {
                if (ra.getAaPassword() != null) {
                    ra.setAaPassword(AESencrp.decrypt(ra.getAaPassword(), dbEncKey));
                }
                racertinfo = certutil.enrollCertByWS(csr, ra, rauserinfo, certValidity);
                log.error("racertinfo=" + racertinfo);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                String oper = "RA调用失败";
                String info = "错误原因:" + e.toString();
                LogUtil.evidencelog(sqlSession, null, oper, info);
                uiModel.addAttribute("error", "RA调用失败");
                return "certificate/create";
            }
            if (racertinfo != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
                EvidenceCert cert = new EvidenceCert();
                cert.setCertBase64(racertinfo.getCertSignBuf());
                cert.setCreateTime(new Date());
                cert.setCertSerialnumber(racertinfo.getCertSerialNumber());
                cert.setIssuerdn(racertinfo.getCertIssuerDn());
                cert.setSubjectdn(racertinfo.getCertSubjectDn());
                cert.setCreateTime(new Date());
                try {
                    configuration.setStartTime(sdf.parse(racertinfo.getCertNotBefore()));
                    configuration.setEndTime(sdf.parse(racertinfo.getCertNotAfter()));
                    cert.setStartTime(sdf.parse(racertinfo.getCertNotBefore()));
                    cert.setEndTime(sdf.parse(racertinfo.getCertNotAfter()));
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                sqlSession.insert("com.itrus.portal.db.EvidenceCertMapper.insert", cert);
                configuration.setCert(cert.getId().toString());
                // configuration.setCert(racertinfo.getCertSignBuf());
                configuration.setCertp7(racertinfo.getCertSignBufP7());
                configuration.setCertSerialnumber(racertinfo.getCertSerialNumber());
                /*try {
						configuration.setStartTime(sdf.parse(racertinfo.getCertNotBefore()));
						configuration.setEndTime(sdf.parse(racertinfo.getCertNotAfter()));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}*/
                boolean is = certificateService.setCertitifate(secret.getAlias(), racertinfo.getCertSignBuf(), racertinfo.getCertSignBufP7());
                if (is) {
                    if ("0".equals(configuration.getArithmetic())) {
                        configuration.setArithmetic(null);
                    }
                    if (StringUtils.isNotEmpty(configuration.getSubject())) {
                        configuration.setSubject(configuration.getSubject());
                    }
                    String s = String.valueOf(secret.getId());
                    int keyid = Integer.parseInt(s);
                    configuration.setEvidenceSecretKey(keyid);
                    configuration.setCreateTime(new Date());
                    configuration.setCreator(getAdmin().getName());
                    certificateService.insert(configuration);
                    String oper = "证书产生成功";
                    String info = "证书名称:" + configuration.getCertificateName();
                    LogUtil.evidencelog(sqlSession, null, oper, info);
                    EvidenceCertificateExample certE = new EvidenceCertificateExample();
                    EvidenceCertificateExample.Criteria tificate = certE.createCriteria();
                    tificate.andEvidenceSecretKeyEqualTo(keyid);
                    configuration = sqlSession.selectOne("com.itrus.portal.db.EvidenceCertificateMapper.selectByExample", certE);
                }
            }
        } else {
            uiModel.addAttribute("error", "csr产生失败");
            return "certificate/create";
        }
    }
    return "redirect:/certificate/" + configuration.getId() + "/" + 1;
// return "redirect:/certificate/show/"+configuration.getId()+"/"+1;
// return "redirect:/certificate";
}
Also used : CertInfo(cn.topca.tca.ra.service.CertInfo) UserInfo(cn.topca.tca.ra.service.UserInfo) EvidenceSecretKey(com.itrus.portal.db.EvidenceSecretKey) ParseException(java.text.ParseException) IOException(java.io.IOException) Date(java.util.Date) CertUtlis(com.itrus.portal.utils.CertUtlis) EvidenceCert(com.itrus.portal.db.EvidenceCert) JSONObject(com.alibaba.fastjson.JSONObject) RaAccount(com.itrus.portal.db.RaAccount) EvidenceCertificateExample(com.itrus.portal.db.EvidenceCertificateExample) EvidenceCertificate(com.itrus.portal.db.EvidenceCertificate) ParseException(java.text.ParseException) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with CertUtlis

use of com.itrus.portal.utils.CertUtlis in project portal by ixinportal.

the class MobileWebSocketLogService method execute.

/**
 *	 service:certApply 申请证书移动端需要传过来的参数
 *	 1.设备信息 device
 *	 equipmentCategory	String	设备型号
 *	 appId	Long	应用的名称
 *	 operatingSystem	String	操作系统
 *	 userId	Long	和用户关联的字段
 *	 createTime	date	创建时间
 *	 updateTime	date	修改时间
 *	 deviceSerialNumber	string	设备序列号 唯一标识
 *	 2.用户信息 即 注册信息 user
 *	 name	String	用户姓名
 *	 phone	String	手机号
 *	 idNumber	String	身份证号
 *	 appId	Long	和应用关联的字段
 *	 email	String	电子邮件
 *	 registrationTime	 date	注册时间
 *	 createTime	date	创建时间
 *	 updateTime	 date	修改时间
 *	 userCode	String	用户编码
 *
 *	 最后保存 用户信息 设备信息 证书信息 // TODO: 2017/12/8
 * @param content
 * @return
 */
@Override
public Map<String, Object> execute(String content) {
    Map<String, Object> result = new HashMap<String, Object>(3);
    result.put("status", 0);
    try {
        JSONObject param = JSON.parseObject(content);
        String appId = param.getString("appId");
        ApplicationInfo applicationInfo = applicationInfoService.getApplicationInfo(appId);
        MobileApplyConfigManage mobileApplyConfig = mobileApplyConfigService.selectByappId(appId);
        CertUtlis certutil = new CertUtlis();
        JSONObject jsonUser = param.getJSONObject("user");
        // 根据证书信息项设置 rauserinfo对象 // TODO: 2017/12/26
        UserInfo rauserinfo = new UserInfo();
        rauserinfo.setUserName(jsonUser.getString("name"));
        rauserinfo.setUserEmail(jsonUser.getString("email"));
        rauserinfo.setUserAdditionalField1(applicationInfo.getName());
        rauserinfo.setUserAdditionalField4(jsonUser.getString("userCode"));
        Integer certValidity = 7;
        if (mobileApplyConfig.getCertificatedeadline() == null || "0".equals(mobileApplyConfig.getCertificatedeadline())) {
            certValidity = null;
        } else if (1 == mobileApplyConfig.getCertificatedeadline()) {
            certValidity = 365 * 1 + 1;
        } else if (2 == mobileApplyConfig.getCertificatedeadline()) {
            certValidity = 365 * 2 + 1;
        } else if (3 == mobileApplyConfig.getCertificatedeadline()) {
            certValidity = 365 * 3 + 1;
        } else if (4 == mobileApplyConfig.getCertificatedeadline()) {
            certValidity = 365 * 5 + 2;
        } else if (5 == mobileApplyConfig.getCertificatedeadline()) {
            certValidity = 365 * 10 + 2;
        }
        RaAccount ra = raAccountService.getRaAccount(mobileApplyConfig.getRaaccountId());
        if (ra.getAaPassword() != null) {
            ra.setAaPassword(AESencrp.decrypt(ra.getAaPassword(), dbEncKey));
        }
        CertInfo racertinfo = certutil.enrollCertByWS(param.getString("csr"), ra, rauserinfo, certValidity);
        result.put("status", 1);
        result.put("message", "制作证书成功");
    } catch (Exception e) {
        result.put("message", e.toString());
    }
    return result;
}
Also used : CertInfo(cn.topca.tca.ra.service.CertInfo) HashMap(java.util.HashMap) ApplicationInfo(com.itrus.portal.db.ApplicationInfo) UserInfo(cn.topca.tca.ra.service.UserInfo) MobileApplyConfigManage(com.itrus.portal.db.MobileApplyConfigManage) CertUtlis(com.itrus.portal.utils.CertUtlis) JSONObject(com.alibaba.fastjson.JSONObject) RaAccount(com.itrus.portal.db.RaAccount) JSONObject(com.alibaba.fastjson.JSONObject)

Example 4 with CertUtlis

use of com.itrus.portal.utils.CertUtlis in project portal by ixinportal.

the class CertUtilsTest method testEnrollCert.

@Test
public void testEnrollCert() throws MalformedURLException, RaServiceUnavailable_Exception {
    RaAccount raAccount = sqlsession.selectOne("com.itrus.portal.db.RaAccountMapper.selectByPrimaryKey", 1);
    InputStream in = EnrolCertTest.class.getResourceAsStream("/com/itrus/portal/utils/test/csr.pem");
    String csr = EnrolCertTest.stream2String(in, "UTF-8");
    CertUtlis cu = new CertUtlis();
// CertInfo certInfo = cu.enrollCertByWS(csr, raAccount, null);
// System.out.println(certInfo.getCertIssuerDn());
}
Also used : InputStream(java.io.InputStream) RaAccount(com.itrus.portal.db.RaAccount) CertUtlis(com.itrus.portal.utils.CertUtlis) Test(org.junit.Test)

Aggregations

CertUtlis (com.itrus.portal.utils.CertUtlis)4 UserInfo (cn.topca.tca.ra.service.UserInfo)3 RaAccount (com.itrus.portal.db.RaAccount)3 CertInfo (cn.topca.tca.ra.service.CertInfo)2 JSONObject (com.alibaba.fastjson.JSONObject)2 Test (org.junit.Test)2 ApplicationInfo (com.itrus.portal.db.ApplicationInfo)1 EvidenceCert (com.itrus.portal.db.EvidenceCert)1 EvidenceCertificate (com.itrus.portal.db.EvidenceCertificate)1 EvidenceCertificateExample (com.itrus.portal.db.EvidenceCertificateExample)1 EvidenceSecretKey (com.itrus.portal.db.EvidenceSecretKey)1 MobileApplyConfigManage (com.itrus.portal.db.MobileApplyConfigManage)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1