1 package org.cacert.gigi;
3 import java.io.ByteArrayInputStream;
5 import java.io.FileInputStream;
6 import java.io.InputStream;
7 import java.io.IOException;
8 import java.net.HttpURLConnection;
9 import java.net.InetSocketAddress;
10 import java.security.GeneralSecurityException;
11 import java.security.Key;
12 import java.security.KeyStore;
13 import java.security.KeyStoreException;
14 import java.security.NoSuchAlgorithmException;
15 import java.security.UnrecoverableKeyException;
16 import java.security.cert.Certificate;
17 import java.security.cert.CertificateException;
18 import java.security.cert.CertificateFactory;
19 import java.security.cert.X509Certificate;
20 import java.util.List;
21 import java.util.Locale;
22 import java.util.Properties;
23 import java.util.TimeZone;
25 import javax.net.ssl.ExtendedSSLSession;
26 import javax.net.ssl.SNIHostName;
27 import javax.net.ssl.SNIServerName;
28 import javax.net.ssl.SSLEngine;
29 import javax.net.ssl.SSLParameters;
30 import javax.net.ssl.SSLSession;
31 import javax.servlet.http.HttpServletResponse;
33 import org.cacert.gigi.api.GigiAPI;
34 import org.cacert.gigi.email.EmailProvider;
35 import org.cacert.gigi.natives.SetUID;
36 import org.cacert.gigi.util.CipherInfo;
37 import org.cacert.gigi.util.PEM;
38 import org.cacert.gigi.util.ServerConstants;
39 import org.eclipse.jetty.http.HttpFields;
40 import org.eclipse.jetty.http.HttpHeader;
41 import org.eclipse.jetty.http.HttpVersion;
42 import org.eclipse.jetty.server.Connector;
43 import org.eclipse.jetty.server.Handler;
44 import org.eclipse.jetty.server.HttpConfiguration;
45 import org.eclipse.jetty.server.HttpConfiguration.Customizer;
46 import org.eclipse.jetty.server.HttpConnectionFactory;
47 import org.eclipse.jetty.server.Request;
48 import org.eclipse.jetty.server.SecureRequestCustomizer;
49 import org.eclipse.jetty.server.Server;
50 import org.eclipse.jetty.server.ServerConnector;
51 import org.eclipse.jetty.server.SessionManager;
52 import org.eclipse.jetty.server.SslConnectionFactory;
53 import org.eclipse.jetty.server.handler.ContextHandler;
54 import org.eclipse.jetty.server.handler.HandlerList;
55 import org.eclipse.jetty.server.handler.HandlerWrapper;
56 import org.eclipse.jetty.server.handler.ResourceHandler;
57 import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
58 import org.eclipse.jetty.servlet.ServletContextHandler;
59 import org.eclipse.jetty.servlet.ServletHolder;
60 import org.eclipse.jetty.util.log.Log;
61 import org.eclipse.jetty.util.resource.Resource;
62 import org.eclipse.jetty.util.ssl.SslContextFactory;
64 public class Launcher {
66 class ExtendedForwarded implements Customizer {
69 public void customize(Connector connector, HttpConfiguration config, Request request) {
70 HttpFields httpFields = request.getHttpFields();
72 String ip = httpFields.getStringField("X-Real-IP");
73 String proto = httpFields.getStringField("X-Real-Proto");
74 String cert = httpFields.getStringField("X-Client-Cert");
75 request.setSecure("https".equals(proto));
76 request.setScheme(proto);
77 if ( !"https".equals(proto)) {
82 X509Certificate[] certs = new X509Certificate[1];
84 certs[0] = (X509Certificate) CertificateFactory.getInstance("X509").generateCertificate(new ByteArrayInputStream(PEM.decode("CERTIFICATE", cert)));
85 request.setAttribute("javax.servlet.request.X509Certificate", certs);
86 } catch (CertificateException e) {
91 String[] parts = ip.split(":");
92 if (parts.length == 2) {
93 request.setRemoteAddr(InetSocketAddress.createUnresolved(parts[0], Integer.parseInt(parts[1])));
100 public static void main(String[] args) throws Exception {
101 System.setProperty("jdk.tls.ephemeralDHKeySize", "4096");
103 if (args.length >= 1) {
104 in = new FileInputStream(new File(args[0]));
108 new Launcher().boot(in);
115 private boolean isSystemPort(int port) {
116 return 1 <= port && port <= 1024;
119 public synchronized void boot(InputStream in) throws Exception {
120 Locale.setDefault(Locale.ENGLISH);
121 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
122 HttpURLConnection.setFollowRedirects(false);
124 conf = GigiConfig.parse(in);
125 ServerConstants.init(conf.getMainProps());
134 if ((isSystemPort(ServerConstants.getSecurePort()) || isSystemPort(ServerConstants.getPort())) && !System.getProperty("os.name").toLowerCase().contains("win")) {
135 String uid_s = conf.getMainProps().getProperty("gigi.uid", Integer.toString(65536 - 2));
136 String gid_s = conf.getMainProps().getProperty("gigi.gid", Integer.toString(65536 - 2));
138 int uid = Integer.parseInt(uid_s);
139 int gid = Integer.parseInt(gid_s);
140 if (uid == -1 && gid == -1) {
142 } else if (uid > 0 && gid > 0 && uid < 65536 && gid < 65536) {
143 SetUID.Status status = new SetUID().setUid(uid, gid);
144 if ( !status.getSuccess()) {
145 Log.getLogger(Launcher.class).warn(status.getMessage());
148 Log.getLogger(Launcher.class).warn("Invalid uid or gid (must satisfy 0 < id < 65536)");
150 } catch (NumberFormatException e) {
151 Log.getLogger(Launcher.class).warn("Invalid gigi.uid or gigi.gid", e);
156 private HttpConfiguration createHttpConfiguration() {
157 // SSL HTTP Configuration
158 HttpConfiguration httpsConfig = new HttpConfiguration();
159 httpsConfig.setSendServerVersion(false);
160 httpsConfig.setSendXPoweredBy(false);
164 private void initConnectors() throws GeneralSecurityException, IOException {
165 HttpConfiguration httpConfig = createHttpConfiguration();
166 if (conf.getMainProps().getProperty("proxy", "false").equals("true")) {
167 httpConfig.addCustomizer(new ExtendedForwarded());
168 s.setConnectors(new Connector[] {
169 ConnectorsLauncher.createConnector(conf, s, httpConfig, false)
172 HttpConfiguration httpsConfig = createHttpConfiguration();
173 // for client-cert auth
174 httpsConfig.addCustomizer(new SecureRequestCustomizer());
175 s.setConnectors(new Connector[] {
176 ConnectorsLauncher.createConnector(conf, s, httpsConfig, true), ConnectorsLauncher.createConnector(conf, s, httpConfig, false)
181 private void initEmails(GigiConfig conf) throws GeneralSecurityException, IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
182 KeyStore privateStore = conf.getPrivateStore();
183 Certificate mail = null;
185 if (privateStore != null && privateStore.containsAlias("mail")) {
186 mail = privateStore.getCertificate("mail");
187 k = privateStore.getKey("mail", conf.getPrivateStorePw().toCharArray());
189 EmailProvider.initSystem(conf.getMainProps(), mail, k);
192 private static class ConnectorsLauncher {
194 private ConnectorsLauncher() {}
196 protected static ServerConnector createConnector(GigiConfig conf, Server s, HttpConfiguration httpConfig, boolean doHttps) throws GeneralSecurityException, IOException {
197 ServerConnector connector;
200 connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(httpConfig));
201 port = ServerConstants.getSecurePort();
203 connector = new ServerConnector(s, new HttpConnectionFactory(httpConfig));
204 port = ServerConstants.getPort();
207 connector.setInheritChannel(true);
209 connector.setHost(conf.getMainProps().getProperty("host"));
210 connector.setPort(port);
212 connector.setAcceptQueueSize(100);
216 private static SslConnectionFactory createConnectionFactory(GigiConfig conf) throws GeneralSecurityException, IOException {
217 final SslContextFactory sslContextFactory = generateSSLContextFactory(conf, "www");
218 final SslContextFactory secureContextFactory = generateSSLContextFactory(conf, "secure");
219 secureContextFactory.setWantClientAuth(true);
220 secureContextFactory.setNeedClientAuth(true);
221 final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
222 final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
223 apiContextFactory.setWantClientAuth(true);
225 secureContextFactory.start();
226 staticContextFactory.start();
227 apiContextFactory.start();
228 } catch (Exception e) {
231 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
234 public boolean shouldRestartSSL() {
239 public SSLEngine restartSSL(SSLSession sslSession) {
241 if (sslSession instanceof ExtendedSSLSession) {
242 ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
243 List<SNIServerName> names = es.getRequestedServerNames();
244 for (SNIServerName sniServerName : names) {
245 if (sniServerName instanceof SNIHostName) {
246 SNIHostName host = (SNIHostName) sniServerName;
247 String hostname = host.getAsciiName();
248 if (hostname.equals(ServerConstants.getWwwHostName())) {
249 e2 = sslContextFactory.newSSLEngine();
250 } else if (hostname.equals(ServerConstants.getStaticHostName())) {
251 e2 = staticContextFactory.newSSLEngine();
252 } else if (hostname.equals(ServerConstants.getSecureHostName())) {
253 e2 = secureContextFactory.newSSLEngine();
254 } else if (hostname.equals(ServerConstants.getApiHostName())) {
255 e2 = apiContextFactory.newSSLEngine();
262 e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
264 e2.setUseClientMode(false);
270 private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
271 SslContextFactory scf = new SslContextFactory() {
273 String[] ciphers = null;
276 public void customize(SSLEngine sslEngine) {
277 super.customize(sslEngine);
279 SSLParameters ssl = sslEngine.getSSLParameters();
280 ssl.setUseCipherSuitesOrder(true);
281 if (ciphers == null) {
282 ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
285 ssl.setCipherSuites(ciphers);
286 sslEngine.setSSLParameters(ssl);
291 scf.setRenegotiationAllowed(false);
293 scf.setProtocol("TLS");
294 scf.setIncludeProtocols("TLSv1", "TLSv1.1", "TLSv1.2");
295 scf.setTrustStore(conf.getTrustStore());
296 KeyStore privateStore = conf.getPrivateStore();
297 scf.setKeyStorePassword(conf.getPrivateStorePw());
298 scf.setKeyStore(privateStore);
299 scf.setCertAlias(alias);
304 private void initHandlers() throws GeneralSecurityException, IOException {
305 HandlerList hl = new HandlerList();
306 hl.setHandlers(new Handler[] {
307 ContextLauncher.generateStaticContext(), ContextLauncher.generateGigiContexts(conf.getMainProps(), conf.getTrustStore()), ContextLauncher.generateAPIContext()
312 private static class ContextLauncher {
314 private ContextLauncher() {}
316 protected static Handler generateGigiContexts(Properties conf, KeyStore trust) {
317 ServletHolder webAppServlet = new ServletHolder(new Gigi(conf, trust));
319 ContextHandler ch = generateGigiServletContext(webAppServlet);
320 ch.setVirtualHosts(new String[] {
321 ServerConstants.getWwwHostName()
323 ContextHandler chSecure = generateGigiServletContext(webAppServlet);
324 chSecure.setVirtualHosts(new String[] {
325 ServerConstants.getSecureHostName()
328 HandlerList hl = new HandlerList();
329 hl.setHandlers(new Handler[] {
335 private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
336 final ResourceHandler rh = generateResourceHandler();
337 rh.setResourceBase("static/www");
339 HandlerWrapper hw = new PolicyRedirector();
342 ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
343 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "SomeCA-Session");
344 servlet.addServlet(webAppServlet, "/*");
345 ErrorPageErrorHandler epeh = new ErrorPageErrorHandler();
346 epeh.addErrorPage(404, "/error");
347 epeh.addErrorPage(403, "/denied");
348 servlet.setErrorHandler(epeh);
350 HandlerList hl = new HandlerList();
351 hl.setHandlers(new Handler[] {
355 ContextHandler ch = new ContextHandler();
360 protected static Handler generateStaticContext() {
361 final ResourceHandler rh = generateResourceHandler();
362 rh.setResourceBase("static/static");
364 ContextHandler ch = new ContextHandler();
366 ch.setVirtualHosts(new String[] {
367 ServerConstants.getStaticHostName()
373 private static ResourceHandler generateResourceHandler() {
374 ResourceHandler rh = new ResourceHandler() {
377 protected void doResponseHeaders(HttpServletResponse response, Resource resource, String mimeType) {
378 super.doResponseHeaders(response, resource, mimeType);
379 response.setDateHeader(HttpHeader.EXPIRES.asString(), System.currentTimeMillis() + 1000L * 60 * 60 * 24 * 7);
386 protected static Handler generateAPIContext() {
387 ServletContextHandler sch = new ServletContextHandler();
389 sch.addVirtualHosts(new String[] {
390 ServerConstants.getApiHostName()
392 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");