static void getAdditionalParameters()

in modules/jdktools/src/main/java/org/apache/harmony/tools/keytool/ArgumentsParser.java [501:666]


    static void getAdditionalParameters(KeytoolParameters param)
            throws KeytoolException, IOException, KeyStoreException,
            UnrecoverableKeyException, NoSuchAlgorithmException,
            CertificateException, NoSuchProviderException {
        // this method must be called after the keystore is loaded.
        KeyStore keyStore = param.getKeyStore();

        // set the alias to "mykey" if it's not set up
        Command command = param.getCommand();
        if (param.getAlias() == null
                && (command == Command.KEYCLONE || command == Command.EXPORT
                        || command == Command.CERTREQ
                        || command == Command.GENKEY
                        || command == Command.SELFCERT
                        || command == Command.IMPORT || command == Command.KEYPASSWD)) {
            param.setAlias("mykey");
        }
        String alias = param.getAlias();
        
        // check if the alias exists
        if (command == Command.CERTREQ || command == Command.DELETE
                || command == Command.EXPORT || command == Command.KEYCLONE
                || command == Command.KEYPASSWD || command == Command.SELFCERT
                || (command == Command.LIST && param.getAlias() != null)) {
            if (!keyStore.containsAlias(param.getAlias())) {
                throw new KeytoolException("Alias <" + alias
                        + "> doesn't exist");
            }
        } else if (command == Command.GENKEY){
            if (keyStore.containsAlias(param.getAlias())) {
                throw new KeytoolException("Key(s) not generated, alias <"
                        + alias + "> already exists.");
            }
        }

        // if the key password has not been entered and the password is required
        // to get the key (it is not a password for a newly created entry)
        if (param.getKeyPass() == null
                && (command == Command.KEYCLONE || command == Command.EXPORT
                        || command == Command.CERTREQ
                        || command == Command.KEYPASSWD
                        || command == Command.SELFCERT
                // if keystore contains alias, import of a certificate reply
                // is considered, otherwise password is unnecessary.
                || (command == Command.IMPORT && keyStore.containsAlias(alias)))) {
            param.setKeyPass(tryStorePassAsKeyPass(keyStore, alias, param
                    .getStorePass()));
        }
        
        switch (command) {
            case GENKEY:
                // if the distinguished name is not specified, get the
                // necessary data
                if (param.getDName() == null && !param.isSecretKey()) {
                    param.setDName(getDistinguishedName());
                }
                // if the key password has not been entered (and can equal store
                // password)
                if (param.getKeyPass() == null) {
                    param.setKeyPass(getNewPassword(null, alias, param
                            .getStorePass()));
                }
                
                String issuerAlias = param.getIssuerAlias(); 
                // if the newly generated certificate should be signed with 
                // another certificate chain from the keystore. 
                if (issuerAlias != null && !param.isSecretKey()) {
                    // Check if the issuer password was entered. If not, try storepass.
                    // If it's not ok, prompt the user.
                    if (param.getIssuerPass() == null) {
                        param.setIssuerPass(tryStorePassAsKeyPass(keyStore, issuerAlias,
                                param.getStorePass()));
                    }
                }
                
                break;

            case KEYCLONE:
                // prompt for a destination alias, if one is not specified
                if (param.getDestAlias() == null) {
                    System.out.print("Enter destination alias name: ");
                    charsRead = in.read(readData);
                    if (charsRead <= newLineLength) {
                        throw new KeytoolException(
                                "Must specify destination alias");
                    } else {
                        param.setDestAlias(new String(readData).substring(0,
                                charsRead - newLineLength));
                    }
                }
                // if the password for a newly created entry is not specified,
                // ask for it.
                if (param.getNewPasswd() == null) {
                    param.setNewPasswd(getNewPassword(alias, param
                            .getDestAlias(), param.getKeyPass()));
                }
                break;
            case DELETE:
                // prompt for an alias to delete, if one is not specified
                if (alias == null) {
                    System.out.print("Enter alias name: ");
                    charsRead = in.read(readData);
                    if (charsRead <= newLineLength) {
                        throw new KeytoolException("Must specify alias");
                    } else {
                        param.setAlias(new String(readData).substring(0,
                                charsRead - newLineLength));
                    }
                }
                break;
            case STOREPASSWD:
            case KEYPASSWD:
                String prompt;
                String promptReenter;
                // prompt for a new password, if it is not specified
                if (command == Command.KEYPASSWD) {
                    prompt = "Enter new key password for <" + alias + ">: ";
                    promptReenter = "Re-enter new keystore password for <"
                            + alias + ">: ";
                } else { // if param.getCommand() == Command.STOREPASSWD
                    // prompt for a new store password, if it is not specified
                    prompt = "Enter new keystore password: ";
                    promptReenter = "Re-enter new keystore password: ";
                }

                // if the new password is not entered
                if (param.getNewPasswd() == null) {
                    System.out.print(prompt);
                    charsRead = in.read(readData);
                    char[] password = promptLongerPassword(prompt);
                    System.out.print(promptReenter);
                    charsRead = in.read(readData);
                    if (charsRead == password.length + newLineLength) {
                        for (int i = 0; i < password.length; i++) {
                            if (readData[i] != password[i]) {
                                throw new KeytoolException(
                                        "Passwords do not match");
                            }
                        }
                        param.setNewPasswd(password);
                    } else {
                        throw new KeytoolException("Passwords do not match");
                    }
                    // if entered a short password in the command line
                } else if (param.getNewPasswd().length < minPwdLength) {
                    throw new KeytoolException("The password must be at least "
                            + minPwdLength + " characters");
                }

                break;
            case LIST:
                if (alias != null) {
                    // This check is not where the same thing for other
                    // commands done, because (alias != null) check is
                    // necessary.
                    if (keyStore.entryInstanceOf(alias,
                            KeyStore.SecretKeyEntry.class)
                            && param.getKeyPass() == null) {
                        param.setKeyPass(tryStorePassAsKeyPass(keyStore, alias,
                                param.getStorePass()));
                    }
                }
                break;
        }// switch (param.getCommand())

    }