001: /*
002: * DeleteCertificate.java
003: *
004: */
005:
006: /**
007: *
008: * @author ss133690
009: * @version
010: */package com.sun.portal.cli.cert;
011:
012: import org.mozilla.jss.crypto.*;
013: import com.sun.portal.log.common.PortalLogger;
014: import org.mozilla.jss.crypto.KeyPairGenerator;
015: import org.mozilla.jss.crypto.X509Certificate;
016: import org.mozilla.jss.util.*;
017: import org.mozilla.jss.ssl.*;
018: import org.mozilla.jss.*;
019: import org.mozilla.jss.pkcs11.*;
020: import java.security.cert.*;
021: import java.security.interfaces.*;
022: import java.security.*;
023: import java.security.PrivateKey;
024: import org.mozilla.jss.pkix.primitive.*;
025: import org.mozilla.jss.pkix.cert.*;
026: import org.mozilla.jss.pkix.cert.Certificate;
027: import org.mozilla.jss.asn1.*;
028: import org.mozilla.jss.pkcs7.*;
029:
030: public class DeleteCertificate implements Command {
031: private JSSContext cntx;
032:
033: public boolean execute(JSSContext cntx) {
034: this .cntx = cntx;
035: //String nick = CertAdminUtil.question("Enter the name of the certificate to be deleted");
036: String nick = CertAdminUtil.question(CertAdminLocale
037: .getPFString("q17", CertAdminConstants.q17));
038: if (nick.trim().equals("")) {
039: //println("Certifcate name entered is not valid.");
040: CertAdminUtil.println(CertAdminLocale.getPFString("m30",
041: CertAdminConstants.m30));
042: return false;
043: }
044:
045: if (!JSSUtil.certExist(cntx, nick)) {
046: //println("Specified certifcate does not exist.");
047: CertAdminUtil.println(CertAdminLocale.getPFString("m31",
048: CertAdminConstants.m31));
049: return false;
050: }
051: try {
052: deleteCertificate(nick);
053: //println("Certificate "+nick+" deleted successfully");
054: CertAdminUtil.println(CertAdminLocale.getPFString("m26",
055: CertAdminConstants.m26)
056: + CertAdminConstants.SPACE
057: + nick
058: + CertAdminConstants.SPACE
059: + CertAdminLocale.getPFString("m32",
060: CertAdminConstants.m32));
061: return true;
062: } catch (Exception ex) {
063: //println("Could not delete the certificate "+nick+" : "+ex.getMessage());
064: CertAdminUtil.println(CertAdminLocale.getPFString("m33",
065: CertAdminConstants.m33)
066: + CertAdminConstants.SPACE
067: + nick
068: + CertAdminConstants.SPACE);
069: ex.printStackTrace();
070: return false;
071: }
072: }
073:
074: //Delete certificate based on nickname
075: private void deleteCertificate(String nick) throws Exception {
076: /*X509Certificate[] certs = cntx.getCryptoManager().findCertsByNickname(nick);
077: for(int i = 0; i < certs.length; i++){
078: deleteCertificate(certs[i]);
079: }*/
080: X509Certificate cert = cntx.getCryptoManager()
081: .findCertByNickname(nick);
082: deleteCertificate(cert);
083: }
084:
085: //Delete certificate
086: private void deleteCertificate(X509Certificate cert)
087: throws Exception {
088: String passphrase = cntx.getPasswordContext()
089: .generatePassphrase(cntx);
090: PasswordCallback password;
091: CryptoToken tok = null;
092: if (cert instanceof PK11TokenCert) {
093: tok = ((PK11TokenCert) cert).getOwningToken();
094: password = new ConsolePasswordCallback();
095: if (!tok.passwordIsInitialized()) {
096: tok.initPassword(new NullPasswordCallback(), password);
097: }
098: } else {
099: tok = cntx.getCryptoManager().getInternalKeyStorageToken();
100: password = new CertAdminPasswordCallback(passphrase);
101: if (!tok.passwordIsInitialized()) {
102: tok.initPassword(new NullPasswordCallback(), password);
103: }
104: }
105: //Login to the crypto token.
106: tok.login(password);
107:
108: CryptoStore store = tok.getCryptoStore();
109: store.deleteCert(cert);
110: //cntx.getKeyStore().deleteEntry(cert.getNickname());
111:
112: }
113:
114: }
|