[Eppnicbr] Acesso ao EPP utilizando java
Carlos Eduardo Fontes Silva
carlos.eduardo at cges.com.br
Thu Jun 28 19:40:42 BRT 2007
Pessoal,
Consegui a conexão com o servidor utilizando o BouncyCastle(bcprov-jdk14-115.jar). Lembrando que gerei o p12 a partir do arquivo client.pem e adicionando o root.pem no keystore.
Segue o código:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
public class testTLS {
protected final static int INT_SZ = 4;
SSLSocket clientSocket;
KeyStore keyStore;
KeyManager[] keyManagers;
TrustManager[] trustManagers;
SecureRandom secureRandom;
SSLContext sslContext;
String KEYSTORE = "E:\\projetos\\ig.com.br\\iG Empresas\\Protocolo EPP\\TesteEPP\\cert\\client.p12";
char[] KEYSTOREPW ="123456".toCharArray();
char[] KEYPW = "123456".toCharArray();
String TRUSTSTORE = "E:\\projetos\\ig.com.br\\iG Empresas\\Protocolo EPP\\TesteEPP\\cert\\registrobr.jks";
char[] TRUSTSTOREPW = "123456".toCharArray();
String[] PROTOCOLS = {"SSLv3", "TLSv1"};
String epp_server = "beta.registro.br";
int epp_por = 700;
/**
* @param args
*/
public static void main(String[] args) {
testTLS x = new testTLS();
x.run();
}
public void run() {
String algoritm = "SunX509";
//algoritm = "PKCS12";
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
//Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
System.setProperty("javax.net.debug", "all");
try {
System.out.println(KeyManagerFactory.getDefaultAlgorithm());
//keyStore = KeyStore.getInstance("JKS");
keyStore = KeyStore.getInstance("PKCS12", "BC");
keyStore.load(new FileInputStream(KEYSTORE), KEYSTOREPW);
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algoritm);
kmf.init(keyStore, KEYSTOREPW);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(algoritm);
KeyStore truststore = KeyStore.getInstance("JKS");
truststore.load(new FileInputStream(TRUSTSTORE), TRUSTSTOREPW);
tmf.init(truststore);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
trustManagerFactory.init(keyStore);
keyManagers = kmf.getKeyManagers();
trustManagers = new TrustManager[] { tmf.getTrustManagers()[0], trustManagerFactory.getTrustManagers()[0] };
SSLContext sslc = SSLContext.getInstance("TLS");
sslc.init(this.keyManagers, this.trustManagers, null);
SSLSocketFactory factory = sslc.getSocketFactory();
SSLSocket socket = (SSLSocket)factory.createSocket(epp_server, epp_por);
socket.setEnabledProtocols(PROTOCOLS);
System.out.println("Connected to " + socket.getRemoteSocketAddress());
socket.startHandshake();
try {
System.out.println( readFromServerByte( socket ) );
} catch (Exception e) {
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
}
public String readFromServer( SSLSocket socket )
{
String line;
StringBuffer buf = null;
try
{
// with a new Socket, come new Reader and Writer
BufferedReader reader_from_server_ = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer_to_server_ = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
buf = new StringBuffer();
while((line = reader_from_server_.readLine()) != null)
{
buf.append(line);
if (line.trim().endsWith("</epp>")) break;
}
if (line == null)
{
//debug(DEBUG_LEVEL_TWO,method_name,"Connection closed by server.");
}
return buf.toString();
}
catch (IOException xcp)
{
xcp.printStackTrace();
}
return null;
}
public String readFromServerByte( SSLSocket socket )
{
// with a new Socket, come new Reader and Writer
BufferedInputStream reader_from_server_;
PrintWriter writer_to_server_ ;
try {
reader_from_server_ = new BufferedInputStream(socket.getInputStream());
writer_to_server_= new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
} catch (IOException e) {
e.printStackTrace();
return null;
}
int len = 0;
try
{
len = readBufferSize(reader_from_server_);
}
catch (Exception xcp)
{
xcp.printStackTrace();
return null;
}
if (len <= 0)
{
return null;
}
len -= INT_SZ;
byte[] in_buf = null;
try
{
in_buf = readInputBuffer(reader_from_server_,len);
}
catch (Exception xcp)
{
xcp.printStackTrace();
}
if (in_buf == null)
{
System.out.println("Failed reading EPP XML instance (in_buf == null).");
}
String value = new String(in_buf);
if (value == null)
{
return null;
}
return value;
}
/**
* Reads 4 bytes and converts them into an integer
* @return length of the XML instance and header
* @throws Exception if there was a Socket problem or less than 4 bytes read
*/
protected int readBufferSize (BufferedInputStream in) throws Exception
{
String method_name = "readBufferSize()";
int inbuf_sz = 0;
byte[] in_buf = new byte[INT_SZ];
int len = 0;
int bytesRead = 0;
while (bytesRead < INT_SZ)
{
try
{
len = in.read(in_buf,bytesRead,INT_SZ - bytesRead);
}
catch(IOException xcp)
{
if (xcp instanceof InterruptedIOException)
throw xcp;
return -1;
}
if (len < 0)
{
return -1;
}
bytesRead += len;
}
return (((in_buf[0] & 0xff) << 24) | ((in_buf[1] & 0xff) << 16) |
((in_buf[2] & 0xff) << 8) | (in_buf[3] & 0xff));
}
/**
* Reads inbuf_sz number of bytes from the socket
* @return bytes read
* @throws Exception if there was a Socket problem
*/
protected byte[] readInputBuffer (BufferedInputStream in, int inbuf_sz) throws Exception
{
String method_name = "readInputBuffer()";
byte[] in_buf = new byte[inbuf_sz];
int len = 0;
int bytesRead = 0;
while (bytesRead < inbuf_sz)
{
try
{
len = in.read(in_buf,bytesRead,inbuf_sz - bytesRead);
}
catch(IOException xcp)
{
if (xcp instanceof InterruptedIOException)
throw xcp;
return null;
}
if (len < 0)
{
return null;
}
bytesRead += len;
}
return in_buf;
}
}
Espero ter ajudado!
Att,
Carlos Eduardo
More information about the eppnicbr
mailing list