Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: hk/hku/cecid/phoenix/pki/SMIMEDecrypter.java


1   /*
2    * Academic Free License
3    * Version 1.0
4    *
5    * This Academic Free License applies to any software and associated 
6    * documentation (the "Software") whose owner (the "Licensor") has placed the 
7    * statement "Licensed under the Academic Free License Version 1.0" immediately 
8    * after the copyright notice that applies to the Software. 
9    *
10   * Permission is hereby granted, free of charge, to any person obtaining a copy 
11   * of the Software (1) to use, copy, modify, merge, publish, perform, 
12   * distribute, sublicense, and/or sell copies of the Software, and to permit 
13   * persons to whom the Software is furnished to do so, and (2) under patent 
14   * claims owned or controlled by the Licensor that are embodied in the Software 
15   * as furnished by the Licensor, to make, use, sell and offer for sale the 
16   * Software and derivative works thereof, subject to the following conditions: 
17   *
18   * - Redistributions of the Software in source code form must retain all 
19   *   copyright notices in the Software as furnished by the Licensor, this list 
20   *   of conditions, and the following disclaimers. 
21   * - Redistributions of the Software in executable form must reproduce all 
22   *   copyright notices in the Software as furnished by the Licensor, this list 
23   *   of conditions, and the following disclaimers in the documentation and/or 
24   *   other materials provided with the distribution. 
25   * - Neither the names of Licensor, nor the names of any contributors to the 
26   *   Software, nor any of their trademarks or service marks, may be used to 
27   *   endorse or promote products derived from this Software without express 
28   *   prior written permission of the Licensor. 
29   *
30   * DISCLAIMERS: LICENSOR WARRANTS THAT THE COPYRIGHT IN AND TO THE SOFTWARE IS 
31   * OWNED BY THE LICENSOR OR THAT THE SOFTWARE IS DISTRIBUTED BY LICENSOR UNDER 
32   * A VALID CURRENT LICENSE. EXCEPT AS EXPRESSLY STATED IN THE IMMEDIATELY 
33   * PRECEDING SENTENCE, THE SOFTWARE IS PROVIDED BY THE LICENSOR, CONTRIBUTORS 
34   * AND COPYRIGHT OWNERS "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
35   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
36   * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE 
37   * LICENSOR, CONTRIBUTORS OR COPYRIGHT OWNERS BE LIABLE FOR ANY CLAIM, DAMAGES 
38   * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
39   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE. 
40   *
41   * This license is Copyright (C) 2002 Lawrence E. Rosen. All rights reserved. 
42   * Permission is hereby granted to copy and distribute this license without 
43   * modification. This license may not be modified without the express written 
44   * permission of its copyright owner. 
45   */
46  
47  /* ===== 
48   *
49   * $Header: /ebxml/staff/cecid/cvs_repository/pki/src/hk/hku/cecid/phoenix/pki/SMIMEDecrypter.java,v 1.4 2002/12/13 03:59:14 kcyee Exp $
50   *
51   * Code authored by:
52   *
53   * achong [2002-08-02]
54   *
55   * Code reviewed by:
56   *
57   * username [YYYY-MM-DD]
58   *
59   * Remarks:
60   *
61   * =====
62   */
63  
64  package hk.hku.cecid.phoenix.pki;
65  
66  import java.io.*;
67  import java.net.*;
68  import java.util.*;
69  import java.security.*;
70  import java.security.cert.*;
71  
72  import javax.mail.MessagingException;
73  import javax.mail.Session;
74  import javax.mail.internet.MimeMessage;
75  
76  import org.bouncycastle.mail.smime.parsers.SMIMEEncryptedParser;
77  import org.bouncycastle.cms.CMSException;
78  import org.bouncycastle.mail.smime.SMIMEException;
79  
80  /**
81  A class for decrypting MimeBodyPart that was encrypted by SMIME.
82  */
83  
84  public class SMIMEDecrypter extends SMIMEHandler {
85  
86      private static SMIMEDecrypter instance;
87      private CompositeKeyStore compositeKs = new CompositeKeyStore();
88      public final static String SMIME_ENCRYPTED =
89      "application/pkcs7-mime";
90      
91      /**
92      Constructor.
93      */
94      protected SMIMEDecrypter(String keyStorePath, String keyStorePass) {
95          initiate();
96          compositeKs.addKeyStoreFile(keyStorePath , null, keyStorePass.
97          toCharArray());
98      }
99  
100     /**
101     The application should use this method to get a instance of SMIMEDecrypter
102     @param keyStorePath The path of the KeyStore for decryption
103     @param keyStorePass The keystore password
104     */
105     public static SMIMEDecrypter getInstance(String keyStorePath, String 
106     keyStorePass) {
107         if (instance == null) {
108             synchronized(SMIMEDecrypter.class) {
109                 if (instance == null) {
110                     instance = new SMIMEDecrypter(keyStorePath, keyStorePass);
111                 }
112             }
113         }
114         return instance;
115     }
116     
117     /**
118     Decrypts a MimeBodyPart that was encrypted by SMIME.
119     @param alias The alias of the private key for decryption. The private key
120     should be associated with a certificate chain, whose target certificate 
121     contains the corresponding public key.
122     @param keyPass The password for the private key entry
123     @param bodyPart The encrypted MimeBodyPart to be decrypted
124     @throws KeyStoreException if the keystore is corrupted
125     @throws NoSuchAlgorithmException if the keystore cannot be read
126     @throws UnrecoverableKeyException if the keystore cannot be read
127     @throws SMIMEException if the internal SMIME library (BouncyCastle) throws
128     a Exception when decryption. SMIMEException wraps the exception thrown by
129     the internal SMIME library.
130     */
131     public MimeMessage decryptMimeMessage(String alias, String keyPass, 
132     MimeMessage mimeMessage, Session session) throws KeyStoreException,
133     NoSuchAlgorithmException, UnrecoverableKeyException,
134     hk.hku.cecid.phoenix.pki.SMIMEException {
135         PrivateKey privateKey = (PrivateKey) compositeKs.getKey(alias, keyPass.
136         toCharArray());
137         java.security.cert.Certificate [] certs = compositeKs.
138         getCertificateChain(alias);
139         java.security.cert.Certificate cert = certs[0];
140         SMIMEEncryptedParser parser = new SMIMEEncryptedParser(session);
141         try {
142             return (MimeMessage) parser.decrypt(mimeMessage,
143                 (java.security.cert.X509Certificate) cert, privateKey);
144         }
145         catch(IOException e) {
146             throw new hk.hku.cecid.phoenix.pki.SMIMEException("Cannot decrypt" +
147             " MimeMessage", e);
148         }
149         catch(MessagingException e) {
150             throw new hk.hku.cecid.phoenix.pki.SMIMEException("Cannot decrypt" +
151             " MimeMessage", e);
152         }
153         catch(GeneralSecurityException e) {
154             throw new hk.hku.cecid.phoenix.pki.SMIMEException("Cannot decrypt" +
155             " MimeMessage", e);
156         }
157         catch(CMSException e) {
158             throw new hk.hku.cecid.phoenix.pki.SMIMEException("Cannot decrypt" +
159             " MimeMessage", e);
160         }
161         catch(org.bouncycastle.mail.smime.SMIMEException e) {
162             throw new hk.hku.cecid.phoenix.pki.SMIMEException("Cannot decrypt" +
163             " MimeMessage", e);
164         }
165     }
166     
167     /**
168     It returns whether the MimeBodyPart is encrypted by SMIME. Note that it 
169     only checks whether the content type string starts with 
170     "application/pkcs7-mime", it does not tell whether the MimeBodyPart can 
171     be correctly decrypted.
172     @param bodyPart The MimeBodyPart to be tested
173     @throws MessagingException thrown by MimeBodyPart.getContentType(). The
174     Javamail 's API does not tell us on what condition we get this exception
175     */
176     public boolean isSMIMEEncrypted(MimeMessage mimeMessage) throws 
177     MessagingException {
178         if (mimeMessage.getContentType().toLowerCase().startsWith(
179         SMIME_ENCRYPTED)) {
180             return true;
181         }
182         else {
183             return false;
184         }
185     }
186 }