1 package com.ca.commons.security.cert; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import java.util.Properties ; 7 8 import java.io.*; 9 10 import java.security.cert.*; 11 12 import com.ca.commons.security.cert.extensions.*; 13 import com.ca.commons.security.asn1.*; 14 import com.ca.commons.security.util.CertUtil; 15 16 import com.ca.commons.cbutil.*; 17 18 import java.util.StringTokenizer ; 19 20 31 32 35 37 public class CertViewer extends CBDialog 38 { 39 43 44 public static class CertAndFileName 45 { 46 public X509Certificate cert; 47 public String fileName; 48 } 49 50 53 54 private X509Certificate cert = null; 55 56 59 60 private String fileName = null; 61 62 private JTabbedPane tabs = new JTabbedPane(); 64 private CertGeneralViewPanel generalView = null; 65 private CertDetailsViewPanel detailsView = null; 66 private CertPathViewPanel pathView = null; 67 private CBButton okButton, saveButton, loadButton; 68 69 70 73 74 public static final int VIEW_ONLY = 0; 75 76 79 80 public static final int VIEW_SAVE = 1; 81 82 85 86 public static final int VIEW_LOAD = 2; 87 88 91 92 public static final int VIEW_SAVE_LOAD = 3; 93 94 97 98 public static final int VIEW_LOAD_SAVE = 3; 99 100 protected int mode = VIEW_SAVE; 101 102 104 public static ImageIcon certLargeIcon = null; 105 public static ImageIcon certIcon = null; 106 public static ImageIcon attributeIcon = null; 107 public static ImageIcon extensionIcon = null; 108 public static ImageIcon criticalExtensionIcon = null; 109 public static ImageIcon thumbprintIcon = null; 110 public static Image frameIcon = null; 111 112 116 117 public static Properties properties = null; 118 119 public static String helpLink = null; 121 125 protected static void setupGraphics() 126 { 127 try 128 { 129 certLargeIcon = getImageIcon("certificate_large.gif"); 130 certIcon = getImageIcon("certificate.gif"); 131 attributeIcon = getImageIcon("attribute.gif"); 132 extensionIcon = getImageIcon("extension.gif"); 133 criticalExtensionIcon = getImageIcon("criticalExtension.gif"); 134 thumbprintIcon = getImageIcon("thumbprint.gif"); 135 frameIcon = getImageIcon("pki_icon.gif").getImage(); 136 } 137 catch (Exception ex) 138 { 139 System.out.println(ex.getMessage()); 140 } 141 } 142 143 146 147 public static ImageIcon getImageIcon(String name) 148 { 149 if (properties == null) return null; 150 151 ImageIcon newIcon = new ImageIcon(properties.getProperty("dir.images") + name); 152 return newIcon; 153 } 154 155 158 159 public static void setupHelpLink(String link) 160 { 161 helpLink = link; 162 } 163 164 172 173 public static void setProperties(Properties props) { properties = props; } 174 175 181 182 public static void setImageDirectory(String imagePath) 183 { 184 if (properties == null) 185 properties = new Properties (); 186 properties.put("dir.images", imagePath); 187 } 188 189 190 public static CertAndFileName loadCertificate(Frame owner) 191 { 192 CertViewer viewer = new CertViewer(owner, null, VIEW_SAVE_LOAD); 193 194 198 if (viewer.getCertificate() == null) 199 return null; 200 201 204 viewer.setVisible(true); 205 206 CertAndFileName returnInfo = new CertAndFileName(); 207 returnInfo.cert = viewer.getCertificate(); 208 returnInfo.fileName = viewer.getFileName(); 209 210 return returnInfo; 211 } 212 213 public static CertAndFileName editCertificate(Frame owner, byte[] certData) 214 { 215 if (certData == null) return loadCertificate(owner); 216 217 X509Certificate cert = CertUtil.loadX509Certificate (certData); 218 219 CertViewer viewer = new CertViewer(owner, cert, VIEW_SAVE_LOAD); 220 221 225 if (viewer.getCertificate() == null) 226 return null; 227 228 231 viewer.setVisible(true); 232 233 CertAndFileName returnInfo = new CertAndFileName(); 234 returnInfo.cert = viewer.getCertificate(); 235 returnInfo.fileName = viewer.getFileName(); 236 237 return returnInfo; 238 } 239 240 247 public CertViewer(X509Certificate cert) 248 { 249 super(null, CBIntText.get("Certificate"), null); 250 init(cert, VIEW_SAVE); 251 } 252 253 258 259 public CertViewer(Frame owner, X509Certificate cert) 260 { 261 super(owner, CBIntText.get("Certificate"), helpLink); init(cert, VIEW_SAVE); 263 } 264 265 272 273 public CertViewer(Frame owner, X509Certificate cert, int mode) 274 { 275 super(owner, CBIntText.get("Certificate"), helpLink); init(cert, mode); 277 } 278 279 public void init(X509Certificate cert, int mode) 280 { 281 if (certLargeIcon == null) 282 setupGraphics(); 283 284 this.mode = mode; 285 286 displayCert(cert); 288 saveButton = new CBButton(CBIntText.get("Copy to File"), CBIntText.get("Copy to File.")); 289 loadButton = new CBButton(CBIntText.get("Read from File"), CBIntText.get("Read from File.")); 290 291 295 if (mode == VIEW_ONLY || mode == VIEW_SAVE) 296 { buttonPanel.remove(Cancel); } 299 300 if (mode == VIEW_SAVE || mode == VIEW_SAVE_LOAD) 301 { 302 buttonPanel.add(saveButton, 0); 303 } 304 305 if (mode == VIEW_LOAD || mode == VIEW_SAVE_LOAD) 306 { 307 buttonPanel.add(loadButton, 0); 308 } 310 311 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); 312 313 setSize(440, 477); 314 CBUtility.center(this, owner); 315 316 saveButton.addActionListener(new ActionListener() 317 { 318 public void actionPerformed(ActionEvent e) { saveCert(); } 319 }); 320 321 loadButton.addActionListener(new ActionListener() 322 { 323 public void actionPerformed(ActionEvent e) { loadCert(); } 324 }); 325 326 329 if (cert == null && (mode & VIEW_LOAD)>0) 330 loadCert(); 331 } 332 333 341 342 public void displayCert(X509Certificate displayCert) 343 { 344 cert = displayCert; 345 346 tabs.removeAll(); 347 generalView = new CertGeneralViewPanel(displayCert); 348 detailsView = new CertDetailsViewPanel(displayCert); 349 pathView = new CertPathViewPanel(displayCert); 350 tabs.add("General", generalView); 351 tabs.add("Details", detailsView); 352 tabs.add("Certification Path", pathView); 353 354 display.removeAll(); 355 makeHeavy(); 356 display.add(tabs); 357 } 358 359 360 365 366 public void doCancel() 367 { 368 cert = null; 369 super.doCancel(); 370 } 371 372 376 377 public X509Certificate getCertificate() { return cert; } 378 379 384 385 public String getFileName() { return fileName; } 386 387 390 391 protected void saveCert() 392 { 393 JFileChooser chooser = new JFileChooser(properties.getProperty("cert.homeDir", System.getProperty("user.dir"))); 394 395 chooser.addChoosableFileFilter(new CBFileFilter(new String [] {"der", "pem"}, CBIntText.get("Certificate File") + " (*.der) (*.pem)")); 396 397 int option = chooser.showSaveDialog(this); 398 399 File readFile = chooser.getSelectedFile(); 400 if (option != JFileChooser.APPROVE_OPTION || readFile == null) { 402 return; } 404 405 if (properties != null) 406 properties.setProperty("cert.homeDir", readFile.getParent()); 407 408 fileName = readFile.toString(); 409 410 try 411 { 412 byte[] derout = cert.getEncoded(); 413 414 if (fileName.toLowerCase().endsWith(".pem")) { 416 derout = CBSecurity.convertToPEMCertificate(derout); 418 } 419 else if (fileName.toLowerCase().endsWith(".der") == false) 420 { 421 fileName = fileName + ".der"; 422 readFile = new File(fileName); 423 } 424 425 if (saveFileCheck(readFile) == false) return; 427 FileOutputStream fos = new FileOutputStream(readFile); 428 fos.write(derout); 430 fos.close(); 431 } 432 catch (Exception ex) 433 { 434 CBUtility.error(CBIntText.get("Unable to save Certificate."), ex); 435 } 436 } 437 438 441 442 public boolean saveFileCheck(File checkMe) 443 { 444 if (checkMe.isDirectory()) 445 { 446 CBUtility.error(checkMe.toString() + " is a directory.", null); 447 return false; 448 } 449 else if (checkMe.exists()) 450 { 451 int saveAnswer = JOptionPane.showConfirmDialog(owner, 452 (checkMe.toString() + "\n " + CBIntText.get("This file already exists.\nDo you want to overwrite this file?")), 453 "Question", JOptionPane.OK_CANCEL_OPTION); 454 455 return (saveAnswer == JOptionPane.OK_OPTION); 456 } 457 458 return true; 459 } 460 461 462 protected void loadCert() 463 { 464 String browseDir = System.getProperty("user.dir"); 465 if (properties != null) 466 { 467 if (properties.getProperty("cert.homeDir") != null) 468 browseDir = properties.getProperty("cert.homeDir"); 469 } 470 JFileChooser chooser = new JFileChooser(browseDir); 471 472 chooser.addChoosableFileFilter(new CBFileFilter(new String [] {"der", "pem"}, CBIntText.get("Certificate File") + " (*.der), (*.pem)")); 473 474 int option = chooser.showOpenDialog(this); 475 476 File readFile = chooser.getSelectedFile(); 477 478 if (option != JFileChooser.APPROVE_OPTION || readFile == null) { if (cert == null) 481 doCancel(); return; 483 } 484 485 try 486 { 487 if (properties != null) 488 properties.setProperty("cert.homeDir", readFile.getParent()); 489 490 byte[] data = getDERCertDataFromFile(readFile); 493 494 X509Certificate newCert = CertUtil.loadX509Certificate(data); 497 498 502 displayCert(newCert); 503 fileName = readFile.getName(); 504 } 505 catch (Exception ex) 506 { 507 CBUtility.error(CBIntText.get("Unable to load Certificate."), ex); 508 } 509 510 } 511 512 513 514 518 public static String getMostSignificantName(String dnstring) 519 { 520 String leftmostname = null; 521 StringTokenizer stok = new StringTokenizer (dnstring, ","); 522 if (stok.hasMoreTokens()) leftmostname = stok.nextToken(); 523 return leftmostname; 524 } 525 526 527 536 537 public static byte[] getDERCertDataFromFile(File file) 538 throws CertificateParsingException, FileNotFoundException, IOException 539 { 540 541 FileInputStream in = new FileInputStream(file); 542 byte [] buffer = new byte[(int) (file.length())]; 543 in.read(buffer); 544 in.close(); 545 546 547 if (CBSecurity.isPEM(buffer)) 548 { 549 551 553 byte[] pemData = CBSecurity.convertFromPEMCertificate(buffer); 554 if (pemData == null) 555 throw new CertificateParsingException("Unable to parse PEM encoded cert - invalid PEM encoding."); 556 557 buffer = pemData; 558 } 559 560 return buffer; 561 } 562 563 564 567 public static void main(String [] args) 568 { 569 JFrame parent = new JFrame(); 570 572 X509Certificate cert = null; 573 574 try 575 { 576 byte[] data = getDERCertDataFromFile(new File(args[0])); 577 578 if ( (cert = CertUtil.loadX509Certificate(data)) == null ) 579 { 580 System.out.println("Problem opening certfile \"" + args[0] + "\""); 581 System.exit(1); 582 } 583 584 585 String localDir = System.getProperty("user.dir") + File.separator; 586 587 Properties props = new Properties (); 588 589 594 595 props.setProperty("cert.homeDir", localDir + "certs" + File.separator); 596 597 601 602 props.setProperty("dir.images", localDir + "images" + File.separator); 603 604 CertViewer.setProperties(props); 605 606 CertViewer me = new CertViewer(parent, cert); 607 608 me.addWindowListener(new WindowAdapter() 609 { 610 public void windowClosing(WindowEvent e) 611 { 612 System.exit(0); 613 } 614 }); 615 616 me.setVisible(true); 617 } 618 catch (Exception e) 619 { 620 System.err.println("ERROR OCCURRED"); 621 e.printStackTrace(); 622 System.exit(-1); 623 } 624 626 } 627 628 } | Popular Tags |