View Javadoc

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      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          super();
49      }
50  
51      public String getSecretKeyRingFileName()
52      {
53          return secretKeyRingFileName;
54      }
55  
56      public void setSecretKeyRingFileName(String value)
57      {
58          this.secretKeyRingFileName = value;
59      }
60  
61      public String getSecretAliasId()
62      {
63          return secretAliasId;
64      }
65  
66      public void setSecretAliasId(String value)
67      {
68          this.secretAliasId = value;
69      }
70  
71      public String getSecretPassphrase()
72      {
73          return secretPassphrase;
74      }
75  
76      public void setSecretPassphrase(String value)
77      {
78          this.secretPassphrase = value;
79      }
80  
81      private void readPrivateKeyBundle() throws Exception
82      {
83          InputStream in = IOUtils.getResourceAsStream(secretKeyRingFileName, getClass());
84  
85          ExtendedKeyStore ring = (ExtendedKeyStore) ExtendedKeyStore.getInstance("OpenPGP/KeyRing");
86          ring.load(in, null);
87  
88          in.close();
89  
90          secretKeyBundle = ring.getKeyBundle(secretAliasId);
91      }
92  
93      public KeyBundle getSecretKeyBundle()
94      {
95          return secretKeyBundle;
96      }
97  
98      /** @return  */
99      public String getPublicKeyRingFileName()
100     {
101         return publicKeyRingFileName;
102     }
103 
104     /** @param value  */
105     public void setPublicKeyRingFileName(String value)
106     {
107         this.publicKeyRingFileName = value;
108     }
109 
110     public KeyBundle getKeyBundle(String principalId)
111     {
112         return (KeyBundle) principalsKeyBundleMap.get(principalId);
113     }
114 
115     public void initialise() throws InitialisationException
116     {
117         try
118         {
119             java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto());
120             java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP());
121 
122             principalsKeyBundleMap = new HashMap();
123 
124             readPublicKeyRing();
125             readPrivateKeyBundle();
126         }
127         catch (Exception e)
128         {
129             logger.error("errore in inizializzazione:" + e.getMessage(), e);
130             throw new InitialisationException(CoreMessages.failedToCreate("PGPKeyRingImpl"), e, this);
131         }
132     }
133 
134     private void readPublicKeyRing() throws Exception
135     {
136         logger.debug(System.getProperties().get("user.dir"));
137         InputStream in = IOUtils.getResourceAsStream(publicKeyRingFileName, getClass());
138 
139         ExtendedKeyStore ring = (ExtendedKeyStore) ExtendedKeyStore.getInstance("OpenPGP/KeyRing");
140         ring.load(in, null);
141         in.close();
142 
143         for (Enumeration e = ring.aliases(); e.hasMoreElements();)
144         {
145             String aliasId = (String) e.nextElement();
146 
147             KeyBundle bundle = ring.getKeyBundle(aliasId);
148 
149             if (bundle != null)
150             {
151                 for (Iterator users = bundle.getPrincipals(); users.hasNext();)
152                 {
153                     Principal princ = (Principal) users.next();
154 
155                     principalsKeyBundleMap.put(princ.getName(), bundle);
156                 }
157             }
158         }
159     }
160 }