Utilitário para geração de TrustStore (JKS) – Java

Olá Pessoal,

Desenvolvi um utilitário que transforma um arquivo zip, contendo “n” certificados de Autoridades Certificadoras para um arquivo JKS.

Para referência segue um link que contém todas as CA’s brasileiras:

http://acraiz.icpbrasil.gov.br/credenciadas/CertificadosAC-ICP-Brasil/ACcompactado.zip

Como seria o processo normal ?

1 – Java Keytool – http://download.oracle.com/javase/6/docs/technotes/tools/windows/keytool.html
É um software que acompanha a máquina virtual Java (jdk), utilizado para diversas finalidades com relação à segurança de aplicações e pode ser usado para gerar o pacote JKS.
Seguem dois comandos exemplo para se trabalhar com arquivos JKS de truststore:

1. Listando todos os certificados contidos em um arquivo JKS:

keytool –list –v –keystore C:\meukeystore.jks

2. Importando um certificado de uma AC para dentro de um JKS pré-existente

keytool –import –trustcacerts –file C:\certificadoAC.cer –alias apelidoentrada –keystore C:\meutruststore.jks

Observação: Caso não exista um jks no diretório especificado em -keystore, será criado um automaticamente.
O arquivo ACcompactado.zip especificado na URL anteriormente, possui atualmente 84 certificados, portanto, deve-se executar o comando (2), 84 vezes, alterando o -alias e o –file. Por ser esta uma forma muito trabalhosa de se gerar um arquivo Trusted JKS, foi criado um utilitário que auxiliará nesta etapa, apresentado no próximo capítulo.

2 – Usando o utilitário – utilitarioTrustJKS.jar
O utilitário desenvolvido basicamente recebe como entrada um arquivo zip com todos os certificados desejados e gera um arquivo trust.jks.
Parâmetros de Entrada:
1 – caminhoZip – Diretório do arquivo zip que contém todos os certificados das AC’s
2 – caminhoSaida – Diretório de saída, onde será gerado o truststore.jks
3 – senhaKestore – Senha do arquivo TrustStore JKS
4 – incluirExpirados (opcional) – valor default => false) – Possui os valores true ou false. Informa se certificados expirados ou não válidos ainda deverão ser incluídos no arquivo JKS gerado.

Exemplo de utilização:

java -jar utilitarioTrustJKS.jar C:\ACcompactado.zip C:\truststore.jks 123456789 true

Link para Download do arquivo Jar:
http://arquivos.victorjabur.com/java/seguranca/utilitarioTrustJKS.jar

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateFactory;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.X509Certificate;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;

public class Main {

	private String caminhoZIP = "C:\\ACcompactado.zip";
	private String caminhoSaida = "C:\\truststore.jks";
	private String senhaKeystore = "123456789";
	private Boolean incluirExpirados = false;

	public static void main(String[] args) throws Exception {
		Main main = new Main();
		main.validarParametrosEntrada(args);
		List<X509Certificate> listaCertificadosValidos = main.getListaCertificadosValidos();
		main.gerarJKS(listaCertificadosValidos);
		System.out.println("Arquivo JKS gerado com sucesso - " + listaCertificadosValidos.size() + " certificados incluidos");
	}

	public void validarParametrosEntrada(String[] args){
		if(args.length < 3){
			throw new RuntimeException("O numero minimo de parametros = 3. caminhoZip caminhoSaida senhaKeystore e incluirExpirados (Opcional) ");
		}else{
			String caminhoZip = args[0];
			validacaoLeituraArquivo(new File(caminhoZip));
			this.caminhoZIP = caminhoZip;
			String caminhoSaida = args[1];
			this.caminhoSaida = caminhoSaida;
			String senhaKeystore = args[2];
			this.senhaKeystore = senhaKeystore;
			if(args.length >= 4){
				String incluirExpirados = args[3];
				if(incluirExpirados.equals("true") || incluirExpirados.equals("false")){
					this.incluirExpirados = Boolean.valueOf(incluirExpirados);
				}else{
					throw new RuntimeException("O parametro incluirExpirados (4) deve ser true ou false");
				}
			}
		}
	}
	
