]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Launcher.java
Implement testing of internal certificate issuing (and login with it)
[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.setWantClientAuth(true);
77                 secureContextFactory.setNeedClientAuth(false);
78                 final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
79                 final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
80                 try {
81                         secureContextFactory.start();
82                         staticContextFactory.start();
83                         apiContextFactory.start();
84                 } catch (Exception e) {
85                         e.printStackTrace();
86                 }
87                 return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
88                         @Override
89                         public boolean shouldRestartSSL() {
90                                 return true;
91                         }
92
93                         @Override
94                         public SSLEngine restartSSL(SSLSession sslSession) {
95                                 SSLEngine e2 = null;
96                                 if (sslSession instanceof ExtendedSSLSession) {
97                                         ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
98                                         List<SNIServerName> names = es.getRequestedServerNames();
99                                         for (SNIServerName sniServerName : names) {
100                                                 if (sniServerName instanceof SNIHostName) {
101                                                         SNIHostName host = (SNIHostName) sniServerName;
102                                                         String hostname = host.getAsciiName();
103                                                         if (hostname.equals(ServerConstants.getWwwHostName())) {
104                                                                 e2 = sslContextFactory.newSSLEngine();
105                                                         } else if (hostname.equals(ServerConstants.getStaticHostName())) {
106                                                                 e2 = staticContextFactory.newSSLEngine();
107                                                         } else if (hostname.equals(ServerConstants.getSecureHostName())) {
108                                                                 e2 = secureContextFactory.newSSLEngine();
109                                                         } else if (hostname.equals(ServerConstants.getApiHostName())) {
110                                                                 e2 = apiContextFactory.newSSLEngine();
111                                                         }
112                                                         break;
113                                                 }
114                                         }
115                                 }
116                                 if (e2 == null) {
117                                         e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
118                                 }
119                                 e2.setUseClientMode(false);
120                                 return e2;
121                         }
122                 };
123         }
124
125         private static ContextHandler generateGigiContext(Properties conf) {
126                 final ResourceHandler rh = new ResourceHandler();
127                 rh.setResourceBase("static/www");
128
129                 HandlerWrapper hw = new PolicyRedirector();
130                 hw.setHandler(rh);
131
132                 ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
133                 servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
134                 servlet.addServlet(new ServletHolder(new Gigi(conf)), "/*");
135
136                 HandlerList hl = new HandlerList();
137                 hl.setHandlers(new Handler[] { hw, servlet });
138
139                 ContextHandler ch = new ContextHandler();
140                 ch.setVirtualHosts(new String[] { ServerConstants.getWwwHostName(), ServerConstants.getSecureHostName() });
141                 ch.setHandler(hl);
142
143                 return ch;
144         }
145
146         private static Handler generateStaticContext() {
147                 final ResourceHandler rh = new ResourceHandler();
148                 rh.setResourceBase("static/static");
149
150                 ContextHandler ch = new ContextHandler();
151                 ch.setHandler(rh);
152                 ch.setVirtualHosts(new String[] { ServerConstants.getStaticHostName() });
153
154                 return ch;
155         }
156
157         private static Handler generateAPIContext() {
158                 ServletContextHandler sch = new ServletContextHandler();
159
160                 sch.addVirtualHosts(new String[] { ServerConstants.getApiHostName() });
161                 sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
162                 return sch;
163         }
164
165         private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias)
166                 throws GeneralSecurityException, IOException {
167                 SslContextFactory scf = new SslContextFactory() {
168
169                         String[] ciphers = null;
170
171                         @Override
172                         public void customize(SSLEngine sslEngine) {
173                                 super.customize(sslEngine);
174
175                                 SSLParameters ssl = sslEngine.getSSLParameters();
176                                 ssl.setUseCipherSuitesOrder(true);
177                                 if (ciphers == null) {
178                                         ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
179                                 }
180
181                                 ssl.setCipherSuites(ciphers);
182                                 sslEngine.setSSLParameters(ssl);
183
184                         }
185
186                 };
187                 scf.setRenegotiationAllowed(false);
188
189                 scf.setProtocol("TLS");
190                 scf.setTrustStore(conf.getTrustStore());
191                 KeyStore privateStore = conf.getPrivateStore();
192                 scf.setKeyStorePassword(conf.getPrivateStorePw());
193                 scf.setKeyStore(privateStore);
194                 scf.setCertAlias(alias);
195                 return scf;
196         }
197 }