[Eppnicbr] Provisionamento de domínios usando protocolo EPP
Carlos Eduardo Fontes Silva
carlos.eduardo at cges.com.br
Thu Apr 15 15:10:12 BRT 2010
Já faz sim!
Toda a comunicação com o protocolo EPP eu peguei em um pacote EPPRTK na internet. Esse pacote já tem o básico para fazer a conexão, login, etc.. É só ler um pouco para saber como utilizá-la.
Depois que você conseguiu executar o comando "socket.startHandshake();", você criar um inputstream e um outputstream:
protected BufferedOutputStream writer_to_server_;
protected BufferedInputStream reader_from_server_;
reader_from_server_ = new BufferedInputStream(socket.getInputStream());
writer_to_server_ = new BufferedOutputStream(socket.getOutputStream());
...
Para mandar mensagens para o servidor você usa a função abaixo:
/**
* Sends an XML string to the Server
* @throws org.openrtk.idl.epprtk.epp_Exception if there was a socket error in writing to the EPP Server.
* The epp_Exception will contain a result with the code epp_Session.RTK_COMMUNICATIONS_FAILURE
* @see org.openrtk.idl.epprtk.epp_Session#RTK_COMMUNICATIONS_FAILURE
*/
public void writeToServer(String xml_to_server) throws epp_Exception
{
String method_name = "writeToServer(String)";
debug(DEBUG_LEVEL_THREE,method_name,"Entered");
try
{
String line;
StringBuffer buf;
BufferedReader reader_outbound_xml = new BufferedReader(new StringReader(xml_to_server));
buf = new StringBuffer();
while((line = reader_outbound_xml.readLine()) != null)
{
line = line.trim();
if (line.startsWith("#") || line.length() == 0) continue;
if (!line.startsWith("<")) buf.append(' ');
// FIXME: Hack to prevent <?xml .. ?> to be sent multiple times
if (!line.endsWith("?>")) buf.append(line);
if (line.trim().endsWith("</epp>")) break;
}
String final_xml = buf.toString();
int len = final_xml.length();
writeBufferSize(writer_to_server_, len + INT_SZ);
writer_to_server_.write(final_xml.getBytes(), 0, len);
writer_to_server_.flush();
}
catch (Exception xcp)
{
debug(DEBUG_LEVEL_ONE,method_name,xcp);
epp_Result[] result = new epp_Result[1];
result[0] = new epp_Result();
result[0].m_code = epp_Session.RTK_COMMUNICATIONS_FAILURE;
result[0].m_msg = "Failed to write to server ["+xcp.getClass().toString()+"] ["+xcp.getMessage()+"]";
throw new epp_Exception( result );
}
debug(DEBUG_LEVEL_THREE,method_name,"Leaving");
}
Mas tive que implementar na época um classe de transporte em cima da que já existia no EPPRTK:
No arquivo rtk.properties troquei para a minha classe
# indicate the transport class that should be used in the connection
# to the EPP server. If no package name is included in the
# string, then "com.tucows.oxrs.epp.rtk.transport" is assumed.
# rtk.transport=EPPTransportTCPTLS
rtk.transport=br.com.XXXX.socket.epp.xml.EPPTransportTCPTLS
Segue o fonte:
package br.com.xxxx.socket.epp.xml;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Properties;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import org.ofbiz.base.util.UtilProperties;
import br.com.xxx.socket.tls.CertX509TLSManager;
import br.com.xxx.socket.tls.RegistroBrSocket;
import com.tucows.oxrs.epprtk.rtk.transport.EPPTransportException;
import com.tucows.oxrs.epprtk.rtk.transport.EPPTransportTCP;
public class EPPTransportTCPTLS extends EPPTransportTCP {
private SSLContext ctx_ = null;
private KeyStore ks_ = null;
private KeyManagerFactory kmf_ = null;
private SecureRandom rnd_ = null;
private TrustManagerFactory tmf_ = null;
protected Properties config = UtilProperties.getProperties("configuracao.properties");
/**
* Default Construtor
*/
public EPPTransportTCPTLS() {
super();
}
/**
* Construtor with Hostname, Host port and timeout value
* @param host_name The server Hostname
* @param host_port The server Host port
* @param timeout The int socket timeout value, in milliseconds
*/
public EPPTransportTCPTLS(String host_name, int host_port, int timeout) {
super(host_name, host_port, timeout);
}
/**
* Connects to the Server using previously set Hostname and port.
* If connection has been already established, the operation will be ignored.
* The method also sets the SO timeout.
* @throws SocketException
* @throws IOException
* @throws UnknownHostException
*/
public void connect() throws SocketException, IOException, UnknownHostException, EPPTransportException {
//System.setProperty("javax.net.debug", "all");
String method_name = "connect()";
debug(DEBUG_LEVEL_THREE, method_name, "Entered");
java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
if (!preset_) {
// Initialize to null the socket to the server
socket_to_server_ = null;
debug(DEBUG_LEVEL_TWO, method_name, "Using SSL/TLS");
SSLSocketFactory ssl_factory = null;
try {
char[] pwdClient = config.getProperty("registar.registrobr.config.certs.client.pwd").toCharArray();
String pkcs12 = config.getProperty("registar.registrobr.config.certs.client"); //"E:\\projetos\\xxxx\\Protocolo EPP\\TesteEPP\\cert\\client.p12";
if (ctx_ == null) {
ctx_ = SSLContext.getInstance("TLS");
}
if (ks_ == null) {
ks_ = KeyStore.getInstance("PKCS12", "BC");
File f = new File(pkcs12);
ks_.load(new FileInputStream(f.getCanonicalPath()), pwdClient);
}
if (kmf_ == null) {
kmf_ = KeyManagerFactory.getInstance("SunX509");
kmf_.init(ks_, pwdClient);
}
/**
* Implement trust store
* @author carlos.eduardo
*/
if ( tmf_ == null ) {
tmf_ = TrustManagerFactory.getInstance("SunX509");
KeyStore truststore = KeyStore.getInstance("JKS");
char[] passphrase2 = config.getProperty("registar.registrobr.config.certs.root.pwd").toCharArray();
String jks = config.getProperty("registar.registrobr.config.certs.root");//"E:\\projetos\\xxxx\\Protocolo EPP\\TesteEPP\\cert\\registrobr.jks";
truststore.load(new FileInputStream( jks ),
passphrase2 );
tmf_.init(truststore);
}
ctx_.init(kmf_.getKeyManagers(), tmf_.getTrustManagers(), rnd_);
//ctx_.init(null, RegistroBrSocket.getInstance().getCertsTrustManagers() , rnd_);
ssl_factory = ctx_.getSocketFactory();
} catch (Exception xcp) {
throw new IOException(xcp.getMessage());
}
// SSL Performance improvement from wessorh
try {
byte seed[] = new byte[1024];
FileInputStream is = new FileInputStream("/dev/urandom");
is.read(seed);
is.close();
rnd_ = java.security.SecureRandom.getInstance("SHA1PRNG");
rnd_.setSeed(seed);
debug(DEBUG_LEVEL_TWO, method_name, "SecureRandom seed set.");
} catch (Exception xcp) {
debug(DEBUG_LEVEL_TWO, method_name, "Error initializing SecureRandom [" + xcp.getMessage() + "], using default initialization.");
rnd_ = null;
}
socket_to_server_ = (Socket) ssl_factory.createSocket(epp_host_name_, epp_host_port_);
//Update protocols
if ( "SSLv3,TLSv1" != null )
((SSLSocket)socket_to_server_).setEnabledProtocols(
"SSLv3,TLSv1".split( "\\," ) );
// Force the handshake to happen now so we can check for a good connection
SSLSession la_session = ((SSLSocket) socket_to_server_).getSession();
if (socket_to_server_ == null || la_session.getProtocol().equals("NONE")) {
throw new EPPTransportException("Failed to establish secure connection to server. Perhaps a bad certificate? -- use -Djavax.net.debug=all to see errors.");
}
}
socket_to_server_.setSoTimeout(epp_timeout_);
reader_from_server_ = new BufferedInputStream(socket_to_server_.getInputStream());
writer_to_server_ = new BufferedOutputStream(socket_to_server_.getOutputStream());
debug(DEBUG_LEVEL_TWO, method_name, "Connected to [" + socket_to_server_.getInetAddress() + ":" + socket_to_server_.getPort() + "]");
return;
}
}
Lembrando que estava utilizando a classe do BouncyCastle para fazer a comunicação SSL/TLS:
java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Att,
Carlos
________________________________
From: eppnicbr-bounces at eng.registro.br [mailto:eppnicbr-bounces at eng.registro.br] On Behalf Of Shankar Cabus
Sent: quinta-feira, 15 de abril de 2010 14:50
To: EPP no .br - protocolo e operacao
Subject: Re: [Eppnicbr]Provisionamento de domínios usando protocolo EPP
Pois é Carlos, acho inclusive que vi esse seu tópico ontem, veja se não é esse: http://eng.registro.br/pipermail/eppnicbr/2007-June/000280.html
Consegui com a ajuda do OpenSSL converter o cliente.pem para PKCS12, contudo, não consegui converter nada pra JSK (Java StoreKey), também não sabia nem o que eu devia convertei para JSK. Então, pra testar, fiz algumas alterações no código para receber ou outro PKCS12. Rodou até um ponto que deu esse erro:
main, handling exception: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found
Mas essa classe já faz a comunicação via socket com o servidor?
Muito obrigado!
Em 15 de abril de 2010 14:39, Carlos Eduardo Fontes Silva <carlos.eduardo at cges.com.br> escreveu:
Amigo,
Eu já implementei esse protocolo em 2008 para o IG Empresas em JAVA, mas não sei onde meu projeto foi parar. A um tempo atrás uma pessoal me pediu informações sobre essa implementação e mandei algumas informações a ela.
O email é pedroh.lira at gmail.com. Pergunte para ele se conseguiu implementar o protocolo em JAVA.
Vou ver se acho o código aqui ainda e te passo. O problema maior, que me lembro, é criar o KeyStore com os arquivos client.pem e root.pem. Deve-se criar primeiro um arquivo PKCS12 utilizando esses arquivos e colocá-lo dentro do keystore.
Att,
Carlos
________________________________
From: eppnicbr-bounces at eng.registro.br [mailto:eppnicbr-bounces at eng.registro.br] On Behalf Of Shankar Cabus
Sent: quinta-feira, 15 de abril de 2010 14:32
To: EPP no .br - protocolo e operacao
Subject: Re: [Eppnicbr]Provisionamento de domínios usando protocolo EPP
Por que? Existem mais materiais em PHP ou pelo nível de dificuldade mesmo?
Em 15 de abril de 2010 14:28, Itamar Reis Peixoto <itamar at ispbrasil.com.br> escreveu:
2010/4/15 Shankar Cabus <shankar at jusbrasil.com.br>:
> Amigos,
>
> Preciso implementar um cliente de comunicação com o Registro.br e estou
> perdido. Encontrei algumas poucas coisas em Java e no geral, tudo que
> encontrei foi meio confuso.
>
> A primeira coisa é em relação aos certificados. Alguns exemplos que
> encontrei usava certificado PKCS12 e JKS, quais certificados devo usar?
>
> Encontrei algumas APIs em Java (epp-rtk-java-0.9.6 e NeuStar-RTK.0.2.6) mas
> não entendi muito bem também como usar. Esta última tem uns "Makefile" que
> também não sei como usar.
>
> O Registro.br tem alguns pré-requisitos para que o sistema seja homologado
> (http://registro.br/info/epp/pt-epp-accreditation-proc.html), esses comandos
> já estão na API?
>
> Enfim, deu pra perceber que estou "meio" perdido né. Se alguem puder me
> ajudar, seja com tutorial, dicas, exemplos... eu agradeço bastante!
>
> Abraços,
>
> Shankar Cabus
>
> _______________________________________________
> eppnicbr mailing list
> eppnicbr at eng.registro.br
> https://eng.registro.br/mailman/listinfo/eppnicbr
>
>
recomendo utilizar php.
--
------------
Itamar Reis Peixoto
_______________________________________________
eppnicbr mailing list
eppnicbr at eng.registro.br
https://eng.registro.br/mailman/listinfo/eppnicbr
_______________________________________________
eppnicbr mailing list
eppnicbr at eng.registro.br
https://eng.registro.br/mailman/listinfo/eppnicbr
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://eng.registro.br/pipermail/eppnicbr/attachments/20100415/f129b7ea/attachment.html>
More information about the eppnicbr
mailing list