Package br.net.woodstock.rockframework.security.cert.util

Source Code of br.net.woodstock.rockframework.security.cert.util.Certificates

/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.security.cert.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.PublicKey;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.security.cert.CertificateException;
import br.net.woodstock.rockframework.security.cert.CertificateType;

public abstract class Certificates {

  private Certificates() {
    //
  }

  public static Certificate get(final InputStream inputStream) {
    return Certificates.get(inputStream, CertificateType.X509);
  }

  public static Certificate get(final InputStream inputStream, final CertificateType type) {
    try {
      Assert.notNull(inputStream, "inputStream");
      Assert.notEmpty(type, "type");
      CertificateFactory certFactory = CertificateFactory.getInstance(type.getType());
      Certificate certificate = certFactory.generateCertificate(inputStream);
      return certificate;
    } catch (GeneralSecurityException e) {
      throw new CertificateException(e);
    }
  }

  public static Certificate get(final byte[] bytes) {
    Assert.notEmpty(bytes, "bytes");
    InputStream inputStream = new ByteArrayInputStream(bytes);

    return Certificates.get(inputStream);
  }

  public static Certificate get(final byte[] bytes, final CertificateType type) {
    Assert.notEmpty(bytes, "bytes");
    Assert.notEmpty(type, "type");
    InputStream inputStream = new ByteArrayInputStream(bytes);

    return Certificates.get(inputStream, type);
  }

  public static boolean isSelfSigned(final Certificate certificate) throws GeneralSecurityException {
    try {
      PublicKey publicKey = certificate.getPublicKey();
      certificate.verify(publicKey);
      return true;
    } catch (SignatureException e) {
      return false;
    }
  }

}
TOP

Related Classes of br.net.woodstock.rockframework.security.cert.util.Certificates

TOP
Copyright © 2018 www.massapi.com. 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.