Example usage for java.security Security addProvider

List of usage examples for java.security Security addProvider

Introduction

In this page you can find the example usage for java.security Security addProvider.

Prototype

public static int addProvider(Provider provider) 

Source Link

Document

Adds a provider to the next position available.

Usage

From source file:org.openlaszlo.utils.LZHttpUtils.java

/**
 * @return the URL for the request // with the query string?
 * @param req the request/*from  w  w  w.j a  va2s .c om*/
 */
public static URL getRequestURL(HttpServletRequest req) {
    StringBuffer surl = req.getRequestURL();
    if (surl.indexOf("https") == 0) {
        try {
            System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
            Class<?> provClass = Class.forName("com.sun.net.ssl.internal.ssl.Provider");
            Provider provider = (Provider) provClass.newInstance();
            Security.addProvider(provider);
        } catch (InstantiationException e) {
            throw new ChainedException(e);
        } catch (IllegalAccessException e) {
            throw new ChainedException(e);
        } catch (ClassNotFoundException e) {
            throw new ChainedException(e);
        }
    }
    // surl.append("?");
    // surl.append(req.getQueryString());
    try {
        return new URL(surl.toString());
    } catch (Exception e) {
        throw new ChainedException(e);
    }
}

From source file:hudson.plugins.ec2.EC2AxisPrivateKey.java

/**
 * Obtains the fingerprint of the key in the "ab:cd:ef:...:12" format.
 *///  w ww .j a va2  s  . c o  m
public String getFingerprint() throws IOException {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    @SuppressWarnings("deprecation")
    Reader r = new BufferedReader(new StringReader(privateKey.toString()));
    @SuppressWarnings("resource")
    PEMReader pem = new PEMReader(r, new PasswordFinder() {
        public char[] getPassword() {
            throw PRIVATE_KEY_WITH_PASSWORD;
        }
    });

    try {
        KeyPair pair = (KeyPair) pem.readObject();
        if (pair == null)
            return null;
        PrivateKey key = pair.getPrivate();
        return digest(key);
    } catch (RuntimeException e) {
        if (e == PRIVATE_KEY_WITH_PASSWORD)
            throw new IOException("This private key is password protected, which isn't supported yet");
        throw e;
    }
}

From source file:test.integ.be.fedict.hsm.jca.HSMProxySignatureTest.java

@Test
public void testSign() throws Exception {
    LOG.debug("sign");
    // operate//  ww w . j  a  v a2s .  com
    Security.addProvider(new HSMProxyProvider());
    KeyStore keyStore = KeyStore.getInstance("HSMProxy");

    HSMProxyTestCredential testCredential = new HSMProxyTestCredential();
    HSMProxyKeyStoreParameter keyStoreParameter = new HSMProxyKeyStoreParameter(
            testCredential.getCredentialPrivateKey(), testCredential.getCredentialCertificate(),
            "http://localhost:8080/hsm-proxy-ws/dss", new MyHSMProxyAudit());
    keyStore.load(keyStoreParameter);

    String alias = keyStore.aliases().nextElement();

    PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, null);
    assertNotNull(privateKey);

    X509Certificate certificate = (X509Certificate) keyStore.getCertificate(alias);

    signAndVerify(certificate, privateKey, "SHA1withRSA");
    signAndVerify(certificate, privateKey, "SHA256withRSA");
    signAndVerify(certificate, privateKey, "SHA512withRSA");
}

From source file:test.integ.be.fedict.trust.XKMSRevocationTest.java

@Before
public void setUp() throws Exception {
    Security.addProvider(new BouncyCastleProvider());

    signCertificateChain = TestUtils.getSignCertificateChain();
    client = new XKMS2Client(TestUtils.XKMS_WS_LOCATION);
}

From source file:org.dasein.security.joyent.SignatureHttpAuth.java

