View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers.truststore;
12  
13  import jakarta.servlet.http.HttpServletRequest;
14  import jakarta.servlet.http.HttpServletResponse;
15  
16  import java.io.IOException;
17  import java.io.InputStream;
18  import java.nio.file.Files;
19  import java.nio.file.Paths;
20  import java.security.KeyStore;
21  import java.security.KeyStoreException;
22  import java.security.NoSuchAlgorithmException;
23  import java.security.cert.CertificateException;
24  import java.security.cert.X509Certificate;
25  import java.text.SimpleDateFormat;
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.springframework.beans.factory.annotation.Value;
35  import org.springframework.stereotype.Controller;
36  import org.springframework.web.bind.annotation.RequestMapping;
37  import org.springframework.web.servlet.ModelAndView;
38  
39  import psiprobe.controllers.AbstractTomcatContainerController;
40  
41  /**
42   * The Class TrustStoreController.
43   */
44  @Controller
45  public class TrustStoreController extends AbstractTomcatContainerController {
46  
47    /** The Constant logger. */
48    private static final Logger logger = LoggerFactory.getLogger(TrustStoreController.class);
49  
50    @RequestMapping(path = "/truststore.htm")
51    @Override
52    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
53        throws Exception {
54      return super.handleRequest(request, response);
55    }
56  
57    @Override
58    protected ModelAndView handleRequestInternal(HttpServletRequest request,
59        HttpServletResponse response) throws Exception {
60      List<Map<String, String>> certificateList = new ArrayList<>();
61      try {
62        String trustStoreType = System.getProperty("javax.net.ssl.trustStoreType");
63        KeyStore ks;
64        if (trustStoreType != null) {
65          ks = KeyStore.getInstance(trustStoreType);
66        } else {
67          ks = KeyStore.getInstance("JKS");
68        }
69        String trustStore = System.getProperty("javax.net.ssl.trustStore");
70        String trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
71        if (trustStore != null) {
72          try (InputStream fis = Files.newInputStream(Paths.get(trustStore))) {
73            ks.load(fis, trustStorePassword != null ? trustStorePassword.toCharArray() : null);
74          } catch (NoSuchAlgorithmException | CertificateException | IOException e) {
75            logger.error("", e);
76          }
77          Map<String, String> attributes;
78          for (String alias : Collections.list(ks.aliases())) {
79            attributes = new HashMap<>();
80            if ("X.509".equals(ks.getCertificate(alias).getType())) {
81              X509Certificate cert = (X509Certificate) ks.getCertificate(alias);
82  
83              attributes.put("alias", alias);
84              attributes.put("cn", cert.getSubjectX500Principal().toString());
85              attributes.put("expirationDate",
86                  new SimpleDateFormat("yyyy-MM-dd").format(cert.getNotAfter()));
87              certificateList.add(attributes);
88            }
89          }
90        }
91      } catch (KeyStoreException e) {
92        logger.error("There was an exception obtaining truststore: ", e);
93      }
94      ModelAndView mv = new ModelAndView(getViewName());
95      mv.addObject("certificates", certificateList);
96      return mv;
97    }
98  
99    @Value("truststore")
100   @Override
101   public void setViewName(String viewName) {
102     super.setViewName(viewName);
103   }
104 
105 }