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
|
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2017 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
import java.net.InetAddress;
import com.netscape.certsrv.ca.CAClient;
import com.netscape.certsrv.cert.CertClient;
import com.netscape.certsrv.cert.CertData;
import com.netscape.certsrv.cert.CertDataInfo;
import com.netscape.certsrv.cert.CertDataInfos;
import com.netscape.certsrv.cert.CertSearchRequest;
import com.netscape.certsrv.client.ClientConfig;
import com.netscape.certsrv.client.PKIClient;
import com.netscape.certsrv.dbs.certdb.CertId;
/**
* To compile the program:
* $ javac -cp "../../lib/*" CACertClientExample.java
*
* To run the program:
* $ java -cp "../../lib/*:." CACertClientExample
*/
public class CACertClientExample {
public static void main(String args[]) throws Exception {
String protocol = "http";
String hostname = InetAddress.getLocalHost().getHostName();
int port = 8080;
ClientConfig config = new ClientConfig();
config.setServerURL(protocol, hostname, port);
PKIClient client = new PKIClient(config);
CAClient caClient = new CAClient(client);
CertClient certClient = new CertClient(caClient);
CertDataInfos infos = certClient.listCerts(null, null, null, null, null);
System.out.println("Total: " + infos.getTotal());
System.out.println();
System.out.println("Certificates:");
for (CertDataInfo info : infos.getEntries()) {
System.out.println("- Serial: " + info.getID());
System.out.println(" Subject: " + info.getSubjectDN());
}
System.out.println();
System.out.println("CA Certificate:");
CertSearchRequest request = new CertSearchRequest();
request.setCommonName("commonName");
infos = certClient.findCerts(request, null, null);
CertId id = infos.getEntries().iterator().next().getID();
CertData data = certClient.getCert(id);
System.out.println(data.getEncoded());
}
}
|