View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util.security;
20  
21  import java.io.InputStream;
22  import java.security.KeyStore;
23  import java.security.cert.CRL;
24  import java.security.cert.CertificateFactory;
25  import java.util.Collection;
26  
27  import org.eclipse.jetty.util.resource.Resource;
28  
29  public class CertificateUtils
30  {
31      /* ------------------------------------------------------------ */
32      public static KeyStore getKeyStore(Resource store, String storeType, String storeProvider, String storePassword) throws Exception
33      {
34          KeyStore keystore = null;
35  
36          if (store != null)
37          {
38              if (storeProvider != null)
39              {
40                  keystore = KeyStore.getInstance(storeType, storeProvider);
41              }
42              else
43              {
44                  keystore = KeyStore.getInstance(storeType);
45              }
46              
47              if (!store.exists())
48                  throw new IllegalStateException("no valid keystore");
49              
50              try (InputStream inStream = store.getInputStream())
51              {
52                  keystore.load(inStream, storePassword == null ? null : storePassword.toCharArray());
53              }
54          }
55          
56          return keystore;
57      }
58  
59      /* ------------------------------------------------------------ */
60      public static Collection<? extends CRL> loadCRL(String crlPath) throws Exception
61      {
62          Collection<? extends CRL> crlList = null;
63  
64          if (crlPath != null)
65          {
66              InputStream in = null;
67              try
68              {
69                  in = Resource.newResource(crlPath).getInputStream();
70                  crlList = CertificateFactory.getInstance("X.509").generateCRLs(in);
71              }
72              finally
73              {
74                  if (in != null)
75                  {
76                      in.close();
77                  }
78              }
79          }
80  
81          return crlList;
82      }
83      
84  }