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