]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Launcher.java
ADD: (most simple) api for issuing certificates first version.
[gigi.git] / src / org / cacert / gigi / Launcher.java
1 package org.cacert.gigi;
2
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
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
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;
48
49 public class Launcher {
50
51     public static void main(String[] args) throws Exception {
52         System.setProperty("jdk.tls.ephemeralDHKeySize", "4096");
53         boot();
54     }
55
56     public static void boot() throws Exception {
57         Locale.setDefault(Locale.ENGLISH);
58         TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
59
60         GigiConfig conf = GigiConfig.parse(System.in);
61         ServerConstants.init(conf.getMainProps());
62         initEmails(conf);
63
64         Server s = new Server();
65         HttpConfiguration httpsConfig = createHttpConfiguration();
66
67         // for client-cert auth
68         httpsConfig.addCustomizer(new SecureRequestCustomizer());
69
70         HttpConfiguration httpConfig = createHttpConfiguration();
71
72         s.setConnectors(new Connector[] {
73                 createConnector(conf, s, httpsConfig, true), createConnector(conf, s, httpConfig, false)
74         });
75
76         HandlerList hl = new HandlerList();
77         hl.setHandlers(new Handler[] {
78                 generateStaticContext(), generateGigiContexts(conf.getMainProps(), conf.getTrustStore()), generateAPIContext()
79         });
80         s.setHandler(hl);
81         s.start();
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!");
86             }
87         }
88         if (conf.getMainProps().containsKey("testrunner")) {
89             DevelLauncher.addDevelPage();
90         }
91     }
92
93     private static ServerConnector createConnector(GigiConfig conf, Server s, HttpConfiguration httpConfig, boolean doHttps) throws GeneralSecurityException, IOException {
94         ServerConnector connector;
95         if (doHttps) {
96             connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(httpConfig));
97         } else {
98             connector = new ServerConnector(s, new HttpConnectionFactory(httpConfig));
99         }
100         connector.setHost(conf.getMainProps().getProperty("host"));
101         if (doHttps) {
102             connector.setPort(ServerConstants.getSecurePort());
103         } else {
104             connector.setPort(ServerConstants.getPort());
105         }
106         connector.setAcceptQueueSize(100);
107         return connector;
108     }
109
110     private static HttpConfiguration createHttpConfiguration() {
111         // SSL HTTP Configuration
112         HttpConfiguration httpsConfig = new HttpConfiguration();
113         httpsConfig.setSendServerVersion(false);
114         httpsConfig.setSendXPoweredBy(false);
115         return httpsConfig;
116     }
117
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);
123     }
124
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);
133         try {
134             secureContextFactory.start();
135             staticContextFactory.start();
136             apiContextFactory.start();
137         } catch (Exception e) {
138             e.printStackTrace();
139         }
140         return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
141
142             @Override
143             public boolean shouldRestartSSL() {
144                 return true;
145             }
146
147             @Override
148             public SSLEngine restartSSL(SSLSession sslSession) {
149                 SSLEngine e2 = null;
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();
165                             }
166                             break;
167                         }
168                     }
169                 }
170                 if (e2 == null) {
171                     e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
172                 }
173                 e2.setUseClientMode(false);
174                 return e2;
175             }
176         };
177     }
178
179     private static Handler generateGigiContexts(Properties conf, KeyStore trust) {
180         ServletHolder webAppServlet = new ServletHolder(new Gigi(conf, trust));
181
182         ContextHandler ch = generateGigiServletContext(webAppServlet);
183         ch.setVirtualHosts(new String[] {
184             ServerConstants.getWwwHostName()
185         });
186         ContextHandler chSecure = generateGigiServletContext(webAppServlet);
187         chSecure.setVirtualHosts(new String[] {
188             ServerConstants.getSecureHostName()
189         });
190
191         HandlerList hl = new HandlerList();
192         hl.setHandlers(new Handler[] {
193                 ch, chSecure
194         });
195         return hl;
196     }
197
198     private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
199         final ResourceHandler rh = generateResourceHandler();
200         rh.setResourceBase("static/www");
201
202         HandlerWrapper hw = new PolicyRedirector();
203         hw.setHandler(rh);
204
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);
212
213         HandlerList hl = new HandlerList();
214         hl.setHandlers(new Handler[] {
215                 hw, servlet
216         });
217
218         ContextHandler ch = new ContextHandler();
219         ch.setHandler(hl);
220         return ch;
221     }
222
223     private static Handler generateStaticContext() {
224         final ResourceHandler rh = generateResourceHandler();
225         rh.setResourceBase("static/static");
226
227         ContextHandler ch = new ContextHandler();
228         ch.setHandler(rh);
229         ch.setVirtualHosts(new String[] {
230             ServerConstants.getStaticHostName()
231         });
232
233         return ch;
234     }
235
236     private static ResourceHandler generateResourceHandler() {
237         ResourceHandler rh = new ResourceHandler() {
238
239             @Override
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);
243             }
244         };
245         rh.setEtags(true);
246         return rh;
247     }
248
249     private static Handler generateAPIContext() {
250         ServletContextHandler sch = new ServletContextHandler();
251
252         sch.addVirtualHosts(new String[] {
253             ServerConstants.getApiHostName()
254         });
255         sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
256         return sch;
257     }
258
259     private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
260         SslContextFactory scf = new SslContextFactory() {
261
262             String[] ciphers = null;
263
264             @Override
265             public void customize(SSLEngine sslEngine) {
266                 super.customize(sslEngine);
267
268                 SSLParameters ssl = sslEngine.getSSLParameters();
269                 ssl.setUseCipherSuitesOrder(true);
270                 if (ciphers == null) {
271                     ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
272                 }
273
274                 ssl.setCipherSuites(ciphers);
275                 sslEngine.setSSLParameters(ssl);
276
277             }
278
279         };
280         scf.setRenegotiationAllowed(false);
281
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);
289         return scf;
290     }
291 }