Search in sources :

Example 1 with PEMReader

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

the class RSA_SHA1 method getPublicKeyFromPem.

private PublicKey getPublicKeyFromPem(String pem) throws GeneralSecurityException, IOException {
    InputStream stream = new ByteArrayInputStream(pem.getBytes("UTF-8"));
    PEMReader reader = new PEMReader(stream);
    byte[] bytes = reader.getDerBytes();
    PublicKey pubKey;
    if (PEMReader.PUBLIC_X509_MARKER.equals(reader.getBeginMarker())) {
        KeySpec keySpec = new X509EncodedKeySpec(bytes);
        KeyFactory fac = KeyFactory.getInstance("RSA");
        pubKey = fac.generatePublic(keySpec);
    } else if (PEMReader.CERTIFICATE_X509_MARKER.equals(reader.getBeginMarker())) {
        pubKey = getPublicKeyFromDerCert(bytes);
    } else {
        throw new IOException("Invalid PEM fileL: Unknown marker for " + " public key or cert " + reader.getBeginMarker());
    }
    return pubKey;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PEMReader(net.oauth.signature.pem.PEMReader) PublicKey(java.security.PublicKey) 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) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) IOException(java.io.IOException) KeyFactory(java.security.KeyFactory)

Example 2 with PEMReader

use of net.oauth.signature.pem.PEMReader 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 3 with PEMReader

use of net.oauth.signature.pem.PEMReader 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)3 InputStream (java.io.InputStream)3 KeyFactory (java.security.KeyFactory)3 PEMReader (net.oauth.signature.pem.PEMReader)3 PKCS1EncodedKeySpec (net.oauth.signature.pem.PKCS1EncodedKeySpec)3 IOException (java.io.IOException)2 EncodedKeySpec (java.security.spec.EncodedKeySpec)2 KeySpec (java.security.spec.KeySpec)2 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)2 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)2 FileInputStream (java.io.FileInputStream)1 KeyStore (java.security.KeyStore)1 PublicKey (java.security.PublicKey)1 CertificateFactory (java.security.cert.CertificateFactory)1 X509Certificate (java.security.cert.X509Certificate)1 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)1 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)1