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