Package br.net.woodstock.rockframework.security.cert.ext.icpbrasil

Source Code of br.net.woodstock.rockframework.security.cert.ext.icpbrasil.ICPBrasilCertificateValidator

/*
* 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.ext.icpbrasil;

import java.io.Serializable;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.core.utils.Collections;
import br.net.woodstock.rockframework.core.utils.Conditions;
import br.net.woodstock.rockframework.security.cert.CertificateValidator;
import br.net.woodstock.rockframework.security.cert.ValidationError;

public class ICPBrasilCertificateValidator implements CertificateValidator, Serializable {

  private static final long  serialVersionUID  = RockFrameworkVersion.VERSION;

  public static final String  VALIDATOR_NAME    = "ICP Brasil Validator";

  private TipoFormato[]    tiposFormato;

  public ICPBrasilCertificateValidator() {
    super();
  }

  public ICPBrasilCertificateValidator(final TipoFormato[] tiposFormato) {
    super();
    Assert.notEmpty(tiposFormato, "tiposFormato");
    this.tiposFormato = tiposFormato;
  }

  @Override
  public ValidationError[] validate(final Certificate[] chain) {
    Assert.notEmpty(chain, "chain");
    X509Certificate x509Certificate = (X509Certificate) chain[0];
    CertificadoICPBrasil certificadoICPBrasil = CertificadoICPBrasil.getInstance(x509Certificate);
    List<ValidationError> list = new ArrayList<ValidationError>();

    if (certificadoICPBrasil.getTipoPessoa() == TipoPessoa.DESCONHECIDO) {
      list.add(new ValidationError(ICPBrasilCertificateValidator.VALIDATOR_NAME, "Tipo do propriet�rio do certificado inv�lido(PF/PJ)"));
    }

    if (Conditions.isNotEmpty(this.tiposFormato)) {
      boolean ok = false;
      for (TipoFormato tipoFormato : this.tiposFormato) {
        if (tipoFormato.equals(certificadoICPBrasil.getTipoFormato())) {
          ok = true;
          break;
        }
      }
      if (!ok) {
        list.add(new ValidationError(ICPBrasilCertificateValidator.VALIDATOR_NAME, "Formato do certificado inv�lido(A1,A2,A3,A4)"));
      }
    }

    return Collections.toArray(list, ValidationError.class);
  }
}
TOP

Related Classes of br.net.woodstock.rockframework.security.cert.ext.icpbrasil.ICPBrasilCertificateValidator

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.