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.Properties;
14 import javax.net.ssl.ExtendedSSLSession;
15 import javax.net.ssl.SNIHostName;
16 import javax.net.ssl.SNIServerName;
17 import javax.net.ssl.SSLEngine;
18 import javax.net.ssl.SSLParameters;
19 import javax.net.ssl.SSLSession;
21 import org.cacert.gigi.api.GigiAPI;
22 import org.cacert.gigi.email.EmailProvider;
23 import org.cacert.gigi.natives.SetUID;
24 import org.cacert.gigi.util.CipherInfo;
25 import org.cacert.gigi.util.ServerConstants;
26 import org.eclipse.jetty.http.HttpVersion;
27 import org.eclipse.jetty.server.Connector;
28 import org.eclipse.jetty.server.Handler;
29 import org.eclipse.jetty.server.HttpConfiguration;
30 import org.eclipse.jetty.server.HttpConnectionFactory;
31 import org.eclipse.jetty.server.SecureRequestCustomizer;
32 import org.eclipse.jetty.server.Server;
33 import org.eclipse.jetty.server.ServerConnector;
34 import org.eclipse.jetty.server.SessionManager;
35 import org.eclipse.jetty.server.SslConnectionFactory;
36 import org.eclipse.jetty.server.handler.ContextHandler;
37 import org.eclipse.jetty.server.handler.HandlerList;
38 import org.eclipse.jetty.server.handler.HandlerWrapper;
39 import org.eclipse.jetty.server.handler.ResourceHandler;
40 import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
41 import org.eclipse.jetty.servlet.ServletContextHandler;
42 import org.eclipse.jetty.servlet.ServletHolder;
43 import org.eclipse.jetty.util.log.Log;
44 import org.eclipse.jetty.util.ssl.SslContextFactory;
46 public class Launcher {
48 public static void main(String[] args) throws Exception {
49 GigiConfig conf = GigiConfig.parse(System.in);
50 ServerConstants.init(conf.getMainProps());
53 Server s = new Server();
54 // === SSL HTTP Configuration ===
55 HttpConfiguration https_config = new HttpConfiguration();
56 https_config.setSendServerVersion(false);
57 https_config.setSendXPoweredBy(false);
59 // for client-cert auth
60 https_config.addCustomizer(new SecureRequestCustomizer());
62 ServerConnector connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(https_config));
63 connector.setHost(conf.getMainProps().getProperty("host"));
64 connector.setPort(Integer.parseInt(conf.getMainProps().getProperty("port")));
65 s.setConnectors(new Connector[] {
69 HandlerList hl = new HandlerList();
70 hl.setHandlers(new Handler[] {
71 generateStaticContext(), generateGigiContexts(conf.getMainProps()), generateAPIContext()
75 if (connector.getPort() <= 1024 && !System.getProperty("os.name").toLowerCase().contains("win")) {
76 SetUID uid = new SetUID();
77 if ( !uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
78 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
83 private static void initEmails(GigiConfig conf) throws GeneralSecurityException, IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
84 KeyStore privateStore = conf.getPrivateStore();
85 Certificate mail = privateStore.getCertificate("mail");
86 Key k = privateStore.getKey("mail", conf.getPrivateStorePw().toCharArray());
87 EmailProvider.initSystem(conf.getMainProps(), mail, k);
90 private static SslConnectionFactory createConnectionFactory(GigiConfig conf) throws GeneralSecurityException, IOException {
91 final SslContextFactory sslContextFactory = generateSSLContextFactory(conf, "www");
92 final SslContextFactory secureContextFactory = generateSSLContextFactory(conf, "secure");
93 secureContextFactory.setWantClientAuth(true);
94 secureContextFactory.setNeedClientAuth(false);
95 final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
96 final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
98 secureContextFactory.start();
99 staticContextFactory.start();
100 apiContextFactory.start();
101 } catch (Exception e) {
104 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
107 public boolean shouldRestartSSL() {
112 public SSLEngine restartSSL(SSLSession sslSession) {
114 if (sslSession instanceof ExtendedSSLSession) {
115 ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
116 List<SNIServerName> names = es.getRequestedServerNames();
117 for (SNIServerName sniServerName : names) {
118 if (sniServerName instanceof SNIHostName) {
119 SNIHostName host = (SNIHostName) sniServerName;
120 String hostname = host.getAsciiName();
121 if (hostname.equals(ServerConstants.getWwwHostName())) {
122 e2 = sslContextFactory.newSSLEngine();
123 } else if (hostname.equals(ServerConstants.getStaticHostName())) {
124 e2 = staticContextFactory.newSSLEngine();
125 } else if (hostname.equals(ServerConstants.getSecureHostName())) {
126 e2 = secureContextFactory.newSSLEngine();
127 } else if (hostname.equals(ServerConstants.getApiHostName())) {
128 e2 = apiContextFactory.newSSLEngine();
135 e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
137 e2.setUseClientMode(false);
143 private static Handler generateGigiContexts(Properties conf) {
144 ServletHolder webAppServlet = new ServletHolder(new Gigi(conf));
146 ContextHandler ch = generateGigiServletContext(webAppServlet);
147 ch.setVirtualHosts(new String[] {
148 ServerConstants.getWwwHostName()
150 ContextHandler chSecure = generateGigiServletContext(webAppServlet);
151 chSecure.setVirtualHosts(new String[] {
152 ServerConstants.getSecureHostName()
155 HandlerList hl = new HandlerList();
156 hl.setHandlers(new Handler[] {
162 private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
163 final ResourceHandler rh = new ResourceHandler();
164 rh.setResourceBase("static/www");
166 HandlerWrapper hw = new PolicyRedirector();
169 ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
170 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
171 servlet.addServlet(webAppServlet, "/*");
172 ErrorPageErrorHandler epeh = new ErrorPageErrorHandler();
173 epeh.addErrorPage(404, "/error");
174 servlet.setErrorHandler(epeh);
176 HandlerList hl = new HandlerList();
177 hl.setHandlers(new Handler[] {
181 ContextHandler ch = new ContextHandler();
186 private static Handler generateStaticContext() {
187 final ResourceHandler rh = new ResourceHandler();
188 rh.setResourceBase("static/static");
190 ContextHandler ch = new ContextHandler();
192 ch.setVirtualHosts(new String[] {
193 ServerConstants.getStaticHostName()
199 private static Handler generateAPIContext() {
200 ServletContextHandler sch = new ServletContextHandler();
202 sch.addVirtualHosts(new String[] {
203 ServerConstants.getApiHostName()
205 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
209 private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
210 SslContextFactory scf = new SslContextFactory() {
212 String[] ciphers = null;
215 public void customize(SSLEngine sslEngine) {
216 super.customize(sslEngine);
218 SSLParameters ssl = sslEngine.getSSLParameters();
219 ssl.setUseCipherSuitesOrder(true);
220 if (ciphers == null) {
221 ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
224 ssl.setCipherSuites(ciphers);
225 sslEngine.setSSLParameters(ssl);
230 scf.setRenegotiationAllowed(false);
232 scf.setProtocol("TLS");
233 scf.setTrustStore(conf.getTrustStore());
234 KeyStore privateStore = conf.getPrivateStore();
235 scf.setKeyStorePassword(conf.getPrivateStorePw());
236 scf.setKeyStore(privateStore);
237 scf.setCertAlias(alias);