View Javadoc

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