Coverage Report - org.openpermis.cert.KeyStoreReader
 
Classes in this File Line Coverage Branch Coverage Complexity
KeyStoreReader
85%
12/14
N/A
1.667
 
 1  
 /*
 2  
  * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport
 3  
  *                     (http://www.vbs.admin.ch)
 4  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 5  
  * All rights reserved.
 6  
  *
 7  
  * Licensed under the Open Permis License which accompanies this distribution,
 8  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 9  
  */
 10  
 package org.openpermis.cert;
 11  
 
 12  
 import java.io.InputStream;
 13  
 import java.security.KeyStore;
 14  
 import java.security.PrivateKey;
 15  
 import java.security.cert.X509Certificate;
 16  
 import java.util.Enumeration;
 17  
 
 18  
 
 19  
 /**
 20  
  * Reader for the first entry in a key store.
 21  
  * @since 0.3.0
 22  
  */
 23  
 public class KeyStoreReader {
 24  
         
 25  
         //---- State
 26  
         
 27  
         private final PrivateKey firstPrivateKey;
 28  
         
 29  
         private final X509Certificate firstX509Certificate;
 30  
         
 31  
         //---- Constructors
 32  
         
 33  
         /**
 34  
          * Creates a {@link KeyStoreReader} that reads the first key in a key store, protected by a 
 35  
          * password. The key store and the first entry must be both protected by the same password.
 36  
          * @param keyStoreIn a {@link InputStream}.
 37  
          * @param password the password.
 38  
          * @throws KeyStoreReaderException if there is a problem reading the key store, if required,
 39  
          * the detail exception can be found as the cause of the reader exception.
 40  
          * @since 0.3.0
 41  
          */
 42  
         public KeyStoreReader (
 43  
                 InputStream keyStoreIn, char[] password
 44  
         )
 45  
                 throws KeyStoreReaderException
 46  1
         {
 47  
                 try {
 48  1
                         final KeyStore ks = KeyStore.getInstance("PKCS12");
 49  1
                         ks.load(keyStoreIn, password);
 50  
         
 51  1
                         final Enumeration<String> aliases = ks.aliases();
 52  1
                         final String firstAlias = aliases.nextElement();
 53  
                         
 54  
                         // Get private key.
 55  1
                         final KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) 
 56  
                         ks.getEntry(firstAlias, new KeyStore.PasswordProtection(password));
 57  1
                         this.firstPrivateKey = pkEntry.getPrivateKey();
 58  
                 
 59  
                         // Get certificate.
 60  1
                         this.firstX509Certificate = (X509Certificate) ks.getCertificate(firstAlias);
 61  0
                 } catch (Exception e) {
 62  0
                         throw new KeyStoreReaderException(e);
 63  1
                 }
 64  1
         }
 65  
         
 66  
         //---- Methods
 67  
         
 68  
         /**
 69  
          * Returns the first {@link PrivateKey} in this key store.
 70  
          * @return the first {@link PrivateKey} in this key store.
 71  
          * @since 0.3.0
 72  
          */
 73  
         public PrivateKey getFirstPrivateKey () {
 74  1
                 return this.firstPrivateKey;
 75  
         }
 76  
 
 77  
         /**
 78  
          * Returns the first {@link X509Certificate} in this key store.
 79  
          * @return the first {@link X509Certificate} in this key store.
 80  
          * @since 0.3.0
 81  
          */
 82  
         public X509Certificate getFirstX509Certificate () {
 83  1
                 return this.firstX509Certificate;
 84  
         }
 85  
         
 86  
 
 87  
 }