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;
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 Locale.setDefault(Locale.ENGLISH);
51 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
52 GigiConfig conf = GigiConfig.parse(System.in);
53 ServerConstants.init(conf.getMainProps());
56 Server s = new Server();
57 HttpConfiguration httpsConfig = createHttpConfiguration();
59 // for client-cert auth
60 httpsConfig.addCustomizer(new SecureRequestCustomizer());
62 HttpConfiguration httpConfig = createHttpConfiguration();
64 s.setConnectors(new Connector[] {
65 createConnector(conf, s, httpsConfig, true), createConnector(conf, s, httpConfig, false)
68 HandlerList hl = new HandlerList();
69 hl.setHandlers(new Handler[] {
70 generateStaticContext(), generateGigiContexts(conf.getMainProps(), conf.getTrustStore()), generateAPIContext()
74 if ((ServerConstants.getSecurePort() <= 1024 || ServerConstants.getPort() <= 1024) && !System.getProperty("os.name").toLowerCase().contains("win")) {
75 SetUID uid = new SetUID();
76 if ( !uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
77 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
82 private static ServerConnector createConnector(GigiConfig conf, Server s, HttpConfiguration httpConfig, boolean doHttps) throws GeneralSecurityException, IOException {
83 ServerConnector connector;
85 connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(httpConfig));
87 connector = new ServerConnector(s, new HttpConnectionFactory(httpConfig));
89 connector.setHost(conf.getMainProps().getProperty("host"));
91 connector.setPort(ServerConstants.getSecurePort());
93 connector.setPort(ServerConstants.getPort());
95 connector.setAcceptQueueSize(100);
99 private static HttpConfiguration createHttpConfiguration() {
100 // SSL HTTP Configuration
101 HttpConfiguration httpsConfig = new HttpConfiguration();
102 httpsConfig.setSendServerVersion(false);
103 httpsConfig.setSendXPoweredBy(false);
107 private static void initEmails(GigiConfig conf) throws GeneralSecurityException, IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
108 KeyStore privateStore = conf.getPrivateStore();
109 Certificate mail = privateStore.getCertificate("mail");
110 Key k = privateStore.getKey("mail", conf.getPrivateStorePw().toCharArray());
111 EmailProvider.initSystem(conf.getMainProps(), mail, k);
114 private static SslConnectionFactory createConnectionFactory(GigiConfig conf) throws GeneralSecurityException, IOException {
115 final SslContextFactory sslContextFactory = generateSSLContextFactory(conf, "www");
116 final SslContextFactory secureContextFactory = generateSSLContextFactory(conf, "secure");
117 secureContextFactory.setWantClientAuth(true);
118 secureContextFactory.setNeedClientAuth(false);
119 final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
120 final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
122 secureContextFactory.start();
123 staticContextFactory.start();
124 apiContextFactory.start();
125 } catch (Exception e) {
128 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
131 public boolean shouldRestartSSL() {
136 public SSLEngine restartSSL(SSLSession sslSession) {
138 if (sslSession instanceof ExtendedSSLSession) {
139 ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
140 List<SNIServerName> names = es.getRequestedServerNames();
141 for (SNIServerName sniServerName : names) {
142 if (sniServerName instanceof SNIHostName) {
143 SNIHostName host = (SNIHostName) sniServerName;
144 String hostname = host.getAsciiName();
145 if (hostname.equals(ServerConstants.getWwwHostName())) {
146 e2 = sslContextFactory.newSSLEngine();
147 } else if (hostname.equals(ServerConstants.getStaticHostName())) {
148 e2 = staticContextFactory.newSSLEngine();
149 } else if (hostname.equals(ServerConstants.getSecureHostName())) {
150 e2 = secureContextFactory.newSSLEngine();
151 } else if (hostname.equals(ServerConstants.getApiHostName())) {
152 e2 = apiContextFactory.newSSLEngine();
159 e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
161 e2.setUseClientMode(false);
167 private static Handler generateGigiContexts(Properties conf, KeyStore trust) {
168 ServletHolder webAppServlet = new ServletHolder(new Gigi(conf, trust));
170 ContextHandler ch = generateGigiServletContext(webAppServlet);
171 ch.setVirtualHosts(new String[] {
172 ServerConstants.getWwwHostName()
174 ContextHandler chSecure = generateGigiServletContext(webAppServlet);
175 chSecure.setVirtualHosts(new String[] {
176 ServerConstants.getSecureHostName()
179 HandlerList hl = new HandlerList();
180 hl.setHandlers(new Handler[] {
186 private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
187 final ResourceHandler rh = new ResourceHandler();
188 rh.setResourceBase("static/www");
190 HandlerWrapper hw = new PolicyRedirector();
193 ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
194 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
195 servlet.addServlet(webAppServlet, "/*");
196 ErrorPageErrorHandler epeh = new ErrorPageErrorHandler();
197 epeh.addErrorPage(404, "/error");
198 servlet.setErrorHandler(epeh);
200 HandlerList hl = new HandlerList();
201 hl.setHandlers(new Handler[] {
205 ContextHandler ch = new ContextHandler();
210 private static Handler generateStaticContext() {
211 final ResourceHandler rh = new ResourceHandler();
212 rh.setResourceBase("static/static");
214 ContextHandler ch = new ContextHandler();
216 ch.setVirtualHosts(new String[] {
217 ServerConstants.getStaticHostName()
223 private static Handler generateAPIContext() {
224 ServletContextHandler sch = new ServletContextHandler();
226 sch.addVirtualHosts(new String[] {
227 ServerConstants.getApiHostName()
229 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
233 private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
234 SslContextFactory scf = new SslContextFactory() {
236 String[] ciphers = null;
239 public void customize(SSLEngine sslEngine) {
240 super.customize(sslEngine);
242 SSLParameters ssl = sslEngine.getSSLParameters();
243 ssl.setUseCipherSuitesOrder(true);
244 if (ciphers == null) {
245 ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
248 ssl.setCipherSuites(ciphers);
249 sslEngine.setSSLParameters(ssl);
254 scf.setRenegotiationAllowed(false);
256 scf.setProtocol("TLS");
257 scf.setTrustStore(conf.getTrustStore());
258 KeyStore privateStore = conf.getPrivateStore();
259 scf.setKeyStorePassword(conf.getPrivateStorePw());
260 scf.setKeyStore(privateStore);
261 scf.setCertAlias(alias);