Coverage Report - org.mule.module.pgp.PGPKeyRingImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PGPKeyRingImpl
0%
0/60
0%
0/12
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.pgp;
 8  
 
 9  
 import java.io.InputStream;
 10  
 import java.util.HashMap;
 11  
 import java.util.Iterator;
 12  
 
 13  
 import org.apache.commons.logging.Log;
 14  
 import org.apache.commons.logging.LogFactory;
 15  
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
 16  
 import org.bouncycastle.openpgp.PGPPublicKey;
 17  
 import org.bouncycastle.openpgp.PGPPublicKeyRing;
 18  
 import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
 19  
 import org.bouncycastle.openpgp.PGPSecretKey;
 20  
 import org.bouncycastle.openpgp.PGPSecretKeyRing;
 21  
 import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
 22  
 import org.mule.api.lifecycle.Initialisable;
 23  
 import org.mule.api.lifecycle.InitialisationException;
 24  
 import org.mule.config.i18n.CoreMessages;
 25  
 import org.mule.module.pgp.i18n.PGPMessages;
 26  
 import org.mule.util.IOUtils;
 27  
 
 28  0
 public class PGPKeyRingImpl implements PGPKeyRing, Initialisable
 29  
 {
 30  0
     protected static final Log logger = LogFactory.getLog(PGPKeyRingImpl.class);
 31  
 
 32  
     private String publicKeyRingFileName;
 33  
 
 34  
     private HashMap<String, PGPPublicKey> principalsKeyBundleMap;
 35  
 
 36  
     private String secretKeyRingFileName;
 37  
 
 38  
     private String secretAliasId;
 39  
 
 40  
     private PGPSecretKey secretKey;
 41  
 
 42  
     private String secretPassphrase;
 43  
 
 44  
     public void initialise() throws InitialisationException
 45  
     {
 46  
         try
 47  
         {
 48  0
             java.security.Security.addProvider(new BouncyCastleProvider());
 49  
 
 50  0
             principalsKeyBundleMap = new HashMap<String, PGPPublicKey>();
 51  
 
 52  0
             readPublicKeyRing();
 53  0
             readPrivateKeyBundle();
 54  
         }
 55  0
         catch (Exception e)
 56  
         {
 57  0
             logger.error("Error in initialise:" + e.getMessage(), e);
 58  0
             throw new InitialisationException(CoreMessages.failedToCreate("PGPKeyRingImpl"), e, this);
 59  0
         }
 60  0
     }
 61  
 
 62  
     private void readPublicKeyRing() throws Exception
 63  
     {
 64  0
         InputStream in = IOUtils.getResourceAsStream(getPublicKeyRingFileName(), getClass());
 65  0
         PGPPublicKeyRingCollection collection = new PGPPublicKeyRingCollection(in);
 66  0
         in.close();
 67  
 
 68  0
         for (Iterator iterator = collection.getKeyRings(); iterator.hasNext();)
 69  
         {
 70  0
             PGPPublicKeyRing ring = (PGPPublicKeyRing) iterator.next();
 71  0
             String userID = "";
 72  0
             for (Iterator iterator2 = ring.getPublicKeys(); iterator2.hasNext();)
 73  
             {
 74  0
                 PGPPublicKey publicKey = (PGPPublicKey) iterator2.next();
 75  0
                 Iterator userIDs = publicKey.getUserIDs();
 76  0
                 if (userIDs.hasNext())
 77  
                 {
 78  0
                     userID = (String) userIDs.next();
 79  
                 }
 80  0
                 principalsKeyBundleMap.put(userID, publicKey);
 81  0
             }
 82  0
         }
 83  0
     }
 84  
 
 85  
     private void readPrivateKeyBundle() throws Exception
 86  
     {
 87  0
         InputStream in = IOUtils.getResourceAsStream(getSecretKeyRingFileName(), getClass());
 88  0
         PGPSecretKeyRingCollection collection = new PGPSecretKeyRingCollection(in);
 89  0
         in.close();
 90  0
         secretKey = collection.getSecretKey(Long.valueOf(getSecretAliasId()));
 91  
         
 92  0
         if (secretKey == null)
 93  
         {
 94  0
             StringBuffer message = new StringBuffer();
 95  0
             message.append('\n');
 96  0
             Iterator iterator = collection.getKeyRings();
 97  0
             while (iterator.hasNext())
 98  
             {
 99  0
                 PGPSecretKeyRing ring = (PGPSecretKeyRing) iterator.next();
 100  0
                 Iterator secretKeysIterator = ring.getSecretKeys();
 101  0
                 while (secretKeysIterator.hasNext())
 102  
                 {
 103  0
                     PGPSecretKey k = (PGPSecretKey) secretKeysIterator.next();
 104  0
                     message.append("Key: ");
 105  0
                     message.append(k.getKeyID());
 106  0
                     message.append('\n');
 107  0
                 }
 108  0
             }
 109  0
             throw new InitialisationException(PGPMessages.noSecretKeyFoundButAvailable(message.toString()),
 110  
                 this);
 111  
         }
 112  0
     }
 113  
 
 114  
     public String getSecretKeyRingFileName()
 115  
     {
 116  0
         return secretKeyRingFileName;
 117  
     }
 118  
 
 119  
     public void setSecretKeyRingFileName(String value)
 120  
     {
 121  0
         this.secretKeyRingFileName = value;
 122  0
     }
 123  
 
 124  
     public String getSecretAliasId()
 125  
     {
 126  0
         return secretAliasId;
 127  
     }
 128  
 
 129  
     public void setSecretAliasId(String value)
 130  
     {
 131  0
         this.secretAliasId = value;
 132  0
     }
 133  
 
 134  
     public String getSecretPassphrase()
 135  
     {
 136  0
         return secretPassphrase;
 137  
     }
 138  
 
 139  
     public void setSecretPassphrase(String value)
 140  
     {
 141  0
         this.secretPassphrase = value;
 142  0
     }
 143  
 
 144  
     public PGPSecretKey getSecretKey()
 145  
     {
 146  0
         return secretKey;
 147  
     }
 148  
 
 149  
     public String getPublicKeyRingFileName()
 150  
     {
 151  0
         return publicKeyRingFileName;
 152  
     }
 153  
 
 154  
     public void setPublicKeyRingFileName(String value)
 155  
     {
 156  0
         this.publicKeyRingFileName = value;
 157  0
     }
 158  
 
 159  
     public PGPPublicKey getPublicKey(String principalId)
 160  
     {
 161  0
         return principalsKeyBundleMap.get(principalId);
 162  
     }
 163  
 }