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");
133 secureContextFactory.start();
134 staticContextFactory.start();
135 apiContextFactory.start();
136 } catch (Exception e) {
139 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
142 public boolean shouldRestartSSL() {
147 public SSLEngine restartSSL(SSLSession sslSession) {
149 if (sslSession instanceof ExtendedSSLSession) {
150 ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
151 List<SNIServerName> names = es.getRequestedServerNames();
152 for (SNIServerName sniServerName : names) {
153 if (sniServerName instanceof SNIHostName) {
154 SNIHostName host = (SNIHostName) sniServerName;
155 String hostname = host.getAsciiName();
156 if (hostname.equals(ServerConstants.getWwwHostName())) {
157 e2 = sslContextFactory.newSSLEngine();
158 } else if (hostname.equals(ServerConstants.getStaticHostName())) {
159 e2 = staticContextFactory.newSSLEngine();
160 } else if (hostname.equals(ServerConstants.getSecureHostName())) {
161 e2 = secureContextFactory.newSSLEngine();
162 } else if (hostname.equals(ServerConstants.getApiHostName())) {
163 e2 = apiContextFactory.newSSLEngine();
170 e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
172 e2.setUseClientMode(false);
178 private static Handler generateGigiContexts(Properties conf, KeyStore trust) {
179 ServletHolder webAppServlet = new ServletHolder(new Gigi(conf, trust));
181 ContextHandler ch = generateGigiServletContext(webAppServlet);
182 ch.setVirtualHosts(new String[] {
183 ServerConstants.getWwwHostName()
185 ContextHandler chSecure = generateGigiServletContext(webAppServlet);
186 chSecure.setVirtualHosts(new String[] {
187 ServerConstants.getSecureHostName()
190 HandlerList hl = new HandlerList();
191 hl.setHandlers(new Handler[] {
197 private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
198 final ResourceHandler rh = generateResourceHandler();
199 rh.setResourceBase("static/www");
201 HandlerWrapper hw = new PolicyRedirector();
204 ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
205 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
206 servlet.addServlet(webAppServlet, "/*");
207 ErrorPageErrorHandler epeh = new ErrorPageErrorHandler();
208 epeh.addErrorPage(404, "/error");
209 epeh.addErrorPage(403, "/denied");
210 servlet.setErrorHandler(epeh);
212 HandlerList hl = new HandlerList();
213 hl.setHandlers(new Handler[] {
217 ContextHandler ch = new ContextHandler();
222 private static Handler generateStaticContext() {
223 final ResourceHandler rh = generateResourceHandler();
224 rh.setResourceBase("static/static");
226 ContextHandler ch = new ContextHandler();
228 ch.setVirtualHosts(new String[] {
229 ServerConstants.getStaticHostName()
235 private static ResourceHandler generateResourceHandler() {
236 ResourceHandler rh = new ResourceHandler() {
239 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 {
240 response.setDateHeader(HttpHeader.EXPIRES.asString(), System.currentTimeMillis() + 1000L * 60 * 60 * 24 * 7);
241 super.handle(target, baseRequest, request, response);
248 private static Handler generateAPIContext() {
249 ServletContextHandler sch = new ServletContextHandler();
251 sch.addVirtualHosts(new String[] {
252 ServerConstants.getApiHostName()
254 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
258 private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
259 SslContextFactory scf = new SslContextFactory() {
261 String[] ciphers = null;
264 public void customize(SSLEngine sslEngine) {
265 super.customize(sslEngine);
267 SSLParameters ssl = sslEngine.getSSLParameters();
268 ssl.setUseCipherSuitesOrder(true);
269 if (ciphers == null) {
270 ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
273 ssl.setCipherSuites(ciphers);
274 sslEngine.setSSLParameters(ssl);
279 scf.setRenegotiationAllowed(false);
281 scf.setProtocol("TLS");
282 scf.setIncludeProtocols("TLSv1", "TLSv1.1", "TLSv1.2");
283 scf.setTrustStore(conf.getTrustStore());
284 KeyStore privateStore = conf.getPrivateStore();
285 scf.setKeyStorePassword(conf.getPrivateStorePw());
286 scf.setKeyStore(privateStore);
287 scf.setCertAlias(alias);