Example usage for java.security NoSuchAlgorithmException getMessage

List of usage examples for java.security NoSuchAlgorithmException getMessage

Introduction

In this page you can find the example usage for java.security NoSuchAlgorithmException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.everrest.websockets.client.WSClient.java

private void validateResponseHeaders() throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String line = br.readLine();// w  w  w. ja v  a 2 s  . co  m
    if (line != null && !line.startsWith("HTTP/1.1 101")) {
        throw new IOException("Invalid server response. Expected status is 101 'Switching Protocols'. ");
    }

    Map<String, String> headers = new HashMap<>();
    while (!((line = br.readLine()) == null || line.isEmpty())) {
        int colon = line.indexOf(':');
        if (colon > 0 && colon < line.length()) {
            headers.put(line.substring(0, colon).trim().toLowerCase(), line.substring(colon + 1).trim());
        }
    }

    // 'Upgrade' header
    String header = headers.get("upgrade");
    if (!"websocket".equals(header)) {
        throw new IOException(String
                .format("Invalid 'Upgrade' response header. Returned '%s' but 'websocket' expected. ", header));
    }

    // 'Connection' header
    header = headers.get("connection");
    if (!"upgrade".equals(header)) {
        throw new IOException(String.format(
                "Invalid 'Connection' response header. Returned '%s' but 'upgrade' expected. ", header));
    }

    // 'Sec-WebSocket-Accept' header
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        // should never happen.
        throw new IllegalStateException(e.getMessage(), e);
    }
    md.reset();
    byte[] digest = md.digest((secWebSocketKey + GLOBAL_WS_SERVER_UUID).getBytes());
    final String expectedWsSecurityAccept = Base64.encodeBase64String(digest);
    header = headers.get("sec-websocket-accept");
    if (!expectedWsSecurityAccept.equals(header)) {
        throw new IOException("Invalid 'Sec-WebSocket-Accept' response header.");
    }
}

From source file:org.josso.selfservices.password.generator.PasswordGeneratorImpl.java

/**
 * @param numberOfPasswords the number of passwords to be generated.
 * @return a list of passwords or <em>null</em> if no suitable passwords
 *         could be generated.//from ww  w . j  av  a2  s . c  o m
 */
