View Javadoc

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