1 package org.cacert.gigi;
2 import java.io.IOException;
3 import java.security.GeneralSecurityException;
4 import java.security.KeyStore;
6 import java.util.Properties;
8 import javax.net.ssl.ExtendedSSLSession;
9 import javax.net.ssl.SNIHostName;
10 import javax.net.ssl.SNIServerName;
11 import javax.net.ssl.SSLEngine;
12 import javax.net.ssl.SSLParameters;
13 import javax.net.ssl.SSLSession;
15 import org.cacert.gigi.natives.SetUID;
16 import org.cacert.gigi.util.CipherInfo;
17 import org.cacert.gigi.util.ServerConstants;
18 import org.eclipse.jetty.http.HttpVersion;
19 import org.eclipse.jetty.server.Connector;
20 import org.eclipse.jetty.server.Handler;
21 import org.eclipse.jetty.server.HttpConfiguration;
22 import org.eclipse.jetty.server.HttpConnectionFactory;
23 import org.eclipse.jetty.server.SecureRequestCustomizer;
24 import org.eclipse.jetty.server.Server;
25 import org.eclipse.jetty.server.ServerConnector;
26 import org.eclipse.jetty.server.SessionManager;
27 import org.eclipse.jetty.server.SslConnectionFactory;
28 import org.eclipse.jetty.server.handler.ContextHandler;
29 import org.eclipse.jetty.server.handler.HandlerList;
30 import org.eclipse.jetty.server.handler.HandlerWrapper;
31 import org.eclipse.jetty.server.handler.ResourceHandler;
32 import org.eclipse.jetty.servlet.ServletContextHandler;
33 import org.eclipse.jetty.servlet.ServletHolder;
34 import org.eclipse.jetty.util.log.Log;
35 import org.eclipse.jetty.util.ssl.SslContextFactory;
37 public class Launcher {
38 public static void main(String[] args) throws Exception {
39 GigiConfig conf = GigiConfig.parse(System.in);
40 ServerConstants.init(conf.getMainProps());
42 Server s = new Server();
43 // === SSL HTTP Configuration ===
44 HttpConfiguration https_config = new HttpConfiguration();
45 https_config.setSendServerVersion(false);
46 https_config.setSendXPoweredBy(false);
48 // for client-cert auth
49 https_config.addCustomizer(new SecureRequestCustomizer());
51 ServerConnector connector = new ServerConnector(s,
52 createConnectionFactory(conf), new HttpConnectionFactory(
54 connector.setHost(conf.getMainProps().getProperty("host"));
55 connector.setPort(Integer.parseInt(conf.getMainProps().getProperty(
57 s.setConnectors(new Connector[]{connector});
59 HandlerList hl = new HandlerList();
60 hl.setHandlers(new Handler[]{generateStaticContext(),
61 generateGigiContext(conf.getMainProps())});
64 if (connector.getPort() <= 1024
65 && !System.getProperty("os.name").toLowerCase().contains("win")) {
66 SetUID uid = new SetUID();
67 if (!uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
68 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
73 private static SslConnectionFactory createConnectionFactory(GigiConfig conf)
74 throws GeneralSecurityException, IOException {
75 final SslContextFactory sslContextFactory = generateSSLContextFactory(
77 final SslContextFactory secureContextFactory = generateSSLContextFactory(
79 secureContextFactory.setNeedClientAuth(true);
80 final SslContextFactory staticContextFactory = generateSSLContextFactory(
83 secureContextFactory.start();
84 staticContextFactory.start();
85 } catch (Exception e) {
88 return new SslConnectionFactory(sslContextFactory,
89 HttpVersion.HTTP_1_1.asString()) {
91 public boolean shouldRestartSSL() {
95 public SSLEngine restartSSL(SSLSession sslSession) {
97 if (sslSession instanceof ExtendedSSLSession) {
98 ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
99 List<SNIServerName> names = es.getRequestedServerNames();
100 for (SNIServerName sniServerName : names) {
101 if (sniServerName instanceof SNIHostName) {
102 SNIHostName host = (SNIHostName) sniServerName;
103 String hostname = host.getAsciiName();
104 if (hostname.equals("www.cacert.local")) {
105 e2 = sslContextFactory.newSSLEngine();
106 } else if (hostname.equals("static.cacert.local")) {
107 e2 = staticContextFactory.newSSLEngine();
108 } else if (hostname.equals("secure.cacert.local")) {
109 e2 = secureContextFactory.newSSLEngine();
116 e2 = sslContextFactory.newSSLEngine(
117 sslSession.getPeerHost(), sslSession.getPeerPort());
119 e2.setUseClientMode(false);
125 private static ContextHandler generateGigiContext(Properties conf) {
126 final ResourceHandler rh = new ResourceHandler();
127 rh.setResourceBase("static/www");
129 HandlerWrapper hw = new PolicyRedirector();
132 ServletContextHandler servlet = new ServletContextHandler(
133 ServletContextHandler.SESSIONS);
134 servlet.setInitParameter(SessionManager.__SessionCookieProperty,
136 servlet.addServlet(new ServletHolder(new Gigi(conf)), "/*");
138 HandlerList hl = new HandlerList();
139 hl.setHandlers(new Handler[]{servlet, hw});
141 ContextHandler ch = new ContextHandler();
142 ch.setVirtualHosts(new String[]{ServerConstants.getWwwHostName(),
143 ServerConstants.getSecureHostName()});
149 private static Handler generateStaticContext() {
150 final ResourceHandler rh = new ResourceHandler();
151 rh.setResourceBase("static/static");
153 ContextHandler ch = new ContextHandler();
155 ch.setVirtualHosts(new String[]{ServerConstants.getStaticHostName()});
160 private static SslContextFactory generateSSLContextFactory(GigiConfig conf,
161 String alias) throws GeneralSecurityException, IOException {
162 SslContextFactory scf = new SslContextFactory() {
164 String[] ciphers = null;
167 public void customize(SSLEngine sslEngine) {
168 super.customize(sslEngine);
170 SSLParameters ssl = sslEngine.getSSLParameters();
171 ssl.setUseCipherSuitesOrder(true);
172 if (ciphers == null) {
173 ciphers = CipherInfo.filter(sslEngine
174 .getSupportedCipherSuites());
177 ssl.setCipherSuites(ciphers);
178 sslEngine.setSSLParameters(ssl);
183 scf.setRenegotiationAllowed(false);
185 scf.setProtocol("TLS");
186 scf.setTrustStore(conf.getTrustStore());
187 KeyStore privateStore = conf.getPrivateStore();
188 scf.setKeyStorePassword(conf.getPrivateStorePw());
189 scf.setKeyStore(privateStore);
190 scf.setCertAlias(alias);