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 System.setProperty("jdk.tls.ephemeralDHKeySize", "4096");
55 public static void boot() throws Exception {
56 Locale.setDefault(Locale.ENGLISH);
57 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
59 GigiConfig conf = GigiConfig.parse(System.in);
60 ServerConstants.init(conf.getMainProps());
63 Server s = new Server();
64 HttpConfiguration httpsConfig = createHttpConfiguration();
66 // for client-cert auth
67 httpsConfig.addCustomizer(new SecureRequestCustomizer());
69 HttpConfiguration httpConfig = createHttpConfiguration();
71 s.setConnectors(new Connector[] {
72 createConnector(conf, s, httpsConfig, true), createConnector(conf, s, httpConfig, false)
75 HandlerList hl = new HandlerList();
76 hl.setHandlers(new Handler[] {
77 generateStaticContext(), generateGigiContexts(conf.getMainProps(), conf.getTrustStore()), generateAPIContext()
81 if ((ServerConstants.getSecurePort() <= 1024 || ServerConstants.getPort() <= 1024) && !System.getProperty("os.name").toLowerCase().contains("win")) {
82 SetUID uid = new SetUID();
83 if ( !uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
84 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
89 private static ServerConnector createConnector(GigiConfig conf, Server s, HttpConfiguration httpConfig, boolean doHttps) throws GeneralSecurityException, IOException {
90 ServerConnector connector;
92 connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(httpConfig));
94 connector = new ServerConnector(s, new HttpConnectionFactory(httpConfig));
96 connector.setHost(conf.getMainProps().getProperty("host"));
98 connector.setPort(ServerConstants.getSecurePort());
100 connector.setPort(ServerConstants.getPort());
102 connector.setAcceptQueueSize(100);
106 private static HttpConfiguration createHttpConfiguration() {
107 // SSL HTTP Configuration
108 HttpConfiguration httpsConfig = new HttpConfiguration();
109 httpsConfig.setSendServerVersion(false);
110 httpsConfig.setSendXPoweredBy(false);
114 private static void initEmails(GigiConfig conf) throws GeneralSecurityException, IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
115 KeyStore privateStore = conf.getPrivateStore();
116 Certificate mail = privateStore.getCertificate("mail");
117 Key k = privateStore.getKey("mail", conf.getPrivateStorePw().toCharArray());
118 EmailProvider.initSystem(conf.getMainProps(), mail, k);
121 private static SslConnectionFactory createConnectionFactory(GigiConfig conf) throws GeneralSecurityException, IOException {
122 final SslContextFactory sslContextFactory = generateSSLContextFactory(conf, "www");
123 final SslContextFactory secureContextFactory = generateSSLContextFactory(conf, "secure");
124 secureContextFactory.setWantClientAuth(true);
125 secureContextFactory.setNeedClientAuth(false);
126 final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
127 final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
129 secureContextFactory.start();
130 staticContextFactory.start();
131 apiContextFactory.start();
132 } catch (Exception e) {
135 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
138 public boolean shouldRestartSSL() {
143 public SSLEngine restartSSL(SSLSession sslSession) {
145 if (sslSession instanceof ExtendedSSLSession) {
146 ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
147 List<SNIServerName> names = es.getRequestedServerNames();
148 for (SNIServerName sniServerName : names) {
149 if (sniServerName instanceof SNIHostName) {
150 SNIHostName host = (SNIHostName) sniServerName;
151 String hostname = host.getAsciiName();
152 if (hostname.equals(ServerConstants.getWwwHostName())) {
153 e2 = sslContextFactory.newSSLEngine();
154 } else if (hostname.equals(ServerConstants.getStaticHostName())) {
155 e2 = staticContextFactory.newSSLEngine();
156 } else if (hostname.equals(ServerConstants.getSecureHostName())) {
157 e2 = secureContextFactory.newSSLEngine();
158 } else if (hostname.equals(ServerConstants.getApiHostName())) {
159 e2 = apiContextFactory.newSSLEngine();
166 e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
168 e2.setUseClientMode(false);
174 private static Handler generateGigiContexts(Properties conf, KeyStore trust) {
175 ServletHolder webAppServlet = new ServletHolder(new Gigi(conf, trust));
177 ContextHandler ch = generateGigiServletContext(webAppServlet);
178 ch.setVirtualHosts(new String[] {
179 ServerConstants.getWwwHostName()
181 ContextHandler chSecure = generateGigiServletContext(webAppServlet);
182 chSecure.setVirtualHosts(new String[] {
183 ServerConstants.getSecureHostName()
186 HandlerList hl = new HandlerList();
187 hl.setHandlers(new Handler[] {
193 private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
194 final ResourceHandler rh = new ResourceHandler();
195 rh.setResourceBase("static/www");
197 HandlerWrapper hw = new PolicyRedirector();
200 ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
201 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
202 servlet.addServlet(webAppServlet, "/*");
203 ErrorPageErrorHandler epeh = new ErrorPageErrorHandler();
204 epeh.addErrorPage(404, "/error");
205 servlet.setErrorHandler(epeh);
207 HandlerList hl = new HandlerList();
208 hl.setHandlers(new Handler[] {
212 ContextHandler ch = new ContextHandler();
217 private static Handler generateStaticContext() {
218 final ResourceHandler rh = new ResourceHandler();
219 rh.setResourceBase("static/static");
221 ContextHandler ch = new ContextHandler();
223 ch.setVirtualHosts(new String[] {
224 ServerConstants.getStaticHostName()
230 private static Handler generateAPIContext() {
231 ServletContextHandler sch = new ServletContextHandler();
233 sch.addVirtualHosts(new String[] {
234 ServerConstants.getApiHostName()
236 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
240 private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
241 SslContextFactory scf = new SslContextFactory() {
243 String[] ciphers = null;
246 public void customize(SSLEngine sslEngine) {
247 super.customize(sslEngine);
249 SSLParameters ssl = sslEngine.getSSLParameters();
250 ssl.setUseCipherSuitesOrder(true);
251 if (ciphers == null) {
252 ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
255 ssl.setCipherSuites(ciphers);
256 sslEngine.setSSLParameters(ssl);
261 scf.setRenegotiationAllowed(false);
263 scf.setProtocol("TLS");
264 scf.setTrustStore(conf.getTrustStore());
265 KeyStore privateStore = conf.getPrivateStore();
266 scf.setKeyStorePassword(conf.getPrivateStorePw());
267 scf.setKeyStore(privateStore);
268 scf.setCertAlias(alias);