public List<String> process(int numberOfPasswords) {

    log.debug(Messages.getString("PwGenerator.PASSWORD_GENERATOR"));

    ArrayList<String> passwords = new ArrayList<String>();

    if (isUseSimpleRandom()) {
        random = randomFactory.getRandom();
        log.debug(Messages.getString("PwGenerator.NORMAL_RANDOM"));
    }

    try {
        random = randomFactory.getSecureRandom(getSecureRandomAlgorithm(), getSecureRandomProvider());
        log.debug(Messages.getString("PwGenerator.SEC_ALG") + getSecureRandomAlgorithm()
                + Messages.getString("PwGenerator.PROV") + getSecureRandomProvider()
                + Messages.getString("PwGenerator.DOR"));
    } catch (NoSuchAlgorithmException e) {
        log.error(Messages.getString("PwGenerator.ERROR") + e.getMessage()
                + Messages.getString("PwGenerator.NEW_LINE"));
        log.debug(Messages.getString("PwGenerator.DEFAUL_RANDOM"));
    } catch (NoSuchProviderException e) {
        log.error(Messages.getString("PwGenerator.ERROR") + e.getMessage()
                + Messages.getString("PwGenerator.NEW_LINE"));
        log.error(Messages.getString("PwGenerator.DEFAUL_RANDOM"));
    }

    if (isGenerateNumerals()) {
        passwordFlags |= PW_DIGITS;
        log.debug(Messages.getString("PwGenerator.DIGITS_ON"));
    } else {
        passwordFlags &= ~PW_DIGITS;
        log.debug(Messages.getString("PwGenerator.DIGITS_OFF"));
    }

    if (isGenerateCapitalLetters()) {
        passwordFlags |= PW_UPPERS;
        log.debug(Messages.getString("PwGenerator.UPPERCASE_ON"));
    } else {
        passwordFlags &= ~PW_UPPERS;
        log.debug(Messages.getString("PwGenerator.UPPERCASE_OFF"));
    }

    if (isIncludeAmbigousChars()) {
        passwordFlags |= PW_AMBIGUOUS;
        log.debug(Messages.getString("PwGenerator.AMBIGOUS_ON"));
    } else {
        passwordFlags &= ~PW_AMBIGUOUS;
        log.debug(Messages.getString("PwGenerator.AMBIGOUS_OFF"));
    }

    if (isIncludeSpecialSymbols()) {
        passwordFlags |= PW_SYMBOLS;
        log.debug(Messages.getString("PwGenerator.SYMBOLS_ON"));
    } else {
        passwordFlags &= ~PW_SYMBOLS;
        log.debug(Messages.getString("PwGenerator.SYMBOLS_OFF"));
    }

    if (isRegexStartsNoSmallLetter())
        passwordFlags |= REGEX_STARTS_NO_SMALL_LETTER_FLAG;

    if (isRegexEndsNoSmallLetter())
        passwordFlags |= REGEX_STARTS_NO_SMALL_LETTER_FLAG;

    if (isRegexStartsNoUpperLetter())
        passwordFlags |= REGEX_STARTS_NO_UPPER_LETTER_FLAG;

    if (isRegexEndsNosUpperLetter())
        passwordFlags |= REGEX_ENDS_NO_UPPER_LETTER_FLAG;

    if (isRegexEndsNoDigit())
        passwordFlags |= REGEX_ENDS_NO_DIGIT_FLAG;

    if (isRegexStartsNoDigit())
        passwordFlags |= REGEX_STARTS_NO_DIGIT_FLAG;

    if (isRegexStartsNoSymbol())
        passwordFlags |= REGEX_STARTS_NO_SYMBOL_FLAG;

    if (isRegexEndsNoSymbol())
        passwordFlags |= REGEX_ENDS_NO_SYMBOL_FLAG;

    if (isRegexOnlyOneCapital())
        passwordFlags |= REGEX_ONLY_1_CAPITAL_FLAG;

    if (isRegexOnlyOneSymbol())
        passwordFlags |= REGEX_ONLY_1_SYMBOL_FLAG;

    if (isRegexAtLeastTwoSymbols())
        passwordFlags |= REGEX_AT_LEAST_2_SYMBOLS_FLAG;

    if (isRegexOnlyOneDigit())
        passwordFlags |= REGEX_ONLY_1_DIGIT_FLAG;

    if (isRegexAtLeastTwoDigits())
        passwordFlags |= REGEX_AT_LEAST_2_DIGITS_FLAG;
    // -------------------------------------------------------------------

    log.debug(Messages.getString("PwGenerator.GENRIC_FLAGS"));

    int res = passwordFlags & PW_DIGITS;
    log.debug(Messages.getString("PwGenerator.DIGITS") + (res != 0));
    res = passwordFlags & PW_AMBIGUOUS;
    log.debug(Messages.getString("PwGenerator.AMBIGOUS") + (res != 0));
    res = passwordFlags & PW_SYMBOLS;
    log.debug(Messages.getString("PwGenerator.SYMBOLS") + (res != 0));
    res = passwordFlags & PW_UPPERS;
    log.debug(Messages.getString("PwGenerator.UPPERS") + (res != 0));
    log.debug(Messages.getString("PwGenerator.SEPARATOR"));

    log.debug(Messages.getString("PwGenerator.GENERATING") + numberOfPasswords
            + Messages.getString("PwGenerator.PW_LENGTH") + passwordLength);
    log.debug(Messages.getString("PwGenerator.PW"));

    int i;
    for (i = 0; i < numberOfPasswords; i++) {
        String password = generatePassword(passwordLength, passwordFlags);
        if (password != null)
            passwords.add(password);
    }
    return passwords;
}

From source file:org.openbaton.marketplace.core.VNFPackageManagement.java

private CloseableHttpClient getHttpClientForSsl(RequestConfig config) {
    SSLContext sslContext = null;
    try {//from   w  w w  .j  a va 2s . co  m
        sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
    } catch (NoSuchAlgorithmException e) {
        log.error("Could not initialize the HttpClient for SSL connections");
        log.error(e.getMessage(), e);
    } catch (KeyManagementException e) {
        log.error("Could not initialize the HttpClient for SSL connections");
        log.error(e.getMessage(), e);
    } catch (KeyStoreException e) {
        log.error("Could not initialize the HttpClient for SSL connections");
        log.error(e.getMessage(), e);
    }

    // necessary to trust self signed certificates
    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
            new String[] { "TLSv1" }, null, new NoopHostnameVerifier());

    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", sslConnectionSocketFactory).build();

    return HttpClientBuilder.create().setDefaultRequestConfig(config)
            .setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry))
            .setSSLSocketFactory(sslConnectionSocketFactory).build();
}

From source file:com.google.code.commons.checksum.digest.DigestUtils.java

/**
 * Returns a <code>MessageDigest</code> for the given <code>algorithm</code> .
 * //from ww w .jav a  2 s . c  o m
 * @param algorithm
 *            the name of the algorithm requested. See <a href=
 *            "http://java.sun.com/j2se/1.3/docs/guide/security/CryptoSpec.html#AppA" >Appendix A in the Java
 *            Cryptography Architecture API Specification &amp; Reference</a> for information about standard algorithm
 *            names.
 * @return a Message Digest object that implements the specified algorithm.
 * @see MessageDigest#getInstance(String)
 * @throws RuntimeException
 *             when a {@link java.security.NoSuchAlgorithmException} is caught.
 * @since Commons Checksum 1.0
 */
