]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Launcher.java
[EMPTY] Formatting with configured formatter.
[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.KeyStore;
6 import java.util.List;
7 import java.util.Properties;
8
9 import javax.net.ssl.ExtendedSSLSession;
10 import javax.net.ssl.SNIHostName;
11 import javax.net.ssl.SNIServerName;
12 import javax.net.ssl.SSLEngine;
13 import javax.net.ssl.SSLParameters;
14 import javax.net.ssl.SSLSession;
15
16 import org.cacert.gigi.api.GigiAPI;
17 import org.cacert.gigi.natives.SetUID;
18 import org.cacert.gigi.util.CipherInfo;
19 import org.cacert.gigi.util.ServerConstants;
20 import org.eclipse.jetty.http.HttpVersion;
21 import org.eclipse.jetty.server.Connector;
22 import org.eclipse.jetty.server.Handler;
23 import org.eclipse.jetty.server.HttpConfiguration;
24 import org.eclipse.jetty.server.HttpConnectionFactory;
25 import org.eclipse.jetty.server.SecureRequestCustomizer;
26 import org.eclipse.jetty.server.Server;
27 import org.eclipse.jetty.server.ServerConnector;
28 import org.eclipse.jetty.server.SessionManager;
29 import org.eclipse.jetty.server.SslConnectionFactory;
30 import org.eclipse.jetty.server.handler.ContextHandler;
31 import org.eclipse.jetty.server.handler.HandlerList;
32 import org.eclipse.jetty.server.handler.HandlerWrapper;
33 import org.eclipse.jetty.server.handler.ResourceHandler;
34 import org.eclipse.jetty.servlet.ServletContextHandler;
35 import org.eclipse.jetty.servlet.ServletHolder;
36 import org.eclipse.jetty.util.log.Log;
37 import org.eclipse.jetty.util.ssl.SslContextFactory;
38
39 public class Launcher {
40         public static void main(String[] args) throws Exception {
41                 GigiConfig conf = GigiConfig.parse(System.in);
42                 ServerConstants.init(conf.getMainProps());
43
44                 Server s = new Server();
45                 // === SSL HTTP Configuration ===
46                 HttpConfiguration https_config = new HttpConfiguration();
47                 https_config.setSendServerVersion(false);
48                 https_config.setSendXPoweredBy(false);
49
50                 // for client-cert auth
51                 https_config.addCustomizer(new SecureRequestCustomizer());
52
53                 ServerConnector connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(
54                         https_config));
55                 connector.setHost(conf.getMainProps().getProperty("host"));
56                 connector.setPort(Integer.parseInt(conf.getMainProps().getProperty("port")));
57                 s.setConnectors(new Connector[] { connector });
58
59                 HandlerList hl = new HandlerList();
60                 hl.setHandlers(new Handler[] { generateStaticContext(), generateGigiContext(conf.getMainProps()),
61                                 generateAPIContext() });
62                 s.setHandler(hl);
63                 s.start();
64                 if (connector.getPort() <= 1024 && !System.getProperty("os.name").toLowerCase().contains("win")) {
65                         SetUID uid = new SetUID();
66                         if (!uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
67                                 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
68                         }
69                 }
70         }
71
72         private static SslConnectionFactory createConnectionFactory(GigiConfig conf) throws GeneralSecurityException,
73                 IOException {
74                 final SslContextFactory sslContextFactory = generateSSLContextFactory(conf, "www");
75                 final SslContextFactory secureContextFactory = generateSSLContextFactory(conf, "secure");
76                 secureContextFactory.setNeedClientAuth(true);
77                 final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
78                 final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
79                 try {
80                         secureContextFactory.start();
81                         staticContextFactory.start();
82                         apiContextFactory.start();
83                 } catch (Exception e) {
84                         e.printStackTrace();
85                 }
86                 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
87                         @Override
88                         public boolean shouldRestartSSL() {
89                                 return true;
90                         }
91
92                         @Override
93                         public SSLEngine restartSSL(SSLSession sslSession) {
94                                 SSLEngine e2 = null;
95                                 if (sslSession instanceof ExtendedSSLSession) {
96                                         ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
97                                         List<SNIServerName> names = es.getRequestedServerNames();
98                                         for (SNIServerName sniServerName : names) {
99                                                 if (sniServerName instanceof SNIHostName) {
100                                                         SNIHostName host = (SNIHostName) sniServerName;
101                                                         String hostname = host.getAsciiName();
102                                                         if (hostname.equals(ServerConstants.getWwwHostName())) {
103                                                                 e2 = sslContextFactory.newSSLEngine();
104                                                         } else if (hostname.equals(ServerConstants.getStaticHostName())) {
105                                                                 e2 = staticContextFactory.newSSLEngine();
106                                                         } else if (hostname.equals(ServerConstants.getSecureHostName())) {
107                                                                 e2 = secureContextFactory.newSSLEngine();
108                                                         } else if (hostname.equals(ServerConstants.getApiHostName())) {
109                                                                 e2 = apiContextFactory.newSSLEngine();
110                                                         }
111                                                         break;
112                                                 }
113                                         }
114                                 }
115                                 if (e2 == null) {
116                                         e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
117                                 }
118                                 e2.setUseClientMode(false);
119                                 return e2;
120                         }
121                 };
122         }
123
124         private static ContextHandler generateGigiContext(Properties conf) {
125                 final ResourceHandler rh = new ResourceHandler();
126                 rh.setResourceBase("static/www");
127
128                 HandlerWrapper hw = new PolicyRedirector();
129                 hw.setHandler(rh);
130
131                 ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
132                 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
133                 servlet.addServlet(new ServletHolder(new Gigi(conf)), "/*");
134
135                 HandlerList hl = new HandlerList();
136                 hl.setHandlers(new Handler[] { hw, servlet });
137
138                 ContextHandler ch = new ContextHandler();
139                 ch.setVirtualHosts(new String[] { ServerConstants.getWwwHostName(), ServerConstants.getSecureHostName() });
140                 ch.setHandler(hl);
141
142                 return ch;
143         }
144
145         private static Handler generateStaticContext() {
146                 final ResourceHandler rh = new ResourceHandler();
147                 rh.setResourceBase("static/static");
148
149                 ContextHandler ch = new ContextHandler();
150                 ch.setHandler(rh);
151                 ch.setVirtualHosts(new String[] { ServerConstants.getStaticHostName() });
152
153                 return ch;
154         }
155
156         private static Handler generateAPIContext() {
157                 ServletContextHandler sch = new ServletContextHandler();
158
159                 sch.addVirtualHosts(new String[] { ServerConstants.getApiHostName() });
160                 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
161                 return sch;
162         }
163
164         private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias)
165                 throws GeneralSecurityException, IOException {
166                 SslContextFactory scf = new SslContextFactory() {
167
168                         String[] ciphers = null;
169
170                         @Override
171                         public void customize(SSLEngine sslEngine) {
172                                 super.customize(sslEngine);
173
174                                 SSLParameters ssl = sslEngine.getSSLParameters();
175                                 ssl.setUseCipherSuitesOrder(true);
176                                 if (ciphers == null) {
177                                         ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
178                                 }
179
180                                 ssl.setCipherSuites(ciphers);
181                                 sslEngine.setSSLParameters(ssl);
182
183                         }
184
185                 };
186                 scf.setRenegotiationAllowed(false);
187
188                 scf.setProtocol("TLS");
189                 scf.setTrustStore(conf.getTrustStore());
190                 KeyStore privateStore = conf.getPrivateStore();
191                 scf.setKeyStorePassword(conf.getPrivateStorePw());
192                 scf.setKeyStore(privateStore);
193                 scf.setCertAlias(alias);
194                 return scf;
195         }
196 }