1 13 14 package org.ejbca.ui.cli; 15 16 import java.io.*; 17 import java.util.ArrayList ; 18 import java.security.cert.X509Certificate ; 19 20 import org.ejbca.util.CertTools; 21 22 23 28 public class CaGetRootCertCommand extends BaseCaAdminCommand { 29 34 public CaGetRootCertCommand(String [] args) { 35 super(args); 36 } 37 38 44 public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException { 45 46 if (args.length < 3) { 47 String msg = "Save root CA certificate (PEM- or DER-format) to file.\n"; 48 msg += "Usage: CA getrootcert <caname> <filename> <-der>"; 49 throw new IllegalAdminCommandException(msg); 50 } 51 52 String caname = args[1]; 53 String filename = args[2]; 54 boolean pem = true; 55 if (args.length > 3) { 56 if (("-der").equals(args[3])) { 57 pem = false; 58 } 59 } 60 61 getOutputStream().flush(); 62 try { 63 ArrayList chain = new ArrayList (getCertChain(caname)); 64 if (chain.size() > 0) { 65 X509Certificate rootcert = (X509Certificate )chain.get(chain.size()-1); 66 67 FileOutputStream fos = new FileOutputStream(filename); 68 if (pem) { 69 fos.write(CertTools.getPEMFromCerts(chain)); 70 } else { 71 fos.write(rootcert.getEncoded()); 72 } 73 fos.close(); 74 getOutputStream().println("Wrote Root CA certificate to '" + filename + "'"); 75 } else { 76 getOutputStream().println("No CA certificate found."); 77 } 78 } catch (Exception e) { 79 throw new ErrorAdminCommandException(e); 80 } 81 } } 83 | Popular Tags |