Example usage for org.bouncycastle.openssl PEMParser PEMParser

List of usage examples for org.bouncycastle.openssl PEMParser PEMParser

Introduction

In this page you can find the example usage for org.bouncycastle.openssl PEMParser PEMParser.

Prototype

public PEMParser(Reader reader) 

Source Link

Document

Create a new PEMReader

Usage

From source file:org.usrz.libs.crypto.pem.PEMReader.java

License:Apache License

/**
 * Create a {@link PEMReader} loading from a {@link Reader}.
 *//*from w w w.j a  va  2  s  . com*/
public PEMReader(String provider, Reader reader) throws NoSuchProviderException {
    if (reader == null)
        throw new NullPointerException("Null reader");
    if (provider == null)
        throw new NullPointerException("Null provider");

    factory = new PEMFactory(provider);
    parser = new PEMParser(reader);
}

From source file:org.usrz.libs.crypto.pem.PEMReader.java

License:Apache License

/**
 * Create a {@link PEMReader} loading from an {@link InputStream}.
 *//*from w  ww .  j  a  v a 2 s . co m*/
public PEMReader(Provider provider, InputStream input) {
    if (input == null)
        throw new NullPointerException("Null input stream");
    if (provider == null)
        throw new NullPointerException("Null provider");

    factory = new PEMFactory(provider);
    parser = new PEMParser(new InputStreamReader(input, ASCII));
}

From source file:org.usrz.libs.crypto.pem.PEMReader.java

License:Apache License

/**
 * Create a {@link PEMReader} loading from a {@link Reader}.
 *///from   w w  w. ja  va2 s .c om
public PEMReader(Provider provider, Reader reader) {
    if (reader == null)
        throw new NullPointerException("Null reader");
    if (provider == null)
        throw new NullPointerException("Null provider");

    factory = new PEMFactory(provider);
    parser = new PEMParser(reader);
}

From source file:org.wso2.carbon.identity.certificateauthority.utils.CsrUtils.java

License:Open Source License

/**
 * convert a base 64 encoded csr request into a PKCS10CertificateRequest class (bouncy-castle class)
 *
 * @param encodedCsr Base 64 encoded csr request
 * @return PKCS10CertificationRequest constructed from the encoded string
 *///from  ww  w.j av a 2  s  .c o m
public static PKCS10CertificationRequest getCRfromEncodedCsr(String encodedCsr) throws IOException {
    PEMParser pemParser = new PEMParser(
            new InputStreamReader(new ByteArrayInputStream(encodedCsr.getBytes()), "8859_1"));
    return (PKCS10CertificationRequest) pemParser.readObject();
}

From source file:org.xdi.oxauth.crypto.cert.CertificateParser.java

License:MIT License

public static X509Certificate parsePem(String pemEncodedCert) throws CertificateException {
    StringReader sr = new StringReader(pemEncodedCert);
    PEMParser pemReader = new PEMParser(sr);
    try {//from  ww w .j  a  v a  2 s .com
        X509CertificateHolder certificateHolder = ((X509CertificateHolder) pemReader.readObject());
        if (certificateHolder == null) {
            return null;
        }

        X509Certificate cert = new JcaX509CertificateConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME)
                .getCertificate(certificateHolder);

        return cert;
    } catch (IOException ex) {
        throw new CertificateException(ex);
    } finally {
        IOUtils.closeQuietly(pemReader);
    }
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

License:MIT License

