Commit 9f2bef75b354f36cca86e3945e4762cde77223ab

Authored by rogerio.costa
1 parent c4b7f8ca
Exists in master

#4583 - Assinar documento digital

cit-ecm-api/src/main/java/br/com/centralit/api/service/AssinaturaUtilService.java 0 → 100644
... ... @@ -0,0 +1,56 @@
  1 +package br.com.centralit.api.service;
  2 +
  3 +/**
  4 + * <p>
  5 + * <img src="http://centralit.com.br/images/logo_central.png">
  6 + * </p>
  7 + *
  8 + * <p>
  9 + * <b>Company: </b> Central IT - Governança Corporativa -
  10 + * </p>
  11 + *
  12 + * <p>
  13 + * <b>Title: </b>
  14 + * </p>
  15 + *
  16 + * <p>
  17 + * <b>Description: </b>
  18 + * </p>
  19 + *
  20 + * <p>
  21 + * <b>Iniciativa(s):</b> <a href="LINK_PORTAL">NUMERO_INICIATIVA</a>
  22 + * </p>
  23 + *
  24 + * <p>
  25 + * <b>Regra(s) de negócio:</b> <a href="LINK_PORTAL">NUMERO_REGRA_DE_NEGOCIO</a>
  26 + * </p>
  27 + *
  28 + * @since 30/04/2016 - 13:47:33
  29 + *
  30 + * @version 1.0.0
  31 + *
  32 + * @author rogerio.costa
  33 + *
  34 + */
  35 +public interface AssinaturaUtilService {
  36 +
  37 + /**
  38 + * <p>
  39 + * <b>Iniciativa(s):</b> <a href="LINK_PORTAL">NUMERO_INICIATIVA</a>
  40 + * </p>
  41 + *
  42 + * <p>
  43 + * <b>Regra(s) de negócio:</b> <a href="LINK_PORTAL">NUMERO_REGRA_DE_NEGOCIO</a>
  44 + * </p>
  45 + *
  46 + * Método responsável por assinar o documento
  47 + *
  48 + * @author rogerio.costa
  49 + *
  50 + * @param mensagem
  51 + * @return byte[]
  52 + * @throws Exception
  53 + */
  54 + byte[] geraAssinatura(byte[] conteudo) throws Exception;
  55 +
  56 +}
