-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPin
More file actions
executable file
·62 lines (47 loc) · 2.12 KB
/
Pin
File metadata and controls
executable file
·62 lines (47 loc) · 2.12 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
#!/usr/bin/env -S java --source 8
// SPDX-License-Identifier: Apache-2.0
import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;
import java.util.stream.Collectors;
public class Pin {
public static void main(String[] args) throws IOException, CertificateParsingException, NoSuchAlgorithmException {
if (args.length < 1) {
System.out.println("Usage: ./Pin <domain:[port]>");
System.exit(1);
}
HttpsURLConnection con = (HttpsURLConnection) new URL("https://" + args[0]).openConnection();
con.connect();
final Certificate[] certificates = con.getServerCertificates();
con.disconnect();
int count = 0;
for (Certificate cert : certificates) {
System.out.println("Certificate Nr. " + ++count);
if (!(cert instanceof X509Certificate)) continue;
X509Certificate xc = (X509Certificate) cert;
System.out.println("Subject: " + xc.getSubjectDN().getName());
System.out.println("Valid from " + xc.getNotBefore() + " to " + xc.getNotAfter());
if (xc.getSubjectAlternativeNames() != null) {
System.out.println("Alternate DNS: " + xc.getSubjectAlternativeNames()
.stream()
.map(l -> l.get(1))
.map(Object::toString)
.collect(Collectors.joining(", ")));
}
String padded = String.format("%64s",
new BigInteger(1, MessageDigest
.getInstance("SHA-256")
.digest(cert.getPublicKey().getEncoded()))
.toString(16))
.replace(" ", "0")
.toLowerCase();
System.out.println("Pin string: SHA256:" + padded + System.lineSeparator());
}
}
}