]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Launcher.java
Merge branch 'tlsSelection'
[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.CertificateException;
9
10 import javax.net.ssl.SSLEngine;
11 import javax.net.ssl.SSLParameters;
12 import javax.net.ssl.TrustManager;
13 import javax.net.ssl.TrustManagerFactory;
14
15 import org.cacert.gigi.natives.SetUID;
16 import org.cacert.gigi.util.CipherInfo;
17 import org.eclipse.jetty.server.Connector;
18 import org.eclipse.jetty.server.Handler;
19 import org.eclipse.jetty.server.HttpConfiguration;
20 import org.eclipse.jetty.server.HttpConnectionFactory;
21 import org.eclipse.jetty.server.SecureRequestCustomizer;
22 import org.eclipse.jetty.server.Server;
23 import org.eclipse.jetty.server.ServerConnector;
24 import org.eclipse.jetty.server.SslConnectionFactory;
25 import org.eclipse.jetty.server.handler.ContextHandler;
26 import org.eclipse.jetty.server.handler.HandlerList;
27 import org.eclipse.jetty.server.handler.ResourceHandler;
28 import org.eclipse.jetty.servlet.ServletContextHandler;
29 import org.eclipse.jetty.servlet.ServletHolder;
30 import org.eclipse.jetty.util.log.Log;
31 import org.eclipse.jetty.util.ssl.SslContextFactory;
32
33 public class Launcher {
34         public static void main(String[] args) throws Exception {
35                 Server s = new Server();
36                 // === SSL HTTP Configuration ===
37                 HttpConfiguration https_config = new HttpConfiguration();
38                 https_config.setSendServerVersion(false);
39                 https_config.setSendXPoweredBy(false);
40
41                 // for client-cert auth
42                 https_config.addCustomizer(new SecureRequestCustomizer());
43
44                 ServerConnector connector = new ServerConnector(s,
45                                 new SslConnectionFactory(generateSSLContextFactory(),
46                                                 "http/1.1"), new HttpConnectionFactory(https_config));
47                 connector.setHost("127.0.0.1");
48                 connector.setPort(443);
49                 s.setConnectors(new Connector[]{connector});
50
51                 HandlerList hl = new HandlerList();
52                 hl.setHandlers(new Handler[]{generateStaticContext(),
53                                 generateGigiContext()});
54                 s.setHandler(hl);
55                 s.start();
56                 if (connector.getPort() <= 1024
57                                 && !System.getProperty("os.name").toLowerCase().contains("win")) {
58                         SetUID uid = new SetUID();
59                         if (!uid.setUid(-2, -2).getSuccess()) {
60                                 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
61                         }
62                 }
63         }
64
65         private static ServletContextHandler generateGigiContext() {
66                 ServletContextHandler servlet = new ServletContextHandler(
67                                 ServletContextHandler.SESSIONS);
68                 servlet.addServlet(new ServletHolder(new Gigi()), "/*");
69                 return servlet;
70         }
71
72         private static ContextHandler generateStaticContext() {
73                 ResourceHandler rh = new ResourceHandler();
74                 rh.setResourceBase("static");
75                 ContextHandler ch = new ContextHandler();
76                 ch.setHandler(rh);
77                 ch.setContextPath("/static");
78                 return ch;
79         }
80
81         private static SslContextFactory generateSSLContextFactory()
82                         throws NoSuchAlgorithmException, KeyStoreException, IOException,
83                         CertificateException, FileNotFoundException {
84                 TrustManagerFactory tmFactory = TrustManagerFactory.getInstance("PKIX");
85                 tmFactory.init((KeyStore) null);
86
87                 final TrustManager[] tm = tmFactory.getTrustManagers();
88
89                 SslContextFactory scf = new SslContextFactory() {
90
91                         String[] ciphers = null;
92
93                         @Override
94                         public void customize(SSLEngine sslEngine) {
95                                 super.customize(sslEngine);
96
97                                 SSLParameters ssl = sslEngine.getSSLParameters();
98                                 ssl.setUseCipherSuitesOrder(true);
99                                 if (ciphers == null) {
100                                         ciphers = CipherInfo.filter(sslEngine
101                                                         .getSupportedCipherSuites());
102                                 }
103
104                                 ssl.setCipherSuites(ciphers);
105                                 sslEngine.setSSLParameters(ssl);
106
107                         }
108
109                 };
110                 scf.setWantClientAuth(true);
111                 KeyStore ks1 = KeyStore.getInstance("pkcs12");
112                 ks1.load(new FileInputStream("config/keystore.pkcs12"),
113                                 "".toCharArray());
114                 scf.setTrustStorePath("config/cacerts.jks");
115                 scf.setTrustStorePassword("changeit");
116                 scf.setProtocol("TLS");
117                 scf.setKeyStore(ks1);
118                 return scf;
119         }
120 }