Search in sources :

Example 56 with Key

use of java.security.Key in project OpenAM by OpenRock.

the class OpenAMClientRegistration method byJWKs.

private boolean byJWKs(OAuth2Jwt jwt) throws IdRepoException, SSOException, MalformedURLException, FailedToLoadJWKException {
    Set<String> set = amIdentity.getAttribute(OAuth2Constants.OAuth2Client.JWKS);
    if (set == null || set.isEmpty()) {
        throw OAuthProblemException.OAuthError.SERVER_ERROR.handle(Request.getCurrent(), "No Client Bearer JWKs_URI set.");
    }
    final String jwkSetStr = set.iterator().next();
    final JWKSet jwkSet = new JWKSet(JsonValueBuilder.toJsonValue(jwkSetStr));
    //0 values as not using for inet comms
    final JWKSetParser setParser = new JWKSetParser(0, 0);
    final Map<String, Key> jwkMap = setParser.jwkSetToMap(jwkSet);
    final Key key = jwkMap.get(jwt.getSignedJwt().getHeader().getKeyId());
    return key != null && jwt.isValid(signingManager.newRsaSigningHandler(key));
}
Also used : JWKSetParser(org.forgerock.jaspi.modules.openid.helpers.JWKSetParser) JWKSet(org.forgerock.json.jose.jwk.JWKSet) Key(java.security.Key)

Example 57 with Key

use of java.security.Key in project yyl_example by Relucent.

the class ExportPrivateKey method getPrivateKey.

public static KeyPair getPrivateKey(KeyStore keystore, String alias, char[] password) {
    try {
        Key key = keystore.getKey(alias, password);
        if (key instanceof PrivateKey) {
            Certificate cert = keystore.getCertificate(alias);
            PublicKey publicKey = cert.getPublicKey();
            return new KeyPair(publicKey, (PrivateKey) key);
        }
    } catch (UnrecoverableKeyException e) {
    } catch (NoSuchAlgorithmException e) {
    } catch (KeyStoreException e) {
    }
    return null;
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) UnrecoverableKeyException(java.security.UnrecoverableKeyException) PublicKey(java.security.PublicKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) Certificate(java.security.cert.Certificate)

Example 58 with Key

use of java.security.Key in project ats-framework by Axway.

the class SslUtils method createKeyStoreFromPemKey.

private static void createKeyStoreFromPemKey(String clientCert, String clientPass, KeyStore store) throws Exception {
    try {
        // Load CA Chain file
        // CertificateFactory cf = CertificateFactory.getInstance("X.509");
        // X509Certificate cert = (X509Certificate) cf.generateCertificate(new FileInputStream(caCert));
        store.load(null);
        // Load client's public and private keys from PKCS12 certificate
        KeyStore inputKeyStore = KeyStore.getInstance("PKCS12");
        FileInputStream fis = new FileInputStream(clientCert);
        char[] nPassword = null;
        if ((clientPass == null) || "".equals(clientPass.trim())) {
            nPassword = null;
        } else {
            nPassword = clientPass.toCharArray();
        }
        inputKeyStore.load(fis, nPassword);
        fis.close();
        store.load(null, ((clientPass != null) ? clientPass.toCharArray() : null));
        Enumeration<String> en = inputKeyStore.aliases();
        while (en.hasMoreElements()) {
            // we are reading just one certificate.
            String keyAlias = en.nextElement();
            if (inputKeyStore.isKeyEntry(keyAlias)) {
                Key key = inputKeyStore.getKey(keyAlias, nPassword);
                Certificate[] certChain = inputKeyStore.getCertificateChain(keyAlias);
                store.setKeyEntry("outkey", key, ((clientPass != null) ? clientPass.toCharArray() : null), certChain);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error creating keystore from Pem key", e);
    }
}
Also used : KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 59 with Key

use of java.security.Key in project graylog2-server by Graylog2.

the class PemKeyStoreTest method testBuildKeyStore.

@Test
public void testBuildKeyStore() throws Exception {
    final Path certChainFile = Paths.get(Resources.getResource("org/graylog2/shared/security/tls/chain.crt").toURI());
    final Path keyFile = Paths.get(Resources.getResource("org/graylog2/shared/security/tls/private.key").toURI());
    final KeyStore keyStore = PemKeyStore.buildKeyStore(certChainFile, keyFile, null);
    final Certificate[] keys = keyStore.getCertificateChain("key");
    assertThat(keys).hasSize(2);
    final Key key = keyStore.getKey("key", new char[0]);
    assertThat(key.getFormat()).isEqualTo("PKCS#8");
    assertThat(key.getEncoded()).isNotEmpty();
}
Also used : Path(java.nio.file.Path) KeyStore(java.security.KeyStore) Key(java.security.Key) Certificate(java.security.cert.Certificate) Test(org.junit.Test)

Example 60 with Key

use of java.security.Key in project async-http-client by AsyncHttpClient.

the class NtlmEngine method lmHash.

/**
     * Creates the LM Hash of the user's password.
     *
     * @param password
     *            The password.
     *
     * @return The LM Hash of the given password, used in the calculation of the
     *         LM Response.
     */
private static byte[] lmHash(final String password) throws NtlmEngineException {
    try {
        final byte[] oemPassword = password.toUpperCase(Locale.ROOT).getBytes(US_ASCII);
        final int length = Math.min(oemPassword.length, 14);
        final byte[] keyBytes = new byte[14];
        System.arraycopy(oemPassword, 0, keyBytes, 0, length);
        final Key lowKey = createDESKey(keyBytes, 0);
        final Key highKey = createDESKey(keyBytes, 7);
        final Cipher des = Cipher.getInstance("DES/ECB/NoPadding");
        des.init(Cipher.ENCRYPT_MODE, lowKey);
        final byte[] lowHash = des.doFinal(MAGIC_CONSTANT);
        des.init(Cipher.ENCRYPT_MODE, highKey);
        final byte[] highHash = des.doFinal(MAGIC_CONSTANT);
        final byte[] lmHash = new byte[16];
        System.arraycopy(lowHash, 0, lmHash, 0, 8);
        System.arraycopy(highHash, 0, lmHash, 8, 8);
        return lmHash;
    } catch (final Exception e) {
        throw new NtlmEngineException(e.getMessage(), e);
    }
}
Also used : Cipher(javax.crypto.Cipher) Key(java.security.Key) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

Key (java.security.Key)302 PrivateKey (java.security.PrivateKey)112 SecretKey (javax.crypto.SecretKey)83 KeyStore (java.security.KeyStore)64 PublicKey (java.security.PublicKey)62 Cipher (javax.crypto.Cipher)60 X509Certificate (java.security.cert.X509Certificate)57 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)50 Test (org.junit.Test)44 IOException (java.io.IOException)42 ByteArrayInputStream (java.io.ByteArrayInputStream)38 Certificate (java.security.cert.Certificate)36 SecretKeySpec (javax.crypto.spec.SecretKeySpec)36 KeyFactory (java.security.KeyFactory)35 InvalidKeyException (java.security.InvalidKeyException)32 KeyGenerator (javax.crypto.KeyGenerator)32 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)26 KeyStoreException (java.security.KeyStoreException)22 SecureRandom (java.security.SecureRandom)21 IvParameterSpec (javax.crypto.spec.IvParameterSpec)21