	public List<X509Certificate> getListaCertificadosValidos() {
		File arquivoZipEntrada = new File(this.caminhoZIP);
		validacaoLeituraArquivo(arquivoZipEntrada);
		List<X509Certificate> x509CertificateList = new LinkedList<X509Certificate>();
		try {
			InputStream in = new FileInputStream(arquivoZipEntrada);
			ZipInputStream zipInputStream = new ZipInputStream(in);

			ZipEntry zipentry = zipInputStream.getNextEntry();
			while (zipentry != null) {
				byte[] buffer = new byte[(int) zipentry.getSize()];
				int offset = 0;
				int numRead = 0;
				while (offset < buffer.length && (numRead = zipInputStream.read(buffer, offset, buffer.length - offset)) >= 0) {
					offset += numRead;
				}
				InputStream bis = new ByteInputStream(buffer, 0, buffer.length);
				CertificateFactory cf = CertificateFactory.getInstance("X.509");
				X509Certificate cert = (X509Certificate) cf.generateCertificate(bis);
				try {
					cert.checkValidity();
					x509CertificateList.add(cert);
				} catch (CertificateExpiredException e) {
					if(this.incluirExpirados){
						x509CertificateList.add(cert);
						System.out.println("Certificado Expirado - " + zipentry.getName());
					}else{
						System.out.println("Certificado Expirado - " + zipentry.getName() + " - nao sera adicionado no JKS");
					}
				} catch (CertificateNotYetValidException e) {
					if(this.incluirExpirados){
						x509CertificateList.add(cert);
						System.out.println("Certificado não válido ainda - " + zipentry.getName());
					}else{
						System.out.println("Certificado não válido ainda - " + zipentry.getName() + " - nao sera adicionado no JKS");
					}
				}
				zipInputStream.closeEntry();
				zipentry = zipInputStream.getNextEntry();
			}
			zipInputStream.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return x509CertificateList;
	}

	public OutputStream gerarJKS(List<X509Certificate> listaCertificados){
		OutputStream out = null;
		try {
			KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
			keystore.load(null, null);
			int indice = 0;
			for (X509Certificate cert : listaCertificados) {
				keystore.setCertificateEntry("ac_" + indice, cert);
				indice++;
			}
			File jks = new File(this.caminhoSaida);
			String diretorioDestino = jks.getParent();
			new File(diretorioDestino).mkdirs();
			out = new FileOutputStream(this.caminhoSaida);
			keystore.store(out, this.senhaKeystore.toCharArray());
			out.close();
			out.flush();
		} catch (Exception e) {
			throw new RuntimeException("Erro ao gerar o arquivo de Keystore - " + e.getCause() + " - " + e.getMessage());
		}
		return out;
	}
	
	public void validacaoLeituraArquivo(File arquivo) {
		if (!arquivo.exists()) {
			throw new RuntimeException("Arquivo Inexistente - " + arquivo.getAbsolutePath());
		}
		if (!arquivo.canRead()) {
			throw new RuntimeException("Sem permissão de Leitura do Arquivo - " + arquivo.getAbsolutePath());
		}
	}
}

Abraços,
Victor Jabur

How to access server MBean properties at weblogic 11g using Java – JMX

Hi,

If you want to get any property of Admin or Managed Server (Weblogic) using Java, then this post will help you.

The Oracle Enterprise Manager have a useful tool for explore MBean (System MBean Browser).

As you can see in the picture bellow:

Do you can to capture any server property desired with these java code:

1 – Reading a property if the application is deployed at server (local connection)

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.naming.InitialContext;

String serverName = System.getProperty("weblogic.Name");
InitialContext ctx = new InitialContext();
MBeanServer server = (MBeanServer)ctx.lookup("java:comp/env/jmx/runtime");
ObjectName objName = new ObjectName("com.bea:Name=" + serverName + ",Type=Server");
String pathJKS = (String) server.getAttribute(objName, "CustomTrustKeyStoreFileName");

2 – Reading a property if the application is remote (remote connection)

import java.util.Hashtable;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;

  public static void main(String [] args) throws Exception{
    String serverName = "AdminServer";
    String hostName = "192.168.1.192";
    String username = "weblogic";
    String password = "welcome1";
    int port = 10000;
    String protocol = "t3";
    String jndiroot = "/jndi/";
    String mserver = "weblogic.management.mbeanservers.domainruntime";
    JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostName, port, jndiroot + mserver);
    Hashtable h = new Hashtable();
    h.put(Context.SECURITY_PRINCIPAL, username);
    h.put(Context.SECURITY_CREDENTIALS, password);
    h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
    JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);
    ObjectName objName = new ObjectName("com.bea:Name=" + serverName + ",Type=Server");
    String pathJKS = (String) connector.getMBeanServerConnection().getAttribute(objName, "CustomTrustKeyStoreFileName");
    System.out.println(pathJKS);
  }

