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