View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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(InputStream storeStream, String storePath, String storeType, String storeProvider, String storePassword) throws Exception
33      {
34          KeyStore keystore = null;
35  
36          if (storeStream != null || storePath != null)
37          {
38              InputStream inStream = storeStream;
39              try
40              {
41                  if (inStream == null)
42                  {
43                      inStream = Resource.newResource(storePath).getInputStream();
44                  }
45                  
46                  if (storeProvider != null)
47                  {
48                      keystore = KeyStore.getInstance(storeType, storeProvider);
49                  }
50                  else
51                  {
52                      keystore = KeyStore.getInstance(storeType);
53                  }
54      
55                  keystore.load(inStream, storePassword == null ? null : storePassword.toCharArray());
56              }
57              finally
58              {
59                  if (inStream != null)
60                  {
61                      inStream.close();
62                  }
63              }
64          }
65          
66          return keystore;
67      }
68  
69      /* ------------------------------------------------------------ */
70      public static Collection<? extends CRL> loadCRL(String crlPath) throws Exception
71      {
72          Collection<? extends CRL> crlList = null;
73  
74          if (crlPath != null)
75          {
76              InputStream in = null;
77              try
78              {
79                  in = Resource.newResource(crlPath).getInputStream();
80                  crlList = CertificateFactory.getInstance("X.509").generateCRLs(in);
81              }
82              finally
83              {
84                  if (in != null)
85                  {
86                      in.close();
87                  }
88              }
89          }
90  
91          return crlList;
92      }
93      
94  }