In the cases above, i’m reading a property that is called “CustomTrustKeyStoreFileName”, that’s a string path of the Trusted Keystore. But many and many properties could be read, for example:

  • CustomIdentityKeyStoreFileName
  • ListenPort
  • UploadDirectoryName

That’s it.

Victor Jabur

Working with Expiration Human Task on Oracle Soa Suite 11g

Hello,

I had that to implement an expiration process and go to leave the main steps here:

1- Creating a new Application: File -> New -> Generic Application -> ApplicationExpirationHT
2- Adding a project to application: Project Name: ProjectExpirationHT Project Type: SOA
3- Select: Empty Composite and click at Finish
4- Make a drag and drop of BPEL Process to your composite.xml screen PS: Choose One Way BPEL Process on template combobox
5- Go to file BPELProcess1.bpel in design mode and drag and drop a Human Task Component
6-Double Click in the green human task icon, click at the green plus button
7-Add a Task parameter, for this example i will make the default input parameter (string), that i will use to set the time of expiration (on duration format)
8-Here goes the magic point; Add the custom outcome action called “EXPIRED”
9-This is the final configuration screen. Your screen should like this:
10-Edit your Humantask1.task file and add an user assignment (weblogic is the default user)
11-This is the main configuration for the task expiration (Deadlines): Check “Expire After” and select the task parameter that exists in your payload variable, for this example is inputstring (/task:task/task:payload/task:Parameter1)
12-This is the final step, when your task expires, will be enter on Expired branch and you could make other things when the task expires
Important Tips-

  1. Your payload parameter should be in a DURATION Format, here is an example (P0Y0M0DT0H0M0S). Here is more details about this default type: http://www.w3.org/TR/xmlschema-2/#duration
  2. When you set the Deadlines on your .task file, the expression for duration should be a parameter of your payload, because the rest of task parameters is READ-ONLY
  3. If your bpel is already created and you want to add the expiration validation, don’t worry, you cannot to recreate all flow, it is enough to add a new branch, copy of the others outcomes expressions and change the status task to EXPIRED, it works !
That is it people, if have any question, invite a email for me or better, add commentary for this post and i can help you !

How to capture the Task programmatically through Managed Bean (Adf – Soa Suite – Human Task)

Hi,

This is another useful code that lets you to capture the task with full attributes from your Java Class (Managed Bean).


import oracle.bpel.services.workflow.WorkflowException;
import oracle.bpel.services.workflow.client.IWorkflowServiceClient;
import oracle.bpel.services.workflow.datacontrol.WorkflowService;
import oracle.bpel.services.workflow.query.ITaskQueryService;
import oracle.bpel.services.workflow.task.model.TaskImpl;
import oracle.bpel.services.workflow.verification.IWorkflowContext;

public TaskImpl getCurrentTaskFromWorkflow() throws WorkflowException {
    TaskImpl task = null;
    Map parameters=AdfFacesContext.getCurrentInstance().getPageFlowScope();
    IWorkflowServiceClient wfSvcClient = WorkflowService.getWorkflowServiceClient();
    ITaskQueryService queryService = wfSvcClient.getTaskQueryService();
    String contextId = (String)parameters.get("bpmWorklistContext");
    IWorkflowContext context = queryService.getWorkflowContext(contextId);
    String taskId = (String)parameters.get("bpmWorklistTaskId");
    String strTaskVersion = (String)parameters.get("bpmWorklistTaskVersion");
    if(taskId != null){
        int taskVersion = 0;
        if(strTaskVersion != null && !strTaskVersion.trim().equals("")){
            try{
                taskVersion = Integer.parseInt(strTaskVersion);
            } catch(NumberFormatException exc){
                taskVersion = 1;
            }
        }
        if(taskVersion == 0){
            task = (TaskImpl)queryService.getTaskDetailsById(context, taskId);
        }else{
            task = (TaskImpl)queryService.getTaskVersionDetails(context, taskId, taskVersion);
        }
    }
    return task;
}
Follow

Get every new post delivered to your Inbox.

Join 447 other followers