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;
22 import javax.servlet.http.HttpServletResponse;
24 import org.cacert.gigi.api.GigiAPI;
25 import org.cacert.gigi.email.EmailProvider;
26 import org.cacert.gigi.natives.SetUID;
27 import org.cacert.gigi.util.CipherInfo;
28 import org.cacert.gigi.util.ServerConstants;
29 import org.eclipse.jetty.http.HttpHeader;
30 import org.eclipse.jetty.http.HttpVersion;
31 import org.eclipse.jetty.server.Connector;
32 import org.eclipse.jetty.server.Handler;
33 import org.eclipse.jetty.server.HttpConfiguration;
34 import org.eclipse.jetty.server.HttpConnectionFactory;
35 import org.eclipse.jetty.server.SecureRequestCustomizer;
36 import org.eclipse.jetty.server.Server;
37 import org.eclipse.jetty.server.ServerConnector;
38 import org.eclipse.jetty.server.SessionManager;
39 import org.eclipse.jetty.server.SslConnectionFactory;
40 import org.eclipse.jetty.server.handler.ContextHandler;
41 import org.eclipse.jetty.server.handler.HandlerList;
42 import org.eclipse.jetty.server.handler.HandlerWrapper;
43 import org.eclipse.jetty.server.handler.ResourceHandler;
44 import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
45 import org.eclipse.jetty.servlet.ServletContextHandler;
46 import org.eclipse.jetty.servlet.ServletHolder;
47 import org.eclipse.jetty.util.log.Log;
48 import org.eclipse.jetty.util.resource.Resource;
49 import org.eclipse.jetty.util.ssl.SslContextFactory;
51 public class Launcher {
53 public static void main(String[] args) throws Exception {
54 System.setProperty("jdk.tls.ephemeralDHKeySize", "4096");
58 public static void boot() throws Exception {
59 Locale.setDefault(Locale.ENGLISH);
60 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
62 GigiConfig conf = GigiConfig.parse(System.in);
63 ServerConstants.init(conf.getMainProps());
66 Server s = new Server();
67 HttpConfiguration httpsConfig = createHttpConfiguration();
69 // for client-cert auth
70 httpsConfig.addCustomizer(new SecureRequestCustomizer());
72 HttpConfiguration httpConfig = createHttpConfiguration();
74 s.setConnectors(new Connector[] {
75 createConnector(conf, s, httpsConfig, true), createConnector(conf, s, httpConfig, false)
78 HandlerList hl = new HandlerList();
79 hl.setHandlers(new Handler[] {
80 generateStaticContext(), generateGigiContexts(conf.getMainProps(), conf.getTrustStore()), generateAPIContext()
84 if ((ServerConstants.getSecurePort() <= 1024 || ServerConstants.getPort() <= 1024) && !System.getProperty("os.name").toLowerCase().contains("win")) {
85 SetUID uid = new SetUID();
86 if ( !uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
87 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
90 if (conf.getMainProps().containsKey("testrunner")) {
91 DevelLauncher.addDevelPage();
95 private static ServerConnector createConnector(GigiConfig conf, Server s, HttpConfiguration httpConfig, boolean doHttps) throws GeneralSecurityException, IOException {
96 ServerConnector connector;
98 connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(httpConfig));
100 connector = new ServerConnector(s, new HttpConnectionFactory(httpConfig));
102 connector.setHost(conf.getMainProps().getProperty("host"));
104 connector.setPort(ServerConstants.getSecurePort());
106 connector.setPort(ServerConstants.getPort());
108 connector.setAcceptQueueSize(100);
112 private static HttpConfiguration createHttpConfiguration() {
113 // SSL HTTP Configuration
114 HttpConfiguration httpsConfig = new HttpConfiguration();
115 httpsConfig.setSendServerVersion(false);
116 httpsConfig.setSendXPoweredBy(false);
120 private static void initEmails(GigiConfig conf) throws GeneralSecurityException, IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
121 KeyStore privateStore = conf.getPrivateStore();
122 Certificate mail = privateStore.getCertificate("mail");
123 Key k = privateStore.getKey("mail", conf.getPrivateStorePw().toCharArray());
124 EmailProvider.initSystem(conf.getMainProps(), mail, k);
127 private static SslConnectionFactory createConnectionFactory(GigiConfig conf) throws GeneralSecurityException, IOException {
128 final SslContextFactory sslContextFactory = generateSSLContextFactory(conf, "www");
129 final SslContextFactory secureContextFactory = generateSSLContextFactory(conf, "secure");
130 secureContextFactory.setWantClientAuth(true);
131 secureContextFactory.setNeedClientAuth(false);
132 final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
133 final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
134 apiContextFactory.setWantClientAuth(true);
136 secureContextFactory.start();
137 staticContextFactory.start();
138 apiContextFactory.start();
139 } catch (Exception e) {
142 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
145 public boolean shouldRestartSSL() {
150 public SSLEngine restartSSL(SSLSession sslSession) {
152 if (sslSession instanceof ExtendedSSLSession) {
153 ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
154 List<SNIServerName> names = es.getRequestedServerNames();
155 for (SNIServerName sniServerName : names) {
156 if (sniServerName instanceof SNIHostName) {
157 SNIHostName host = (SNIHostName) sniServerName;
158 String hostname = host.getAsciiName();
159 if (hostname.equals(ServerConstants.getWwwHostName())) {
160 e2 = sslContextFactory.newSSLEngine();
161 } else if (hostname.equals(ServerConstants.getStaticHostName())) {
162 e2 = staticContextFactory.newSSLEngine();
163 } else if (hostname.equals(ServerConstants.getSecureHostName())) {
164 e2 = secureContextFactory.newSSLEngine();
165 } else if (hostname.equals(ServerConstants.getApiHostName())) {
166 e2 = apiContextFactory.newSSLEngine();
173 e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
175 e2.setUseClientMode(false);
181 private static Handler generateGigiContexts(Properties conf, KeyStore trust) {
182 ServletHolder webAppServlet = new ServletHolder(new Gigi(conf, trust));
184 ContextHandler ch = generateGigiServletContext(webAppServlet);
185 ch.setVirtualHosts(new String[] {
186 ServerConstants.getWwwHostName()
188 ContextHandler chSecure = generateGigiServletContext(webAppServlet);
189 chSecure.setVirtualHosts(new String[] {
190 ServerConstants.getSecureHostName()
193 HandlerList hl = new HandlerList();
194 hl.setHandlers(new Handler[] {
200 private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
201 final ResourceHandler rh = generateResourceHandler();
202 rh.setResourceBase("static/www");
204 HandlerWrapper hw = new PolicyRedirector();
207 ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
208 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
209 servlet.addServlet(webAppServlet, "/*");
210 ErrorPageErrorHandler epeh = new ErrorPageErrorHandler();
211 epeh.addErrorPage(404, "/error");
212 epeh.addErrorPage(403, "/denied");
213 servlet.setErrorHandler(epeh);
215 HandlerList hl = new HandlerList();
216 hl.setHandlers(new Handler[] {
220 ContextHandler ch = new ContextHandler();
225 private static Handler generateStaticContext() {
226 final ResourceHandler rh = generateResourceHandler();
227 rh.setResourceBase("static/static");
229 ContextHandler ch = new ContextHandler();
231 ch.setVirtualHosts(new String[] {
232 ServerConstants.getStaticHostName()
238 private static ResourceHandler generateResourceHandler() {
239 ResourceHandler rh = new ResourceHandler() {
242 protected void doResponseHeaders(HttpServletResponse response, Resource resource, String mimeType) {
243 super.doResponseHeaders(response, resource, mimeType);
244 response.setDateHeader(HttpHeader.EXPIRES.asString(), System.currentTimeMillis() + 1000L * 60 * 60 * 24 * 7);
251 private static Handler generateAPIContext() {
252 ServletContextHandler sch = new ServletContextHandler();
254 sch.addVirtualHosts(new String[] {
255 ServerConstants.getApiHostName()
257 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
261 private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
262 SslContextFactory scf = new SslContextFactory() {
264 String[] ciphers = null;
267 public void customize(SSLEngine sslEngine) {
268 super.customize(sslEngine);
270 SSLParameters ssl = sslEngine.getSSLParameters();
271 ssl.setUseCipherSuitesOrder(true);
272 if (ciphers == null) {
273 ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
276 ssl.setCipherSuites(ciphers);
277 sslEngine.setSSLParameters(ssl);
282 scf.setRenegotiationAllowed(false);
284 scf.setProtocol("TLS");
285 scf.setIncludeProtocols("TLSv1", "TLSv1.1", "TLSv1.2");
286 scf.setTrustStore(conf.getTrustStore());
287 KeyStore privateStore = conf.getPrivateStore();
288 scf.setKeyStorePassword(conf.getPrivateStorePw());
289 scf.setKeyStore(privateStore);
290 scf.setCertAlias(alias);