View Javadoc

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