@Override
public void addPreemptiveAuth(@Nonnull HttpRequest request) throws CloudException, InternalException {
    if (provider.getContext() == null) {
        throw new CloudException("No context was defined for this request");
    }/*from w  ww .j  av  a2  s. com*/
    Date date = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
    String now = RFC1123_DATE_FORMAT.format(date);
    request.setHeader("Date", now);
    try {
        Security.addProvider(new BouncyCastleProvider());
        Signature signature = Signature.getInstance(SIGN_ALGORITHM);

        List<ContextRequirements.Field> fields = provider.getContextRequirements().getConfigurableValues();
        String keyName = "";
        String privateKey = "";
        char[] keyPassword = null;
        for (ContextRequirements.Field f : fields) {
            if (f.type.equals(ContextRequirements.FieldType.KEYPAIR)) {
                byte[][] keyPair = (byte[][]) provider.getContext().getConfigurationValue(f);
                keyName = new String(keyPair[0], "utf-8");
                privateKey = new String(keyPair[1], "utf-8");
            } else if (f.type.equals(ContextRequirements.FieldType.PASSWORD)) {
                byte[] password = (byte[]) provider.getContext().getConfigurationValue(f);
                if (password != null) {
                    keyPassword = new String(password, "utf-8").toCharArray();
                }
            }
        }

        signature.initSign(getKeyPair(privateKey, keyPassword).getPrivate());
        String signingString = String.format(AUTH_SIGN, now);
        signature.update(signingString.getBytes("UTF-8"));
        byte[] signedDate = signature.sign();
        byte[] encodedSignedDate = Base64.encode(signedDate);

        request.addHeader("Authorization", String.format(AUTH_HEADER, provider.getContext().getAccountNumber(),
                keyName, new String(encodedSignedDate)));

    } catch (NoSuchAlgorithmException e) {
        throw new InternalException(e);
    } catch (UnsupportedEncodingException e) {
        throw new InternalException(e);
    } catch (SignatureException e) {
        throw new InternalException(e);
    } catch (InvalidKeyException e) {
        throw new InternalException(e);
    } catch (IOException e) {
        throw new InternalException(e);
    }
}

From source file:test.unit.be.fedict.eid.idp.protocol.saml2.SAML2Test.java

@BeforeClass
public static void before() {
    Security.addProvider(new BouncyCastleProvider());
    // Init.init();
}

From source file:hudson.plugins.ec2.EC2PrivateKey.java

/**
 * Obtains the fingerprint of the key in the "ab:cd:ef:...:12" format.
 *//*ww  w . ja va 2  s .c  o m*/
public String getFingerprint() throws IOException {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    Reader r = new BufferedReader(new StringReader(privateKey.toString()));
    PEMReader pem = new PEMReader(r, new PasswordFinder() {
        public char[] getPassword() {
            throw PRIVATE_KEY_WITH_PASSWORD;
        }
    });

    try {
        KeyPair pair = (KeyPair) pem.readObject();
        if (pair == null)
            return null;
        PrivateKey key = pair.getPrivate();
        return digest(key);
    } catch (RuntimeException e) {
        if (e == PRIVATE_KEY_WITH_PASSWORD)
            throw new IOException("This private key is password protected, which isn't supported yet");
        throw e;
    }
}

From source file:com.streamsets.pipeline.lib.remote.FTPAndSSHDUnitTest.java

@BeforeClass
public static void beforeClass() {
    Security.addProvider(new BouncyCastleProvider());
}

From source file:com.liferay.util.Encryptor.java

public static String decrypt(Key key, String encryptedString) throws EncryptorException {

    try {/*from   ww w.  j  a  v a 2  s.  com*/
        Security.addProvider(getProvider());

        Cipher cipher = Cipher.getInstance(key.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, key);

        byte[] encryptedBytes = Base64.decode(encryptedString);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

        String decryptedString = new String(decryptedBytes, ENCODING);

        return decryptedString;
    } catch (Exception e) {
        throw new EncryptorException(e);
    }
}

From source file:test.unit.be.fedict.eid.idp.protocol.ws_federation.WSFederationMetadataHttpServletTest.java

@BeforeClass
public static void init() throws Exception {
    Security.addProvider(new BouncyCastleProvider());
}