Desculpem estar upando este tópico, mas é que estou tendo esse mesmo problema, mas só com o serviço NfeConsultaCadastro (os outros de NFe funcionam) e para a UF do RS, e só com A3.
Código da função que efetua a consulta de cadastro:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import javax.xml.stream.XMLStreamException;
//import java.io.BufferedReader;
//import java.io.FileInputStream;
//import java.io.InputStreamReader;
//import java.util.Iterator;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.commons.httpclient.protocol.Protocol;
import br.inf.portalfiscal.www.nfe.wsdl.cadconsultacadastro2.*;
public class SendXmlConsultaCadastro {
private static final int SSL_PORT = 443;
public static String SendXmlConsCad(String strXml, int tipoCert,
String senhaDoCertificado, String urlRec, String cdUF)
throws KeyStoreException, NoSuchAlgorithmException,
CertificateException, IOException, UnrecoverableKeyException,
XMLStreamException {
String codigoDoEstado = cdUF;
URL url = new URL(urlRec);// NfeRecepcao
// String configName = "SmartCard.cfg";
String configName = "";
if (tipoCert == 1)
configName = "SmartCardSafeSign.cfg";
else if (tipoCert == 2)
configName = "SmartCardSSI.cfg";
else if (tipoCert == 3)
configName = "Token.cfg";
else
configName = "SmartCardSSISafeWeb.cfg";
// String senhaDoCertificado = "1234";
String arquivoCacertsGeradoTodosOsEstados = "NFeCacerts";
char SEP = File.separatorChar;
Provider p = new sun.security.pkcs11.SunPKCS11(System.getenv("APPDATA")
+ SEP + "JavaTotalNFe" + SEP + configName);
Security.addProvider(p);
char[] pin = senhaDoCertificado.toCharArray();
KeyStore ks = KeyStore.getInstance("pkcs11");
ks.load(null, pin);
String alias = "";
Enumeration<String> aliasesEnum = ks.aliases();
while (aliasesEnum.hasMoreElements()) {
alias = (String) aliasesEnum.nextElement();
if (ks.isKeyEntry(alias))
break;
}
X509Certificate certificate = (X509Certificate) ks
.getCertificate(alias);
PrivateKey privateKey = (PrivateKey) ks.getKey(alias,
senhaDoCertificado.toCharArray());
SocketFactoryDinamico socketFactoryDinamico = new SocketFactoryDinamico(
certificate, privateKey);
socketFactoryDinamico
.setFileCacerts(arquivoCacertsGeradoTodosOsEstados);
Protocol protocol = new Protocol("https", socketFactoryDinamico,
SSL_PORT);
Protocol.registerProtocol("https", protocol);
OMElement ome = AXIOMUtil.stringToOM(strXml);
NfeDadosMsg dadosMsg = new NfeDadosMsg();
dadosMsg.setExtraElement(ome);
NfeCabecMsg nfeCabecMsg = new NfeCabecMsg();
/**
* Código do Estado.
*/
nfeCabecMsg.setCUF(codigoDoEstado);
/**
* Versao do XML
*/
nfeCabecMsg.setVersaoDados("2.00");
NfeCabecMsgE nfeCabecMsgE = new NfeCabecMsgE();
nfeCabecMsgE.setNfeCabecMsg(nfeCabecMsg);
CadConsultaCadastro2Stub stub = new CadConsultaCadastro2Stub(
url.toString());
ConsultaCadastro2Result result = stub.consultaCadastro2(dadosMsg,
nfeCabecMsgE);
return result.getExtraElement().toString();
}
}
Código da classe SocketFactoryDinamico (é basicamente igual ao que foi postado anteriormente aqui):
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.SocketFactory;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509KeyManager;
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
public class SocketFactoryDinamico implements ProtocolSocketFactory {
private SSLContext ssl = null;
private X509Certificate certificate;
private PrivateKey privateKey;
private String fileCacerts;
public SocketFactoryDinamico(X509Certificate certificate,
PrivateKey privateKey) {
this.certificate = certificate;
this.privateKey = privateKey;
}
private SSLContext createSSLContext() {
try {
KeyManager[] keyManagers = createKeyManagers();
TrustManager[] trustManagers = createTrustManagers();
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, trustManagers, null);
return sslContext;
} catch (KeyManagementException e) {
} catch (KeyStoreException e) {
} catch (NoSuchAlgorithmException e) {
} catch (CertificateException e) {
} catch (IOException e) {
}
return null;
}
private SSLContext getSSLContext() {
if (ssl == null) {
ssl = createSSLContext();
}
return ssl;
}
public Socket createSocket(String host, int port, InetAddress localAddress,
int localPort, HttpConnectionParams params) throws IOException,
UnknownHostException, ConnectTimeoutException {
if (params == null) {
throw new IllegalArgumentException("Parameters may not be null");
}
int timeout = params.getConnectionTimeout();
SocketFactory socketfactory = getSSLContext().getSocketFactory();
if (timeout == 0) {
return socketfactory.createSocket(host, port, localAddress,
localPort);
}
Socket socket = socketfactory.createSocket();
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
SocketAddress remoteaddr = new InetSocketAddress(host, port);
socket.bind(localaddr);
try {
socket.connect(remoteaddr, timeout);
} catch (Exception e) {
throw new ConnectTimeoutException("Possível timeout de conexão", e);
}
return socket;
}
public Socket createSocket(String host, int port, InetAddress clientHost,
int clientPort) throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(host, port,
clientHost, clientPort);
}
public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(host, port);
}
public Socket createSocket(Socket socket, String host, int port,
boolean autoClose) throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(socket, host,
port, autoClose);
}
public KeyManager[] createKeyManagers() {
HSKeyManager keyManager = new HSKeyManager(certificate, privateKey);
return new KeyManager[] { keyManager };
}
public TrustManager[] createTrustManagers() throws KeyStoreException,
NoSuchAlgorithmException, CertificateException, IOException {
KeyStore trustStore = KeyStore.getInstance("JKS");
char SEP = File.separatorChar;
trustStore
.load(new FileInputStream(System.getenv("APPDATA") + SEP
+ "JavaTotalNFe" + SEP + fileCacerts),
"changeit".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
return trustManagerFactory.getTrustManagers();
}
class HSKeyManager implements X509KeyManager {
private X509Certificate certificate;
private PrivateKey privateKey;
public HSKeyManager(X509Certificate certificate, PrivateKey privateKey) {
this.certificate = certificate;
this.privateKey = privateKey;
}
public String chooseClientAlias(String[] arg0, Principal[] arg1,
Socket arg2) {
return certificate.getIssuerDN().getName();
}
public String chooseServerAlias(String arg0, Principal[] arg1,
Socket arg2) {
return null;
}
public X509Certificate[] getCertificateChain(String arg0) {
return new X509Certificate[] { certificate };
}
public String[] getClientAliases(String arg0, Principal[] arg1) {
return new String[] { certificate.getIssuerDN().getName() };
}
public PrivateKey getPrivateKey(String arg0) {
return privateKey;
}
public String[] getServerAliases(String arg0, Principal[] arg1) {
return null;
}
}
public void setFileCacerts(String fileCacerts) {
this.fileCacerts = fileCacerts;
}
}