1 13 14 package org.ejbca.ui.cli; 15 16 import java.math.BigInteger ; 17 import java.security.cert.X509Certificate ; 18 19 import javax.naming.InitialContext ; 20 21 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionHome; 22 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionRemote; 23 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionHome; 24 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionRemote; 25 import org.ejbca.core.model.ra.UserDataVO; 26 27 32 public class RaKeyRecoverCommand extends BaseRaAdminCommand { 33 38 public RaKeyRecoverCommand(String [] args) { 39 super(args); 40 } 41 42 48 public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException { 49 try { 50 if (args.length != 3) { 51 getOutputStream().println("Usage: RA keyrecover <CertificateSN (HEX)> <IssuerDN>"); 52 53 return; 54 } 55 56 InitialContext jndicontext = getInitialContext(); 58 59 Object obj1 = jndicontext.lookup("CertificateStoreSession"); 60 ICertificateStoreSessionHome certificatesessionhome = (ICertificateStoreSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, 61 ICertificateStoreSessionHome.class); 62 ICertificateStoreSessionRemote certificatesession = certificatesessionhome.create(); 63 64 obj1 = jndicontext.lookup("KeyRecoverySession"); 65 66 IKeyRecoverySessionHome keyrecoverysessionhome = (IKeyRecoverySessionHome) javax.rmi.PortableRemoteObject.narrow(jndicontext.lookup( 67 "KeyRecoverySession"), IKeyRecoverySessionHome.class); 68 IKeyRecoverySessionRemote keyrecoverysession = keyrecoverysessionhome.create(); 69 70 BigInteger certificatesn = new BigInteger (args[1], 16); 71 String issuerdn = args[2]; 72 73 boolean usekeyrecovery = getRaAdminSession().loadGlobalConfiguration(administrator).getEnableKeyRecovery(); 74 if(!usekeyrecovery){ 75 getOutputStream().println("Keyrecovery have to be enabled in the system configuration in order to use this command."); 76 return; 77 } 78 79 X509Certificate cert = (X509Certificate ) certificatesession.findCertificateByIssuerAndSerno( 80 administrator, issuerdn, 81 certificatesn); 82 83 if(cert == null){ 84 getOutputStream().println("Certificate couldn't be found in database."); 85 return; 86 } 87 88 String username = certificatesession.findUsernameByCertSerno(administrator, certificatesn, issuerdn); 89 90 if(!keyrecoverysession.existsKeys(administrator,cert)){ 91 getOutputStream().println("Specified keys doesn't exist in database."); 92 return; 93 } 94 95 if(keyrecoverysession.isUserMarked(administrator,username)){ 96 getOutputStream().println("User is already marked for recovery."); 97 return; 98 } 99 100 UserDataVO userdata = getAdminSession().findUser(administrator, username); 101 if(userdata == null){ 102 getOutputStream().println("Error, The user doesn't exist."); 103 return; 104 } 105 106 keyrecoverysession.markAsRecoverable(administrator, 107 cert, userdata.getEndEntityProfileId()); 108 109 110 getOutputStream().println("Keys corresponding to given certificate has been marked for recovery."); 111 112 } catch (Exception e) { 113 throw new ErrorAdminCommandException(e); 114 } 115 } 116 117 } 119 | Popular Tags |