Java tutorial
// --- BEGIN COPYRIGHT BLOCK --- // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; version 2 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // // (C) 2018 Red Hat, Inc. // All rights reserved. // --- END COPYRIGHT BLOCK --- package com.netscape.cmstools.pkcs11; import java.security.Key; import java.security.KeyStore; import org.apache.commons.cli.CommandLine; import org.dogtagpki.util.logging.PKILogger; import org.mozilla.jss.crypto.CryptoToken; import org.mozilla.jss.provider.java.security.JSSLoadStoreParameter; import com.netscape.cmstools.cli.CLI; import com.netscape.cmsutil.crypto.CryptoUtil; /** * @author Endi S. Dewata */ public class PKCS11KeyRemoveCLI extends CLI { public PKCS11KeyRemoveCLI(PKCS11KeyCLI parent) { super("del", "Remove PKCS #11 key", parent); createOptions(); } public void printHelp() { formatter.printHelp(getFullName() + " [OPTIONS...] <Key ID>", options); } public void createOptions() { options.addOption("v", "verbose", false, "Run in verbose mode."); options.addOption(null, "debug", false, "Run in debug mode."); options.addOption(null, "help", false, "Show help message."); } public void execute(String[] args) throws Exception { CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("help")) { printHelp(); return; } if (cmd.hasOption("verbose")) { PKILogger.setLevel(PKILogger.Level.INFO); } else if (cmd.hasOption("debug")) { PKILogger.setLevel(PKILogger.Level.DEBUG); } String[] cmdArgs = cmd.getArgs(); if (cmdArgs.length < 1) { throw new Exception("Missing key ID."); } String alias = cmdArgs[0]; String tokenName = getConfig().getTokenName(); CryptoToken token = CryptoUtil.getKeyStorageToken(tokenName); KeyStore ks = KeyStore.getInstance("pkcs11"); ks.load(new JSSLoadStoreParameter(token)); Key key = ks.getKey(alias, null); if (key == null) { throw new Exception("Key not found: " + alias); } ks.deleteEntry(alias); } }