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.setNeedClientAuth(true);
77 final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
78 final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
80 secureContextFactory.start();
81 staticContextFactory.start();
82 apiContextFactory.start();
83 } catch (Exception e) {
86 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
88 public boolean shouldRestartSSL() {
93 public SSLEngine restartSSL(SSLSession sslSession) {
95 if (sslSession instanceof ExtendedSSLSession) {
96 ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
97 List<SNIServerName> names = es.getRequestedServerNames();
98 for (SNIServerName sniServerName : names) {
99 if (sniServerName instanceof SNIHostName) {
100 SNIHostName host = (SNIHostName) sniServerName;
101 String hostname = host.getAsciiName();
102 if (hostname.equals(ServerConstants.getWwwHostName())) {
103 e2 = sslContextFactory.newSSLEngine();
104 } else if (hostname.equals(ServerConstants.getStaticHostName())) {
105 e2 = staticContextFactory.newSSLEngine();
106 } else if (hostname.equals(ServerConstants.getSecureHostName())) {
107 e2 = secureContextFactory.newSSLEngine();
108 } else if (hostname.equals(ServerConstants.getApiHostName())) {
109 e2 = apiContextFactory.newSSLEngine();
116 e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
118 e2.setUseClientMode(false);
124 private static ContextHandler generateGigiContext(Properties conf) {
125 final ResourceHandler rh = new ResourceHandler();
126 rh.setResourceBase("static/www");
128 HandlerWrapper hw = new PolicyRedirector();
131 ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
132 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
133 servlet.addServlet(new ServletHolder(new Gigi(conf)), "/*");
135 HandlerList hl = new HandlerList();
136 hl.setHandlers(new Handler[] { hw, servlet });
138 ContextHandler ch = new ContextHandler();
139 ch.setVirtualHosts(new String[] { ServerConstants.getWwwHostName(), ServerConstants.getSecureHostName() });
145 private static Handler generateStaticContext() {
146 final ResourceHandler rh = new ResourceHandler();
147 rh.setResourceBase("static/static");
149 ContextHandler ch = new ContextHandler();
151 ch.setVirtualHosts(new String[] { ServerConstants.getStaticHostName() });
156 private static Handler generateAPIContext() {
157 ServletContextHandler sch = new ServletContextHandler();
159 sch.addVirtualHosts(new String[] { ServerConstants.getApiHostName() });
160 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
164 private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias)
165 throws GeneralSecurityException, IOException {
166 SslContextFactory scf = new SslContextFactory() {
168 String[] ciphers = null;
171 public void customize(SSLEngine sslEngine) {
172 super.customize(sslEngine);
174 SSLParameters ssl = sslEngine.getSSLParameters();
175 ssl.setUseCipherSuitesOrder(true);
176 if (ciphers == null) {
177 ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
180 ssl.setCipherSuites(ciphers);
181 sslEngine.setSSLParameters(ssl);
186 scf.setRenegotiationAllowed(false);
188 scf.setProtocol("TLS");
189 scf.setTrustStore(conf.getTrustStore());
190 KeyStore privateStore = conf.getPrivateStore();
191 scf.setKeyStorePassword(conf.getPrivateStorePw());
192 scf.setKeyStore(privateStore);
193 scf.setCertAlias(alias);