View Javadoc

1   /*
2    * $Id: PGPKeyRingImpl.java 20310 2010-11-24 10:40:35Z esteban.robles $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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 java.io.InputStream;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.bouncycastle.jce.provider.BouncyCastleProvider;
25  import org.bouncycastle.openpgp.PGPPublicKey;
26  import org.bouncycastle.openpgp.PGPPublicKeyRing;
27  import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
28  import org.bouncycastle.openpgp.PGPSecretKey;
29  import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
30  
31  public class PGPKeyRingImpl implements PGPKeyRing, Initialisable
32  {
33      protected static final Log logger = LogFactory.getLog(PGPKeyRingImpl.class);
34  
35      private String publicKeyRingFileName;
36  
37      private HashMap<String, PGPPublicKey> principalsKeyBundleMap;
38  
39      private String secretKeyRingFileName;
40  
41      private String secretAliasId;
42  
43      private PGPSecretKey secretKey;
44  
45      private String secretPassphrase;
46  
47      public void initialise() throws InitialisationException
48      {
49          try
50          {
51              java.security.Security.addProvider(new BouncyCastleProvider());
52  
53              principalsKeyBundleMap = new HashMap<String, PGPPublicKey>();
54  
55              readPublicKeyRing();
56              readPrivateKeyBundle();
57          }
58          catch (Exception e)
59          {
60              logger.error("Error in initialise:" + e.getMessage(), e);
61              throw new InitialisationException(CoreMessages.failedToCreate("PGPKeyRingImpl"), e, this);
62          }
63      }
64  
65      private void readPublicKeyRing() throws Exception
66      {
67          InputStream in = IOUtils.getResourceAsStream(getPublicKeyRingFileName(), getClass());
68          PGPPublicKeyRingCollection collection = new PGPPublicKeyRingCollection(in);
69          in.close();
70          
71          for (Iterator iterator = collection.getKeyRings(); iterator.hasNext();)
72          {
73              PGPPublicKeyRing ring = (PGPPublicKeyRing) iterator.next();
74              String userID = "";
75              for (Iterator iterator2 = ring.getPublicKeys(); iterator2.hasNext();)
76              {
77                  PGPPublicKey publicKey = (PGPPublicKey) iterator2.next();
78                  Iterator userIDs = publicKey.getUserIDs();
79                  if (userIDs.hasNext()) {
80                      userID = (String) userIDs.next();
81                  }
82                  principalsKeyBundleMap.put(userID, publicKey);
83              }
84          }
85      }
86  
87      private void readPrivateKeyBundle() throws Exception
88      {
89          InputStream in = IOUtils.getResourceAsStream(getSecretKeyRingFileName(), getClass());
90          PGPSecretKeyRingCollection collection = new PGPSecretKeyRingCollection(in);
91          in.close();
92          secretKey = collection.getSecretKey(Long.valueOf(getSecretAliasId()));
93      }
94  
95      public String getSecretKeyRingFileName()
96      {
97          return secretKeyRingFileName;
98      }
99  
100     public void setSecretKeyRingFileName(String value)
101     {
102         this.secretKeyRingFileName = value;
103     }
104 
105     public String getSecretAliasId()
106     {
107         return secretAliasId;
108     }
109 
110     public void setSecretAliasId(String value)
111     {
112         this.secretAliasId = value;
113     }
114 
115     public String getSecretPassphrase()
116     {
117         return secretPassphrase;
118     }
119 
120     public void setSecretPassphrase(String value)
121     {
122         this.secretPassphrase = value;
123     }
124 
125     public PGPSecretKey getSecretKey()
126     {
127         return secretKey;
128     }
129 
130     public String getPublicKeyRingFileName()
131     {
132         return publicKeyRingFileName;
133     }
134 
135     public void setPublicKeyRingFileName(String value)
136     {
137         this.publicKeyRingFileName = value;
138     }
139 
140     public PGPPublicKey getPublicKey(String principalId)
141     {
142         return principalsKeyBundleMap.get(principalId);
143     }
144 }