public static PublicKey getPublicKey(String jwksUri, String jwks, SignatureAlgorithm signatureAlgorithm,
        String keyId) {/*  w  w w.ja  v a2 s.  c o m*/
    log.debug("Retrieving JWK...");

    JSONObject jsonKeyValue = getJsonKey(jwksUri, jwks, keyId);

    if (jsonKeyValue == null) {
        return null;
    }

    org.xdi.oxauth.model.crypto.PublicKey publicKey = null;

    try {
        String resultKeyId = jsonKeyValue.getString(KEY_ID);
        if (signatureAlgorithm == null) {
            signatureAlgorithm = SignatureAlgorithm.fromString(jsonKeyValue.getString(ALGORITHM));
            if (signatureAlgorithm == null) {
                log.error(String.format("Failed to determine key '%s' signature algorithm", resultKeyId));
                return null;
            }
        }

        JSONObject jsonPublicKey = jsonKeyValue;
        if (jsonKeyValue.has(PUBLIC_KEY)) {
            // Use internal jwks.json format
            jsonPublicKey = jsonKeyValue.getJSONObject(PUBLIC_KEY);
        }

        if (signatureAlgorithm == SignatureAlgorithm.RS256 || signatureAlgorithm == SignatureAlgorithm.RS384
                || signatureAlgorithm == SignatureAlgorithm.RS512) {
            //String alg = jsonKeyValue.getString(ALGORITHM);
            //String use = jsonKeyValue.getString(KEY_USE);
            String exp = jsonPublicKey.getString(EXPONENT);
            String mod = jsonPublicKey.getString(MODULUS);

            BigInteger publicExponent = new BigInteger(1, Base64Util.base64urldecode(exp));
            BigInteger modulus = new BigInteger(1, Base64Util.base64urldecode(mod));

            publicKey = new RSAPublicKey(modulus, publicExponent);
        } else if (signatureAlgorithm == SignatureAlgorithm.ES256
                || signatureAlgorithm == SignatureAlgorithm.ES384
                || signatureAlgorithm == SignatureAlgorithm.ES512) {
            //String alg = jsonKeyValue.getString(ALGORITHM);
            //String use = jsonKeyValue.getString(KEY_USE);
            //String crv = jsonKeyValue.getString(CURVE);
            String xx = jsonPublicKey.getString(X);
            String yy = jsonPublicKey.getString(Y);

            BigInteger x = new BigInteger(1, Base64Util.base64urldecode(xx));
            BigInteger y = new BigInteger(1, Base64Util.base64urldecode(yy));

            publicKey = new ECDSAPublicKey(signatureAlgorithm, x, y);
        }

        if (publicKey != null && jsonKeyValue.has(CERTIFICATE_CHAIN)) {
            final String BEGIN = "-----BEGIN CERTIFICATE-----";
            final String END = "-----END CERTIFICATE-----";

            JSONArray certChain = jsonKeyValue.getJSONArray(CERTIFICATE_CHAIN);
            String certificateString = BEGIN + "\n" + certChain.getString(0) + "\n" + END;
            StringReader sr = new StringReader(certificateString);
            PEMParser pemReader = new PEMParser(sr);
            X509Certificate cert = (X509CertificateObject) pemReader.readObject();
            Certificate certificate = new Certificate(signatureAlgorithm, cert);
            publicKey.setCertificate(certificate);
        }
        if (publicKey != null) {
            publicKey.setKeyId(resultKeyId);
            publicKey.setSignatureAlgorithm(signatureAlgorithm);
        }
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }

    return publicKey;
}

From source file:pv181.jca.Globals.java

/**
 * Basic PEM file parser, returns parsed object.
 * @param s//from  w ww.  j  av  a  2s  .  c  om
 * @return
 * @throws IOException 
 */
public static Object readPEM(InputStream s) throws IOException {
    // initialize buffered reader of input stream
    Reader fRd = new BufferedReader(new InputStreamReader(s));

    // PEM parser from Bouncy castle library
    PEMParser parser = new PEMParser(fRd);

    // Parse given PEM file, decide if it is X509Certificate
    return parser.readObject();
}

From source file:ru.pflb.samlsampler.SamlSampler.java

private static PrivateKey getEPMPrivateKey(String pathToPemPrivateKey) {
    PEMParser pEMParser;// w  w w  . j a v a  2  s .co  m
    try {
        pEMParser = new PEMParser(new InputStreamReader(new FileInputStream(pathToPemPrivateKey)));
        Object privateKeyObject = pEMParser.readObject();
        byte[] keyBytes = PrivateKeyInfo.getInstance(privateKeyObject).getEncoded();
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey pk = kf.generatePrivate(spec);

        return pk;
    } catch (Exception ePEM) {
        java.io.StringWriter stringWriter = new java.io.StringWriter();
        ePEM.printStackTrace(new java.io.PrintWriter(stringWriter));
        PrivateKey pk = null;
        return pk;
    }
}

From source file:shiver.me.timbers.spring.security.keys.BouncyCastlePemKeyPairs.java

License:Apache License

@Override
public KeyPair createPair(String secret) throws IOException {
    final PEMParser pemParser = new PEMParser(
            new InputStreamReader(new ByteArrayInputStream(secret.getBytes())));
    return new JcaPEMKeyConverter().setProvider("BC").getKeyPair((PEMKeyPair) pemParser.readObject());
}

From source file:uk.ac.cam.gpe21.droidssl.mitm.crypto.cert.CertificateAuthority.java

License:Apache License

public CertificateAuthority(Path certificateFile, Path keyFile) throws IOException, CertificateException {
    try (PEMParser parser = new PEMParser(Files.newBufferedReader(certificateFile, StandardCharsets.UTF_8))) {
        Object object = parser.readObject();
        if (!(object instanceof X509CertificateHolder))
            throw new IOException("Failed to read CA certificate file");

        certificate = (X509CertificateHolder) object;
        jcaCertificate = new JcaX509CertificateConverter().getCertificate(certificate);
    }/* w w w . j  a  va  2s.c om*/

    try (PEMParser parser = new PEMParser(Files.newBufferedReader(keyFile, StandardCharsets.UTF_8))) {
        Object object = parser.readObject();
        if (!(object instanceof PEMKeyPair))
            throw new IOException("Failed to read CA key file");

        PEMKeyPair pair = (PEMKeyPair) object;
        publicKey = PublicKeyFactory.createKey(pair.getPublicKeyInfo());
        privateKey = PrivateKeyFactory.createKey(pair.getPrivateKeyInfo());
    }
}