Coverage Report - org.mule.module.pgp.PGPKeyRingImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PGPKeyRingImpl
85%
41/48
83%
5/6
1.357
 
 1  
 /*
 2  
  * $Id: PGPKeyRingImpl.java 11517 2008-03-31 21:34:19Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.module.pgp;
 12  
 
 13  
 import org.mule.api.lifecycle.Initialisable;
 14  
 import org.mule.api.lifecycle.InitialisationException;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 import org.mule.util.IOUtils;
 17  
 
 18  
 import cryptix.pki.ExtendedKeyStore;
 19  
 import cryptix.pki.KeyBundle;
 20  
 
 21  
 import java.io.InputStream;
 22  
 import java.security.Principal;
 23  
 import java.util.Enumeration;
 24  
 import java.util.HashMap;
 25  
 import java.util.Iterator;
 26  
 
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 
 30  
 public class PGPKeyRingImpl implements PGPKeyRing, Initialisable
 31  
 {
 32  2
     protected static final Log logger = LogFactory.getLog(PGPKeyRingImpl.class);
 33  
 
 34  
     private String publicKeyRingFileName;
 35  
 
 36  
     private HashMap principalsKeyBundleMap;
 37  
 
 38  
     private String secretKeyRingFileName;
 39  
 
 40  
     private String secretAliasId;
 41  
 
 42  
     private KeyBundle secretKeyBundle;
 43  
 
 44  
     private String secretPassphrase;
 45  
 
 46  
     public PGPKeyRingImpl()
 47  
     {
 48  10
         super();
 49  10
     }
 50  
 
 51  
     public String getSecretKeyRingFileName()
 52  
     {
 53  0
         return secretKeyRingFileName;
 54  
     }
 55  
 
 56  
     public void setSecretKeyRingFileName(String value)
 57  
     {
 58  10
         this.secretKeyRingFileName = value;
 59  10
     }
 60  
 
 61  
     public String getSecretAliasId()
 62  
     {
 63  0
         return secretAliasId;
 64  
     }
 65  
 
 66  
     public void setSecretAliasId(String value)
 67  
     {
 68  10
         this.secretAliasId = value;
 69  10
     }
 70  
 
 71  
     public String getSecretPassphrase()
 72  
     {
 73  0
         return secretPassphrase;
 74  
     }
 75  
 
 76  
     public void setSecretPassphrase(String value)
 77  
     {
 78  10
         this.secretPassphrase = value;
 79  10
     }
 80  
 
 81  
     private void readPrivateKeyBundle() throws Exception
 82  
     {
 83  10
         InputStream in = IOUtils.getResourceAsStream(secretKeyRingFileName, getClass());
 84  
 
 85  10
         ExtendedKeyStore ring = (ExtendedKeyStore) ExtendedKeyStore.getInstance("OpenPGP/KeyRing");
 86  10
         ring.load(in, null);
 87  
 
 88  10
         in.close();
 89  
 
 90  10
         secretKeyBundle = ring.getKeyBundle(secretAliasId);
 91  10
     }
 92  
 
 93  
     public KeyBundle getSecretKeyBundle()
 94  
     {
 95  2
         return secretKeyBundle;
 96  
     }
 97  
 
 98  
     /** @return  */
 99  
     public String getPublicKeyRingFileName()
 100  
     {
 101  0
         return publicKeyRingFileName;
 102  
     }
 103  
 
 104  
     /** @param value  */
 105  
     public void setPublicKeyRingFileName(String value)
 106  
     {
 107  10
         this.publicKeyRingFileName = value;
 108  10
     }
 109  
 
 110  
     public KeyBundle getKeyBundle(String principalId)
 111  
     {
 112  4
         return (KeyBundle) principalsKeyBundleMap.get(principalId);
 113  
     }
 114  
 
 115  
     public void initialise() throws InitialisationException
 116  
     {
 117  
         try
 118  
         {
 119  10
             java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto());
 120  10
             java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP());
 121  
 
 122  10
             principalsKeyBundleMap = new HashMap();
 123  
 
 124  10
             readPublicKeyRing();
 125  10
             readPrivateKeyBundle();
 126  
         }
 127  0
         catch (Exception e)
 128  
         {
 129  0
             logger.error("errore in inizializzazione:" + e.getMessage(), e);
 130  0
             throw new InitialisationException(CoreMessages.failedToCreate("PGPKeyRingImpl"), e, this);
 131  10
         }
 132  10
     }
 133  
 
 134  
     private void readPublicKeyRing() throws Exception
 135  
     {
 136  10
         logger.debug(System.getProperties().get("user.dir"));
 137  10
         InputStream in = IOUtils.getResourceAsStream(publicKeyRingFileName, getClass());
 138  
 
 139  10
         ExtendedKeyStore ring = (ExtendedKeyStore) ExtendedKeyStore.getInstance("OpenPGP/KeyRing");
 140  10
         ring.load(in, null);
 141  10
         in.close();
 142  
 
 143  10
         for (Enumeration e = ring.aliases(); e.hasMoreElements();)
 144  
         {
 145  20
             String aliasId = (String) e.nextElement();
 146  
 
 147  20
             KeyBundle bundle = ring.getKeyBundle(aliasId);
 148  
 
 149  20
             if (bundle != null)
 150  
             {
 151  20
                 for (Iterator users = bundle.getPrincipals(); users.hasNext();)
 152  
                 {
 153  20
                     Principal princ = (Principal) users.next();
 154  
 
 155  20
                     principalsKeyBundleMap.put(princ.getName(), bundle);
 156  20
                 }
 157  
             }
 158  20
         }
 159  10
     }
 160  
 }