Package org.bouncycastle.openssl

Examples of org.bouncycastle.openssl.PEMReader


    protected KeyPair readKeyPair()
            throws IOException {
        KeyPair kp = null;
        org.bouncycastle.openssl.PasswordFinder pFinder = makeBouncyPasswordFinder();
        PEMReader r = null;
        Object o = null;
        try {
            for (; ; ) {
                // while the PasswordFinder tells us we should retry
                try {
                    r = new PEMReader(resource.getReader(), pFinder);
                    o = r.readObject();
                } catch (EncryptionException e) {
                    if (pwdf.shouldRetry(resource))
                        continue;
                    else
                        throw e;
View Full Code Here


  }

  public static List<X509Certificate> fromPem(String cert) {
    List<X509Certificate> certificates = Lists.newArrayList();

    PEMReader reader = null;
    try {
      reader = new PEMReader(new StringReader(cert), null, BouncyCastleLoader.getName());
      while (true) {
        Object o = reader.readObject();
        if (o == null) {
          return certificates;
        }

        certificates.add((X509Certificate) o);
View Full Code Here

import com.fathomdb.io.IoUtils;

public class KeyPairUtils {

  public static KeyPair deserialize(String keyData) throws IOException {
    PEMReader r = new PEMReader(new StringReader(keyData), null, BouncyCastleLoader.getName());
    try {
      return (KeyPair) r.readObject();
    } finally {
      IoUtils.safeClose(r);
    }
  }
View Full Code Here

            throw new IllegalStateException("BouncyCastle must be registered as a JCE provider");
        }
        List<KeyPair> keys = new ArrayList<KeyPair>();
        for (int i = 0; i < files.length; i++) {
            try {
                PEMReader r = new PEMReader(new InputStreamReader(new FileInputStream(files[i])), passwordFinder);
                try {
                    Object o = r.readObject();
                    if (o instanceof KeyPair) {
                        keys.add((KeyPair) o);
                    }
                } finally {
                    r.close();
                }
            } catch (Exception e) {
                log.warn("Unable to read key {}: {}", files[i], e);
            }
        }
View Full Code Here

    private X509Certificate loadCert(
        String certName)
        throws Exception
    {
        Reader    in = new InputStreamReader(getClass().getResourceAsStream(certName));
        PEMReader rd = new PEMReader(in);
       
        return (X509Certificate)rd.readObject();
    }
View Full Code Here

            throw new IllegalStateException("BouncyCastle must be registered as a JCE provider");
        }
        List<KeyPair> keys = new ArrayList<KeyPair>();
        try
        {
            PEMReader r = new PEMReader(new InputStreamReader(getClass().getResourceAsStream("/ftp-hostkey.pem")), null);
            try
            {
                Object o = r.readObject();
                if (o instanceof KeyPair)
                {
                    keys.add((KeyPair) o);
                }
            }
            finally
            {
                r.close();
            }
        }
        catch (Exception e)
        {
            LOG.info("Unable to read key ", e);
View Full Code Here

    public void performTest()
        throws Exception
    {
        Reader          fRd =new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("test.pem")));
        PasswordFinder  pGet = new Password("secret".toCharArray());
        PEMReader       pemRd = new PEMReader(fRd, pGet);
        Object          o;

        while ((o = pemRd.readObject()) != null)
        {
            if (o instanceof KeyPair)
            {
                KeyPair     pair = (KeyPair)o;
       
                //System.out.println(pair.getPublic());
                //System.out.println(pair.getPrivate());
            }
            else
            {
                //System.out.println(o.toString());
            }
        }
       
        //
        // pkcs 7 data
        //
        fRd = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("pkcs7.pem")));
        pemRd = new PEMReader(fRd);
       
        ContentInfo d = (ContentInfo)pemRd.readObject();   
           
        if (!d.getContentType().equals(CMSObjectIdentifiers.envelopedData))
        {
            fail("failed envelopedData check");
        }
       
        //
        // writer/reader test
        //
        KeyPairGenerator      kpGen = KeyPairGenerator.getInstance("RSA", "BC");
        KeyPair               pair = kpGen.generateKeyPair();
       
        keyPairTest("RSA", pair);
       
        kpGen = KeyPairGenerator.getInstance("DSA", "BC");
        kpGen.initialize(512, new SecureRandom());
        pair = kpGen.generateKeyPair();
       
        keyPairTest("DSA", pair);
       
        //
        // PKCS7
        //
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        PEMWriter             pWrt = new PEMWriter(new OutputStreamWriter(bOut));
       
        pWrt.writeObject(d);
       
        pWrt.close();
       
        pemRd = new PEMReader(new InputStreamReader(new ByteArrayInputStream(bOut.toByteArray())));
        d = (ContentInfo)pemRd.readObject();   
       
        if (!d.getContentType().equals(CMSObjectIdentifiers.envelopedData))
        {
            fail("failed envelopedData recode check");
        }
View Full Code Here

    private void keyPairTest(
        String   name,
        KeyPair pair)
        throws IOException
    {
        PEMReader pemRd;
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        PEMWriter             pWrt = new PEMWriter(new OutputStreamWriter(bOut));
       
        pWrt.writeObject(pair.getPublic());
       
        pWrt.close();

        pemRd = new PEMReader(new InputStreamReader(new ByteArrayInputStream(bOut.toByteArray())));
       
        PublicKey k = (PublicKey)pemRd.readObject();
        if (!k.equals(pair.getPublic()))
        {
            fail("Failed public key read: " + name);
        }
       
        bOut = new ByteArrayOutputStream();
        pWrt = new PEMWriter(new OutputStreamWriter(bOut));
       
        pWrt.writeObject(pair.getPrivate());
       
        pWrt.close();
       
        pemRd = new PEMReader(new InputStreamReader(new ByteArrayInputStream(bOut.toByteArray())));
       
        KeyPair kPair = (KeyPair)pemRd.readObject();
        if (!kPair.getPrivate().equals(pair.getPrivate()))
        {
            fail("Failed private key read: " + name);
        }
       
View Full Code Here

        PasswordFinder passwordFinder = new PasswordFinder() {
            public char[] getPassword() {
                return passPhrase != null ? passPhrase.toCharArray() : new char[] {};
            }
        };
        PEMReader pemReader = new PEMReader(new InputStreamReader(pkInputStream), passwordFinder);
        KeyPair kp = (KeyPair) pemReader.readObject();
        PrivateKey privKey = kp.getPrivate();

        // Load certificates found in the PEM input stream
        List<X509Certificate> certs = new ArrayList<X509Certificate>();
        for (Certificate cert : CertificateFactory.getInstance("X509").generateCertificates(inputStream)) {
View Full Code Here

  public Certificate createFromPEM(String pemCsr) {
        if (pemCsr == null) {
            log.warn("pemCsr was null");
            return null;
        }
    PEMReader pemReader = new PEMReader(new StringReader(pemCsr));
    Object pemObject;
    try {
      pemObject = pemReader.readObject();
      if (pemObject instanceof PKCS10CertificationRequest) {
        PKCS10CertificationRequest pkcs10Obj = (PKCS10CertificationRequest) pemObject;
        DefaultCertificate cert = new DefaultCertificate(this);
        cert.setDefaultSerialisation(new PEMSerialisation(cert));
        try {
View Full Code Here

TOP

Related Classes of org.bouncycastle.openssl.PEMReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.