1 package org.cacert.gigi;
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;
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;
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;
48 public class Launcher {
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");
55 GigiConfig conf = GigiConfig.parse(System.in);
56 ServerConstants.init(conf.getMainProps());
59 Server s = new Server();
60 HttpConfiguration httpsConfig = createHttpConfiguration();
62 // for client-cert auth
63 httpsConfig.addCustomizer(new SecureRequestCustomizer());
65 HttpConfiguration httpConfig = createHttpConfiguration();
67 s.setConnectors(new Connector[] {
68 createConnector(conf, s, httpsConfig, true), createConnector(conf, s, httpConfig, false)
71 HandlerList hl = new HandlerList();
72 hl.setHandlers(new Handler[] {
73 generateStaticContext(), generateGigiContexts(conf.getMainProps(), conf.getTrustStore()), generateAPIContext()
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!");
85 private static ServerConnector createConnector(GigiConfig conf, Server s, HttpConfiguration httpConfig, boolean doHttps) throws GeneralSecurityException, IOException {
86 ServerConnector connector;
88 connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(httpConfig));
90 connector = new ServerConnector(s, new HttpConnectionFactory(httpConfig));
92 connector.setHost(conf.getMainProps().getProperty("host"));
94 connector.setPort(ServerConstants.getSecurePort());
96 connector.setPort(ServerConstants.getPort());
98 connector.setAcceptQueueSize(100);
102 private static HttpConfiguration createHttpConfiguration() {
103 // SSL HTTP Configuration
104 HttpConfiguration httpsConfig = new HttpConfiguration();
105 httpsConfig.setSendServerVersion(false);
106 httpsConfig.setSendXPoweredBy(false);
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);
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");
125 secureContextFactory.start();
126 staticContextFactory.start();
127 apiContextFactory.start();
128 } catch (Exception e) {
131 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
134 public boolean shouldRestartSSL() {
139 public SSLEngine restartSSL(SSLSession sslSession) {
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();
162 e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
164 e2.setUseClientMode(false);
170 private static Handler generateGigiContexts(Properties conf, KeyStore trust) {
171 ServletHolder webAppServlet = new ServletHolder(new Gigi(conf, trust));
173 ContextHandler ch = generateGigiServletContext(webAppServlet);
174 ch.setVirtualHosts(new String[] {
175 ServerConstants.getWwwHostName()
177 ContextHandler chSecure = generateGigiServletContext(webAppServlet);
178 chSecure.setVirtualHosts(new String[] {
179 ServerConstants.getSecureHostName()
182 HandlerList hl = new HandlerList();
183 hl.setHandlers(new Handler[] {
189 private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
190 final ResourceHandler rh = new ResourceHandler();
191 rh.setResourceBase("static/www");
193 HandlerWrapper hw = new PolicyRedirector();
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);
203 HandlerList hl = new HandlerList();
204 hl.setHandlers(new Handler[] {
208 ContextHandler ch = new ContextHandler();
213 private static Handler generateStaticContext() {
214 final ResourceHandler rh = new ResourceHandler();
215 rh.setResourceBase("static/static");
217 ContextHandler ch = new ContextHandler();
219 ch.setVirtualHosts(new String[] {
220 ServerConstants.getStaticHostName()
226 private static Handler generateAPIContext() {
227 ServletContextHandler sch = new ServletContextHandler();
229 sch.addVirtualHosts(new String[] {
230 ServerConstants.getApiHostName()
232 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
236 private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
237 SslContextFactory scf = new SslContextFactory() {
239 String[] ciphers = null;
242 public void customize(SSLEngine sslEngine) {
243 super.customize(sslEngine);
245 SSLParameters ssl = sslEngine.getSSLParameters();
246 ssl.setUseCipherSuitesOrder(true);
247 if (ciphers == null) {
248 ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
251 ssl.setCipherSuites(ciphers);
252 sslEngine.setSSLParameters(ssl);
257 scf.setRenegotiationAllowed(false);
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);