Search in sources :

Example 1 with PKCS1EncodedKeySpec

use of net.oauth.signature.pem.PKCS1EncodedKeySpec in project bigbluebutton by bigbluebutton.

the class RSA_SHA1 method getPrivateKeyFromPem.

private PrivateKey getPrivateKeyFromPem(String pem) throws GeneralSecurityException, IOException {
    InputStream stream = new ByteArrayInputStream(pem.getBytes("UTF-8"));
    PEMReader reader = new PEMReader(stream);
    byte[] bytes = reader.getDerBytes();
    KeySpec keySpec;
    if (PEMReader.PRIVATE_PKCS1_MARKER.equals(reader.getBeginMarker())) {
        keySpec = (new PKCS1EncodedKeySpec(bytes)).getKeySpec();
    } else if (PEMReader.PRIVATE_PKCS8_MARKER.equals(reader.getBeginMarker())) {
        keySpec = new PKCS8EncodedKeySpec(bytes);
    } else {
        throw new IOException("Invalid PEM file: Unknown marker " + "for private key " + reader.getBeginMarker());
    }
    KeyFactory fac = KeyFactory.getInstance("RSA");
    return fac.generatePrivate(keySpec);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PEMReader(net.oauth.signature.pem.PEMReader) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) IOException(java.io.IOException) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 2 with PKCS1EncodedKeySpec

use of net.oauth.signature.pem.PKCS1EncodedKeySpec in project fabric8 by fabric8io.

the class WebClients method createKeyStore.

public static KeyStore createKeyStore(String clientCertData, File clientCertFile, String clientKeyData, File clientKeyFile, String clientKeyAlgo, char[] clientKeyPassword) throws Exception {
    try (InputStream certInputStream = getInputStreamFromDataOrFile(clientCertData, clientCertFile)) {
        CertificateFactory certFactory = CertificateFactory.getInstance("X509");
        X509Certificate cert = (X509Certificate) certFactory.generateCertificate(certInputStream);
        InputStream keyInputStream = getInputStreamFromDataOrFile(clientKeyData, clientKeyFile);
        PEMReader reader = new PEMReader(keyInputStream);
        RSAPrivateCrtKeySpec keySpec = new PKCS1EncodedKeySpec(reader.getDerBytes()).getKeySpec();
        KeyFactory kf = KeyFactory.getInstance(clientKeyAlgo);
        RSAPrivateKey privKey = (RSAPrivateKey) kf.generatePrivate(keySpec);
        KeyStore keyStore = KeyStore.getInstance("JKS");
        keyStore.load(null, clientKeyPassword);
        String alias = cert.getSubjectX500Principal().getName();
        keyStore.setKeyEntry(alias, privKey, clientKeyPassword, new Certificate[] { cert });
        return keyStore;
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PEMReader(net.oauth.signature.pem.PEMReader) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec) CertificateFactory(java.security.cert.CertificateFactory) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) KeyFactory(java.security.KeyFactory)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 KeyFactory (java.security.KeyFactory)2 PEMReader (net.oauth.signature.pem.PEMReader)2 PKCS1EncodedKeySpec (net.oauth.signature.pem.PKCS1EncodedKeySpec)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 KeyStore (java.security.KeyStore)1 CertificateFactory (java.security.cert.CertificateFactory)1 X509Certificate (java.security.cert.X509Certificate)1 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)1 EncodedKeySpec (java.security.spec.EncodedKeySpec)1 KeySpec (java.security.spec.KeySpec)1 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)1 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1