1 13 14 package org.ejbca.core.ejb.ca.publisher; 15 16 import java.security.cert.Certificate ; 17 import java.security.cert.X509Certificate ; 18 import java.util.Collection ; 19 import java.util.Date ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Random ; 24 25 import javax.ejb.CreateException ; 26 import javax.ejb.EJBException ; 27 import javax.ejb.FinderException ; 28 29 import org.ejbca.core.ejb.BaseSessionBean; 30 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal; 31 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome; 32 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocal; 33 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocalHome; 34 import org.ejbca.core.ejb.log.ILogSessionLocal; 35 import org.ejbca.core.ejb.log.ILogSessionLocalHome; 36 import org.ejbca.core.model.InternalResources; 37 import org.ejbca.core.model.authorization.AuthorizationDeniedException; 38 import org.ejbca.core.model.authorization.AvailableAccessRules; 39 import org.ejbca.core.model.ca.publisher.BasePublisher; 40 import org.ejbca.core.model.ca.publisher.PublisherConnectionException; 41 import org.ejbca.core.model.ca.publisher.PublisherException; 42 import org.ejbca.core.model.ca.publisher.PublisherExistsException; 43 import org.ejbca.core.model.log.Admin; 44 import org.ejbca.core.model.log.LogEntry; 45 import org.ejbca.core.model.ra.ExtendedInformation; 46 47 48 114 public class LocalPublisherSessionBean extends BaseSessionBean { 115 116 117 private static final InternalResources intres = InternalResources.getInstance(); 118 119 122 private PublisherDataLocalHome publisherhome = null; 123 124 127 private ICAAdminSessionLocal caadminsession = null; 128 129 132 private IAuthorizationSessionLocal authorizationsession = null; 133 134 137 private ILogSessionLocal logsession = null; 138 139 140 145 public void ejbCreate() throws CreateException { 146 publisherhome = (PublisherDataLocalHome) getLocator().getLocalHome(PublisherDataLocalHome.COMP_NAME); 147 } 148 149 150 155 private ILogSessionLocal getLogSession() { 156 if (logsession == null) { 157 try { 158 ILogSessionLocalHome logsessionhome = (ILogSessionLocalHome) getLocator().getLocalHome(ILogSessionLocalHome.COMP_NAME); 159 logsession = logsessionhome.create(); 160 } catch (CreateException e) { 161 throw new EJBException (e); 162 } 163 } 164 return logsession; 165 } 167 168 173 private IAuthorizationSessionLocal getAuthorizationSession() { 174 if (authorizationsession == null) { 175 try { 176 IAuthorizationSessionLocalHome authorizationsessionhome = (IAuthorizationSessionLocalHome) getLocator().getLocalHome(IAuthorizationSessionLocalHome.COMP_NAME); 177 authorizationsession = authorizationsessionhome.create(); 178 } catch (CreateException e) { 179 throw new EJBException (e); 180 } 181 } 182 return authorizationsession; 183 } 185 190 private ICAAdminSessionLocal getCAAdminSession() { 191 if (caadminsession == null) { 192 try { 193 ICAAdminSessionLocalHome caadminsessionhome = (ICAAdminSessionLocalHome) getLocator().getLocalHome(ICAAdminSessionLocalHome.COMP_NAME); 194 caadminsession = caadminsessionhome.create(); 195 } catch (CreateException e) { 196 throw new EJBException (e); 197 } 198 } 199 return caadminsession; 200 } 202 203 212 public boolean storeCertificate(Admin admin, Collection publisherids, Certificate incert, String username, String password, String cafp, int status, int type, long revocationDate, int revocationReason, ExtendedInformation extendedinformation) { 213 Iterator iter = publisherids.iterator(); 214 boolean returnval = true; 215 while (iter.hasNext()) { 216 Integer id = (Integer ) iter.next(); 217 try { 218 PublisherDataLocal pdl = publisherhome.findByPrimaryKey(id); 219 try { 220 returnval &= pdl.getPublisher().storeCertificate(admin, incert, username, password, cafp, status, type, revocationDate, revocationReason, extendedinformation); 221 String msg = intres.getLocalizedMessage("publisher.store", ((X509Certificate ) incert).getSubjectDN().toString(), pdl.getName()); 222 getLogSession().log(admin, (X509Certificate ) incert, LogEntry.MODULE_CA, new java.util.Date (), username, 223 (X509Certificate ) incert, LogEntry.EVENT_INFO_STORECERTIFICATE, msg); 224 } catch (PublisherException pe) { 225 String msg = intres.getLocalizedMessage("publisher.errorstore", pdl.getName()); 226 getLogSession().log(admin, (X509Certificate ) incert, LogEntry.MODULE_CA, new java.util.Date (), username, (X509Certificate ) incert, 227 LogEntry.EVENT_ERROR_STORECERTIFICATE, msg, pe); 228 229 } 230 } catch (FinderException fe) { 231 String msg = intres.getLocalizedMessage("publisher.nopublisher", id); 232 getLogSession().log(admin, (X509Certificate ) incert, LogEntry.MODULE_CA, new java.util.Date (), null, (X509Certificate ) incert, 233 LogEntry.EVENT_ERROR_STORECERTIFICATE, msg); 234 235 } 236 } 237 238 return returnval; 239 } 240 241 250 public boolean storeCRL(Admin admin, Collection publisherids, byte[] incrl, String cafp, int number) { 251 Iterator iter = publisherids.iterator(); 252 boolean returnval = true; 253 while (iter.hasNext()) { 254 Integer id = (Integer ) iter.next(); 255 try { 256 PublisherDataLocal pdl = publisherhome.findByPrimaryKey(id); 257 try { 258 returnval &= pdl.getPublisher().storeCRL(admin, incrl, cafp, number); 259 String msg = intres.getLocalizedMessage("publisher.store", "CRL", pdl.getName()); 260 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, 261 null, LogEntry.EVENT_INFO_STORECRL, msg); 262 } catch (PublisherException pe) { 263 String msg = intres.getLocalizedMessage("publisher.errorstorecert", pdl.getName()); 264 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, 265 LogEntry.EVENT_ERROR_STORECRL, msg, pe); 266 267 } 268 } catch (FinderException fe) { 269 String msg = intres.getLocalizedMessage("publisher.nopublisher", id); 270 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, 271 LogEntry.EVENT_ERROR_STORECRL, msg); 272 273 } 274 } 275 276 return returnval; 277 } 278 279 287 public void revokeCertificate(Admin admin, Collection publisherids, Certificate cert, int reason) { 288 Iterator iter = publisherids.iterator(); 289 while (iter.hasNext()) { 290 Integer id = (Integer ) iter.next(); 291 try { 292 PublisherDataLocal pdl = publisherhome.findByPrimaryKey(id); 293 try { 294 pdl.getPublisher().revokeCertificate(admin, cert, reason); 295 String msg = intres.getLocalizedMessage("publisher.store", ((X509Certificate ) cert).getSubjectDN().toString(), pdl.getName()); 296 getLogSession().log(admin, (X509Certificate ) cert, LogEntry.MODULE_CA, new java.util.Date (), null, 297 (X509Certificate ) cert, LogEntry.EVENT_INFO_REVOKEDCERT, msg); 298 } catch (PublisherException pe) { 299 String msg = intres.getLocalizedMessage("publisher.errorstore", pdl.getName()); 300 getLogSession().log(admin, (X509Certificate ) cert, LogEntry.MODULE_CA, new java.util.Date (), null, (X509Certificate ) cert, 301 LogEntry.EVENT_ERROR_REVOKEDCERT, msg, pe); 302 303 } 304 } catch (FinderException fe) { 305 String msg = intres.getLocalizedMessage("publisher.nopublisher", id); 306 getLogSession().log(admin, (X509Certificate ) cert, LogEntry.MODULE_CA, new java.util.Date (), null, (X509Certificate ) cert, 307 LogEntry.EVENT_ERROR_REVOKEDCERT, msg); 308 309 } 310 } 311 } 312 313 320 public void testConnection(Admin admin, int publisherid) throws PublisherConnectionException { 321 debug(">testConnection(id: " + publisherid + ")"); 322 try { 323 PublisherDataLocal pdl = publisherhome.findByPrimaryKey(new Integer (publisherid)); 324 try { 325 pdl.getPublisher().testConnection(admin); 326 String msg = intres.getLocalizedMessage("publisher.testedpublisher", pdl.getName()); 327 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, 328 null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg); 329 } catch (PublisherConnectionException pe) { 330 String msg = intres.getLocalizedMessage("publisher.errortestpublisher", pdl.getName()); 331 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, 332 LogEntry.EVENT_ERROR_PUBLISHERDATA, msg, pe); 333 334 throw new PublisherConnectionException(pe.getMessage()); 335 } 336 } catch (FinderException fe) { 337 String msg = intres.getLocalizedMessage("publisher.nopublisher", new Integer (publisherid)); 338 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, 339 LogEntry.EVENT_ERROR_PUBLISHERDATA, msg); 340 341 } 342 debug("<testConnection(id: " + publisherid + ")"); 343 } 344 345 352 353 public void addPublisher(Admin admin, String name, BasePublisher publisher) throws PublisherExistsException { 354 debug(">addPublisher(name: " + name + ")"); 355 addPublisher(admin,findFreePublisherId().intValue(),name,publisher); 356 debug("<addPublisher()"); 357 } 359 360 368 369 public void addPublisher(Admin admin, int id, String name, BasePublisher publisher) throws PublisherExistsException { 370 debug(">addPublisher(name: " + name + ", id: " + id + ")"); 371 boolean success = false; 372 try { 373 publisherhome.findByName(name); 374 } catch (FinderException e) { 375 try { 376 publisherhome.findByPrimaryKey(new Integer (id)); 377 } catch (FinderException f) { 378 try { 379 publisherhome.create(new Integer (id), name, publisher); 380 success = true; 381 } catch (CreateException g) { 382 String msg = intres.getLocalizedMessage("publisher.erroraddpublisher", name); 383 error(msg, g); 384 } 385 } 386 } 387 if (success) { 388 String msg = intres.getLocalizedMessage("publisher.addedpublisher", name); 389 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg); 390 } else { 391 String msg = intres.getLocalizedMessage("publisher.erroraddpublisher", name); 392 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_PUBLISHERDATA, msg); 393 } 394 if (!success) 395 throw new PublisherExistsException(); 396 debug("<addPublisher()"); 397 } 399 405 406 public void changePublisher(Admin admin, String name, BasePublisher publisher) { 407 debug(">changePublisher(name: " + name + ")"); 408 boolean success = false; 409 try { 410 PublisherDataLocal htp = publisherhome.findByName(name); 411 htp.setPublisher(publisher); 412 success = true; 413 } catch (FinderException e) { 414 } 415 416 if (success) { 417 String msg = intres.getLocalizedMessage("publisher.changedpublisher", name); 418 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg); 419 } else { 420 String msg = intres.getLocalizedMessage("publisher.errorchangepublisher", name); 421 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_PUBLISHERDATA, msg); 422 } 423 424 debug("<changePublisher()"); 425 } 427 434 public void clonePublisher(Admin admin, String oldname, String newname) { 435 debug(">clonePublisher(name: " + oldname + ")"); 436 BasePublisher publisherdata = null; 437 try { 438 PublisherDataLocal htp = publisherhome.findByName(oldname); 439 publisherdata = (BasePublisher) htp.getPublisher().clone(); 440 try { 441 addPublisher(admin, newname, publisherdata); 442 String msg = intres.getLocalizedMessage("publisher.clonedpublisher", newname, oldname); 443 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg); 444 } catch (PublisherExistsException f) { 445 String msg = intres.getLocalizedMessage("publisher.errorclonepublisher", newname, oldname); 446 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_PUBLISHERDATA, msg); 447 throw f; 448 } 449 } catch (Exception e) { 450 String msg = intres.getLocalizedMessage("publisher.errorclonepublisher", newname, oldname); 451 error(msg, e); 452 throw new EJBException (e); 453 } 454 455 debug("<clonePublisher()"); 456 } 458 464 public void removePublisher(Admin admin, String name) { 465 debug(">removePublisher(name: " + name + ")"); 466 try { 467 PublisherDataLocal htp = publisherhome.findByName(name); 468 htp.remove(); 469 String msg = intres.getLocalizedMessage("publisher.removedpublisher", name); 470 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg); 471 } catch (Exception e) { 472 String msg = intres.getLocalizedMessage("publisher.errorremovepublisher", name); 473 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_PUBLISHERDATA, msg, e); 474 } 475 debug("<removePublisher()"); 476 } 478 485 public void renamePublisher(Admin admin, String oldname, String newname) throws PublisherExistsException { 486 debug(">renamePublisher(from " + oldname + " to " + newname + ")"); 487 boolean success = false; 488 try { 489 publisherhome.findByName(newname); 490 } catch (FinderException e) { 491 try { 492 PublisherDataLocal htp = publisherhome.findByName(oldname); 493 htp.setName(newname); 494 success = true; 495 } catch (FinderException g) { 496 } 497 } 498 499 if (success) { 500 String msg = intres.getLocalizedMessage("publisher.renamedpublisher", oldname, newname); 501 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_INFO_PUBLISHERDATA, msg); 502 } else { 503 String msg = intres.getLocalizedMessage("publisher.errorrenamepublisher", oldname, newname); 504 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_CA, new java.util.Date (), null, null, LogEntry.EVENT_ERROR_PUBLISHERDATA, msg); 505 } 506 if (!success) 507 throw new PublisherExistsException(); 508 debug("<renamePublisher()"); 509 } 511 517 public Collection getAuthorizedPublisherIds(Admin admin) { 518 HashSet returnval = new HashSet (); 519 Collection result = null; 520 boolean superadmin = false; 521 try { 523 superadmin = getAuthorizationSession().isAuthorizedNoLog(admin, AvailableAccessRules.ROLE_SUPERADMINISTRATOR); 524 result = this.publisherhome.findAll(); 525 Iterator i = result.iterator(); 526 while (i.hasNext()) { 527 PublisherDataLocal next = (PublisherDataLocal) i.next(); 528 returnval.add(next.getId()); 529 } 530 } catch (AuthorizationDeniedException e1) { 531 log.debug("AuthorizationDeniedException: ", e1); 532 } catch (FinderException fe) { 533 log.error("FinderException looking for all publishers: ", fe); 534 } 535 536 if (!superadmin) { 538 Iterator authorizedcas = this.getAuthorizationSession().getAuthorizedCAIds(admin).iterator(); 539 while (authorizedcas.hasNext()) { 540 returnval.addAll(this.getCAAdminSession().getCAInfo(admin, ((Integer ) authorizedcas.next()).intValue()).getCRLPublishers()); 541 } 542 } 543 return returnval; 544 } 546 552 public HashMap getPublisherIdToNameMap(Admin admin) { 553 HashMap returnval = new HashMap (); 554 Collection result = null; 555 556 try { 557 result = publisherhome.findAll(); 558 Iterator i = result.iterator(); 559 while (i.hasNext()) { 560 PublisherDataLocal next = (PublisherDataLocal) i.next(); 561 returnval.put(next.getId(), next.getName()); 562 } 563 } catch (FinderException e) { 564 } 565 return returnval; 566 } 568 569 575 public BasePublisher getPublisher(Admin admin, String name) { 576 BasePublisher returnval = null; 577 578 try { 579 returnval = (publisherhome.findByName(name)).getPublisher(); 580 } catch (FinderException e) { 581 } 583 return returnval; 584 } 586 592 public BasePublisher getPublisher(Admin admin, int id) { 593 BasePublisher returnval = null; 594 595 try { 596 returnval = (publisherhome.findByPrimaryKey(new Integer (id))).getPublisher(); 597 } catch (FinderException e) { 598 } 600 return returnval; 601 } 603 610 611 public int getPublisherUpdateCount(Admin admin, int publisherid) { 612 int returnval = 0; 613 614 try { 615 returnval = (publisherhome.findByPrimaryKey(new Integer (publisherid))).getUpdateCounter(); 616 } catch (FinderException e) { 617 } 618 619 return returnval; 620 } 621 622 623 630 public int getPublisherId(Admin admin, String name) { 631 int returnval = 0; 632 633 try { 634 Integer id = (publisherhome.findByName(name)).getId(); 635 returnval = id.intValue(); 636 } catch (FinderException e) { 637 } 638 639 return returnval; 640 } 642 650 public String getPublisherName(Admin admin, int id) { 651 debug(">getPublisherName(id: " + id + ")"); 652 String returnval = null; 653 PublisherDataLocal htp = null; 654 try { 655 htp = publisherhome.findByPrimaryKey(new Integer (id)); 656 if (htp != null) { 657 returnval = htp.getName(); 658 } 659 } catch (FinderException e) { 660 } 661 662 debug("<getPublisherName()"); 663 return returnval; 664 } 666 667 private Integer findFreePublisherId() { 668 Random ran = (new Random ((new Date ()).getTime())); 669 int id = ran.nextInt(); 670 boolean foundfree = false; 671 672 while (!foundfree) { 673 try { 674 if (id > 1) 675 publisherhome.findByPrimaryKey(new Integer (id)); 676 id = ran.nextInt(); 677 } catch (FinderException e) { 678 foundfree = true; 679 } 680 } 681 return new Integer (id); 682 } 684 685 } | Popular Tags |