... ...
cit-ecm-api/src/main/java/br/com/centralit/api/service/impl/AssinaturaServiceImpl.java
... ... @@ -24,76 +24,84 @@ import br.com.centralit.framework.service.arquitetura.GenericServiceImpl;
24 24 import br.com.centralit.framework.util.UtilObjeto;
25 25 import br.com.centralit.framework.util.UtilString;
26 26  
27   -
28 27 /**
29   - * <p><b>Title: </b></p>
30   - * <p><b>Description: </b></p>
  28 + * <p>
  29 + * <b>Title: </b>
  30 + * </p>
  31 + * <p>
  32 + * <b>Description: </b>
  33 + * </p>
31 34 *
32 35 * @since 22/04/2016 - 11:50:56
33 36 * @author rogerio.cassimiro
34   - *
  37 + *
35 38 */
36 39 @Service("assinaturaService")
37 40 public class AssinaturaServiceImpl extends GenericServiceImpl<Assinatura, Long> implements AssinaturaService {
38 41  
39 42 @Autowired
40 43 private AssinaturaDao assinaturaDao;
41   -
  44 +
42 45 @Autowired
43 46 private DocumentoGedService documentoGedService;
44   -
  47 +
45 48 @Autowired
46 49 private UsuarioService usuarioService;
47   -
  50 +
48 51 @Autowired
49 52 private DominioService dominioService;
50   -
  53 +
51 54 @Autowired
52 55 private FuncaoService funcaoService;
53   -
  56 +
54 57 @Autowired
55 58 private PessoaService pessoaService;
56   -
  59 +
57 60 @Autowired
58 61 private HistoricoAlteracaoProcessoService historicoAlteracaoProcessoService;
59   -
  62 +
60 63 @Autowired
61 64 private TarjaAssinaturaService tarjaAssinaturaService;
62   -
  65 +
63 66 @Value("${metodo.autenticacao.ldap}")
64 67 private String ldap;
65   -
  68 +
66 69 @Autowired
67   - public AssinaturaServiceImpl( AssinaturaDao assinaturaDao) {
  70 + public AssinaturaServiceImpl( AssinaturaDao assinaturaDao ) {
  71 +
68 72 this.dao = assinaturaDao;
69 73 }
70   -
  74 +
71 75 /**
72 76 * Salva assinatura do documento e gera histórico
73 77 */
74 78 @Override
75 79 public Assinatura saveAssinaturaInterna(Assinatura assinatura) {
  80 +
76 81 Usuario usuario = (Usuario) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
77 82 validarSenha(assinatura.getPassword(), usuario);
78 83 montarTransients(assinatura, usuario);
79 84 historicoAlteracaoProcessoService.gerarHistoricoAssinatura(assinatura);
80   - tarjaAssinaturaService.gerarAssinaturaInternaUsuario(assinatura,assinatura.getDocumentoGed().getFormaCriacao().getCodigo());
  85 + tarjaAssinaturaService.gerarAssinaturaInternaUsuario(assinatura, assinatura.getDocumentoGed().getFormaCriacao().getCodigo());
81 86 alterarEstadoDocumento(assinatura.getDocumentoGed());
82 87 return super.save(assinatura);
83 88 }
84 89  
85 90 /**
86 91 * Método responsável por alterar estado do documento se a forma for online
  92 + *
87 93 * @author rogerio.cassimiro
88 94 * @param documentoGed
89 95 */
90 96 private void alterarEstadoDocumento(DocumentoGed documentoGed) {
91   - if(documentoGed.getFormaCriacao().getCodigo().equals(1L) && !documentoGed.getEstado().getCodigo().equals(2L))
92   - documentoGed.setEstado(dominioService.findByChaveAndCodigo("estadoDocumentoGed", 2L));
  97 +
  98 + if (documentoGed.getFormaCriacao().getCodigo().equals(1L) && !documentoGed.getEstado().getCodigo().equals(2L))
  99 + documentoGed.setEstado(dominioService.findByChaveAndCodigo("estadoDocumentoGed", 2L));
93 100 }
94 101  
95 102 /**
96 103 * Método responsável por montar transients da assinatura
  104 + *
97 105 * @author rogerio.cassimiro
98 106 * @param assinatura
99 107 * @param usuario
... ... @@ -106,45 +114,50 @@ public class AssinaturaServiceImpl extends GenericServiceImpl&lt;Assinatura, Long&gt;
106 114 assinatura.setTipoAssinatura(dominioService.findByChaveAndCodigo(Dominio.TIPO_ASSINATURA, Dominio.TIPO_ASSINATURA_INTERNA));
107 115 assinatura.setFuncao(funcaoService.getReference(assinatura.getFuncao().getId()));
108 116 }
109   -
  117 +
110 118 /**
111 119 * Método responsável por validar senha para assinar o documento
  120 + *
112 121 * @author rogerio.cassimiro
113 122 * @param password
114 123 * @param usuario
115 124 */
116 125 private void validarSenha(String password, Usuario usuario) {
117   -
118   - if(!UtilString.isNullOrEmpty(ldap) && ldap.equals("false")) {
119 126  
120   - if(UtilString.isNullOrEmpty(password))
  127 + if (!UtilString.isNullOrEmpty(ldap) && ldap.equals("false")) {
  128 +
  129 + if (UtilString.isNullOrEmpty(password))
121 130 throw new BusinessException("ECM.VALIDACAO.SENHA_ASSINATURA", CodigoErro.REGRA_NEGOCIO.getValue(), "");
122   -
123   - if(!UtilObjeto.isReferencia(this.usuarioService.loadUserByUsernamePasswordMobile(usuario.getUsername(), password)))
  131 +
  132 + if (!UtilObjeto.isReferencia(this.usuarioService.loadUserByUsernamePasswordMobile(usuario.getUsername(), password)))
124 133 throw new BusinessException("ECM.VALIDACAO.SENHA_ASSINATURA_INVALIDA", CodigoErro.REGRA_NEGOCIO.getValue(), "");
125   -
  134 +
126 135 } else {
127   - //TODO LDAP validação
  136 + // TODO LDAP validação
128 137 }
129 138 }
130   -
  139 +
131 140 /**
132 141 * Método responsável por validar se o usuário já teve assinatura anterior para o documento informado
  142 + *
133 143 * @author rogerio.cassimiro
134 144 * @return {@link Boolean}
135 145 */
136 146 @Override
137 147 public Boolean validarAssinaturaPorUsuario(Long idDocumento) {
138   - return this.assinaturaDao.validarAssinaturaPorUsuario(idDocumento, ((Usuario) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
  148 +
  149 + return this.assinaturaDao.validarAssinaturaPorUsuario(idDocumento, ( (Usuario) SecurityContextHolder.getContext().getAuthentication().getPrincipal() ).getId());
139 150 }
140 151  
141 152 @Override
142 153 public Assinatura getReference(Long id) {
  154 +
143 155 Assinatura assinatura = (Assinatura) this.assinaturaDao.getReference(id);
144 156 assinatura.getConteudoTarja().setConteudo(tarjaAssinaturaService.decrypted(assinatura.getConteudoTarja().getConteudoCriptografado()));
145   - if(UtilObjeto.isReferencia(assinatura.getDocumentoGed().getConteudoTarja())){
  157 + if (UtilObjeto.isReferencia(assinatura.getDocumentoGed().getConteudoTarja())) {
146 158 assinatura.getDocumentoGed().getConteudoTarja().setConteudo(tarjaAssinaturaService.decrypted(assinatura.getDocumentoGed().getConteudoTarja().getConteudoCriptografado()));
147 159 }
148 160 return assinatura;
149 161 }
  162 +
150 163 }
... ...
cit-ecm-api/src/main/java/br/com/centralit/api/service/impl/AssinaturaUtilServiceImpl.java 0 → 100644
... ... @@ -0,0 +1,224 @@
  1 +package br.com.centralit.api.service.impl;
  2 +
  3 +import java.io.DataInputStream;
  4 +import java.io.File;
  5 +import java.io.FileInputStream;
  6 +import java.security.InvalidKeyException;
  7 +import java.security.KeyFactory;
  8 +import java.security.MessageDigest;
  9 +import java.security.NoSuchAlgorithmException;
  10 +import java.security.PrivateKey;
  11 +import java.security.PublicKey;
  12 +import java.security.Signature;
  13 +import java.security.spec.PKCS8EncodedKeySpec;
  14 +import java.security.spec.X509EncodedKeySpec;
  15 +
  16 +import org.springframework.beans.factory.annotation.Value;
  17 +import org.springframework.stereotype.Service;
  18 +
  19 +import br.com.centralit.api.service.AssinaturaUtilService;
  20 +
  21 +/**
  22 + * <p>
  23 + * <img src="http://centralit.com.br/images/logo_central.png">
  24 + * </p>
  25 + *
  26 + * <p>
  27 + * <b>Company: </b> Central IT - Governança Corporativa -
  28 + * </p>
  29 + *
  30 + * <p>
  31 + * <b>Title: </b>AssinaturaUtilServiceImpl
  32 + * </p>
  33 + *
  34 + * <p>
  35 + * <b>Description: </b> Classe responsável por assinar e validar autenticidade do documento.
  36 + * </p>
  37 + *
  38 + * <p>
  39 + * <b>Iniciativa(s):</b> <a href="LINK_PORTAL">NUMERO_INICIATIVA</a>
  40 + * </p>
  41 + *
  42 + * <p>
  43 + * <b>Regra(s) de negócio:</b> <a href="LINK_PORTAL">NUMERO_REGRA_DE_NEGOCIO</a>
  44 + * </p>
  45 + *
  46 + * @since 30/04/2016 - 13:47:59
  47 + *
  48 + * @version 1.0.0
  49 + *
  50 + * @author rogerio.costa
  51 + *
  52 + */
  53 +@Service("assinaturaUtilService")
  54 +public class AssinaturaUtilServiceImpl implements AssinaturaUtilService {
  55 +
  56 + /** Atributo publicKeyFile. */
  57 + @Value("${ecm.signature.publicKeyFile}")
  58 + private File publicKeyFile;
  59 +
  60 + /** Atributo privateKeyFile. */
  61 + @Value("${ecm.signature.privateKeyFile}")
  62 + private File privateKeyFile;
  63 +
  64 + /**
  65 + * <p>
  66 + * <b>Iniciativa(s):</b> <a href="LINK_PORTAL">NUMERO_INICIATIVA</a>
  67 + * </p>
  68 + *
  69 + * <p>
  70 + * <b>Regra(s) de negócio:</b> <a href="LINK_PORTAL">NUMERO_REGRA_DE_NEGOCIO</a>
  71 + * </p>
  72 + *
  73 + * Método responsável por assinar o documento
  74 + *
  75 + * @author rogerio.costa
  76 + *
  77 + * @param mensagem
  78 + * @return byte[]
  79 + * @throws InvalidKeyException
  80 + * @throws Exception
  81 + */
  82 + public byte[] geraAssinatura(byte[] conteudo) throws Exception {
  83 +
  84 + Signature sig = Signature.getInstance("SHA1withRSA");
  85 +
  86 + // Inicializando Obj Signature com a Chave Privada
  87 + sig.initSign(this.getPrivateKey());
  88 +
  89 + // Gerar assinatura
  90 + sig.update(this.gerarHash(conteudo));
  91 +
  92 + byte[] assinatura = sig.sign();
  93 +
  94 + return assinatura;
  95 + }
  96 +
  97 + /**
  98 + * <p>
  99 + * <b>Iniciativa(s):</b> <a href="LINK_PORTAL">NUMERO_INICIATIVA</a>
  100 + * </p>
  101 + *
  102 + * <p>
  103 + * <b>Regra(s) de negócio:</b> <a href="LINK_PORTAL">NUMERO_REGRA_DE_NEGOCIO</a>
  104 + * </p>
  105 + *
  106 + * Método responsável por
  107 + *
  108 + * @author rogerio.costa
  109 + *
  110 + * @param conteudo
  111 + * @param assinatura
  112 + * @throws Exception
  113 + */
  114 + public Boolean validarAutenticidade(byte[] conteudo, byte[] assinatura) throws Exception {
  115 +
  116 + Signature clientSig = Signature.getInstance("SHA1withRSA");
  117 +
  118 + clientSig.initVerify(getPublicKey());
  119 +
  120 + clientSig.update(this.gerarHash(conteudo));
  121 +
  122 + return clientSig.verify(assinatura);
  123 +
  124 + }
  125 +
  126 + /**
  127 + * <p>
  128 + * <b>Iniciativa(s):</b> <a href="LINK_PORTAL">NUMERO_INICIATIVA</a>
  129 + * </p>
  130 + *
  131 + * <p>
  132 + * <b>Regra(s) de negócio:</b> <a href="LINK_PORTAL">NUMERO_REGRA_DE_NEGOCIO</a>
  133 + * </p>
  134 + *
  135 + * Método responsável por obter a chave privada
  136 + *
  137 + * @author rogerio.costa
  138 + *
  139 + * @return PrivateKey
  140 + *
  141 + * @throws Exception
  142 + */
  143 + public PrivateKey getPrivateKey() throws Exception {
  144 +
  145 + FileInputStream fis = new FileInputStream(this.privateKeyFile);
  146 +
  147 + DataInputStream dataInputStream = new DataInputStream(fis);
  148 +
  149 + byte[] keyBytes = new byte[(int) this.privateKeyFile.length()];
  150 +
  151 + dataInputStream.readFully(keyBytes);
  152 +
  153 + dataInputStream.close();
  154 +
  155 + PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
  156 +
  157 + KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  158 +
  159 + return keyFactory.generatePrivate(spec);
  160 +
  161 + }
  162 +
  163 + /**
  164 + * <p>
  165 + * <b>Iniciativa(s):</b> <a href="LINK_PORTAL">NUMERO_INICIATIVA</a>
  166 + * </p>
  167 + *
  168 + * <p>
  169 + * <b>Regra(s) de negócio:</b> <a href="LINK_PORTAL">NUMERO_REGRA_DE_NEGOCIO</a>
  170 + * </p>
  171 + *
  172 + * Método responsável por obter a chace pública
  173 + *
  174 + * @author rogerio.costa
  175 + *
  176 + * @return
  177 + * @throws Exception
  178 + */
  179 + public PublicKey getPublicKey() throws Exception {
  180 +
  181 + FileInputStream fis = new FileInputStream(this.publicKeyFile);
  182 +
  183 + DataInputStream dataInputStream = new DataInputStream(fis);
  184 +
  185 + byte[] keyBytes = new byte[(int) this.publicKeyFile.length()];
  186 +
  187 + dataInputStream.readFully(keyBytes);
  188 +
  189 + dataInputStream.close();
  190 +
  191 + X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
  192 +
  193 + KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  194 +
  195 + return keyFactory.generatePublic(spec);
  196 + }
  197 +
  198 + /**
  199 + * <p>
  200 + * <b>Iniciativa(s):</b> <a href="LINK_PORTAL">NUMERO_INICIATIVA</a>
  201 + * </p>
  202 + *
  203 + * <p>
  204 + * <b>Regra(s) de negócio:</b> <a href="LINK_PORTAL">NUMERO_REGRA_DE_NEGOCIO</a>
  205 + * </p>
  206 + *
  207 + * Método responsável por gerar o hash do documento
  208 + *
  209 + * @author rogerio.costa
  210 + *
  211 + * @param doc
  212 + * @return
  213 + * @throws NoSuchAlgorithmException
  214 + */
  215 + public byte[] gerarHash(byte[] doc) throws NoSuchAlgorithmException {
  216 +
  217 + MessageDigest md = MessageDigest.getInstance("MD5");
  218 +
  219 + md.update(doc);
  220 +
  221 + return md.digest();
  222 + }
  223 +
  224 +}
... ...
cit-ecm-api/src/main/resources/keys/private_key.der 0 → 100644
No preview for this file type
cit-ecm-api/src/main/resources/keys/private_key.pem 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +-----BEGIN RSA PRIVATE KEY-----
  2 +MIIEowIBAAKCAQEAuz6F91j0K2cogHAWhvTON6TKqHwxn97zA4U2TrjuczwQxAnz
  3 +Z/EzxbNKwxpMktuoVMTRYrqRAPWEK4mEMC4yQnH0O5NmwVtqf1/URd3CuNwszWRC
  4 +EXY1H1e4K26aLFQHaudBkVxkAPdzozX1guJWrG7y6pKn2RNt17h0DLJGvf21RFKl
  5 +7M06UewXartSF0VtxiOXA5RktmNh/E7zwaY/y7CBEoiYR/Vhg+xv1a9x1TwslbR5
  6 +/KpVDiarLXJ4ssU0Zx/dT7l9wdx8wNox2SpkrC6jHyum8v++3vV1qMIbQ91nkyaD
  7 +qQ4QRhGxd35iqSIolbkP3eq63zX8kHw/RxwP6wIDAQABAoIBAHkIDxw1GY88nQd4
  8 +bHdLokiBcqW5sXH/cOGjp9j23NsLxmKRdA6J8Mcn/3lhdIGxDnvMxPOmWLhf+tER
  9 +kKp8Eey1MAOaYd82Mf6neNS4ZXeMikExPyt+VxQtZVRdp7Zjkm/dj5pgOBwP12V2
  10 +KX4yw4euJ8LMjUZZsrSv/AOA8Q045tVpcN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MKqZ1njJhLUTa4547bSwf3VAwmoWcsaEl/b5dyGN/f4lBxP
  27 +-----END RSA PRIVATE KEY-----
... ...
cit-ecm-api/src/main/resources/keys/public_key.der 0 → 100644
No preview for this file type