1 package org.cacert.gigi;
3 import java.io.IOException;
4 import java.security.GeneralSecurityException;
5 import java.security.KeyStore;
7 import java.util.Properties;
9 import javax.net.ssl.ExtendedSSLSession;
10 import javax.net.ssl.SNIHostName;
11 import javax.net.ssl.SNIServerName;
12 import javax.net.ssl.SSLEngine;
13 import javax.net.ssl.SSLParameters;
14 import javax.net.ssl.SSLSession;
16 import org.cacert.gigi.api.GigiAPI;
17 import org.cacert.gigi.natives.SetUID;
18 import org.cacert.gigi.util.CipherInfo;
19 import org.cacert.gigi.util.ServerConstants;
20 import org.eclipse.jetty.http.HttpVersion;
21 import org.eclipse.jetty.server.Connector;
22 import org.eclipse.jetty.server.Handler;
23 import org.eclipse.jetty.server.HttpConfiguration;
24 import org.eclipse.jetty.server.HttpConnectionFactory;
25 import org.eclipse.jetty.server.SecureRequestCustomizer;
26 import org.eclipse.jetty.server.Server;
27 import org.eclipse.jetty.server.ServerConnector;
28 import org.eclipse.jetty.server.SessionManager;
29 import org.eclipse.jetty.server.SslConnectionFactory;
30 import org.eclipse.jetty.server.handler.ContextHandler;
31 import org.eclipse.jetty.server.handler.HandlerList;
32 import org.eclipse.jetty.server.handler.HandlerWrapper;
33 import org.eclipse.jetty.server.handler.ResourceHandler;
34 import org.eclipse.jetty.servlet.ServletContextHandler;
35 import org.eclipse.jetty.servlet.ServletHolder;
36 import org.eclipse.jetty.util.log.Log;
37 import org.eclipse.jetty.util.ssl.SslContextFactory;
39 public class Launcher {
40 public static void main(String[] args) throws Exception {
41 GigiConfig conf = GigiConfig.parse(System.in);
42 ServerConstants.init(conf.getMainProps());
44 Server s = new Server();
45 // === SSL HTTP Configuration ===
46 HttpConfiguration https_config = new HttpConfiguration();
47 https_config.setSendServerVersion(false);
48 https_config.setSendXPoweredBy(false);
50 // for client-cert auth
51 https_config.addCustomizer(new SecureRequestCustomizer());
53 ServerConnector connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(
55 connector.setHost(conf.getMainProps().getProperty("host"));
56 connector.setPort(Integer.parseInt(conf.getMainProps().getProperty("port")));
57 s.setConnectors(new Connector[] { connector });
59 HandlerList hl = new HandlerList();
60 hl.setHandlers(new Handler[] { generateStaticContext(), generateGigiContext(conf.getMainProps()),
61 generateAPIContext() });
64 if (connector.getPort() <= 1024 && !System.getProperty("os.name").toLowerCase().contains("win")) {
65 SetUID uid = new SetUID();
66 if (!uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
67 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
72 private static SslConnectionFactory createConnectionFactory(GigiConfig conf) throws GeneralSecurityException,
74 final SslContextFactory sslContextFactory = generateSSLContextFactory(conf, "www");
75 final SslContextFactory secureContextFactory = generateSSLContextFactory(conf, "secure");
76 secureContextFactory.setWantClientAuth(true);
77 secureContextFactory.setNeedClientAuth(false);
78 final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
79 final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
81 secureContextFactory.start();
82 staticContextFactory.start();
83 apiContextFactory.start();
84 } catch (Exception e) {
87 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
89 public boolean shouldRestartSSL() {
94 public SSLEngine restartSSL(SSLSession sslSession) {
96 if (sslSession instanceof ExtendedSSLSession) {
97 ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
98 List<SNIServerName> names = es.getRequestedServerNames();
99 for (SNIServerName sniServerName : names) {
100 if (sniServerName instanceof SNIHostName) {
101 SNIHostName host = (SNIHostName) sniServerName;
102 String hostname = host.getAsciiName();
103 if (hostname.equals(ServerConstants.getWwwHostName())) {
104 e2 = sslContextFactory.newSSLEngine();
105 } else if (hostname.equals(ServerConstants.getStaticHostName())) {
106 e2 = staticContextFactory.newSSLEngine();
107 } else if (hostname.equals(ServerConstants.getSecureHostName())) {
108 e2 = secureContextFactory.newSSLEngine();
109 } else if (hostname.equals(ServerConstants.getApiHostName())) {
110 e2 = apiContextFactory.newSSLEngine();
117 e2 = sslContextFactory.newSSLEngine(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(ServletContextHandler.SESSIONS);
133 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
134 servlet.addServlet(new ServletHolder(new Gigi(conf)), "/*");
136 HandlerList hl = new HandlerList();
137 hl.setHandlers(new Handler[] { hw, servlet });
139 ContextHandler ch = new ContextHandler();
140 ch.setVirtualHosts(new String[] { ServerConstants.getWwwHostName(), ServerConstants.getSecureHostName() });
146 private static Handler generateStaticContext() {
147 final ResourceHandler rh = new ResourceHandler();
148 rh.setResourceBase("static/static");
150 ContextHandler ch = new ContextHandler();
152 ch.setVirtualHosts(new String[] { ServerConstants.getStaticHostName() });
157 private static Handler generateAPIContext() {
158 ServletContextHandler sch = new ServletContextHandler();
160 sch.addVirtualHosts(new String[] { ServerConstants.getApiHostName() });
161 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
165 private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias)
166 throws GeneralSecurityException, IOException {
167 SslContextFactory scf = new SslContextFactory() {
169 String[] ciphers = null;
172 public void customize(SSLEngine sslEngine) {
173 super.customize(sslEngine);
175 SSLParameters ssl = sslEngine.getSSLParameters();
176 ssl.setUseCipherSuitesOrder(true);
177 if (ciphers == null) {
178 ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
181 ssl.setCipherSuites(ciphers);
182 sslEngine.setSSLParameters(ssl);
187 scf.setRenegotiationAllowed(false);
189 scf.setProtocol("TLS");
190 scf.setTrustStore(conf.getTrustStore());
191 KeyStore privateStore = conf.getPrivateStore();
192 scf.setKeyStorePassword(conf.getPrivateStorePw());
193 scf.setKeyStore(privateStore);
194 scf.setCertAlias(alias);