1 13 14 package org.ejbca.core.ejb.ca.store; 15 16 import java.math.BigInteger ; 17 import java.security.cert.Certificate ; 18 import java.security.cert.X509CRL ; 19 import java.security.cert.X509Certificate ; 20 import java.sql.Connection ; 21 import java.sql.PreparedStatement ; 22 import java.sql.ResultSet ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Date ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Random ; 31 32 import javax.ejb.CreateException ; 33 import javax.ejb.EJBException ; 34 import javax.ejb.FinderException ; 35 36 import org.apache.commons.lang.StringUtils; 37 import org.apache.log4j.Logger; 38 import org.ejbca.core.ejb.BaseSessionBean; 39 import org.ejbca.core.ejb.JNDINames; 40 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal; 41 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome; 42 import org.ejbca.core.ejb.ca.publisher.IPublisherSessionLocal; 43 import org.ejbca.core.ejb.ca.publisher.IPublisherSessionLocalHome; 44 import org.ejbca.core.ejb.log.ILogSessionLocal; 45 import org.ejbca.core.ejb.log.ILogSessionLocalHome; 46 import org.ejbca.core.ejb.protect.TableProtectSessionLocal; 47 import org.ejbca.core.ejb.protect.TableProtectSessionLocalHome; 48 import org.ejbca.core.model.InternalResources; 49 import org.ejbca.core.model.SecConst; 50 import org.ejbca.core.model.authorization.AuthorizationDeniedException; 51 import org.ejbca.core.model.ca.certificateprofiles.CACertificateProfile; 52 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfile; 53 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfileExistsException; 54 import org.ejbca.core.model.ca.certificateprofiles.EndUserCertificateProfile; 55 import org.ejbca.core.model.ca.certificateprofiles.HardTokenAuthCertificateProfile; 56 import org.ejbca.core.model.ca.certificateprofiles.HardTokenAuthEncCertificateProfile; 57 import org.ejbca.core.model.ca.certificateprofiles.HardTokenEncCertificateProfile; 58 import org.ejbca.core.model.ca.certificateprofiles.HardTokenSignCertificateProfile; 59 import org.ejbca.core.model.ca.certificateprofiles.OCSPSignerCertificateProfile; 60 import org.ejbca.core.model.ca.certificateprofiles.RootCACertificateProfile; 61 import org.ejbca.core.model.ca.crl.RevokedCertInfo; 62 import org.ejbca.core.model.ca.store.CRLInfo; 63 import org.ejbca.core.model.ca.store.CertReqHistory; 64 import org.ejbca.core.model.ca.store.CertificateInfo; 65 import org.ejbca.core.model.log.Admin; 66 import org.ejbca.core.model.log.LogConstants; 67 import org.ejbca.core.model.log.LogEntry; 68 import org.ejbca.core.model.protect.TableVerifyResult; 69 import org.ejbca.core.model.ra.UserDataVO; 70 import org.ejbca.util.CertTools; 71 import org.ejbca.util.JDBCUtil; 72 import org.ejbca.util.StringTools; 73 74 178 public class LocalCertificateStoreSessionBean extends BaseSessionBean { 179 180 181 private static final InternalResources intres = InternalResources.getInstance(); 182 183 186 private CertificateDataLocalHome certHome = null; 187 188 191 private CertificateProfileDataLocalHome certprofilehome = null; 192 193 196 private CRLDataLocalHome crlHome = null; 197 198 201 private CertReqHistoryDataLocalHome certReqHistoryHome = null; 202 203 204 207 private ILogSessionLocal logsession = null; 208 209 212 private IAuthorizationSessionLocal authorizationsession = null; 213 214 215 private TableProtectSessionLocalHome protecthome = null; 216 217 218 private boolean protect = false; 219 220 223 private IPublisherSessionLocal publishersession = null; 224 225 final private CertificateDataUtil.Adapter adapter; 226 227 public LocalCertificateStoreSessionBean() { 228 super(); 229 adapter = new MyAdapter(); 230 } 231 232 237 public void ejbCreate() throws CreateException { 238 crlHome = (CRLDataLocalHome) getLocator().getLocalHome(CRLDataLocalHome.COMP_NAME); 239 certHome = (CertificateDataLocalHome) getLocator().getLocalHome(CertificateDataLocalHome.COMP_NAME); 240 certReqHistoryHome = (CertReqHistoryDataLocalHome) getLocator().getLocalHome(CertReqHistoryDataLocalHome.COMP_NAME); 241 certprofilehome = (CertificateProfileDataLocalHome) getLocator().getLocalHome(CertificateProfileDataLocalHome.COMP_NAME); 242 String sign = getLocator().getString("java:comp/env/certSigning"); 243 if (StringUtils.equalsIgnoreCase(sign, "true")) { 244 protect = true; 245 protecthome = (TableProtectSessionLocalHome) getLocator().getLocalHome(TableProtectSessionLocalHome.COMP_NAME); 246 } 247 248 } 249 250 253 protected ILogSessionLocal getLogSession() { 254 if (logsession == null) { 255 try { 256 ILogSessionLocalHome home = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME); 257 logsession = home.create(); 258 } catch (Exception e) { 259 throw new EJBException (e); 260 } 261 } 262 return logsession; 263 } 265 266 271 private IAuthorizationSessionLocal getAuthorizationSession() { 272 if (authorizationsession == null) { 273 try { 274 IAuthorizationSessionLocalHome home = (IAuthorizationSessionLocalHome) getLocator().getLocalHome(IAuthorizationSessionLocalHome.COMP_NAME); 275 authorizationsession = home.create(); 276 } catch (Exception e) { 277 throw new EJBException (e); 278 } 279 } 280 return authorizationsession; 281 } 283 288 private IPublisherSessionLocal getPublisherSession() { 289 if (publishersession == null) { 290 try { 291 IPublisherSessionLocalHome home = (IPublisherSessionLocalHome) getLocator().getLocalHome(IPublisherSessionLocalHome.COMP_NAME); 292 publishersession = home.create(); 293 } catch (Exception e) { 294 throw new EJBException (e); 295 } 296 } 297 return publishersession; 298 } 300 301 313 public boolean storeCertificate(Admin admin, Certificate incert, String username, String cafp, 314 int status, int type) { 315 debug(">storeCertificate(" + cafp + ", " + status + ", " + type + ")"); 316 317 try { 318 username = StringTools.strip(username); 320 321 X509Certificate cert = (X509Certificate ) incert; 322 CertificateDataPK pk = new CertificateDataPK(); 323 pk.fingerprint = CertTools.getFingerprintAsString(cert); 324 CertificateDataLocal data1 = null; 325 data1 = certHome.create(cert); 326 data1.setUsername(username); 327 data1.setCaFingerprint(cafp); 328 data1.setStatus(status); 329 data1.setType(type); 330 String msg = intres.getLocalizedMessage("store.storecert"); 331 getLogSession().log(admin, cert, LogEntry.MODULE_CA, new java.util.Date (), username, (X509Certificate ) incert, LogEntry.EVENT_INFO_STORECERTIFICATE, msg); 332 if (protect) { 333 CertificateInfo entry = new CertificateInfo(data1.getFingerprint(), data1.getCaFingerprint(), data1.getSerialNumber(), data1.getIssuerDN(), data1.getSubjectDN(), data1.getStatus(), data1.getType(), data1.getExpireDate(), data1.getRevocationDate(), data1.getRevocationReason()); 334 TableProtectSessionLocal protect = protecthome.create(); 335 protect.protect(admin, entry); 336 } 337 } catch (Exception e) { 338 String msg = intres.getLocalizedMessage("store.errorstorecert"); 339 getLogSession().log(admin, (X509Certificate ) incert, LogEntry.MODULE_CA, new java.util.Date (), username, (X509Certificate ) incert, LogEntry.EVENT_ERROR_STORECERTIFICATE, msg); 340 throw new EJBException (e); 341 } 342 debug("<storeCertificate()"); 343 return true; 344 } 346 356 public boolean storeCRL(Admin admin, byte[] incrl, String cafp, int number) { 357 debug(">storeCRL(" + cafp + ", " + number + ")"); 358 359 try { 360 X509CRL crl = CertTools.getCRLfromByteArray(incrl); 361 CRLDataLocal data1 = crlHome.create(crl, number); 362 data1.setCaFingerprint(cafp); 363 String msg = intres.getLocalizedMessage("store.storecrl", new Integer (number), CertTools.getFingerprintAsString(crl)); 364 getLogSession().log(admin, crl.getIssuerDN().toString().hashCode(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_STORECRL, msg); 365 } catch (Exception e) { 366 String msg = intres.getLocalizedMessage("store.storecrl"); 367 getLogSession().log(admin, LogConstants.INTERNALCAID, LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_STORECRL, msg); 368 throw new EJBException (e); 369 } 370 debug("<storeCRL()"); 371 372 return true; 373 } 375 387 public Collection listAllCertificates(Admin admin, String issuerdn) { 388 debug(">listAllCertificates()"); 389 Connection con = null; 390 PreparedStatement ps = null; 391 ResultSet result = null; 392 String dn = CertTools.stringToBCDNString(issuerdn); 393 dn = StringTools.strip(dn); 394 try { 395 con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE); 396 ps = con.prepareStatement("select fingerprint, expireDate from CertificateData where issuerDN=? ORDER BY expireDate DESC"); 397 ps.setString(1, dn); 398 result = ps.executeQuery(); 399 ArrayList vect = new ArrayList (); 400 while (result.next()) { 401 vect.add(result.getString(1)); 402 } 403 debug("<listAllCertificates()"); 404 return vect; 405 } catch (Exception e) { 406 throw new EJBException (e); 407 } finally { 408 JDBCUtil.close(con, ps, result); 409 } 410 } 412 422 public Collection listRevokedCertificates(Admin admin, String issuerdn) { 423 debug(">listRevokedCertificates()"); 424 425 Connection con = null; 426 PreparedStatement ps = null; 427 ResultSet result = null; 428 String dn = CertTools.stringToBCDNString(issuerdn); 429 dn = StringTools.strip(dn); 430 try { 431 con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE); 435 ps = con.prepareStatement("select fingerprint from CertificateData where status=? and issuerDN=? ORDER BY expireDate DESC"); 436 ps.setInt(1, CertificateDataBean.CERT_REVOKED); 437 ps.setString(2, dn); 438 result = ps.executeQuery(); 439 ArrayList vect = new ArrayList (); 440 while (result.next()) { 441 vect.add(result.getString(1)); 442 } 443 debug("<listRevokedCertificates()"); 444 return vect; 445 } catch (Exception e) { 446 throw new EJBException (e); 447 } finally { 448 JDBCUtil.close(con, ps, result); 449 } 450 } 452 462 public Collection findCertificatesBySubjectAndIssuer(Admin admin, String subjectDN, String issuerDN) { 463 debug(">findCertificatesBySubjectAndIssuer(), dn='" + subjectDN + "' and issuer='" + issuerDN + "'"); 464 String dn = CertTools.stringToBCDNString(subjectDN); 466 dn = StringTools.strip(dn); 467 String issuerdn = CertTools.stringToBCDNString(issuerDN); 468 issuerdn = StringTools.strip(issuerdn); 469 debug("Looking for cert with (transformed)DN: " + dn); 470 try { 471 Collection coll = certHome.findBySubjectDNAndIssuerDN(dn, issuerdn); 472 Collection ret = new ArrayList (); 473 if (coll != null) { 474 Iterator iter = coll.iterator(); 475 while (iter.hasNext()) { 476 ret.add(((CertificateDataLocal) iter.next()).getCertificate()); 477 } 478 } 479 debug("<findCertificatesBySubjectAndIssuer(), dn='" + subjectDN + "' and issuer='" + issuerDN + "'"); 480 return ret; 481 } catch (javax.ejb.FinderException fe) { 482 throw new EJBException (fe); 483 } 484 } 486 494 public Collection findCertificatesBySubject(Admin admin, String subjectDN) { 495 debug(">findCertificatesBySubjectAndIssuer(), dn='" + subjectDN + "'"); 496 String dn = CertTools.stringToBCDNString(subjectDN); 498 dn = StringTools.strip(dn); 499 debug("Looking for cert with (transformed)DN: " + dn); 500 try { 501 Collection coll = certHome.findBySubjectDN(dn); 502 Collection ret = new ArrayList (); 503 if (coll != null) { 504 Iterator iter = coll.iterator(); 505 while (iter.hasNext()) { 506 ret.add(((CertificateDataLocal) iter.next()).getCertificate()); 507 } 508 } 509 debug("<findCertificatesBySubject(), dn='" + subjectDN + "'"); 510 return ret; 511 } catch (javax.ejb.FinderException fe) { 512 throw new EJBException (fe); 513 } 514 } 516 519 public Collection findCertificatesByExpireTime(Admin admin, Date expireTime) { 520 debug(">findCertificatesByExpireTime(), time=" + expireTime); 521 debug("Looking for certs that expire before: " + expireTime); 523 524 try { 525 Collection coll = certHome.findByExpireDate(expireTime.getTime()); 526 Collection ret = new ArrayList (); 527 528 if (coll != null) { 529 Iterator iter = coll.iterator(); 530 531 while (iter.hasNext()) { 532 ret.add(((CertificateDataLocal) iter.next()).getCertificate()); 533 } 534 } 535 debug("<findCertificatesByExpireTime(), time=" + expireTime); 536 return ret; 537 } catch (javax.ejb.FinderException fe) { 538 throw new EJBException (fe); 539 } 540 } 541 542 544 550 public Collection findCertificatesByExpireTimeWithLimit(Admin admin, Date expiretime) { 551 debug(">findCertificatesByExpireTimeWithLimit"); 552 553 Connection con = null; 554 PreparedStatement ps = null; 555 ResultSet result = null; 556 ArrayList returnval = new ArrayList (); 557 long currentdate = new Date ().getTime(); 558 559 try { 560 con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE); 561 ps = con.prepareStatement("SELECT DISTINCT username FROM CertificateData WHERE expireDate>=? AND expireDate<? AND status=?"); 562 ps.setLong(1, currentdate); 563 ps.setLong(2, expiretime.getTime()); 564 ps.setInt(3, CertificateDataBean.CERT_ACTIVE); 565 result = ps.executeQuery(); 566 while (result.next() && returnval.size() <= SecConst.MAXIMUM_QUERY_ROWCOUNT + 1) { 567 if (result.getString(1) != null && !result.getString(1).equals("")) 568 returnval.add(result.getString(1)); 569 } 570 debug("<findCertificatesByExpireTimeWithLimit()"); 571 return returnval; 572 } catch (Exception e) { 573 throw new EJBException (e); 574 } finally { 575 JDBCUtil.close(con, ps, result); 576 } 577 } 579 588 public Certificate findCertificateByIssuerAndSerno(Admin admin, String issuerDN, BigInteger serno) { 589 return CertificateDataUtil.findCertificateByIssuerAndSerno(admin, issuerDN, serno, certHome, adapter); 590 } 592 606 public Collection findCertificatesByIssuerAndSernos(Admin admin, String issuerDN, Collection sernos) { 607 debug(">findCertificateByIssuerAndSernos()"); 608 609 Connection con = null; 610 PreparedStatement ps = null; 611 ResultSet result = null; 612 ArrayList vect = null; 613 614 if (null == admin) { 615 throw new IllegalArgumentException (); 616 } 617 618 619 if (null == issuerDN || issuerDN.length() <= 0 620 || null == sernos || sernos.isEmpty()) { 621 return new ArrayList (); 622 } 623 624 String dn = CertTools.stringToBCDNString(issuerDN); 625 debug("Looking for cert with (transformed)DN: " + dn); 626 627 try { 628 629 final StringBuffer sb = new StringBuffer (); 630 { 631 Iterator iter = sernos.iterator(); 632 while (iter.hasNext()) { 633 sb.append(", '"); 634 BigInteger serno = (BigInteger ) iter.next(); 636 sb.append(serno.toString()); 637 sb.append("'"); 638 } 639 } 640 645 sb.delete(0, ", ".length()); 646 con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE); 647 ps = con.prepareStatement("SELECT DISTINCT fingerprint" 648 + " FROM CertificateData WHERE" 649 + " issuerDN = ?" 650 + " AND serialNumber IN (" + sb.toString() + ")"); 651 ps.setString(1, dn); 652 result = ps.executeQuery(); 653 654 vect = new ArrayList (); 655 while (result.next()) { 656 Certificate cert = findCertificateByFingerprint(admin, result.getString(1)); 657 if (cert != null) { 658 vect.add(cert); 659 } 660 } 661 662 debug("<findCertificateByIssuerAndSernos()"); 663 return vect; 664 } catch (Exception fe) { 665 throw new EJBException (fe); 666 } finally { 667 JDBCUtil.close(con, ps, result); 668 } 669 } 671 679 public Collection findCertificatesBySerno(Admin admin, BigInteger serno) { 680 debug(">findCertificateBySerno(), serno=" + serno); 681 try { 682 Collection coll = certHome.findBySerialNumber(serno.toString()); 683 ArrayList ret = new ArrayList (); 684 685 if (coll != null) { 686 Iterator iter = coll.iterator(); 687 688 while (iter.hasNext()) { 689 ret.add(((CertificateDataLocal) iter.next()).getCertificate()); 690 } 691 } 692 693 debug("<findCertificateBySerno(), serno=" + serno); 694 695 return ret; 696 } catch (javax.ejb.FinderException fe) { 697 throw new EJBException (fe); 698 } 699 } 701 709 public String findUsernameByCertSerno(Admin admin, BigInteger serno, String issuerdn) { 710 if (log.isDebugEnabled()) { 711 debug(">findUsernameByCertSerno(), serno: " + serno.toString(16) + ", issuerdn: " + issuerdn); 712 } 713 String dn = CertTools.stringToBCDNString(issuerdn); 714 try { 715 Collection coll = certHome.findByIssuerDNSerialNumber(dn, serno.toString()); 716 String ret = null; 717 718 if (coll != null) { 719 Iterator iter = coll.iterator(); 720 while (iter.hasNext()) { 721 ret = ((CertificateDataLocal) iter.next()).getUsername(); 722 } 723 } 724 debug("<findUsernameByCertSerno(), ret=" + ret); 725 return ret; 726 } catch (javax.ejb.FinderException fe) { 727 throw new EJBException (fe); 728 } 729 } 731 739 public Collection findCertificatesByUsername(Admin admin, String username) { 740 debug(">findCertificateBySerno(), username=" + username); 741 742 try { 743 username = StringTools.strip(username); 745 746 Collection coll = certHome.findByUsername(username); 747 ArrayList ret = new ArrayList (); 748 749 if (coll != null) { 750 Iterator iter = coll.iterator(); 751 while (iter.hasNext()) { 752 ret.add(((CertificateDataLocal) iter.next()).getCertificate()); 753 } 754 } 755 756 debug("<findCertificateBySerno(), username=" + username); 757 return ret; 758 } catch (javax.ejb.FinderException fe) { 759 throw new EJBException (fe); 760 } 761 } 763 766 public CertificateInfo getCertificateInfo(Admin admin, String fingerprint) { 767 debug(">getCertificateInfo()"); 768 CertificateInfo ret = null; 769 770 try { 771 CertificateDataLocal res = certHome.findByPrimaryKey(new CertificateDataPK(fingerprint)); 772 ret = new CertificateInfo(res.getFingerprint(), res.getCaFingerprint(), res.getSerialNumber(), res.getIssuerDN(), res.getSubjectDN(), 773 res.getStatus(), res.getType(), res.getExpireDate(), res.getRevocationDate(), res.getRevocationReason()); 774 debug("<getCertificateInfo()"); 775 } catch (FinderException fe) { 776 } catch (Exception e) { 778 String msg = intres.getLocalizedMessage("store.errorcertinfo", fingerprint); 779 log.error(msg); 780 throw new EJBException (e); 781 } 782 return ret; 783 } 785 788 public Certificate findCertificateByFingerprint(Admin admin, String fingerprint) { 789 return CertificateDataUtil.findCertificateByFingerprint(admin, fingerprint, certHome, adapter); 790 } 792 860 public Collection findCertificatesByType(Admin admin, int type, String issuerDN) { 861 return CertificateDataUtil.findCertificatesByType(admin, type, issuerDN, certHome, adapter); 862 } 864 875 public void setRevokeStatus(Admin admin, String username, Collection publishers, int reason) { 876 debug(">setRevokeStatus(), username=" + username); 877 username = StringTools.strip(username); 879 try { 880 Collection certs = findCertificatesByUsername(admin, username); 881 if (!certs.isEmpty()) { 883 Iterator j = certs.iterator(); 884 while (j.hasNext()) { 885 setRevokeStatus(admin, (X509Certificate ) j.next(), publishers, reason); 886 } 887 } 888 } catch (FinderException e) { 889 String msg = intres.getLocalizedMessage("store.errorfindcertuser", username); 890 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_REVOKEDCERT, msg); 891 throw new EJBException (e); 892 } 893 debug("<setRevokeStatus(), username=" + username); 894 } 896 906 public void setRevokeStatus(Admin admin, String issuerdn, BigInteger serno, Collection publishers, int reason) { 907 debug(">setRevokeStatus(), issuerdn=" + issuerdn + ", serno=" + serno); 908 X509Certificate certificate = null; 909 try { 910 certificate = (X509Certificate ) this.findCertificateByIssuerAndSerno(admin, issuerdn, serno); 911 setRevokeStatus(admin, certificate, publishers, reason); 912 } catch (FinderException e) { 913 String msg = intres.getLocalizedMessage("store.errorfindcertserno", serno.toString(16)); 914 getLogSession().log(admin, issuerdn.hashCode(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_REVOKEDCERT, msg); 915 throw new EJBException (e); 916 } 917 debug("<setRevokeStatus(), issuerdn=" + issuerdn + ", serno=" + serno); 918 } 920 929 private void setRevokeStatus(Admin admin, X509Certificate certificate, Collection publishers, int reason) throws FinderException { 930 if (certificate == null) { 931 return; 932 } 933 debug(">setRevokeStatus(X509Certificate), issuerdn=" + certificate.getIssuerDN() + ", serno=" + certificate.getSerialNumber()); 934 935 if (certificate != null) { 936 CertificateDataPK revpk = new CertificateDataPK(); 937 revpk.fingerprint = CertTools.getFingerprintAsString(certificate); 938 CertificateDataLocal rev = certHome.findByPrimaryKey(revpk); 939 String serialNo = certificate.getSerialNumber().toString(16); if ( (rev.getStatus() != CertificateDataBean.CERT_REVOKED) 941 && (reason != RevokedCertInfo.NOT_REVOKED) && (reason != RevokedCertInfo.REVOKATION_REASON_REMOVEFROMCRL) ) { 942 rev.setStatus(CertificateDataBean.CERT_REVOKED); 943 rev.setRevocationDate(new Date ()); 944 rev.setRevocationReason(reason); 945 String msg = intres.getLocalizedMessage("store.revokedcert", new Integer (reason)); 946 getLogSession().log(admin, certificate, LogEntry.MODULE_CA, new java.util.Date (), null, certificate, LogEntry.EVENT_INFO_REVOKEDCERT, msg); 947 if (publishers != null) { 949 getPublisherSession().revokeCertificate(admin, publishers, certificate, reason); 950 } 951 } else if ( ((reason == RevokedCertInfo.NOT_REVOKED) || (reason == RevokedCertInfo.REVOKATION_REASON_REMOVEFROMCRL)) 952 && (rev.getRevocationReason() == RevokedCertInfo.REVOKATION_REASON_CERTIFICATEHOLD) ) { 953 rev.setStatus(CertificateDataBean.CERT_ACTIVE); 955 rev.setRevocationDate(null); 956 rev.setRevocationReason(RevokedCertInfo.NOT_REVOKED); 957 try { 960 CertReqHistory certreqhist = getCertReqHistory(admin, certificate.getSerialNumber(), certificate.getIssuerDN().getName()); 961 if(certreqhist == null){ 962 throw new Exception ("Unrevoked cert:" + serialNo + " reason: " + reason + " Must not be republished."); 963 } 964 UserDataVO userdata = certreqhist.getUserDataVO(); 965 if ( userdata == null ){ 966 throw new Exception ("Unrevoked cert:" + serialNo + " reason: " + reason + " Could not be republished, there ane no UserData in History."); 967 } 968 CertificateProfile certprofile = getCertificateProfile(admin, userdata.getCertificateProfileId()); 969 if(certprofile == null){ 970 throw new Exception ("Unrevoked cert:" + serialNo + " reason: " + reason + " Could not be republished, can't find certificate profile."); 971 } 972 CertificateInfo certinfo = getCertificateInfo(admin, CertTools.getFingerprintAsString(certificate)); 973 if(certprofile.getPublisherList().size() <= 0){ 974 throw new Exception ("Unrevoked cert:" + serialNo + " reason: " + reason + " Could not be republished, there are no publishers defined."); 975 } 976 boolean published = publishersession.storeCertificate(admin, certprofile.getPublisherList(), certificate, certreqhist.getUserDataVO().getUsername(), certreqhist.getUserDataVO().getPassword(), 977 certinfo.getCAFingerprint(), certinfo.getStatus() , certinfo.getType(), certinfo.getRevocationDate().getTime(), certinfo.getRevocationReason(), certreqhist.getUserDataVO().getExtendedinformation()); 978 if ( !published ) { 979 throw new Exception ("Unrevoked cert:" + serialNo + " reason: " + reason + " Could not be republished."); 980 } 981 String msg = intres.getLocalizedMessage("store.republishunrevokedcert", new Integer (reason)); 982 getLogSession().log(admin, certificate.getIssuerDN().hashCode(), LogEntry.MODULE_CA, new java.util.Date (), null, certificate, LogEntry.EVENT_INFO_NOTIFICATION, msg); 983 } catch (Exception ex) { 984 getLogSession().log(admin, certificate.getIssuerDN().hashCode(), LogEntry.MODULE_CA, new java.util.Date (), null, certificate, LogEntry.EVENT_INFO_NOTIFICATION, ex.getMessage()); 986 } 987 } else { 988 String msg = intres.getLocalizedMessage("store.ignorerevoke", serialNo, new Integer (rev.getStatus()), new Integer (reason)); 989 getLogSession().log(admin, certificate.getIssuerDN().hashCode(), LogEntry.MODULE_CA, new java.util.Date (), null, certificate, LogEntry.EVENT_INFO_NOTIFICATION, msg); 990 } 991 if (protect) { 993 CertificateInfo entry = new CertificateInfo(rev.getFingerprint(), rev.getCaFingerprint(), rev.getSerialNumber(), rev.getIssuerDN(), rev.getSubjectDN(), rev.getStatus(), rev.getType(), rev.getExpireDate(), rev.getRevocationDate(), rev.getRevocationReason()); 994 TableProtectSessionLocal protect; 995 try { 996 protect = protecthome.create(); 997 protect.protect(admin, entry); 998 } catch (CreateException e) { 999 String msg = intres.getLocalizedMessage("protect.errorcreatesession"); 1000 error(msg, e); 1001 } 1002 } 1003 1004 } 1005 1006 debug("<setRevokeStatus(), issuerdn=" + certificate.getIssuerDN() + ", serno=" + certificate.getSerialNumber()); 1007 } 1009 1017 public void revokeCertificate(Admin admin, Certificate cert, Collection publishers, int reason) { 1018 if (cert instanceof X509Certificate ) { 1019 setRevokeStatus(admin, ((X509Certificate ) cert).getIssuerDN().toString(), ((X509Certificate ) cert).getSerialNumber(), publishers, reason); 1020 } 1021 } 1023 1033 public void revokeAllCertByCA(Admin admin, String issuerdn, int reason) { 1034 Connection con = null; 1035 PreparedStatement ps = null; 1036 PreparedStatement ps2 = null; 1037 int temprevoked = 0; 1038 int revoked = 0; 1039 1040 String bcdn = CertTools.stringToBCDNString(issuerdn); 1041 1042 final String firstsqlstatement = "UPDATE CertificateData SET status=?" + 1043 " WHERE issuerDN=? AND status = ? "; 1044 final String secondsqlstatement = "UPDATE CertificateData SET status=?, revocationDate=?, revocationReason=?" + 1045 " WHERE issuerDN=? AND status <> ?"; 1046 1047 long currentdate = new Date ().getTime(); 1048 1049 try { 1050 con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE); 1052 ps = con.prepareStatement(firstsqlstatement); 1053 ps.setInt(1, CertificateDataBean.CERT_REVOKED); ps.setString(2, bcdn); ps.setInt(3, CertificateDataBean.CERT_TEMP_REVOKED); temprevoked = ps.executeUpdate(); 1057 1058 ps2 = con.prepareStatement(secondsqlstatement); 1060 ps2.setInt(1, CertificateDataBean.CERT_REVOKED); ps2.setLong(2, currentdate); ps2.setInt(3, reason); ps2.setString(4, bcdn); ps2.setInt(5, CertificateDataBean.CERT_REVOKED); 1066 revoked = ps2.executeUpdate(); 1067 1068 String msg = intres.getLocalizedMessage("store.revokedallbyca", issuerdn, new Integer (revoked + temprevoked), new Integer (reason)); 1069 getLogSession().log(admin, bcdn.hashCode(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_REVOKEDCERT, msg); 1070 } catch (Exception e) { 1071 String msg = intres.getLocalizedMessage("store.errorrevokeallbyca", issuerdn); 1072 getLogSession().log(admin, bcdn.hashCode(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_REVOKEDCERT, msg, e); 1073 throw new EJBException (e); 1074 } finally { 1075 JDBCUtil.close(con, ps, null); 1076 JDBCUtil.close(ps2); 1077 } 1078 } 1080 1088 public boolean checkIfAllRevoked(Admin admin, String username) { 1089 boolean returnval = true; 1090 X509Certificate certificate = null; 1091 username = StringTools.strip(username); 1093 try { 1094 Collection certs = findCertificatesByUsername(admin, username); 1095 if (!certs.isEmpty()) { 1097 Iterator j = certs.iterator(); 1098 while (j.hasNext()) { 1099 CertificateDataPK revpk = new CertificateDataPK(); 1100 certificate = (X509Certificate ) j.next(); 1101 revpk.fingerprint = CertTools.getFingerprintAsString(certificate); 1102 CertificateDataLocal rev = certHome.findByPrimaryKey(revpk); 1103 if (protect) { 1104 CertificateInfo entry = new CertificateInfo(rev.getFingerprint(), rev.getCaFingerprint(), rev.getSerialNumber(), rev.getIssuerDN(), rev.getSubjectDN(), rev.getStatus(), rev.getType(), rev.getExpireDate(), rev.getRevocationDate(), rev.getRevocationReason()); 1105 TableProtectSessionLocal protect; 1106 try { 1107 protect = protecthome.create(); 1108 TableVerifyResult res = protect.verify(entry); 1110 if (res.getResultCode() != TableVerifyResult.VERIFY_SUCCESS) { 1111 } 1113 } catch (CreateException e) { 1114 String msg = intres.getLocalizedMessage("protect.errorcreatesession"); 1115 error(msg, e); 1116 } 1117 } 1118 if (rev.getStatus() != CertificateDataBean.CERT_REVOKED) { 1119 returnval = false; 1120 } 1121 } 1122 } 1123 1124 } catch (FinderException e) { 1125 throw new EJBException (e); 1126 } 1127 1128 return returnval; 1129 } 1130 1131 1140 public RevokedCertInfo isRevoked(Admin admin, String issuerDN, BigInteger serno) { 1141 return CertificateDataUtil.isRevoked(admin, issuerDN, serno, certHome, protecthome, adapter); 1142 } 1144 1152 public byte[] getLastCRL(Admin admin, String issuerdn) { 1153 debug(">getLastCRL(" + issuerdn + ")"); 1154 1155 try { 1156 int maxnumber = getLastCRLNumber(admin, issuerdn); 1157 X509CRL crl = null; 1158 try { 1159 CRLDataLocal data = crlHome.findByIssuerDNAndCRLNumber(issuerdn, maxnumber); 1160 crl = data.getCRL(); 1161 } catch (FinderException e) { 1162 crl = null; 1163 } 1164 debug("<getLastCRL()"); 1165 if (crl == null) 1166 return null; 1167 1168 String msg = intres.getLocalizedMessage("store.getcrl", issuerdn, new Integer (maxnumber)); 1169 getLogSession().log(admin, crl.getIssuerDN().toString().hashCode(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_GETLASTCRL, msg); 1170 return crl.getEncoded(); 1171 } catch (Exception e) { 1172 String msg = intres.getLocalizedMessage("store.errorgetcrl", issuerdn); 1173 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_GETLASTCRL, msg); 1174 throw new EJBException (e); 1175 } 1176 } 1178 1186 public CRLInfo getLastCRLInfo(Admin admin, String issuerdn) { 1187 debug(">getLastCRLInfo(" + issuerdn + ")"); 1188 try { 1189 int maxnumber = getLastCRLNumber(admin, issuerdn); 1190 CRLInfo crlinfo = null; 1191 try { 1192 CRLDataLocal data = crlHome.findByIssuerDNAndCRLNumber(issuerdn, maxnumber); 1193 crlinfo = new CRLInfo(data.getIssuerDN(), maxnumber, data.getThisUpdate(), data.getNextUpdate()); 1194 } catch (FinderException e) { 1195 String msg = intres.getLocalizedMessage("store.errorgetcrl", issuerdn, new Integer (maxnumber)); 1196 log.error(msg, e); 1197 crlinfo = null; 1198 } 1199 debug("<getLastCRLInfo()"); 1200 return crlinfo; 1201 } catch (Exception e) { 1202 String msg = intres.getLocalizedMessage("store.errorgetcrlinfo", issuerdn); 1203 getLogSession().log(admin, issuerdn.hashCode(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_GETLASTCRL, msg); 1204 throw new EJBException (e); 1205 } 1206 } 1208 1215 public int getLastCRLNumber(Admin admin, String issuerdn) { 1216 debug(">getLastCRLNumber(" + issuerdn + ")"); 1217 1218 Connection con = null; 1219 PreparedStatement ps = null; 1220 ResultSet result = null; 1221 try { 1222 con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE); 1223 ps = con.prepareStatement("select MAX(CRLNumber) from CRLData where issuerDN=?"); 1224 ps.setString(1, issuerdn); 1225 result = ps.executeQuery(); 1226 1227 int maxnumber = 0; 1228 if (result.next()) 1229 maxnumber = result.getInt(1); 1230 debug("<getLastCRLNumber(" + maxnumber + ")"); 1231 1232 return maxnumber; 1233 } catch (Exception e) { 1234 throw new EJBException (e); 1235 } finally { 1236 JDBCUtil.close(con, ps, result); 1237 } 1238 } 1240 1249 public void addCertReqHistoryData(Admin admin, Certificate certificate, UserDataVO useradmindata){ 1250 X509Certificate cert = (X509Certificate ) certificate; 1251 debug(">addCertReqHistData(" + cert.getSerialNumber() + ", " + cert.getIssuerDN() + ", " + useradmindata.getUsername() + ")"); 1252 try { 1253 CertReqHistoryDataPK pk = new CertReqHistoryDataPK(); 1254 pk.fingerprint = CertTools.getFingerprintAsString(cert); 1255 certReqHistoryHome.create(cert,useradmindata); 1256 String msg = intres.getLocalizedMessage("store.storehistory", useradmindata.getUsername()); 1257 getLogSession().log(admin, cert, LogEntry.MODULE_CA, new java.util.Date (), useradmindata.getUsername(), cert, LogEntry.EVENT_INFO_STORECERTIFICATE, msg); 1258 } catch (Exception e) { 1259 String msg = intres.getLocalizedMessage("store.errorstorehistory", useradmindata.getUsername()); 1260 getLogSession().log(admin, cert, LogEntry.MODULE_CA, new java.util.Date (), useradmindata.getUsername(), cert, LogEntry.EVENT_ERROR_STORECERTIFICATE, msg); 1261 throw new EJBException (e); 1262 } 1263 debug("<addCertReqHistData()"); 1264 } 1265 1266 1273 public void removeCertReqHistoryData(Admin admin, String certFingerprint){ 1274 debug(">removeCertReqHistData(" + certFingerprint + ")"); 1275 try { 1276 CertReqHistoryDataPK pk = new CertReqHistoryDataPK(); 1277 pk.fingerprint = certFingerprint; 1278 String msg = intres.getLocalizedMessage("store.removehistory", certFingerprint); 1279 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_STORECERTIFICATE, msg); 1280 this.certReqHistoryHome.remove(pk); 1281 } catch (Exception e) { 1282 String msg = intres.getLocalizedMessage("store.errorremovehistory", certFingerprint); 1283 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_STORECERTIFICATE, msg); 1284 throw new EJBException (e); 1285 } 1286 debug("<removeCertReqHistData()"); 1287 } 1288 1289 1298 public CertReqHistory getCertReqHistory(Admin admin, BigInteger certificateSN, String issuerDN){ 1299 CertReqHistory retval = null; 1300 1301 try{ 1302 Collection result = certReqHistoryHome.findByIssuerDNSerialNumber(issuerDN, certificateSN.toString()); 1303 if(result.iterator().hasNext()) 1304 retval = ((CertReqHistoryDataLocal) result.iterator().next()).getCertReqHistory(); 1305 }catch(FinderException fe){ 1306 } 1308 1309 return retval; 1310 } 1311 1312 1313 1320 public List getCertReqHistory(Admin admin, String username){ 1321 ArrayList retval = new ArrayList (); 1322 1323 try{ 1324 Collection result = certReqHistoryHome.findByUsername(username); 1325 Iterator iter = result.iterator(); 1326 while(iter.hasNext()){ 1327 retval.add(((CertReqHistoryDataLocal) iter.next()).getCertReqHistory()); 1328 } 1329 }catch(FinderException fe){ 1330 } 1332 1333 return retval; 1334 } 1335 1336 1337 1346 public void addCertificateProfile(Admin admin, String certificateprofilename, 1347 CertificateProfile certificateprofile) throws CertificateProfileExistsException { 1348 addCertificateProfile(admin, findFreeCertificateProfileId(), certificateprofilename, certificateprofile); 1349 } 1351 1361 public void addCertificateProfile(Admin admin, int certificateprofileid, String certificateprofilename, 1362 CertificateProfile certificateprofile) throws CertificateProfileExistsException { 1363 if (isCertificateProfileNameFixed(certificateprofilename)) { 1364 String msg = intres.getLocalizedMessage("store.errorcertprofilefixed", certificateprofilename); 1365 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_CERTPROFILE, msg); 1366 throw new CertificateProfileExistsException(msg); 1367 } 1368 1369 if (isFreeCertificateProfileId(certificateprofileid)) { 1370 try { 1371 certprofilehome.findByCertificateProfileName(certificateprofilename); 1372 String msg = intres.getLocalizedMessage("store.errorcertprofileexists", certificateprofilename); 1373 throw new CertificateProfileExistsException(msg); 1374 } catch (FinderException e) { 1375 try { 1376 certprofilehome.create(new Integer (certificateprofileid), certificateprofilename, 1377 certificateprofile); 1378 String msg = intres.getLocalizedMessage("store.addedcertprofile", certificateprofilename); 1379 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_CERTPROFILE, msg); 1380 } catch (Exception f) { 1381 String msg = intres.getLocalizedMessage("store.errorcreatecertprofile", certificateprofilename); 1382 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_CERTPROFILE, msg); 1383 } 1384 } 1385 } 1386 } 1388 1397 public void cloneCertificateProfile(Admin admin, String originalcertificateprofilename, String newcertificateprofilename) throws CertificateProfileExistsException { 1398 CertificateProfile certificateprofile = null; 1399 1400 if (isCertificateProfileNameFixed(newcertificateprofilename)) { 1401 String msg = intres.getLocalizedMessage("store.errorcertprofilefixed", newcertificateprofilename); 1402 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_CERTPROFILE, msg); 1403 throw new CertificateProfileExistsException(msg); 1404 } 1405 1406 try { 1407 certificateprofile = (CertificateProfile) getCertificateProfile(admin, originalcertificateprofilename).clone(); 1408 1409 boolean issuperadministrator = false; 1410 try { 1411 issuperadministrator = getAuthorizationSession().isAuthorizedNoLog(admin, "/super_administrator"); 1412 } catch (AuthorizationDeniedException ade) { 1413 } 1414 1415 if (!issuperadministrator && certificateprofile.isApplicableToAnyCA()) { 1416 Collection authcas = getAuthorizationSession().getAuthorizedCAIds(admin); 1418 certificateprofile.setAvailableCAs(authcas); 1419 } 1420 1421 try { 1422 certprofilehome.findByCertificateProfileName(newcertificateprofilename); 1423 String msg = intres.getLocalizedMessage("store.erroraddprofilewithtempl", newcertificateprofilename, originalcertificateprofilename); 1424 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_CERTPROFILE, msg); 1425 throw new CertificateProfileExistsException(); 1426 } catch (FinderException e) { 1427 try { 1428 certprofilehome.create(new Integer (findFreeCertificateProfileId()), newcertificateprofilename, certificateprofile); 1429 String msg = intres.getLocalizedMessage("store.addedprofilewithtempl", newcertificateprofilename, originalcertificateprofilename); 1430 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_CERTPROFILE, msg); 1431 } catch (CreateException f) { 1432 } 1433 } 1434 } catch (CloneNotSupportedException f) { 1435 } 1436 1437 } 1439 1446 public void removeCertificateProfile(Admin admin, String certificateprofilename) { 1447 try { 1448 CertificateProfileDataLocal pdl = certprofilehome.findByCertificateProfileName(certificateprofilename); 1449 pdl.remove(); 1450 String msg = intres.getLocalizedMessage("store.removedprofile", certificateprofilename); 1451 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_CERTPROFILE, msg); 1452 } catch (Exception e) { 1453 String msg = intres.getLocalizedMessage("store.errorremoveprofile", certificateprofilename); 1454 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_CERTPROFILE, msg); 1455 } 1456 } 1458 1464 public void renameCertificateProfile(Admin admin, String oldcertificateprofilename, String newcertificateprofilename) throws CertificateProfileExistsException { 1465 if (isCertificateProfileNameFixed(newcertificateprofilename)) { 1466 String msg = intres.getLocalizedMessage("store.errorcertprofilefixed", newcertificateprofilename); 1467 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_CERTPROFILE, msg); 1468 throw new CertificateProfileExistsException(msg); 1469 } 1470 if (isCertificateProfileNameFixed(oldcertificateprofilename)) { 1471 String msg = intres.getLocalizedMessage("store.errorcertprofilefixed", oldcertificateprofilename); 1472 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_CERTPROFILE, msg); 1473 throw new CertificateProfileExistsException(msg); 1474 } 1475 1476 try { 1477 certprofilehome.findByCertificateProfileName(newcertificateprofilename); 1478 String msg = intres.getLocalizedMessage("store.errorcertprofileexists", newcertificateprofilename); 1479 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_CERTPROFILE, msg); 1480 throw new CertificateProfileExistsException(); 1481 } catch (FinderException e) { 1482 try { 1483 CertificateProfileDataLocal pdl = certprofilehome.findByCertificateProfileName(oldcertificateprofilename); 1484 pdl.setCertificateProfileName(newcertificateprofilename); 1485 String msg = intres.getLocalizedMessage("store.renamedprofile", oldcertificateprofilename, newcertificateprofilename); 1486 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_CERTPROFILE, msg); 1487 } catch (FinderException f) { 1488 String msg = intres.getLocalizedMessage("store.errorrenameprofile", oldcertificateprofilename, newcertificateprofilename); 1489 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_CERTPROFILE, msg); 1490 } 1491 } 1492 } 1494 1501 public void changeCertificateProfile(Admin admin, String certificateprofilename, CertificateProfile certificateprofile) { 1502 try { 1503 CertificateProfileDataLocal pdl = certprofilehome.findByCertificateProfileName(certificateprofilename); 1504 pdl.setCertificateProfile(certificateprofile); 1505 String msg = intres.getLocalizedMessage("store.editedprofile", certificateprofilename); 1506 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_CERTPROFILE, msg); 1507 } catch (FinderException e) { 1508 String msg = intres.getLocalizedMessage("store.erroreditprofile", certificateprofilename); 1509 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_CERTPROFILE, msg); 1510 } 1511 } 1513 1522 public Collection getAuthorizedCertificateProfileIds(Admin admin, int certprofiletype) { 1523 ArrayList returnval = new ArrayList (); 1524 Collection result = null; 1525 1526 HashSet authorizedcaids = new HashSet (getAuthorizationSession().getAuthorizedCAIds(admin)); 1527 1528 if (certprofiletype == 0 || certprofiletype == CertificateDataBean.CERTTYPE_ENDENTITY || certprofiletype == CertificateDataBean.CERTTYPE_HARDTOKEN){ 1530 returnval.add(new Integer (SecConst.CERTPROFILE_FIXED_ENDUSER)); 1531 returnval.add(new Integer (SecConst.CERTPROFILE_FIXED_OCSPSIGNER)); 1532 } 1533 if (certprofiletype == 0 || certprofiletype == CertificateDataBean.CERTTYPE_SUBCA) 1534 returnval.add(new Integer (SecConst.CERTPROFILE_FIXED_SUBCA)); 1535 if (certprofiletype == 0 || certprofiletype == CertificateDataBean.CERTTYPE_ROOTCA) 1536 returnval.add(new Integer (SecConst.CERTPROFILE_FIXED_ROOTCA)); 1537 1538 if (certprofiletype == 0 || certprofiletype == CertificateDataBean.CERTTYPE_HARDTOKEN) { 1539 returnval.add(new Integer (SecConst.CERTPROFILE_FIXED_HARDTOKENAUTH)); 1540 returnval.add(new Integer (SecConst.CERTPROFILE_FIXED_HARDTOKENAUTHENC)); 1541 returnval.add(new Integer (SecConst.CERTPROFILE_FIXED_HARDTOKENENC)); 1542 returnval.add(new Integer (SecConst.CERTPROFILE_FIXED_HARDTOKENSIGN)); 1543 } 1544 1545 try { 1546 result = certprofilehome.findAll(); 1547 Iterator i = result.iterator(); 1548 while (i.hasNext()) { 1549 CertificateProfileDataLocal next = (CertificateProfileDataLocal) i.next(); 1550 CertificateProfile profile = next.getCertificateProfile(); 1551 if (certprofiletype == 0 || certprofiletype == profile.getType() 1553 || (profile.getType() == CertificateDataBean.CERTTYPE_ENDENTITY && 1554 certprofiletype == CertificateDataBean.CERTTYPE_HARDTOKEN)) { 1555 Iterator availablecas = profile.getAvailableCAs().iterator(); 1556 boolean allexists = true; 1557 while (availablecas.hasNext()) { 1558 Integer nextcaid = (Integer ) availablecas.next(); 1559 if (nextcaid.intValue() == CertificateProfile.ANYCA) { 1560 allexists = true; 1561 break; 1562 } 1563 1564 if (!authorizedcaids.contains(nextcaid)) { 1565 allexists = false; 1566 break; 1567 } 1568 } 1569 1570 if (allexists) 1571 returnval.add(next.getId()); 1572 } 1573 } 1574 } catch (FinderException e) { 1575 } 1576 return returnval; 1577 } 1579 1580 1586 public HashMap getCertificateProfileIdToNameMap(Admin admin) { 1587 HashMap returnval = new HashMap (); 1588 Collection result = null; 1589 returnval.put(new Integer (SecConst.CERTPROFILE_FIXED_ENDUSER), 1590 EndUserCertificateProfile.CERTIFICATEPROFILENAME); 1591 returnval.put(new Integer (SecConst.CERTPROFILE_FIXED_SUBCA), 1592 CACertificateProfile.CERTIFICATEPROFILENAME); 1593 returnval.put(new Integer (SecConst.CERTPROFILE_FIXED_ROOTCA), 1594 RootCACertificateProfile.CERTIFICATEPROFILENAME); 1595 1596 returnval.put(new Integer (SecConst.CERTPROFILE_FIXED_OCSPSIGNER), 1597 OCSPSignerCertificateProfile.CERTIFICATEPROFILENAME); 1598 1599 returnval.put(new Integer (SecConst.CERTPROFILE_FIXED_HARDTOKENAUTH), 1600 HardTokenAuthCertificateProfile.CERTIFICATEPROFILENAME); 1601 returnval.put(new Integer (SecConst.CERTPROFILE_FIXED_HARDTOKENAUTHENC), 1602 HardTokenAuthEncCertificateProfile.CERTIFICATEPROFILENAME); 1603 returnval.put(new Integer (SecConst.CERTPROFILE_FIXED_HARDTOKENENC), 1604 HardTokenEncCertificateProfile.CERTIFICATEPROFILENAME); 1605 returnval.put(new Integer (SecConst.CERTPROFILE_FIXED_HARDTOKENSIGN), 1606 HardTokenSignCertificateProfile.CERTIFICATEPROFILENAME); 1607 1608 1609 try { 1610 result = certprofilehome.findAll(); 1611 Iterator i = result.iterator(); 1612 while (i.hasNext()) { 1613 CertificateProfileDataLocal next = (CertificateProfileDataLocal) i.next(); 1614 returnval.put(next.getId(), next.getCertificateProfileName()); 1615 } 1616 } catch (FinderException e) { 1617 } 1618 return returnval; 1619 } 1621 1622 1627 public CertificateProfile getCertificateProfile(Admin admin, String certificateprofilename) { 1628 CertificateProfile returnval = null; 1629 1630 if (certificateprofilename.equals(EndUserCertificateProfile.CERTIFICATEPROFILENAME)) 1631 return new EndUserCertificateProfile(); 1632 1633 if (certificateprofilename.equals(CACertificateProfile.CERTIFICATEPROFILENAME)) 1634 return new CACertificateProfile(); 1635 1636 if (certificateprofilename.equals(OCSPSignerCertificateProfile.CERTIFICATEPROFILENAME)) 1637 return new OCSPSignerCertificateProfile(); 1638 1639 if (certificateprofilename.equals(RootCACertificateProfile.CERTIFICATEPROFILENAME)) 1640 return new RootCACertificateProfile(); 1641 1642 if (certificateprofilename.equals(HardTokenAuthCertificateProfile.CERTIFICATEPROFILENAME)) 1643 return new HardTokenAuthCertificateProfile(); 1644 1645 if (certificateprofilename.equals(HardTokenAuthEncCertificateProfile.CERTIFICATEPROFILENAME)) 1646 return new HardTokenAuthEncCertificateProfile(); 1647 1648 if (certificateprofilename.equals(HardTokenEncCertificateProfile.CERTIFICATEPROFILENAME)) 1649 return new HardTokenEncCertificateProfile(); 1650 1651 if (certificateprofilename.equals(HardTokenSignCertificateProfile.CERTIFICATEPROFILENAME)) 1652 return new HardTokenSignCertificateProfile(); 1653 1654 1655 try { 1656 returnval = (certprofilehome.findByCertificateProfileName(certificateprofilename)).getCertificateProfile(); 1657 } catch (FinderException e) { 1658 } 1660 return returnval; 1661 } 1663 1669 public CertificateProfile getCertificateProfile(Admin admin, int id) { 1670 CertificateProfile returnval = null; 1671 1672 if (id < SecConst.FIXED_CERTIFICATEPROFILE_BOUNDRY) { 1673 switch (id) { 1674 case SecConst.CERTPROFILE_FIXED_ENDUSER: 1675 returnval = new EndUserCertificateProfile(); 1676 break; 1677 case SecConst.CERTPROFILE_FIXED_SUBCA: 1678 returnval = new CACertificateProfile(); 1679 break; 1680 case SecConst.CERTPROFILE_FIXED_ROOTCA: 1681 returnval = new RootCACertificateProfile(); 1682 break; 1683 case SecConst.CERTPROFILE_FIXED_HARDTOKENAUTH: 1684 returnval = new HardTokenAuthCertificateProfile(); 1685 break; 1686 case SecConst.CERTPROFILE_FIXED_HARDTOKENAUTHENC: 1687 returnval = new HardTokenAuthEncCertificateProfile(); 1688 break; 1689 case SecConst.CERTPROFILE_FIXED_HARDTOKENENC: 1690 returnval = new HardTokenEncCertificateProfile(); 1691 break; 1692 case SecConst.CERTPROFILE_FIXED_HARDTOKENSIGN: 1693 returnval = new HardTokenSignCertificateProfile(); 1694 break; 1695 default: 1696 returnval = new EndUserCertificateProfile(); 1697 } 1698 } else { 1699 try { 1700 returnval = (certprofilehome.findByPrimaryKey(new Integer (id))).getCertificateProfile(); 1701 } catch (FinderException e) { 1702 } 1704 } 1705 return returnval; 1706 } 1708 1709 1716 public int getCertificateProfileId(Admin admin, String certificateprofilename) { 1717 int returnval = 0; 1718 1719 if (certificateprofilename.equals(EndUserCertificateProfile.CERTIFICATEPROFILENAME)) 1720 return SecConst.CERTPROFILE_FIXED_ENDUSER; 1721 1722 if (certificateprofilename.equals(CACertificateProfile.CERTIFICATEPROFILENAME)) 1723 return SecConst.CERTPROFILE_FIXED_SUBCA; 1724 1725 if (certificateprofilename.equals(RootCACertificateProfile.CERTIFICATEPROFILENAME)) 1726 return SecConst.CERTPROFILE_FIXED_ROOTCA; 1727 1728 if (certificateprofilename.equals(HardTokenAuthCertificateProfile.CERTIFICATEPROFILENAME)) 1729 return SecConst.CERTPROFILE_FIXED_HARDTOKENAUTH; 1730 1731 if (certificateprofilename.equals(HardTokenAuthEncCertificateProfile.CERTIFICATEPROFILENAME)) 1732 return SecConst.CERTPROFILE_FIXED_HARDTOKENAUTHENC; 1733 1734 if (certificateprofilename.equals(HardTokenEncCertificateProfile.CERTIFICATEPROFILENAME)) 1735 return SecConst.CERTPROFILE_FIXED_HARDTOKENENC; 1736 1737 if (certificateprofilename.equals(HardTokenSignCertificateProfile.CERTIFICATEPROFILENAME)) 1738 return SecConst.CERTPROFILE_FIXED_HARDTOKENSIGN; 1739 1740 try { 1741 Integer id = (certprofilehome.findByCertificateProfileName(certificateprofilename)).getId(); 1742 returnval = id.intValue(); 1743 } catch (FinderException e) { 1744 } 1745 1746 return returnval; 1747 } 1749 1756 public String getCertificateProfileName(Admin admin, int id) { 1757 String returnval = null; 1758 1759 if (id < SecConst.FIXED_CERTIFICATEPROFILE_BOUNDRY) { 1761 switch (id) { 1762 case SecConst.CERTPROFILE_FIXED_ENDUSER: 1763 returnval = EndUserCertificateProfile.CERTIFICATEPROFILENAME; 1764 break; 1765 case SecConst.CERTPROFILE_FIXED_SUBCA: 1766 returnval = CACertificateProfile.CERTIFICATEPROFILENAME; 1767 break; 1768 case SecConst.CERTPROFILE_FIXED_ROOTCA: 1769 returnval = RootCACertificateProfile.CERTIFICATEPROFILENAME; 1770 break; 1771 case SecConst.CERTPROFILE_FIXED_OCSPSIGNER: 1772 returnval = OCSPSignerCertificateProfile.CERTIFICATEPROFILENAME; 1773 break; 1774 case SecConst.CERTPROFILE_FIXED_HARDTOKENAUTH: 1775 returnval = HardTokenAuthCertificateProfile.CERTIFICATEPROFILENAME; 1776 break; 1777 case SecConst.CERTPROFILE_FIXED_HARDTOKENAUTHENC: 1778 returnval = HardTokenAuthEncCertificateProfile.CERTIFICATEPROFILENAME; 1779 break; 1780 case SecConst.CERTPROFILE_FIXED_HARDTOKENENC: 1781 returnval = HardTokenEncCertificateProfile.CERTIFICATEPROFILENAME; 1782 break; 1783 case SecConst.CERTPROFILE_FIXED_HARDTOKENSIGN: 1784 returnval = HardTokenSignCertificateProfile.CERTIFICATEPROFILENAME; 1785 break; 1786 default: 1787 returnval = EndUserCertificateProfile.CERTIFICATEPROFILENAME; 1788 1789 1790 } 1791 } else { 1792 try { 1793 returnval = (certprofilehome.findByPrimaryKey(new Integer (id))).getCertificateProfileName(); 1794 } catch (FinderException e) { 1795 } 1796 } 1797 1798 return returnval; 1799 1800 } 1802 1810 public boolean existsCAInCertificateProfiles(Admin admin, int caid) { 1811 Iterator availablecas = null; 1812 boolean exists = false; 1813 try { 1814 Collection result = certprofilehome.findAll(); 1815 Iterator i = result.iterator(); 1816 while (i.hasNext() && !exists) { 1817 CertificateProfile certProfile = ((CertificateProfileDataLocal) i.next()).getCertificateProfile(); 1818 if(certProfile.getType() == CertificateProfile.TYPE_ENDENTITY){ 1819 availablecas = certProfile.getAvailableCAs().iterator(); 1820 while (availablecas.hasNext()) { 1821 if (((Integer ) availablecas.next()).intValue() == caid ) { 1822 exists = true; 1823 break; 1824 } 1825 } 1826 } 1827 } 1828 } catch (FinderException e) { 1829 } 1830 1831 return exists; 1832 } 1834 1841 public boolean existsPublisherInCertificateProfiles(Admin admin, int publisherid) { 1842 Iterator availablepublishers = null; 1843 boolean exists = false; 1844 try { 1845 Collection result = certprofilehome.findAll(); 1846 Iterator i = result.iterator(); 1847 while (i.hasNext() && !exists) { 1848 availablepublishers = ((CertificateProfileDataLocal) i.next()).getCertificateProfile().getPublisherList().iterator(); 1849 while (availablepublishers.hasNext()) { 1850 if (((Integer ) availablepublishers.next()).intValue() == publisherid) { 1851 exists = true; 1852 break; 1853 } 1854 } 1855 } 1856 } catch (FinderException e) { 1857 } 1858 1859 return exists; 1860 } 1862 1864 private int findFreeCertificateProfileId() { 1865 Random random = new Random ((new Date ()).getTime()); 1866 int id = random.nextInt(); 1867 boolean foundfree = false; 1868 1869 while (!foundfree) { 1870 try { 1871 if (id > SecConst.FIXED_CERTIFICATEPROFILE_BOUNDRY) { 1872 certprofilehome.findByPrimaryKey(new Integer (id)); 1873 } else { 1874 id = random.nextInt(); 1875 } 1876 } catch (FinderException e) { 1877 foundfree = true; 1878 } 1879 } 1880 return id; 1881 } 1883 1884 private boolean isCertificateProfileNameFixed(String certificateprofilename) { 1885 boolean returnval = false; 1886 1887 if (certificateprofilename.equals(EndUserCertificateProfile.CERTIFICATEPROFILENAME)) 1888 return true; 1889 1890 if (certificateprofilename.equals(CACertificateProfile.CERTIFICATEPROFILENAME)) 1891 return true; 1892 1893 if (certificateprofilename.equals(RootCACertificateProfile.CERTIFICATEPROFILENAME)) 1894 return true; 1895 1896 return returnval; 1897 } 1898 1899 private boolean isFreeCertificateProfileId(int id) { 1900 boolean foundfree = false; 1901 try { 1902 if (id > SecConst.FIXED_CERTIFICATEPROFILE_BOUNDRY) { 1903 certprofilehome.findByPrimaryKey(new Integer (id)); 1904 } 1905 } catch (FinderException e) { 1906 foundfree = true; 1907 } 1908 return foundfree; 1909 } 1911 private class MyAdapter implements CertificateDataUtil.Adapter { 1912 1915 public Logger getLogger() { 1916 return log; 1917 } 1918 1921 public void log(Admin admin, int caid, int module, Date time, String username, 1922 X509Certificate certificate, int event, String comment) { 1923 getLogSession().log(admin, caid, module, new java.util.Date (), 1924 username, certificate, event, comment); 1925 } 1926 1929 public void debug(String s) { 1930 LocalCertificateStoreSessionBean.this.debug(s); 1931 } 1932 1935 public void error(String s) { 1936 LocalCertificateStoreSessionBean.this.error(s); 1937 } 1938 1941 public void error(String s, Exception e) { 1942 LocalCertificateStoreSessionBean.this.error(s, e); 1943 } 1944 } 1945} | Popular Tags |