protected static MessageDigest getDigest(String algorithm) {
    try {
        String providerName = getDigestProviderName(algorithm);
        if (providerName == null) {
            return MessageDigest.getInstance(algorithm);
        }
        return MessageDigest.getInstance(algorithm, providerName);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    } catch (NoSuchProviderException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskSMWar.CFAsteriskSMWarChangePasswordHtml.java

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */// w  w w. j a  v a 2s.c o  m
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    final String S_ProcName = "doPost";

    ICFAsteriskSchemaObj schemaObj;
    HttpSession sess = request.getSession(false);
    if (sess == null) {
        sess = request.getSession(true);
        schemaObj = new CFAsteriskSchemaPooledObj();
        sess.setAttribute("SchemaObj", schemaObj);
    } else {
        schemaObj = (ICFAsteriskSchemaObj) sess.getAttribute("SchemaObj");
        if (schemaObj == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                    "schemaObj");
        }
    }

    ICFAsteriskSchema dbSchema = null;
    try {
        CFSecurityAuthorization auth = schemaObj.getAuthorization();
        if (auth == null) {
            response.sendRedirect("CFAsteriskSMWarLoginHtml");
            return;
        }

        dbSchema = (ICFAsteriskSchema) CFAsteriskSchemaPool.getSchemaPool().getInstance();
        schemaObj.setBackingStore(dbSchema);
        schemaObj.beginTransaction();
        ICFSecuritySecUserObj systemUser = schemaObj.getSecUserTableObj().readSecUserByULoginIdx("system");
        String passwordHash = systemUser.getRequiredPasswordHash();
        if ((passwordHash == null) || (passwordHash.length() <= 0) || passwordHash.equals("bootstrap")) {
            response.sendRedirect("CFAsteriskSMWarSetSystemPasswordHtml");
        }

        ICFSecuritySecUserObj secUser = schemaObj.getSecUserTableObj().readSecUserByIdIdx(auth.getSecUserId());
        if (secUser == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                    "secUser");
        }

        ICFSecurityClusterObj secCluster = schemaObj.getClusterTableObj()
                .readClusterByIdIdx(auth.getSecClusterId());
        if (secCluster == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                    "secCluster");
        }
        String clusterDescription = secCluster.getRequiredDescription();

        String oldPassword = (String) request.getParameter("OldPassword");
        if ((oldPassword == null) || (oldPassword.length() <= 0)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarChangePasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("Old Password must be specified.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + secUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Old Password:</th><td><input type=\"password\" name=\"OldPassword\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAsteriskSMWarLoginHtml\">Take me to the "
                            + secCluster.getRequiredDescription() + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        String password = (String) request.getParameter("Password");
        if ((password == null) || (password.length() <= 0)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarChangePasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("New Password must be specified.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + secUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Old Password:</th><td><input type=\"password\" name=\"OldPassword\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAsteriskSMWarLoginHtml\">Take me to the "
                            + secCluster.getRequiredDescription() + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        String confirmPassword = (String) request.getParameter("ConfirmPassword");
        if ((confirmPassword == null) || (confirmPassword.length() <= 0)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarChangePasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("Confirm New Password must be specified.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + secUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Old Password:</th><td><input type=\"password\" name=\"OldPassword\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAsteriskSMWarLoginHtml\">Take me to the "
                            + secCluster.getRequiredDescription() + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        if (!confirmPassword.equals(password)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarChangePasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("New Password and Confirm New Password do not match.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + secUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Old Password:</th><td><input type=\"password\" name=\"OldPassword\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAsteriskSMWarLoginHtml\">Take me to the "
                            + secCluster.getRequiredDescription() + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        MessageDigest msgDigest = MessageDigest.getInstance("SHA-512");
        msgDigest.update(oldPassword.getBytes("UTF-8"));
        byte[] hash = msgDigest.digest();
        byte[] encodedHash = Base64.encodeBase64(hash);
        msgDigest.update(encodedHash);
        hash = msgDigest.digest();
        encodedHash = Base64.encodeBase64(hash);
        String oldHashedAndEncodedPassword = new String(encodedHash);

        if (!oldHashedAndEncodedPassword.equals(secUser.getRequiredPasswordHash())) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarChangePasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("Old Password is invalid.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + secUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Old Password:</th><td><input type=\"password\" name=\"OldPassword\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAsteriskSMWarLoginHtml\">Take me to the "
                            + secCluster.getRequiredDescription() + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        msgDigest.update(password.getBytes("UTF-8"));
        hash = msgDigest.digest();
        encodedHash = Base64.encodeBase64(hash);
        msgDigest.update(encodedHash);
        hash = msgDigest.digest();
        encodedHash = Base64.encodeBase64(hash);
        String newHashedAndEncodedPassword = new String(encodedHash);

        ICFSecurityClusterObj systemCluster = schemaObj.getClusterTableObj()
                .readClusterByUDomainNameIdx("system");
        ICFSecurityTenantObj systemTenant = schemaObj.getTenantTableObj()
                .readTenantByUNameIdx(systemCluster.getRequiredId(), "system");
        ICFSecuritySecSessionObj systemSession = schemaObj.getSecSessionTableObj().newInstance();
        ICFSecuritySecSessionEditObj editSystemSession = (ICFSecuritySecSessionEditObj) systemSession
                .beginEdit();
        editSystemSession.setRequiredContainerSecUser(systemUser);
        editSystemSession.setRequiredStart(Calendar.getInstance());
        systemSession = editSystemSession.create();
        editSystemSession.endEdit();

        CFSecurityAuthorization oldAuth = schemaObj.getAuthorization();

        auth = new CFSecurityAuthorization();
        auth.setSecCluster(systemCluster);
        auth.setSecTenant(systemTenant);
        auth.setSecSession(systemSession);
        schemaObj.setAuthorization(auth);

        ICFSecuritySecUserEditObj editSecUser = secUser.beginEdit();
        editSecUser.setRequiredPasswordHash(newHashedAndEncodedPassword);
        editSecUser.update();
        editSecUser.endEdit();

        editSystemSession = (ICFSecuritySecSessionEditObj) systemSession.beginEdit();
        editSystemSession.setOptionalFinish(Calendar.getInstance());
        editSystemSession.update();
        editSystemSession.endEdit();

        schemaObj.commit();

        schemaObj.setAuthorization(oldAuth);

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
        out.println("<HTML>");
        out.println("<BODY>");
        out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarChangePasswordHtml\">");
        out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
        out.println("<H2 style=\"text-align:center\">Password Set.</H2>");
        out.println("<p>");
        out.println("<center>");
        out.println("<table style=\"width:75%\">");
        out.println(
                "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAsteriskSMWarLoginHtml\">Take me back to the "
                        + secCluster.getRequiredDescription() + " Security Manager Main page</A></td></tr>");
        out.println("</table>");
        out.println("</center>");
        out.println("</form>");
        out.println("</BODY>");
        out.println("</HTML>");
    } catch (NoSuchAlgorithmException e) {
        throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                "Caught NoSuchAlgorithmException -- " + e.getMessage(), e);
    } catch (RuntimeException e) {
        throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                "Caught RuntimeException -- " + e.getMessage(), e);
    } finally {
        if (dbSchema != null) {
            try {
                if (schemaObj.isTransactionOpen()) {
                    schemaObj.rollback();
                }
            } catch (RuntimeException e) {
            }
            schemaObj.setBackingStore(null);
            CFAsteriskSchemaPool.getSchemaPool().releaseInstance(dbSchema);
        }
    }
}

From source file:org.cesecore.keys.util.KeyStoreTools.java

/** Generates keys in the Keystore token.
 * @param spec AlgorithmParameterSpec for the KeyPairGenerator. Can be anything like RSAKeyGenParameterSpec, DSAParameterSpec, ECParameterSpec or ECGenParameterSpec. 
 * @param keyEntryName/*from   www  . j  a  v  a2 s .co  m*/
 */
public void generateKeyPair(final AlgorithmParameterSpec spec, final String keyEntryName)
        throws InvalidAlgorithmParameterException, CertificateException, IOException {
    if (log.isTraceEnabled()) {
        log.trace(">generate from AlgorithmParameterSpec: " + spec.getClass().getName());
    }
    // Generate the Keypair
    String algorithm = "EC";
    String sigAlg = "SHA1withECDSA";
    String specName = spec.getClass().getName();
    if (specName.contains("DSA")) {
        algorithm = "DSA";
        sigAlg = "SHA1withDSA";
    } else if (specName.contains("RSA")) {
        algorithm = "RSA";
        sigAlg = "SHA1withRSA";
    }
    KeyPairGenerator kpg;
    try {
        kpg = KeyPairGenerator.getInstance(algorithm, this.providerName);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Algorithm " + algorithm + " was not recognized.", e);
    } catch (NoSuchProviderException e) {
        throw new IllegalStateException("BouncyCastle was not found as a provider.", e);
    }
    try {
        kpg.initialize(spec);
    } catch (InvalidAlgorithmParameterException e) {
        log.debug("Algorithm parameters not supported: " + e.getMessage());
        throw e;
    }
    generateKeyPair(kpg, keyEntryName, sigAlg);
    if (log.isTraceEnabled()) {
        log.trace("<generate from AlgorithmParameterSpec: " + spec.getClass().getName());
    }
}

From source file:org.openbaton.sdk.api.util.RestRequest.java

private CloseableHttpClient getHttpClientForSsl() {
    SSLContext sslContext = null;
    try {// ww  w. ja  v a 2  s.  com
        sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
    } catch (NoSuchAlgorithmException e) {
        log.error("Could not initialize the HttpClient for SSL connections");
        log.error(e.getMessage(), e);
    } catch (KeyManagementException e) {
        log.error("Could not initialize the HttpClient for SSL connections");
        log.error(e.getMessage(), e);
    } catch (KeyStoreException e) {
        log.error("Could not initialize the HttpClient for SSL connections");
        log.error(e.getMessage(), e);
    }

    // necessary to trust self signed certificates
    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
            new String[] { "TLSv1" }, null, new NoopHostnameVerifier());

    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", sslConnectionSocketFactory).build();

    return HttpClientBuilder.create().setDefaultRequestConfig(config)
            .setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry))
            .setSSLSocketFactory(sslConnectionSocketFactory).build();
}

From source file:eu.europa.ec.markt.dss.signature.xades.XAdESProfileBES.java

/**
 * The ID of xades:SignedProperties is contained in the signed content of the xades Signature. We must create this
 * ID in a deterministic way. The signingDate and signingCertificate are mandatory in the more basic level of
 * signature, we use them as "seed" for generating the ID.
 * //w  w  w. ja v a  2s.  c  o  m
 * @param params
 * @return
 */
String computeDeterministicId(SignatureParameters params) {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(Long.toString(params.getSigningDate().getTime()).getBytes());
        digest.update(params.getSigningCertificate().getEncoded());
        String md5id = "id" + Hex.encodeHexString(digest.digest());
        return md5id;
    } catch (NoSuchAlgorithmException ex) {
        LOG.severe(ex.getMessage());
        throw new RuntimeException("MD5 Algorithm not found !");
    } catch (CertificateEncodingException ex) {
        throw new RuntimeException("Certificate encoding exception");
    }
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_2.CFAstSMWar.CFAstSMWarChangePasswordHtml.java

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 *//*w  w  w  . java  2  s .  c o m*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    final String S_ProcName = "doPost";

    ICFAstSchemaObj schemaObj;
    HttpSession sess = request.getSession(false);
    if (sess == null) {
        sess = request.getSession(true);
        schemaObj = new CFAstSchemaObj();
        sess.setAttribute("SchemaObj", schemaObj);
    } else {
        schemaObj = (ICFAstSchemaObj) sess.getAttribute("SchemaObj");
        if (schemaObj == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                    "schemaObj");
        }
    }

    ICFAstSchema dbSchema = null;
    try {
        CFAstAuthorization auth = schemaObj.getAuthorization();
        if (auth == null) {
            response.sendRedirect("CFAstSMWarLoginHtml");
            return;
        }

        dbSchema = CFAstSchemaPool.getSchemaPool().getInstance();
        schemaObj.setBackingStore(dbSchema);
        schemaObj.beginTransaction();
        ICFAstSecUserObj systemUser = schemaObj.getSecUserTableObj().readSecUserByULoginIdx("system");
        String passwordHash = systemUser.getRequiredPasswordHash();
        if ((passwordHash == null) || (passwordHash.length() <= 0) || passwordHash.equals("bootstrap")) {
            response.sendRedirect("CFAstSMWarSetSystemPasswordHtml");
        }

        ICFAstSecUserObj secUser = schemaObj.getSecUserTableObj().readSecUserByIdIdx(auth.getSecUserId());
        if (secUser == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                    "secUser");
        }

        ICFAstClusterObj secCluster = schemaObj.getClusterTableObj().readClusterByIdIdx(auth.getSecClusterId());
        if (secCluster == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                    "secCluster");
        }
        String clusterDescription = secCluster.getRequiredDescription();

        String oldPassword = (String) request.getParameter("OldPassword");
        if ((oldPassword == null) || (oldPassword.length() <= 0)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAstSMWarChangePasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("Old Password must be specified.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + secUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Old Password:</th><td><input type=\"password\" name=\"OldPassword\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAstSMWarLoginHtml\">Take me to the "
                            + secCluster.getRequiredDescription() + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        String password = (String) request.getParameter("Password");
        if ((password == null) || (password.length() <= 0)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAstSMWarChangePasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("New Password must be specified.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + secUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Old Password:</th><td><input type=\"password\" name=\"OldPassword\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAstSMWarLoginHtml\">Take me to the "
                            + secCluster.getRequiredDescription() + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        String confirmPassword = (String) request.getParameter("ConfirmPassword");
        if ((confirmPassword == null) || (confirmPassword.length() <= 0)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAstSMWarChangePasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("Confirm New Password must be specified.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + secUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Old Password:</th><td><input type=\"password\" name=\"OldPassword\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAstSMWarLoginHtml\">Take me to the "
                            + secCluster.getRequiredDescription() + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        if (!confirmPassword.equals(password)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAstSMWarChangePasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("New Password and Confirm New Password do not match.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + secUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Old Password:</th><td><input type=\"password\" name=\"OldPassword\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAstSMWarLoginHtml\">Take me to the "
                            + secCluster.getRequiredDescription() + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        MessageDigest msgDigest = MessageDigest.getInstance("SHA-512");
        msgDigest.update(oldPassword.getBytes("UTF-8"));
        byte[] hash = msgDigest.digest();
        byte[] encodedHash = Base64.encodeBase64(hash);
        msgDigest.update(encodedHash);
        hash = msgDigest.digest();
        encodedHash = Base64.encodeBase64(hash);
        String oldHashedAndEncodedPassword = new String(encodedHash);

        if (!oldHashedAndEncodedPassword.equals(secUser.getRequiredPasswordHash())) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAstSMWarChangePasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("Old Password is invalid.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + secUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Old Password:</th><td><input type=\"password\" name=\"OldPassword\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAstSMWarLoginHtml\">Take me to the "
                            + secCluster.getRequiredDescription() + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        msgDigest.update(password.getBytes("UTF-8"));
        hash = msgDigest.digest();
        encodedHash = Base64.encodeBase64(hash);
        msgDigest.update(encodedHash);
        hash = msgDigest.digest();
        encodedHash = Base64.encodeBase64(hash);
        String newHashedAndEncodedPassword = new String(encodedHash);

        ICFAstClusterObj systemCluster = schemaObj.getClusterTableObj().readClusterByUDomainNameIdx("system");
        ICFAstTenantObj systemTenant = schemaObj.getTenantTableObj()
                .readTenantByUNameIdx(systemCluster.getRequiredId(), "system");
        ICFAstSecSessionObj systemSession = schemaObj.getSecSessionTableObj().newInstance();
        ICFAstSecSessionEditObj editSystemSession = (ICFAstSecSessionEditObj) systemSession.beginEdit();
        editSystemSession.setRequiredContainerSecUser(systemUser);
        editSystemSession.setRequiredStart(Calendar.getInstance());
        systemSession = editSystemSession.create();
        editSystemSession.endEdit();

        CFAstAuthorization oldAuth = schemaObj.getAuthorization();

        auth = new CFAstAuthorization();
        auth.setSecCluster(systemCluster);
        auth.setSecTenant(systemTenant);
        auth.setSecSession(systemSession);
        schemaObj.setAuthorization(auth);

        ICFAstSecUserEditObj editSecUser = secUser.beginEdit();
        editSecUser.setRequiredPasswordHash(newHashedAndEncodedPassword);
        editSecUser.update();
        editSecUser.endEdit();

        editSystemSession = (ICFAstSecSessionEditObj) systemSession.beginEdit();
        editSystemSession.setOptionalFinish(Calendar.getInstance());
        editSystemSession.update();
        editSystemSession.endEdit();

        schemaObj.commit();

        schemaObj.setAuthorization(oldAuth);

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
        out.println("<HTML>");
        out.println("<BODY>");
        out.println("<form method=\"post\" formaction=\"CFAstSMWarChangePasswordHtml\">");
        out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
        out.println("<H2 style=\"text-align:center\">Password Set.</H2>");
        out.println("<p>");
        out.println("<center>");
        out.println("<table style=\"width:75%\">");
        out.println(
                "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAstSMWarLoginHtml\">Take me back to the "
                        + secCluster.getRequiredDescription() + " Security Manager Main page</A></td></tr>");
        out.println("</table>");
        out.println("</center>");
        out.println("</form>");
        out.println("</BODY>");
        out.println("</HTML>");
    } catch (NoSuchAlgorithmException e) {
        throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                "Caught NoSuchAlgorithmException -- " + e.getMessage(), e);
    } catch (RuntimeException e) {
        throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                "Caught RuntimeException -- " + e.getMessage(), e);
    } finally {
        if (dbSchema != null) {
            try {
                if (schemaObj.isTransactionOpen()) {
                    schemaObj.rollback();
                }
            } catch (RuntimeException e) {
            }
            schemaObj.setBackingStore(null);
            CFAstSchemaPool.getSchemaPool().releaseInstance(dbSchema);
        }
    }
}

From source file:net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskSMWar.CFAsteriskSMWarResetPasswordHtml.java

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 *//*from   www  .  j ava 2  s  . c  om*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    final String S_ProcName = "doPost";

    ICFAsteriskSchemaObj schemaObj;
    HttpSession sess = request.getSession(false);
    if (sess == null) {
        sess = request.getSession(true);
        schemaObj = new CFAsteriskSchemaPooledObj();
        sess.setAttribute("SchemaObj", schemaObj);
    } else {
        schemaObj = (ICFAsteriskSchemaObj) sess.getAttribute("SchemaObj");
        if (schemaObj == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                    "schemaObj");
        }
    }

    ICFAsteriskSchema dbSchema = null;
    try {
        CFSecurityAuthorization auth = schemaObj.getAuthorization();
        if (auth != null) {
            response.sendRedirect("CFAsteriskSMWarSecurityMainHtml");
            return;
        }

        dbSchema = (ICFAsteriskSchema) CFAsteriskSchemaPool.getSchemaPool().getInstance();
        schemaObj.setBackingStore(dbSchema);
        schemaObj.beginTransaction();
        ICFSecuritySecUserObj systemUser = schemaObj.getSecUserTableObj().readSecUserByULoginIdx("system");
        String passwordHash = systemUser.getRequiredPasswordHash();
        if ((passwordHash == null) || (passwordHash.length() <= 0) || passwordHash.equals("bootstrap")) {
            response.sendRedirect("CFAsteriskSMWarSetSystemPasswordHtml");
            return;
        }

        ICFSecurityClusterObj resolvedCluster;
        ICFSecuritySysClusterObj sysCluster = schemaObj.getSysClusterTableObj().readSysClusterByIdIdx(1, false);
        if (sysCluster == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0,
                    "sysCluster");
        }
        resolvedCluster = sysCluster.getRequiredContainerCluster();
        if (resolvedCluster == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName,
                    "resolvedCluster");
        }
        String clusterDescription = resolvedCluster.getRequiredDescription();

        String resetUUIDStr = (String) request.getParameter("ResetUUID");
        if ((resetUUIDStr == null) || (resetUUIDStr.length() <= 0)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarResetPasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p>");
            out.println("<center>");
            out.println("<p>");
            out.println(
                    "The ResetUUID parameter was missing in your request.  Please use the link provided by your Password Reset email.");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        UUID resetUUID = UUID.fromString(resetUUIDStr);
        if (resetUUID == null) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarResetPasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p>");
            out.println("<center>");
            out.println("<p>");
            out.println("Invalid ResetUUID \"" + resetUUIDStr
                    + "\".  Please use the link provided by your Password Reset email.");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        ICFSecuritySecUserObj resetUser = null;
        Iterator<ICFSecuritySecUserObj> secUserForUUID = schemaObj.getSecUserTableObj()
                .readSecUserByPwdResetIdx(resetUUID).iterator();
        if (secUserForUUID.hasNext()) {
            resetUser = secUserForUUID.next();
            if (secUserForUUID.hasNext()) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Multiple SecUser instances found for ResetUUID \"" + resetUUIDStr + "\"");
            }
        } else {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarResetPasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p>");
            out.println("<center>");
            out.println("<p>");
            out.println("Invalid ResetUUID \"" + resetUUIDStr
                    + "\".  Please use the link provided by your HTML Passsword Reset email.");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        String password = (String) request.getParameter("Password");
        if ((password == null) || (password.length() <= 0)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarResetPasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("New Password must be specified.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Reset UUID:</th><td><input type=\"text\" name=\"ResetUUID\" readonly=\"true\" value=\""
                            + resetUUID.toString() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + resetUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAsteriskSMWarLoginHtml\">Take me to the "
                            + clusterDescription + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        String confirmPassword = (String) request.getParameter("ConfirmPassword");
        if ((confirmPassword == null) || (confirmPassword.length() <= 0)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarResetPasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("Confirm New Password must be specified.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Reset UUID:</th><td><input type=\"text\" name=\"ResetUUID\" readonly=\"true\" value=\""
                            + resetUUID.toString() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + resetUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAsteriskSMWarLoginHtml\">Take me to the "
                            + clusterDescription + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        if (!confirmPassword.equals(password)) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
            out.println("<HTML>");
            out.println("<BODY>");
            out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarResetPasswordHtml\">");
            out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
            out.println("<H2 style=\"text-align:center\">ERROR</H2>");
            out.println("<p style=\"text-align:center\">");
            out.println("New Password and Confirm New Password do not match.");
            out.println("<p>");
            out.println("<center>");
            out.println("<table style=\"width:75%\">");
            out.println(
                    "<tr><th style=\"text-align:left\">Reset UUID:</th><td><input type=\"text\" name=\"ResetUUID\" readonly=\"true\" value=\""
                            + resetUUID.toString() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Login Id:</th><td><input type=\"text\" name=\"LoginId\" readonly=\"true\" value=\""
                            + resetUser.getRequiredEMailAddress() + "\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">New Password:</th><td><input type=\"password\" name=\"Password\"/></td></tr>");
            out.println(
                    "<tr><th style=\"text-align:left\">Confirm New Password:</th><td><input type=\"password\" name=\"ConfirmPassword\"/></td></tr>");
            out.println(
                    "<tr><td colspan=\"2\" style=\"text-align:center\"><button type=\"submit\" name=\"Ok\"\">Ok</button></td></tr>");
            out.println(
                    "<tr><td colSpan=\"2\" style=\"text-align:center\"><A HRef=\"CFAsteriskSMWarLoginHtml\">Take me to the "
                            + clusterDescription + " Security Manager Login</A></td></tr>");
            out.println("</table>");
            out.println("</center>");
            out.println("</form>");
            out.println("</BODY>");
            out.println("</HTML>");
            return;
        }

        MessageDigest msgDigest = MessageDigest.getInstance("SHA-512");
        msgDigest.update(password.getBytes("UTF-8"));
        byte[] hash = msgDigest.digest();
        byte[] encodedHash = Base64.encodeBase64(hash);
        msgDigest.update(encodedHash);
        hash = msgDigest.digest();
        encodedHash = Base64.encodeBase64(hash);
        String hashedAndEncodedPassword = new String(encodedHash);

        ICFSecurityClusterObj systemCluster = schemaObj.getClusterTableObj()
                .readClusterByUDomainNameIdx("system");
        ICFSecurityTenantObj systemTenant = schemaObj.getTenantTableObj()
                .readTenantByUNameIdx(systemCluster.getRequiredId(), "system");
        ICFSecuritySecSessionObj systemSession = schemaObj.getSecSessionTableObj().newInstance();
        ICFSecuritySecSessionEditObj editSystemSession = (ICFSecuritySecSessionEditObj) systemSession
                .beginEdit();
        editSystemSession.setRequiredContainerSecUser(systemUser);
        editSystemSession.setRequiredStart(Calendar.getInstance());
        systemSession = editSystemSession.create();
        editSystemSession.endEdit();

        auth = new CFSecurityAuthorization();
        auth.setSecCluster(systemCluster);
        auth.setSecTenant(systemTenant);
        auth.setSecSession(systemSession);
        schemaObj.setAuthorization(auth);

        ICFSecuritySecUserEditObj editResetUser = resetUser.beginEdit();
        editResetUser.setRequiredPasswordHash(hashedAndEncodedPassword);
        editResetUser.setOptionalPasswordResetUuid(null);
        editResetUser.update();
        editResetUser.endEdit();

        editSystemSession = (ICFSecuritySecSessionEditObj) systemSession.beginEdit();
        editSystemSession.setOptionalFinish(Calendar.getInstance());
        editSystemSession.update();
        editSystemSession.endEdit();

        schemaObj.commit();

        schemaObj.setAuthorization(null);

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
        out.println("<HTML>");
        out.println("<BODY>");
        out.println("<form method=\"post\" formaction=\"CFAsteriskSMWarConfirmEMailAddressHtml\">");
        out.println("<H1 style=\"text-align:center\">" + clusterDescription + " Security Manager</H1>");
        out.println("<H2 style=\"text-align:center\">Password Set.</H2>");
        out.println("<p>");
        out.println("<center>");
        out.println("<table style=\"width:75%\">");
        out.println(
                "<tr><td colSpan=\"2\" style=\"text-align:center\">You may now <A HRef=\"CFAsteriskSMWarLoginHtml\">log in</A> to the "
                        + clusterDescription + " Security Manager</td></tr>");
        out.println("</table>");
        out.println("</center>");
        out.println("</form>");
        out.println("</BODY>");
        out.println("</HTML>");
    } catch (NoSuchAlgorithmException e) {
        throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                "Caught NoSuchAlgorithmException -- " + e.getMessage(), e);
    } catch (RuntimeException e) {
        throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                "Caught RuntimeException -- " + e.getMessage(), e);
    } finally {
        if (dbSchema != null) {
            try {
                if (schemaObj.isTransactionOpen()) {
                    schemaObj.rollback();
                }
            } catch (RuntimeException e) {
            }
            schemaObj.setBackingStore(null);
            CFAsteriskSchemaPool.getSchemaPool().releaseInstance(dbSchema);
        }
    }
}