Example usage for java.security GeneralSecurityException getLocalizedMessage

List of usage examples for java.security GeneralSecurityException getLocalizedMessage

Introduction

In this page you can find the example usage for java.security GeneralSecurityException getLocalizedMessage.

Prototype

public String getLocalizedMessage() 

Source Link

Document

Creates a localized description of this throwable.

Usage

From source file:com.intuit.tank.proxy.Main.java

static SSLContextSelector getSSLContextSelector() throws GeneralSecurityException, IOException {
    File ks = new File("auto_generated_ca.p12");
    String type = "PKCS12";
    char[] password = "password".toCharArray();
    String alias = "CA";
    if (ks.exists()) {
        try {//from  w  ww  . j a  v a 2  s.  c o  m
            return new AutoGeneratingContextSelector(ks, type, password, password, alias);
        } catch (GeneralSecurityException e) {
            System.err.println("Error loading CA keys from keystore: " + e.getLocalizedMessage());
        } catch (IOException e) {
            System.err.println("Error loading CA keys from keystore: " + e.getLocalizedMessage());
        }
    }
    System.err.println("Generating a new CA");
    X500Principal ca = new X500Principal(
            "cn=OWASP Custom CA for Tank,ou=Tank Custom CA,o=Tank,l=Tank,st=Tank,c=Tank");
    AutoGeneratingContextSelector ssl = new AutoGeneratingContextSelector(ca);
    try {
        ssl.save(ks, type, password, password, alias);
    } catch (GeneralSecurityException e) {
        System.err.println("Error saving CA keys to keystore: " + e.getLocalizedMessage());
    } catch (IOException e) {
        System.err.println("Error saving CA keys to keystore: " + e.getLocalizedMessage());
    }
    FileWriter pem = null;
    try {
        pem = new FileWriter("auto_generated_ca.pem");
        pem.write(ssl.getCACert());
    } catch (IOException e) {
        System.err.println("Error writing CA cert : " + e.getLocalizedMessage());
    } finally {
        if (pem != null)
            pem.close();
    }
    return ssl;
}

From source file:org.owasp.proxy.Main.java

private static SSLContextSelector getClientSSLContextSelector(Configuration config) {
    String type = config.keystoreType;
    char[] password = config.keyStorePassword == null ? null : config.keyStorePassword.toCharArray();
    File location = config.keyStoreLocation == null ? null : new File(config.keyStoreLocation);
    if (type != null) {
        KeyStore ks = null;//from w  ww . jav a  2  s. co  m
        if (type.equals("PKCS11")) {
            try {
                int slot = config.pkcs11SlotLocation;
                ks = KeystoreUtils.getPKCS11Keystore("PKCS11", location, slot, password);
            } catch (Exception e) {
                System.err.println(e.getLocalizedMessage());
                System.exit(2);
            }
        } else {
            try {
                FileInputStream in = new FileInputStream(location);
                ks = KeyStore.getInstance(type);
                ks.load(in, password);
            } catch (Exception e) {
                System.err.println(e.getLocalizedMessage());
                System.exit(2);
            }
        }
        String alias = config.keyStoreAlias;
        if (alias == null) {
            try {
                Map<String, String> aliases = KeystoreUtils.getAliases(ks);
                if (aliases.size() > 0) {
                    System.err.println("Keystore contains the following aliases: \n");
                    for (String a : aliases.keySet()) {
                        System.err.println("Alias \"" + a + "\"" + " : " + aliases.get(a));
                    }
                    alias = aliases.keySet().iterator().next();
                    System.err.println("Using " + alias + " : " + aliases.get(alias));
                } else {
                    System.err.println("Keystore contains no aliases!");
                    System.exit(3);
                }
            } catch (KeyStoreException kse) {
                System.err.println(kse.getLocalizedMessage());
                System.exit(4);
            }
        }
        try {
            final X509KeyManager km = KeystoreUtils.getKeyManagerForAlias(ks, alias, password);
            return new DefaultClientContextSelector(km);
        } catch (GeneralSecurityException gse) {
            System.err.println(gse.getLocalizedMessage());
            System.exit(5);
        }
    }
    return new DefaultClientContextSelector();
}

From source file:org.owasp.proxy.Main.java

