Exemplo n.º 1
0
  protected void load(InputStream input) throws CredentialException {

    if (input == null) {
      throw new IllegalArgumentException("input stream cannot be null");
    }

    X509Certificate cert = null;
    Vector chain = new Vector(3);
    String line;
    BufferedReader reader = null;

    try {
      reader = new BufferedReader(new InputStreamReader(input));
      while ((line = reader.readLine()) != null) {

        if (line.indexOf("BEGIN CERTIFICATE") != -1) {
          byte[] data = getDecodedPEMObject(reader);
          cert = CertificateLoadUtil.loadCertificate(new ByteArrayInputStream(data));
          chain.addElement(cert);
        } else if (line.indexOf("BEGIN RSA PRIVATE KEY") != -1) {
          byte[] data = getDecodedPEMObject(reader);
          this.opensslKey = new BouncyCastleOpenSSLKey("RSA", data);
        }
      }
    } catch (Exception e) {
      throw new CredentialException(e);
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
        }
      }
    }

    int size = chain.size();

    if (size == 0) {
      throw new CredentialException("no certs");
    }

    if (opensslKey == null) {
      throw new CredentialException("no key");
    }

    // set chain
    this.certChain = new X509Certificate[size];
    chain.copyInto(certChain);
  }
Exemplo n.º 2
0
  protected void loadCertificate(InputStream input) throws CredentialException {

    if (input == null) {
      throw new IllegalArgumentException("Input stream to load X509Credential is null");
    }

    X509Certificate cert;
    Vector<X509Certificate> chain = new Vector<X509Certificate>();

    String line;
    BufferedReader reader = null;
    try {
      if (input.markSupported()) {
        input.reset();
      }
      reader = new BufferedReader(new InputStreamReader(input));

      while ((line = reader.readLine()) != null) {

        if (line.indexOf("BEGIN CERTIFICATE") != -1) {
          byte[] data = getDecodedPEMObject(reader);
          cert = CertificateLoadUtil.loadCertificate(new ByteArrayInputStream(data));
          chain.addElement(cert);
        }
      }

    } catch (IOException e) {
      throw new CredentialException(e);
    } catch (GeneralSecurityException e) {
      throw new CredentialException(e);
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
          logger.debug("error closing reader", e);
          // This is ok
        }
      }
    }

    int size = chain.size();
    if (size > 0) {
      this.certChain = new X509Certificate[size];
      chain.copyInto(this.certChain);
    }
  }