Search in sources :

Example 66 with UnsupportedEncodingException

use of java.io.UnsupportedEncodingException in project OpenAM by OpenRock.

the class SoapSTSInstanceModule method getEncryptionProperties.

private Properties getEncryptionProperties() {
    Properties properties = new Properties();
    properties.put("org.apache.ws.security.crypto.provider", "org.apache.ws.security.components.crypto.Merlin");
    String keystorePassword;
    if (stsInstanceConfig.getKeystoreConfig() != null) {
        try {
            keystorePassword = new String(stsInstanceConfig.getKeystoreConfig().getKeystorePassword(), AMSTSConstants.UTF_8_CHARSET_ID);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Unsupported string encoding for keystore password: " + e);
        }
        properties.put("org.apache.ws.security.crypto.merlin.keystore.password", keystorePassword);
        properties.put("org.apache.ws.security.crypto.merlin.keystore.file", stsInstanceConfig.getKeystoreConfig().getKeystoreFileName());
        properties.put("org.apache.ws.security.crypto.merlin.keystore.type", "jks");
    }
    return properties;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) StaticSTSProperties(org.apache.cxf.sts.StaticSTSProperties) Properties(java.util.Properties)

Example 67 with UnsupportedEncodingException

use of java.io.UnsupportedEncodingException in project OpenAM by OpenRock.

the class JCECrypt method encode.

private static String encode(String clearText, AMEncryption encr) {
    if (clearText == null || clearText.length() == 0) {
        return null;
    }
    // Encrypt the data
    byte[] encData = null;
    try {
        encData = encr.encrypt(clearText.getBytes("utf-8"));
    } catch (UnsupportedEncodingException uee) {
        Debug debug = Debug.getInstance("amSDK");
        debug.error("Crypt:: utf-8 encoding is not supported");
        encData = encryptor.encrypt(clearText.getBytes());
    }
    // BASE64 encode the data
    String str = null;
    // Perf Improvement : Removed the sync block and newed up the Encoder
    // object for every call. Its a trade off b/w CPU and mem usage.
    str = Base64.encode(encData).trim();
    // Serialize the data, i.e., remove \n and \r
    BufferedReader bufReader = new BufferedReader(new StringReader(str));
    StringBuilder strClean = new StringBuilder(str.length());
    String strTemp = null;
    try {
        while ((strTemp = bufReader.readLine()) != null) {
            strClean.append(strTemp);
        }
    } catch (IOException ioe) {
        Debug debug = Debug.getInstance("amSDK");
        debug.error("Crypt:: Error while base64 encoding", ioe);
    }
    return (strClean.toString());
}
Also used : BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Debug(com.sun.identity.shared.debug.Debug)

Example 68 with UnsupportedEncodingException

use of java.io.UnsupportedEncodingException in project OpenAM by OpenRock.

the class TokenRequestMarshallerImpl method buildUsernameTokenTransformValidatorParameters.

