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.Locale;
13 import java.util.Properties;
14 import java.util.TimeZone;
16 import javax.net.ssl.ExtendedSSLSession;
17 import javax.net.ssl.SNIHostName;
18 import javax.net.ssl.SNIServerName;
19 import javax.net.ssl.SSLEngine;
20 import javax.net.ssl.SSLParameters;
21 import javax.net.ssl.SSLSession;
23 import org.cacert.gigi.api.GigiAPI;
24 import org.cacert.gigi.email.EmailProvider;
25 import org.cacert.gigi.natives.SetUID;
26 import org.cacert.gigi.util.CipherInfo;
27 import org.cacert.gigi.util.ServerConstants;
28 import org.eclipse.jetty.http.HttpHeader;
29 import org.eclipse.jetty.http.HttpVersion;
30 import org.eclipse.jetty.server.Connector;
31 import org.eclipse.jetty.server.Handler;
32 import org.eclipse.jetty.server.HttpConfiguration;
33 import org.eclipse.jetty.server.HttpConnectionFactory;
34 import org.eclipse.jetty.server.SecureRequestCustomizer;
35 import org.eclipse.jetty.server.Server;
36 import org.eclipse.jetty.server.ServerConnector;
37 import org.eclipse.jetty.server.SessionManager;
38 import org.eclipse.jetty.server.SslConnectionFactory;
39 import org.eclipse.jetty.server.handler.ContextHandler;
40 import org.eclipse.jetty.server.handler.HandlerList;
41 import org.eclipse.jetty.server.handler.HandlerWrapper;
42 import org.eclipse.jetty.server.handler.ResourceHandler;
43 import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
44 import org.eclipse.jetty.servlet.ServletContextHandler;
45 import org.eclipse.jetty.servlet.ServletHolder;
46 import org.eclipse.jetty.util.log.Log;
47 import org.eclipse.jetty.util.ssl.SslContextFactory;
49 public class Launcher {
51 public static void main(String[] args) throws Exception {
52 System.setProperty("jdk.tls.ephemeralDHKeySize", "4096");
56 public static void boot() throws Exception {
57 Locale.setDefault(Locale.ENGLISH);
58 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
60 GigiConfig conf = GigiConfig.parse(System.in);
61 ServerConstants.init(conf.getMainProps());
64 Server s = new Server();
65 HttpConfiguration httpsConfig = createHttpConfiguration();
67 // for client-cert auth
68 httpsConfig.addCustomizer(new SecureRequestCustomizer());
70 HttpConfiguration httpConfig = createHttpConfiguration();
72 s.setConnectors(new Connector[] {
73 createConnector(conf, s, httpsConfig, true), createConnector(conf, s, httpConfig, false)
76 HandlerList hl = new HandlerList();
77 hl.setHandlers(new Handler[] {
78 generateStaticContext(), generateGigiContexts(conf.getMainProps(), conf.getTrustStore()), generateAPIContext()
82 if ((ServerConstants.getSecurePort() <= 1024 || ServerConstants.getPort() <= 1024) && !System.getProperty("os.name").toLowerCase().contains("win")) {
83 SetUID uid = new SetUID();
84 if ( !uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
85 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
88 if (conf.getMainProps().containsKey("testrunner")) {
89 DevelLauncher.addDevelPage();
93 private static ServerConnector createConnector(GigiConfig conf, Server s, HttpConfiguration httpConfig, boolean doHttps) throws GeneralSecurityException, IOException {
94 ServerConnector connector;
96 connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(httpConfig));
98 connector = new ServerConnector(s, new HttpConnectionFactory(httpConfig));
100 connector.setHost(conf.getMainProps().getProperty("host"));
102 connector.setPort(ServerConstants.getSecurePort());
104 connector.setPort(ServerConstants.getPort());
106 connector.setAcceptQueueSize(100);
110 private static HttpConfiguration createHttpConfiguration() {
111 // SSL HTTP Configuration
112 HttpConfiguration httpsConfig = new HttpConfiguration();
113 httpsConfig.setSendServerVersion(false);
114 httpsConfig.setSendXPoweredBy(false);
118 private static void initEmails(GigiConfig conf) throws GeneralSecurityException, IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
119 KeyStore privateStore = conf.getPrivateStore();
120 Certificate mail = privateStore.getCertificate("mail");
121 Key k = privateStore.getKey("mail", conf.getPrivateStorePw().toCharArray());
122 EmailProvider.initSystem(conf.getMainProps(), mail, k);
125 private static SslConnectionFactory createConnectionFactory(GigiConfig conf) throws GeneralSecurityException, IOException {
126 final SslContextFactory sslContextFactory = generateSSLContextFactory(conf, "www");
127 final SslContextFactory secureContextFactory = generateSSLContextFactory(conf, "secure");
128 secureContextFactory.setWantClientAuth(true);
129 secureContextFactory.setNeedClientAuth(false);
130 final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
131 final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
132 apiContextFactory.setWantClientAuth(true);
134 secureContextFactory.start();
135 staticContextFactory.start();
136 apiContextFactory.start();
137 } catch (Exception e) {
140 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
143 public boolean shouldRestartSSL() {
148 public SSLEngine restartSSL(SSLSession sslSession) {
150 if (sslSession instanceof ExtendedSSLSession) {
151 ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
152 List<SNIServerName> names = es.getRequestedServerNames();
153 for (SNIServerName sniServerName : names) {
154 if (sniServerName instanceof SNIHostName) {
155 SNIHostName host = (SNIHostName) sniServerName;
156 String hostname = host.getAsciiName();
157 if (hostname.equals(ServerConstants.getWwwHostName())) {
158 e2 = sslContextFactory.newSSLEngine();
159 } else if (hostname.equals(ServerConstants.getStaticHostName())) {
160 e2 = staticContextFactory.newSSLEngine();
161 } else if (hostname.equals(ServerConstants.getSecureHostName())) {
162 e2 = secureContextFactory.newSSLEngine();
163 } else if (hostname.equals(ServerConstants.getApiHostName())) {
164 e2 = apiContextFactory.newSSLEngine();
171 e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
173 e2.setUseClientMode(false);
179 private static Handler generateGigiContexts(Properties conf, KeyStore trust) {
180 ServletHolder webAppServlet = new ServletHolder(new Gigi(conf, trust));
182 ContextHandler ch = generateGigiServletContext(webAppServlet);
183 ch.setVirtualHosts(new String[] {
184 ServerConstants.getWwwHostName()
186 ContextHandler chSecure = generateGigiServletContext(webAppServlet);
187 chSecure.setVirtualHosts(new String[] {
188 ServerConstants.getSecureHostName()
191 HandlerList hl = new HandlerList();
192 hl.setHandlers(new Handler[] {
198 private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
199 final ResourceHandler rh = generateResourceHandler();
200 rh.setResourceBase("static/www");
202 HandlerWrapper hw = new PolicyRedirector();
205 ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
206 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
207 servlet.addServlet(webAppServlet, "/*");
208 ErrorPageErrorHandler epeh = new ErrorPageErrorHandler();
209 epeh.addErrorPage(404, "/error");
210 epeh.addErrorPage(403, "/denied");
211 servlet.setErrorHandler(epeh);
213 HandlerList hl = new HandlerList();
214 hl.setHandlers(new Handler[] {
218 ContextHandler ch = new ContextHandler();
223 private static Handler generateStaticContext() {
224 final ResourceHandler rh = generateResourceHandler();
225 rh.setResourceBase("static/static");
227 ContextHandler ch = new ContextHandler();
229 ch.setVirtualHosts(new String[] {
230 ServerConstants.getStaticHostName()
236 private static ResourceHandler generateResourceHandler() {
237 ResourceHandler rh = new ResourceHandler() {
240 public void handle(String target, org.eclipse.jetty.server.Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException {
241 response.setDateHeader(HttpHeader.EXPIRES.asString(), System.currentTimeMillis() + 1000L * 60 * 60 * 24 * 7);
242 super.handle(target, baseRequest, request, response);
249 private static Handler generateAPIContext() {
250 ServletContextHandler sch = new ServletContextHandler();
252 sch.addVirtualHosts(new String[] {
253 ServerConstants.getApiHostName()
255 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
259 private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
260 SslContextFactory scf = new SslContextFactory() {
262 String[] ciphers = null;
265 public void customize(SSLEngine sslEngine) {
266 super.customize(sslEngine);
268 SSLParameters ssl = sslEngine.getSSLParameters();
269 ssl.setUseCipherSuitesOrder(true);
270 if (ciphers == null) {
271 ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
274 ssl.setCipherSuites(ciphers);
275 sslEngine.setSSLParameters(ssl);
280 scf.setRenegotiationAllowed(false);
282 scf.setProtocol("TLS");
283 scf.setIncludeProtocols("TLSv1", "TLSv1.1", "TLSv1.2");
284 scf.setTrustStore(conf.getTrustStore());
285 KeyStore privateStore = conf.getPrivateStore();
286 scf.setKeyStorePassword(conf.getPrivateStorePw());
287 scf.setKeyStore(privateStore);
288 scf.setCertAlias(alias);