You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitblitSslContextFactory.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2012 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit;
  17. import java.io.File;
  18. import java.security.KeyStore;
  19. import java.security.cert.CRL;
  20. import java.util.Collection;
  21. import javax.net.ssl.TrustManager;
  22. import javax.net.ssl.X509TrustManager;
  23. import org.eclipse.jetty.util.ssl.SslContextFactory;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import com.gitblit.utils.StringUtils;
  27. /**
  28. * Special SSL context factory that configures Gitblit GO and replaces the
  29. * primary trustmanager with a GitblitTrustManager.
  30. *
  31. * @author James Moger
  32. */
  33. public class GitblitSslContextFactory extends SslContextFactory {
  34. private static final Logger logger = LoggerFactory.getLogger(GitblitSslContextFactory.class);
  35. private final File caRevocationList;
  36. public GitblitSslContextFactory(String certAlias, File keyStore, File clientTrustStore,
  37. String storePassword, File caRevocationList) {
  38. super(keyStore.getAbsolutePath());
  39. this.caRevocationList = caRevocationList;
  40. if (!StringUtils.isEmpty(certAlias)) {
  41. logger.info(" certificate alias = " + certAlias);
  42. setCertAlias(certAlias);
  43. }
  44. setKeyStorePassword(storePassword);
  45. setTrustStorePath(clientTrustStore.getAbsolutePath());
  46. setTrustStorePassword(storePassword);
  47. logger.info(" keyStorePath = " + keyStore.getAbsolutePath());
  48. logger.info(" trustStorePath = " + clientTrustStore.getAbsolutePath());
  49. logger.info(" crlPath = " + caRevocationList.getAbsolutePath());
  50. }
  51. @Override
  52. protected TrustManager[] getTrustManagers(KeyStore trustStore, Collection<? extends CRL> crls)
  53. throws Exception {
  54. TrustManager[] managers = super.getTrustManagers(trustStore, crls);
  55. X509TrustManager delegate = (X509TrustManager) managers[0];
  56. GitblitTrustManager root = new GitblitTrustManager(delegate, caRevocationList);
  57. // replace first manager with the GitblitTrustManager
  58. managers[0] = root;
  59. return managers;
  60. }
  61. }