private static SSLContextSelector getServerSSLContextSelector() throws GeneralSecurityException, IOException {
    File ks = new File("ca.p12");
    String type = "PKCS12";
    char[] password = "password".toCharArray();
    String alias = "CA";
    if (ks.exists()) {
        try {/*from w  w w.  j a  va 2 s  . c o  m*/
            return new AutoGeneratingContextSelector(ks, type, password, password, alias);
        } catch (GeneralSecurityException e) {
            System.err.println("Error loading CA keys from keystore: " + e.getLocalizedMessage());
        } catch (IOException e) {
            System.err.println("Error loading CA keys from keystore: " + e.getLocalizedMessage());
        }
    }
    System.err.println("Generating a new CA");
    X500Principal ca = new X500Principal(
            "cn=OWASP Custom CA for " + java.net.InetAddress.getLocalHost().getHostName()
                    + ",ou=OWASP Custom CA,o=OWASP,l=OWASP,st=OWASP,c=OWASP");
    AutoGeneratingContextSelector ssl = new AutoGeneratingContextSelector(ca);
    try {
        ssl.save(ks, type, password, password, alias);
    } catch (GeneralSecurityException e) {
        System.err.println("Error saving CA keys to keystore: " + e.getLocalizedMessage());
    } catch (IOException e) {
        System.err.println("Error saving CA keys to keystore: " + e.getLocalizedMessage());
    }
    FileWriter pem = null;
    try {
        pem = new FileWriter("ca.pem");
        pem.write(ssl.getCACert());
    } catch (IOException e) {
        System.err.println("Error writing CA cert : " + e.getLocalizedMessage());
    } finally {
        if (pem != null)
            pem.close();
    }
    return ssl;
}

From source file:birch.filter.EncryptionFilter.java

private String encrypt(String target, String message) {
    String result;/*from www .  j ava2s  .  co  m*/

    result = message;
    try {

        result = PREFIX + "|" + targets.get(target).encrypt(message);

    } catch (GeneralSecurityException ex) {
        Logger.getLogger(EncryptionFilter.class.getName()).log(Level.WARNING, ex.getLocalizedMessage());
    }

    return result;
}

From source file:birch.filter.EncryptionFilter.java

private String decrypt(String target, String message) {
    String[] splited;// w  ww .j a v a  2s  .co  m
    Encryptor encryptor;

    splited = message.split("\\|", 3);
    try {

        if (splited.length == 3) {
            encryptor = targets.get(target);
            if (encryptor == null) {
                Logger.getLogger(EncryptionFilter.class.getName()).log(Level.INFO,
                        "No Encryptor for " + target);
            } else {
                return encryptedPrefix + encryptor.decrypt(splited[1], splited[2]);
            }
        }

    } catch (GeneralSecurityException ex) {
        Logger.getLogger(EncryptionFilter.class.getName()).log(Level.WARNING, ex.getLocalizedMessage());
    }

    return message;
}

From source file:birch.filter.EncryptionFilter.java

public EncryptionFilter(Filter filter, Config config) {
    super(filter);

    this.targets = new HashMap<String, Encryptor>();
    this.keyToTarget = new HashMap<String, String>();
    linebreakFilter = new LinebreakFilter(Filter.nullObject);

    keyParsers = new ArrayList();
    keyParsers.add(new OTPKeyParser());
    keyParsers.add(new ExtendedKeyParser());
    keyParsers.add(new DefaultKeyParser());

    encryptedPrefix = config.getProperties().get("prefixencrypted") == null ? ""
            : config.getProperties().get("prefixencrypted");
    plainPrefix = config.getProperties().get("prefixplain") == null ? ""
            : config.getProperties().get("prefixplain");
    ignorePrefix = config.getProperties().get("ignoreprefix") == null ? ""
            : config.getProperties().get("ignoreprefix");

    if (ignorePrefix.matches(".*[^\\\\]*(\\\\\\\\)*$")) {
        activeEncryptedPattern = Pattern
                .compile(MessageFormat.format("(?:{0})?{1}", ignorePrefix, encryptedPattern.pattern()));
    } else {/*from   w  w w  .jav  a2s  .  c  o m*/
        activeEncryptedPattern = encryptedPattern;
    }

    Logger logger = Logger.getLogger(EncryptionFilter.class.getName());
    logger.finer("encryptedPrefix: " + encryptedPrefix);
    logger.finer("plainPrefix: " + plainPrefix);
    logger.finer("ignoreprefix: " + ignorePrefix);
    logger.finer("activeEncryptedPattern: " + activeEncryptedPattern.pattern());

    String key;
    Encryptor encryptor;
    Map<String, String> configTargets;
    configTargets = config.getTargets();

    for (String target : configTargets.keySet()) {
        try {

            key = configTargets.get(target);
            if (key != null) {
                encryptor = getEncryptor(key);
                if (encryptor != null) {
                    this.targets.put(target, encryptor);
                    this.keyToTarget.put(key, target);
                }
            }

        } catch (GeneralSecurityException ex) {
            Logger.getLogger(EncryptionFilter.class.getName()).log(Level.WARNING, ex.getLocalizedMessage());
        } catch (IOException ex) {
            Logger.getLogger(EncryptionFilter.class.getName()).log(Level.WARNING, ex.getLocalizedMessage());
        }
    }
}