Search in sources :

Example 6 with PEMReader

use of org.bouncycastle.openssl.PEMReader in project MSEC by Tencent.

the class Tools method loadPubKeyFromFile.

public static PublicKey loadPubKeyFromFile(String filename) {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    try {
        FileReader fr = new FileReader(filename);
        PEMReader pr = new PEMReader(fr);
        PublicKey key = (PublicKey) (pr.readObject());
        pr.close();
        fr.close();
        return key;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : PEMReader(org.bouncycastle.openssl.PEMReader) FileReader(java.io.FileReader)

Example 7 with PEMReader

use of org.bouncycastle.openssl.PEMReader in project XobotOS by xamarin.

the class Credentials method convertFromPem.

/**
     * Convert objects from PEM format, which is used for
     * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY
     * entries.
     */
public static List<Object> convertFromPem(byte[] bytes) throws IOException {
    ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
    Reader reader = new InputStreamReader(bai, Charsets.US_ASCII);
    PEMReader pr = new PEMReader(reader);
    List<Object> result = new ArrayList<Object>();
    Object o;
    while ((o = pr.readObject()) != null) {
        result.add(o);
    }
    pr.close();
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) PEMReader(org.bouncycastle.openssl.PEMReader) ArrayList(java.util.ArrayList) PEMReader(org.bouncycastle.openssl.PEMReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader)

Example 8 with PEMReader

use of org.bouncycastle.openssl.PEMReader in project OpenAttestation by OpenAttestation.

the class TAHelper method pemToX509Certificate.

/**
     * Convert a PEM formatted certificate in X509 format.
     * @param machineCertPEM Machine certificate in PEM/text format. 
     * @return An X509 certificate object.
     */
public static X509Certificate pemToX509Certificate(String machineCertPEM) {
    try {
        File f = new File(machineCertPEM);
        FileInputStream filestr = new FileInputStream(f);
        DataInputStream datastr = new DataInputStream(filestr);
        byte[] keyBytes = new byte[(int) f.length()];
        datastr.readFully(keyBytes);
        String temp = new String(keyBytes);
        PEMReader reader = new PEMReader(new StringReader(temp.replace("-----BEGIN CERTIFICATE-----", "-----BEGIN CERTIFICATE-----\n").replace("-----END CERTIFICATE-----", "\n-----END CERTIFICATE-----")));
        datastr.close();
        return (X509Certificate) reader.readObject();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : PEMReader(org.bouncycastle.openssl.PEMReader) X509Certificate(java.security.cert.X509Certificate) ASException(com.intel.mountwilson.as.common.ASException) KeyStoreException(java.security.KeyStoreException) XMLStreamException(javax.xml.stream.XMLStreamException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException)

Aggregations

PEMReader (org.bouncycastle.openssl.PEMReader)8 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 X509Certificate (java.security.cert.X509Certificate)3 FileReader (java.io.FileReader)2 InputStreamReader (java.io.InputStreamReader)2 KeyPair (java.security.KeyPair)2 Cipher (javax.crypto.Cipher)2 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)2 ASException (com.intel.mountwilson.as.common.ASException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 Reader (java.io.Reader)1 UnknownHostException (java.net.UnknownHostException)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SignatureException (java.security.SignatureException)1 CertificateException (java.security.cert.CertificateException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1