1 package org.cacert.gigi;
2 import java.io.FileInputStream;
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.security.KeyStore;
6 import java.security.KeyStoreException;
7 import java.security.NoSuchAlgorithmException;
8 import java.security.cert.CRL;
9 import java.security.cert.CertificateException;
10 import java.util.Collection;
12 import javax.net.ssl.TrustManager;
13 import javax.net.ssl.TrustManagerFactory;
15 import org.cacert.gigi.natives.SetUID;
16 import org.eclipse.jetty.server.Connector;
17 import org.eclipse.jetty.server.Handler;
18 import org.eclipse.jetty.server.HttpConfiguration;
19 import org.eclipse.jetty.server.HttpConnectionFactory;
20 import org.eclipse.jetty.server.SecureRequestCustomizer;
21 import org.eclipse.jetty.server.Server;
22 import org.eclipse.jetty.server.ServerConnector;
23 import org.eclipse.jetty.server.SslConnectionFactory;
24 import org.eclipse.jetty.server.handler.ContextHandler;
25 import org.eclipse.jetty.server.handler.HandlerList;
26 import org.eclipse.jetty.server.handler.ResourceHandler;
27 import org.eclipse.jetty.servlet.ServletContextHandler;
28 import org.eclipse.jetty.servlet.ServletHolder;
29 import org.eclipse.jetty.util.log.Log;
30 import org.eclipse.jetty.util.ssl.SslContextFactory;
32 public class Launcher {
33 public static void main(String[] args) throws Exception {
34 Server s = new Server();
35 // === SSL HTTP Configuration ===
36 HttpConfiguration https_config = new HttpConfiguration();
37 https_config.setSendServerVersion(false);
38 https_config.setSendXPoweredBy(false);
40 // for client-cert auth
41 https_config.addCustomizer(new SecureRequestCustomizer());
43 ServerConnector connector = new ServerConnector(s,
44 new SslConnectionFactory(generateSSLContextFactory(),
45 "http/1.1"), new HttpConnectionFactory(https_config));
46 connector.setHost("127.0.0.1");
47 connector.setPort(443);
48 s.setConnectors(new Connector[]{connector});
50 HandlerList hl = new HandlerList();
51 hl.setHandlers(new Handler[]{generateStaticContext(),
52 generateGigiContext()});
55 if (connector.getPort() <= 1024
56 && !System.getProperty("os.name").toLowerCase().contains("win")) {
57 SetUID uid = new SetUID();
58 if (!uid.setUid(-2, -2).getSuccess()) {
59 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
64 private static ServletContextHandler generateGigiContext() {
65 ServletContextHandler servlet = new ServletContextHandler(
66 ServletContextHandler.SESSIONS);
67 servlet.addServlet(new ServletHolder(new Gigi()), "/*");
71 private static ContextHandler generateStaticContext() {
72 ResourceHandler rh = new ResourceHandler();
73 rh.setResourceBase("static");
74 ContextHandler ch = new ContextHandler();
76 ch.setContextPath("/static");
80 private static SslContextFactory generateSSLContextFactory()
81 throws NoSuchAlgorithmException, KeyStoreException, IOException,
82 CertificateException, FileNotFoundException {
83 TrustManagerFactory tmFactory = TrustManagerFactory.getInstance("PKIX");
84 tmFactory.init((KeyStore) null);
86 final TrustManager[] tm = tmFactory.getTrustManagers();
88 SslContextFactory scf = new SslContextFactory() {
90 protected TrustManager[] getTrustManagers(KeyStore trustStore,
91 Collection<? extends CRL> crls) throws Exception {
95 scf.setWantClientAuth(true);
96 KeyStore ks1 = KeyStore.getInstance("pkcs12");
97 ks1.load(new FileInputStream("config/keystore.pkcs12"),
100 scf.setProtocol("TLSv1");