-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNFeBuildAllCacerts.java
More file actions
169 lines (149 loc) · 6.6 KB
/
Copy pathNFeBuildAllCacerts.java
File metadata and controls
169 lines (149 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Segue abaixo código que gera o arquivo Cacerts (KeyStore) para todos os estados em um único arquivo.
* Utilizando esse único arquivo, gerado pela Classe abaixo, é possível consumir todos os Webservices do Projeto NF-e:
*/
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.MessageDigest;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
public class NFeBuildAllCacerts {
private static final String JSSECACERTS = "NFeCacerts";
private static final int TIMEOUT_WS = 30;
public static void main(String[] args) {
try {
char[] passphrase = "changeit".toCharArray();
File file = new File(JSSECACERTS);
if (file.isFile() == false) {
char SEP = File.separatorChar;
File dir = new File(System.getProperty("java.home") + SEP + "lib" + SEP + "security");
file = new File(dir, JSSECACERTS);
if (file.isFile() == false) {
file = new File(dir, "cacerts");
}
}
info("| Loading KeyStore " + file + "...");
InputStream in = new FileInputStream(file);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(in, passphrase);
in.close();
/**
* AM - 2.00: homnfe.sefaz.am.gov.br
* BA - 2.00: hnfe.sefaz.ba.gov.br
* CE - 2.00: nfeh.sefaz.ce.gov.br
* GO - 2.00: homolog.sefaz.go.gov.br
* MG - 2.00: hnfe.fazenda.mg.gov.br
* MS - 2.00: homologacao.nfe.ms.gov.br
* MT - 2.00: homologacao.sefaz.mt.gov.br
* PE - 2.00: nfehomolog.sefaz.pe.gov.br
* PR - 2.00: homologacao.nfe2.fazenda.pr.gov.br
* RS - 2.00: homologacao.nfe.sefaz.rs.gov.br
* SP - 2.00: homologacao.nfe.fazenda.sp.gov.br
* SCAN - 2.00: hom.nfe.fazenda.gov.br
* SVAN - 2.00: hom.sefazvirtual.fazenda.gov.br
* SVRS - 2.00: homologacao.nfe.sefazvirtual.rs.gov.br
*/
get("homnfe.sefaz.am.gov.br", 443, ks);
get("hnfe.sefaz.ba.gov.br", 443, ks);
get("nfeh.sefaz.ce.gov.br", 443, ks);
get("homolog.sefaz.go.gov.br", 443, ks);
get("hnfe.fazenda.mg.gov.br", 443, ks);
get("homologacao.nfe.ms.gov.br", 443, ks);
get("homologacao.sefaz.mt.gov.br", 443, ks);
get("nfehomolog.sefaz.pe.gov.br", 443, ks);
get("homologacao.nfe2.fazenda.pr.gov.br", 443, ks);
get("homologacao.nfe.sefaz.rs.gov.br", 443, ks);
get("homologacao.nfe.fazenda.sp.gov.br", 443, ks);
get("hom.nfe.fazenda.gov.br", 443, ks);
get("hom.sefazvirtual.fazenda.gov.br", 443, ks);
get("homologacao.nfe.sefazvirtual.rs.gov.br", 443, ks);
File cafile = new File(JSSECACERTS);
OutputStream out = new FileOutputStream(cafile);
ks.store(out, passphrase);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void get(String host, int port, KeyStore ks) throws Exception {
SSLContext context = SSLContext.getInstance("TLS");
TrustManagerFactory tmf = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0];
SavingTrustManager tm = new SavingTrustManager(defaultTrustManager);
context.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory factory = context.getSocketFactory();
info("| Opening connection to " + host + ":" + port + "...");
SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
socket.setSoTimeout(TIMEOUT_WS * 1000);
try {
info("| Starting SSL handshake...");
socket.startHandshake();
socket.close();
info("| No errors, certificate is already trusted");
} catch (SSLHandshakeException e) {
/**
* PKIX path building failed:
* sun.security.provider.certpath.SunCertPathBuilderException:
* unable to find valid certification path to requested target
* Não tratado, pois sempre ocorre essa exceção quando o cacerts
* nao esta gerado.
*/
} catch (SSLException e) {
error("| " + e.toString());
}
X509Certificate[] chain = tm.chain;
if (chain == null) {
info("| Could not obtain server certificate chain");
}
info("| Server sent " + chain.length + " certificate(s):");
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
MessageDigest md5 = MessageDigest.getInstance("MD5");
for (int i = 0; i < chain.length; i++) {
X509Certificate cert = chain[i];
sha1.update(cert.getEncoded());
md5.update(cert.getEncoded());
String alias = host + "-" + (i);
ks.setCertificateEntry(alias, cert);
info("| Added certificate to keystore '" + JSSECACERTS + "' using alias '" + alias + "'");
}
}
private static class SavingTrustManager implements X509TrustManager {
private final X509TrustManager tm;
private X509Certificate[] chain;
SavingTrustManager(X509TrustManager tm) {
this.tm = tm;
}
public X509Certificate[] getAcceptedIssuers() {
throw new UnsupportedOperationException();
}
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
throw new UnsupportedOperationException();
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
this.chain = chain;
tm.checkServerTrusted(chain, authType);
}
}
private static void info(String log) {
System.out.println("INFO: " + log);
}
private static void error(String log) {
System.out.println("ERROR: " + log);
}
}