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;
13 import java.util.TimeZone;
15 import javax.net.ssl.ExtendedSSLSession;
16 import javax.net.ssl.SNIHostName;
17 import javax.net.ssl.SNIServerName;
18 import javax.net.ssl.SSLEngine;
19 import javax.net.ssl.SSLParameters;
20 import javax.net.ssl.SSLSession;
22 import org.cacert.gigi.api.GigiAPI;
23 import org.cacert.gigi.email.EmailProvider;
24 import org.cacert.gigi.natives.SetUID;
25 import org.cacert.gigi.util.CipherInfo;
26 import org.cacert.gigi.util.ServerConstants;
27 import org.eclipse.jetty.http.HttpVersion;
28 import org.eclipse.jetty.server.Connector;
29 import org.eclipse.jetty.server.Handler;
30 import org.eclipse.jetty.server.HttpConfiguration;
31 import org.eclipse.jetty.server.HttpConnectionFactory;
32 import org.eclipse.jetty.server.SecureRequestCustomizer;
33 import org.eclipse.jetty.server.Server;
34 import org.eclipse.jetty.server.ServerConnector;
35 import org.eclipse.jetty.server.SessionManager;
36 import org.eclipse.jetty.server.SslConnectionFactory;
37 import org.eclipse.jetty.server.handler.ContextHandler;
38 import org.eclipse.jetty.server.handler.HandlerList;
39 import org.eclipse.jetty.server.handler.HandlerWrapper;
40 import org.eclipse.jetty.server.handler.ResourceHandler;
41 import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
42 import org.eclipse.jetty.servlet.ServletContextHandler;
43 import org.eclipse.jetty.servlet.ServletHolder;
44 import org.eclipse.jetty.util.log.Log;
45 import org.eclipse.jetty.util.ssl.SslContextFactory;
47 public class Launcher {
49 public static void main(String[] args) throws Exception {
50 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
51 GigiConfig conf = GigiConfig.parse(System.in);
52 ServerConstants.init(conf.getMainProps());
55 Server s = new Server();
56 // === SSL HTTP Configuration ===
57 HttpConfiguration https_config = new HttpConfiguration();
58 https_config.setSendServerVersion(false);
59 https_config.setSendXPoweredBy(false);
61 // for client-cert auth
62 https_config.addCustomizer(new SecureRequestCustomizer());
64 ServerConnector connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(https_config));
65 connector.setHost(conf.getMainProps().getProperty("host"));
66 connector.setPort(Integer.parseInt(conf.getMainProps().getProperty("port")));
67 s.setConnectors(new Connector[] {
71 HandlerList hl = new HandlerList();
72 hl.setHandlers(new Handler[] {
73 generateStaticContext(), generateGigiContexts(conf.getMainProps()), generateAPIContext()
77 if (connector.getPort() <= 1024 && !System.getProperty("os.name").toLowerCase().contains("win")) {
78 SetUID uid = new SetUID();
79 if ( !uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
80 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
85 private static void initEmails(GigiConfig conf) throws GeneralSecurityException, IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
86 KeyStore privateStore = conf.getPrivateStore();
87 Certificate mail = privateStore.getCertificate("mail");
88 Key k = privateStore.getKey("mail", conf.getPrivateStorePw().toCharArray());
89 EmailProvider.initSystem(conf.getMainProps(), mail, k);
92 private static SslConnectionFactory createConnectionFactory(GigiConfig conf) throws GeneralSecurityException, IOException {
93 final SslContextFactory sslContextFactory = generateSSLContextFactory(conf, "www");
94 final SslContextFactory secureContextFactory = generateSSLContextFactory(conf, "secure");
95 secureContextFactory.setWantClientAuth(true);
96 secureContextFactory.setNeedClientAuth(false);
97 final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
98 final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
100 secureContextFactory.start();
101 staticContextFactory.start();
102 apiContextFactory.start();
103 } catch (Exception e) {
106 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
109 public boolean shouldRestartSSL() {
114 public SSLEngine restartSSL(SSLSession sslSession) {
116 if (sslSession instanceof ExtendedSSLSession) {
117 ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
118 List<SNIServerName> names = es.getRequestedServerNames();
119 for (SNIServerName sniServerName : names) {
120 if (sniServerName instanceof SNIHostName) {
121 SNIHostName host = (SNIHostName) sniServerName;
122 String hostname = host.getAsciiName();
123 if (hostname.equals(ServerConstants.getWwwHostName())) {
124 e2 = sslContextFactory.newSSLEngine();
125 } else if (hostname.equals(ServerConstants.getStaticHostName())) {
126 e2 = staticContextFactory.newSSLEngine();
127 } else if (hostname.equals(ServerConstants.getSecureHostName())) {
128 e2 = secureContextFactory.newSSLEngine();
129 } else if (hostname.equals(ServerConstants.getApiHostName())) {
130 e2 = apiContextFactory.newSSLEngine();
137 e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
139 e2.setUseClientMode(false);
145 private static Handler generateGigiContexts(Properties conf) {
146 ServletHolder webAppServlet = new ServletHolder(new Gigi(conf));
148 ContextHandler ch = generateGigiServletContext(webAppServlet);
149 ch.setVirtualHosts(new String[] {
150 ServerConstants.getWwwHostName()
152 ContextHandler chSecure = generateGigiServletContext(webAppServlet);
153 chSecure.setVirtualHosts(new String[] {
154 ServerConstants.getSecureHostName()
157 HandlerList hl = new HandlerList();
158 hl.setHandlers(new Handler[] {
164 private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
165 final ResourceHandler rh = new ResourceHandler();
166 rh.setResourceBase("static/www");
168 HandlerWrapper hw = new PolicyRedirector();
171 ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
172 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
173 servlet.addServlet(webAppServlet, "/*");
174 ErrorPageErrorHandler epeh = new ErrorPageErrorHandler();
175 epeh.addErrorPage(404, "/error");
176 servlet.setErrorHandler(epeh);
178 HandlerList hl = new HandlerList();
179 hl.setHandlers(new Handler[] {
183 ContextHandler ch = new ContextHandler();
188 private static Handler generateStaticContext() {
189 final ResourceHandler rh = new ResourceHandler();
190 rh.setResourceBase("static/static");
192 ContextHandler ch = new ContextHandler();
194 ch.setVirtualHosts(new String[] {
195 ServerConstants.getStaticHostName()
201 private static Handler generateAPIContext() {
202 ServletContextHandler sch = new ServletContextHandler();
204 sch.addVirtualHosts(new String[] {
205 ServerConstants.getApiHostName()
207 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
211 private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
212 SslContextFactory scf = new SslContextFactory() {
214 String[] ciphers = null;
217 public void customize(SSLEngine sslEngine) {
218 super.customize(sslEngine);
220 SSLParameters ssl = sslEngine.getSSLParameters();
221 ssl.setUseCipherSuitesOrder(true);
222 if (ciphers == null) {
223 ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
226 ssl.setCipherSuites(ciphers);
227 sslEngine.setSSLParameters(ssl);
232 scf.setRenegotiationAllowed(false);
234 scf.setProtocol("TLS");
235 scf.setTrustStore(conf.getTrustStore());
236 KeyStore privateStore = conf.getPrivateStore();
237 scf.setKeyStorePassword(conf.getPrivateStorePw());
238 scf.setKeyStore(privateStore);
239 scf.setCertAlias(alias);