001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.q2.cli.ssm.actions;
020
021import org.jpos.q2.CLIContext;
022import org.jpos.q2.cli.ssm.SsmActionBase;
023import org.jpos.security.SMException;
024import org.jpos.security.jceadapter.JCESecurityModule;
025
026/**
027 * Form a key from clear components.
028 *
029 * @author Alwyn Schoeman - alwyn.schoeman@gmail.com
030 */
031public class FK extends SsmActionBase {
032    /** Default constructor; no instance state to initialise. */
033    public FK() {}
034    private boolean prompt = false;
035
036    @Override
037    protected boolean checkUsage(CLIContext cli, String[] strings) {
038        // This doesn't cover all combinations, but it is assumed the operation will fail elsewhere.
039        if (strings.length == 4) {
040            if (!strings[3].equals("-prompt")) {
041                cli.println("Usage: FK KeyLength keyType -prompt");
042                cli.println("Usage: FK KeyLength keyType component1 component2 component3");
043                return false;
044            } else {
045                prompt = true;
046                return true;
047            }
048        }
049        if (strings.length < 6) {
050            cli.println("Usage: FK KeyLength keyType -prompt");
051            cli.println("Usage: FK KeyLength keyType component1 component2 component3");
052            return false;
053        }
054
055        return true;
056    }
057
058    @Override
059    protected void doCommand(
060            CLIContext cli,
061            JCESecurityModule sm,
062            short keyLength,
063            String[] strings)
064            throws SMException {
065        if (prompt) {
066            String key1 = readKeyComponent(cli, 1);
067            String key2 = readKeyComponent(cli, 2);
068            String key3 = readKeyComponent(cli, 3);
069            sm.formKEYfromThreeClearComponents(
070                    keyLength, strings[2].toUpperCase(), key1, key2, key3);
071        } else {
072            sm.formKEYfromThreeClearComponents(
073                    keyLength, strings[2].toUpperCase(), strings[3], strings[4], strings[5]);
074        }
075    }
076
077    private String readKeyComponent(CLIContext cli, int kcNumber) {
078        boolean validComponent = false;
079        String key = null;
080        cli.println("Key component " + kcNumber + ":");
081        while (!validComponent) {
082            while (true) {
083                key = cli.readSecurely("Please enter key component:");
084                if (key != null && key.length() == 32) break;
085                cli.println("Key component must be 32 hexadecimal characters.");
086            }
087            String second;
088            while (true) {
089                second = cli.readSecurely("Please re-enter key component:");
090                if (second != null && second.length() == 32) break;
091                cli.println("Key component must be 32 hexadecimal characters.");
092            }
093            validComponent = key.equals(second);
094            if (!validComponent) {
095                cli.println("Entered key components don't match.");
096            }
097        }
098        return key;
099    }
100}