Merge branch 'nativeuid'
[gigi.git] / src / org / cacert / gigi / Launcher.java
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;
11
12 import javax.net.ssl.TrustManager;
13 import javax.net.ssl.TrustManagerFactory;
14
15 import org.cacert.gigi.natives.SetUID;
16 import org.eclipse.jetty.server.Connector;
17 import org.eclipse.jetty.server.HttpConfiguration;
18 import org.eclipse.jetty.server.HttpConnectionFactory;
19 import org.eclipse.jetty.server.SecureRequestCustomizer;
20 import org.eclipse.jetty.server.Server;
21 import org.eclipse.jetty.server.ServerConnector;
22 import org.eclipse.jetty.server.SslConnectionFactory;
23 import org.eclipse.jetty.servlet.ServletContextHandler;
24 import org.eclipse.jetty.servlet.ServletHolder;
25 import org.eclipse.jetty.util.log.Log;
26 import org.eclipse.jetty.util.ssl.SslContextFactory;
27
28 public class Launcher {
29         public static void main(String[] args) throws Exception {
30                 Server s = new Server();
31                 // === SSL HTTP Configuration ===
32                 HttpConfiguration https_config = new HttpConfiguration();
33                 // for client-cert auth
34                 https_config.addCustomizer(new SecureRequestCustomizer());
35
36                 ServerConnector connector = new ServerConnector(s,
37                                 new SslConnectionFactory(generateSSLContextFactory(),
38                                                 "http/1.1"), new HttpConnectionFactory(https_config));
39                 connector.setHost("127.0.0.1");
40                 connector.setPort(443);
41                 s.setConnectors(new Connector[]{connector});
42                 ServletContextHandler sh = new ServletContextHandler();
43                 s.setHandler(sh);
44                 sh.addServlet(new ServletHolder(new TestServlet()), "/");
45                 s.start();
46                 if (connector.getPort() <= 1024
47                                 && !System.getProperty("os.name").toLowerCase().contains("win")) {
48                         SetUID uid = new SetUID();
49                         if (!uid.setUid(-2, -2).getSuccess()) {
50                                 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
51                         }
52                 }
53         }
54
55         private static SslContextFactory generateSSLContextFactory()
56                         throws NoSuchAlgorithmException, KeyStoreException, IOException,
57                         CertificateException, FileNotFoundException {
58                 TrustManagerFactory tmFactory = TrustManagerFactory.getInstance("PKIX");
59                 tmFactory.init((KeyStore) null);
60
61                 final TrustManager[] tm = tmFactory.getTrustManagers();
62
63                 SslContextFactory scf = new SslContextFactory() {
64                         @Override
65                         protected TrustManager[] getTrustManagers(KeyStore trustStore,
66                                         Collection<? extends CRL> crls) throws Exception {
67                                 return tm;
68                         }
69                 };
70                 scf.setWantClientAuth(true);
71                 KeyStore ks1 = KeyStore.getInstance("pkcs12");
72                 ks1.load(new FileInputStream("config/keystore.pkcs12"),
73                                 "".toCharArray());
74                 scf.setKeyStore(ks1);
75                 scf.setProtocol("TLSv1");
76                 return scf;
77         }
78 }