private RestTokenTransformValidatorParameters<RestUsernameToken> buildUsernameTokenTransformValidatorParameters(JsonValue receivedToken) throws TokenMarshalException {
    if (!receivedToken.get(AMSTSConstants.USERNAME_TOKEN_USERNAME).isString()) {
        final String message = "Exception: json representation of UNT does not contain a username field. The representation: " + receivedToken;
        throw new TokenMarshalException(ResourceException.BAD_REQUEST, message);
    }
    if (!receivedToken.get(AMSTSConstants.USERNAME_TOKEN_PASSWORD).isString()) {
        final String message = "Exception: json representation of UNT does not contain a password field. The representation: \n" + receivedToken;
        throw new TokenMarshalException(ResourceException.BAD_REQUEST, message);
    }
    final String username = receivedToken.get(AMSTSConstants.USERNAME_TOKEN_USERNAME).asString();
    final String password = receivedToken.get(AMSTSConstants.USERNAME_TOKEN_PASSWORD).asString();
    try {
        final RestUsernameToken restUsernameToken = new RestUsernameToken(username.getBytes(AMSTSConstants.UTF_8_CHARSET_ID), password.getBytes(AMSTSConstants.UTF_8_CHARSET_ID));
        return new RestTokenTransformValidatorParameters<RestUsernameToken>() {

            @Override
            public RestUsernameToken getInputToken() {
                return restUsernameToken;
            }
        };
    } catch (UnsupportedEncodingException e) {
        throw new TokenMarshalException(ResourceException.INTERNAL_ERROR, "Unable to marshal username token state to strings: " + e.getMessage(), e);
    }
}
Also used : RestTokenTransformValidatorParameters(org.forgerock.openam.sts.rest.token.validator.RestTokenTransformValidatorParameters) TokenMarshalException(org.forgerock.openam.sts.TokenMarshalException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RestUsernameToken(org.forgerock.openam.sts.token.model.RestUsernameToken)

Example 69 with UnsupportedEncodingException

use of java.io.UnsupportedEncodingException in project OpenAM by OpenRock.

the class TokenRequestMarshallerImpl method pullClientCertFromHeader.

private X509Certificate[] pullClientCertFromHeader(HttpContext httpContext) throws TokenMarshalException {
    List<String> clientCertHeader = httpContext.getHeader(offloadedTlsClientCertKey);
    if (clientCertHeader.isEmpty()) {
        return null;
    } else {
        int ndx = 0;
        X509Certificate[] certificates = new X509Certificate[clientCertHeader.size()];
        final CertificateFactory certificateFactory;
        try {
            certificateFactory = CertificateFactory.getInstance("X.509");
        } catch (CertificateException e) {
            throw new TokenMarshalException(ResourceException.INTERNAL_ERROR, "Exception caught creating X.509 CertificateFactory: " + e, e);
        }
        for (String headerCertValue : clientCertHeader) {
            try {
                certificates[ndx++] = (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(Base64.decode(headerCertValue.getBytes(AMSTSConstants.UTF_8_CHARSET_ID))));
            } catch (CertificateException | UnsupportedEncodingException e) {
                throw new TokenMarshalException(ResourceException.BAD_REQUEST, "Exception caught marshalling X509 cert from value set in " + offloadedTlsClientCertKey + " header: " + e, e);
            }
        }
        return certificates;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TokenMarshalException(org.forgerock.openam.sts.TokenMarshalException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 70 with UnsupportedEncodingException

use of java.io.UnsupportedEncodingException in project OpenAM by OpenRock.

the class ExportServiceConfiguration method handleRequest.

/**
     * Handles request.
     *
     * @param rc Request Context.
     * @throws CLIException if request cannot be processed.
     */
public void handleRequest(RequestContext rc) throws CLIException {
    super.handleRequest(rc);
    ldapLogin();
    SSOToken adminSSOToken = getAdminSSOToken();
    String outputFile = getStringOptionValue(IArgument.OUTPUT_FILE);
    String encryptSecret = getStringOptionValue(IArgument.ENCRYPT_SECRET);
    FileOutputStream fout = null;
    String[] param = { "tty" };
    String[] paramException = { "tty", "" };
    try {
        if ((outputFile != null) && (outputFile.length() > 0)) {
            fout = new FileOutputStream(outputFile);
            param[0] = outputFile;
            paramException[0] = outputFile;
        }
        writeLog(LogWriter.LOG_ACCESS, Level.INFO, "ATTEMPT_EXPORT_SM_CONFIG_DATA", param);
        ServiceManager sm = new ServiceManager(adminSSOToken);
        AMEncryption encryptObj = new JCEEncryption();
        ((ConfigurableKey) encryptObj).setPassword(encryptSecret);
        String resultXML = sm.toXML(encryptObj);
        resultXML += "<!-- " + Hash.hash(encryptSecret) + " -->";
        if (fout != null) {
            fout.write(resultXML.getBytes("UTF-8"));
        } else {
            System.out.write(resultXML.getBytes("UTF-8"));
        }
        getOutputWriter().printlnMessage(getResourceString("export-service-configuration-succeeded"));
        writeLog(LogWriter.LOG_ACCESS, Level.INFO, "SUCCEEDED_IMPORT_SM_CONFIG_DATA", param);
    } catch (UnsupportedEncodingException e) {
        paramException[1] = e.getMessage();
        writeLog(LogWriter.LOG_ACCESS, Level.INFO, "FAILED_EXPORT_SM_CONFIG_DATA", paramException);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (IOException e) {
        paramException[1] = e.getMessage();
        writeLog(LogWriter.LOG_ACCESS, Level.INFO, "FAILED_EXPORT_SM_CONFIG_DATA", paramException);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (SSOException e) {
        paramException[1] = e.getMessage();
        writeLog(LogWriter.LOG_ACCESS, Level.INFO, "FAILED_EXPORT_SM_CONFIG_DATA", paramException);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (SMSException e) {
        paramException[1] = e.getMessage();
        writeLog(LogWriter.LOG_ACCESS, Level.INFO, "FAILED_EXPORT_SM_CONFIG_DATA", paramException);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } catch (Exception e) {
        paramException[1] = e.getMessage();
        writeLog(LogWriter.LOG_ACCESS, Level.INFO, "FAILED_EXPORT_SM_CONFIG_DATA", paramException);
        throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
    } finally {
        if (fout != null) {
            try {
                fout.close();
            } catch (IOException ioe) {
            //ignored
            }
        }
    }
}
Also used : AMEncryption(com.iplanet.services.util.AMEncryption) SSOToken(com.iplanet.sso.SSOToken) JCEEncryption(com.iplanet.services.util.JCEEncryption) SMSException(com.sun.identity.sm.SMSException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SSOException(com.iplanet.sso.SSOException) IOException(java.io.IOException) ConfigurableKey(com.iplanet.services.util.ConfigurableKey) SMSException(com.sun.identity.sm.SMSException) IOException(java.io.IOException) CLIException(com.sun.identity.cli.CLIException) SSOException(com.iplanet.sso.SSOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServiceManager(com.sun.identity.sm.ServiceManager) FileOutputStream(java.io.FileOutputStream) CLIException(com.sun.identity.cli.CLIException)

Aggregations

UnsupportedEncodingException (java.io.UnsupportedEncodingException)3108 IOException (java.io.IOException)878 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)284 InputStream (java.io.InputStream)275 ArrayList (java.util.ArrayList)268 InputStreamReader (java.io.InputStreamReader)243 File (java.io.File)234 ByteArrayInputStream (java.io.ByteArrayInputStream)209 ByteArrayOutputStream (java.io.ByteArrayOutputStream)201 FileNotFoundException (java.io.FileNotFoundException)198 HashMap (java.util.HashMap)182 MessageDigest (java.security.MessageDigest)180 BufferedReader (java.io.BufferedReader)150 URL (java.net.URL)150 Map (java.util.Map)148 OutputStreamWriter (java.io.OutputStreamWriter)145 FileOutputStream (java.io.FileOutputStream)120 MalformedURLException (java.net.MalformedURLException)110 FileInputStream (java.io.FileInputStream)107 List (java.util.List)105