]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Launcher.java
ADD: expires headers.
[gigi.git] / src / org / cacert / gigi / Launcher.java
1 package org.cacert.gigi;
2
3 import java.io.IOException;
4 import java.security.GeneralSecurityException;
5 import java.security.Key;
6 import java.security.KeyStore;
7 import java.security.KeyStoreException;
8 import java.security.NoSuchAlgorithmException;
9 import java.security.UnrecoverableKeyException;
10 import java.security.cert.Certificate;
11 import java.util.List;
12 import java.util.Locale;
13 import java.util.Properties;
14 import java.util.TimeZone;
15
16 import javax.net.ssl.ExtendedSSLSession;
17 import javax.net.ssl.SNIHostName;
18 import javax.net.ssl.SNIServerName;
19 import javax.net.ssl.SSLEngine;
20 import javax.net.ssl.SSLParameters;
21 import javax.net.ssl.SSLSession;
22
23 import org.cacert.gigi.api.GigiAPI;
24 import org.cacert.gigi.email.EmailProvider;
25 import org.cacert.gigi.natives.SetUID;
26 import org.cacert.gigi.util.CipherInfo;
27 import org.cacert.gigi.util.ServerConstants;
28 import org.eclipse.jetty.http.HttpHeader;
29 import org.eclipse.jetty.http.HttpVersion;
30 import org.eclipse.jetty.server.Connector;
31 import org.eclipse.jetty.server.Handler;
32 import org.eclipse.jetty.server.HttpConfiguration;
33 import org.eclipse.jetty.server.HttpConnectionFactory;
34 import org.eclipse.jetty.server.SecureRequestCustomizer;
35 import org.eclipse.jetty.server.Server;
36 import org.eclipse.jetty.server.ServerConnector;
37 import org.eclipse.jetty.server.SessionManager;
38 import org.eclipse.jetty.server.SslConnectionFactory;
39 import org.eclipse.jetty.server.handler.ContextHandler;
40 import org.eclipse.jetty.server.handler.HandlerList;
41 import org.eclipse.jetty.server.handler.HandlerWrapper;
42 import org.eclipse.jetty.server.handler.ResourceHandler;
43 import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
44 import org.eclipse.jetty.servlet.ServletContextHandler;
45 import org.eclipse.jetty.servlet.ServletHolder;
46 import org.eclipse.jetty.util.log.Log;
47 import org.eclipse.jetty.util.ssl.SslContextFactory;
48
49 public class Launcher {
50
51     public static void main(String[] args) throws Exception {
52         System.setProperty("jdk.tls.ephemeralDHKeySize", "4096");
53         boot();
54     }
55
56     public static void boot() throws Exception {
57         Locale.setDefault(Locale.ENGLISH);
58         TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
59
60         GigiConfig conf = GigiConfig.parse(System.in);
61         ServerConstants.init(conf.getMainProps());
62         initEmails(conf);
63
64         Server s = new Server();
65         HttpConfiguration httpsConfig = createHttpConfiguration();
66
67         // for client-cert auth
68         httpsConfig.addCustomizer(new SecureRequestCustomizer());
69
70         HttpConfiguration httpConfig = createHttpConfiguration();
71
72         s.setConnectors(new Connector[] {
73                 createConnector(conf, s, httpsConfig, true), createConnector(conf, s, httpConfig, false)
74         });
75
76         HandlerList hl = new HandlerList();
77         hl.setHandlers(new Handler[] {
78                 generateStaticContext(), generateGigiContexts(conf.getMainProps(), conf.getTrustStore()), generateAPIContext()
79         });
80         s.setHandler(hl);
81         s.start();
82         if ((ServerConstants.getSecurePort() <= 1024 || ServerConstants.getPort() <= 1024) && !System.getProperty("os.name").toLowerCase().contains("win")) {
83             SetUID uid = new SetUID();
84             if ( !uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
85                 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
86             }
87         }
88         if (conf.getMainProps().containsKey("testrunner")) {
89             DevelLauncher.addDevelPage();
90         }
91     }
92
93     private static ServerConnector createConnector(GigiConfig conf, Server s, HttpConfiguration httpConfig, boolean doHttps) throws GeneralSecurityException, IOException {
94         ServerConnector connector;
95         if (doHttps) {
96             connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(httpConfig));
97         } else {
98             connector = new ServerConnector(s, new HttpConnectionFactory(httpConfig));
99         }
100         connector.setHost(conf.getMainProps().getProperty("host"));
101         if (doHttps) {
102             connector.setPort(ServerConstants.getSecurePort());
103         } else {
104             connector.setPort(ServerConstants.getPort());
105         }
106         connector.setAcceptQueueSize(100);
107         return connector;
108     }
109
110     private static HttpConfiguration createHttpConfiguration() {
111         // SSL HTTP Configuration
112         HttpConfiguration httpsConfig = new HttpConfiguration();
113         httpsConfig.setSendServerVersion(false);
114         httpsConfig.setSendXPoweredBy(false);
115         return httpsConfig;
116     }
117
118     private static void initEmails(GigiConfig conf) throws GeneralSecurityException, IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
119         KeyStore privateStore = conf.getPrivateStore();
120         Certificate mail = privateStore.getCertificate("mail");
121         Key k = privateStore.getKey("mail", conf.getPrivateStorePw().toCharArray());
122         EmailProvider.initSystem(conf.getMainProps(), mail, k);
123     }
124
125     private static SslConnectionFactory createConnectionFactory(GigiConfig conf) throws GeneralSecurityException, IOException {
126         final SslContextFactory sslContextFactory = generateSSLContextFactory(conf, "www");
127         final SslContextFactory secureContextFactory = generateSSLContextFactory(conf, "secure");
128         secureContextFactory.setWantClientAuth(true);
129         secureContextFactory.setNeedClientAuth(false);
130         final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
131         final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
132         try {
133             secureContextFactory.start();
134             staticContextFactory.start();
135             apiContextFactory.start();
136         } catch (Exception e) {
137             e.printStackTrace();
138         }
139         return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
140
141             @Override
142             public boolean shouldRestartSSL() {
143                 return true;
144             }
145
146             @Override
147             public SSLEngine restartSSL(SSLSession sslSession) {
148                 SSLEngine e2 = null;
149                 if (sslSession instanceof ExtendedSSLSession) {
150                     ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
151                     List<SNIServerName> names = es.getRequestedServerNames();
152                     for (SNIServerName sniServerName : names) {
153                         if (sniServerName instanceof SNIHostName) {
154                             SNIHostName host = (SNIHostName) sniServerName;
155                             String hostname = host.getAsciiName();
156                             if (hostname.equals(ServerConstants.getWwwHostName())) {
157                                 e2 = sslContextFactory.newSSLEngine();
158                             } else if (hostname.equals(ServerConstants.getStaticHostName())) {
159                                 e2 = staticContextFactory.newSSLEngine();
160                             } else if (hostname.equals(ServerConstants.getSecureHostName())) {
161                                 e2 = secureContextFactory.newSSLEngine();
162                             } else if (hostname.equals(ServerConstants.getApiHostName())) {
163                                 e2 = apiContextFactory.newSSLEngine();
164                             }
165                             break;
166                         }
167                     }
168                 }
169                 if (e2 == null) {
170                     e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
171                 }
172                 e2.setUseClientMode(false);
173                 return e2;
174             }
175         };
176     }
177
178     private static Handler generateGigiContexts(Properties conf, KeyStore trust) {
179         ServletHolder webAppServlet = new ServletHolder(new Gigi(conf, trust));
180
181         ContextHandler ch = generateGigiServletContext(webAppServlet);
182         ch.setVirtualHosts(new String[] {
183             ServerConstants.getWwwHostName()
184         });
185         ContextHandler chSecure = generateGigiServletContext(webAppServlet);
186         chSecure.setVirtualHosts(new String[] {
187             ServerConstants.getSecureHostName()
188         });
189
190         HandlerList hl = new HandlerList();
191         hl.setHandlers(new Handler[] {
192                 ch, chSecure
193         });
194         return hl;
195     }
196
197     private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
198         final ResourceHandler rh = generateResourceHandler();
199         rh.setResourceBase("static/www");
200
201         HandlerWrapper hw = new PolicyRedirector();
202         hw.setHandler(rh);
203
204         ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
205         servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
206         servlet.addServlet(webAppServlet, "/*");
207         ErrorPageErrorHandler epeh = new ErrorPageErrorHandler();
208         epeh.addErrorPage(404, "/error");
209         epeh.addErrorPage(403, "/denied");
210         servlet.setErrorHandler(epeh);
211
212         HandlerList hl = new HandlerList();
213         hl.setHandlers(new Handler[] {
214                 hw, servlet
215         });
216
217         ContextHandler ch = new ContextHandler();
218         ch.setHandler(hl);
219         return ch;
220     }
221
222     private static Handler generateStaticContext() {
223         final ResourceHandler rh = generateResourceHandler();
224         rh.setResourceBase("static/static");
225
226         ContextHandler ch = new ContextHandler();
227         ch.setHandler(rh);
228         ch.setVirtualHosts(new String[] {
229             ServerConstants.getStaticHostName()
230         });
231
232         return ch;
233     }
234
235     private static ResourceHandler generateResourceHandler() {
236         ResourceHandler rh = new ResourceHandler() {
237
238             @Override
239             public void handle(String target, org.eclipse.jetty.server.Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException {
240                 response.setDateHeader(HttpHeader.EXPIRES.asString(), System.currentTimeMillis() + 1000L * 60 * 60 * 24 * 7);
241                 super.handle(target, baseRequest, request, response);
242             }
243         };
244         rh.setEtags(true);
245         return rh;
246     }
247
248     private static Handler generateAPIContext() {
249         ServletContextHandler sch = new ServletContextHandler();
250
251         sch.addVirtualHosts(new String[] {
252             ServerConstants.getApiHostName()
253         });
254         sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
255         return sch;
256     }
257
258     private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
259         SslContextFactory scf = new SslContextFactory() {
260
261             String[] ciphers = null;
262
263             @Override
264             public void customize(SSLEngine sslEngine) {
265                 super.customize(sslEngine);
266
267                 SSLParameters ssl = sslEngine.getSSLParameters();
268                 ssl.setUseCipherSuitesOrder(true);
269                 if (ciphers == null) {
270                     ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
271                 }
272
273                 ssl.setCipherSuites(ciphers);
274                 sslEngine.setSSLParameters(ssl);
275
276             }
277
278         };
279         scf.setRenegotiationAllowed(false);
280
281         scf.setProtocol("TLS");
282         scf.setIncludeProtocols("TLSv1", "TLSv1.1", "TLSv1.2");
283         scf.setTrustStore(conf.getTrustStore());
284         KeyStore privateStore = conf.getPrivateStore();
285         scf.setKeyStorePassword(conf.getPrivateStorePw());
286         scf.setKeyStore(privateStore);
287         scf.setCertAlias(alias);
288         return scf;
289     }
290 }