001    // Copyright (C) 2002 IAIK
002    // https://jce.iaik.tugraz.at
003    //
004    // Copyright (C) 2003 - 2025 Stiftung Secure Information and
005    //                           Communication Technologies SIC
006    // https://sic.tech
007    //
008    // All rights reserved.
009    //
010    // Redistribution and use in source and binary forms, with or without
011    // modification, are permitted provided that the following conditions
012    // are met:
013    // 1. Redistributions of source code must retain the above copyright
014    //    notice, this list of conditions and the following disclaimer.
015    // 2. Redistributions in binary form must reproduce the above copyright
016    //    notice, this list of conditions and the following disclaimer in the
017    //    documentation and/or other materials provided with the distribution.
018    //
019    // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
020    // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021    // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022    // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
023    // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
024    // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
025    // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
026    // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
027    // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
028    // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
029    // SUCH DAMAGE.
030    
031    // Copyright (C) 2002 IAIK
032    // https://sic.tech/
033    //
034    // Copyright (C) 2003 - 2025 Stiftung Secure Information and 
035    //                           Communication Technologies SIC
036    // https://sic.tech/
037    //
038    // All rights reserved.
039    //
040    // This source is provided for inspection purposes and recompilation only,
041    // unless specified differently in a contract with IAIK. This source has to
042    // be kept in strict confidence and must not be disclosed to any third party
043    // under any circumstances. Redistribution in source and binary forms, with
044    // or without modification, are <not> permitted in any case!
045    //
046    // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
047    // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
048    // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
049    // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
050    // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
051    // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
052    // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
053    // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
054    // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
055    // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
056    // SUCH DAMAGE.
057    //
058    // $Header: /IAIK-CMS/current/src/demo/smime/basic/SMimeV3CamelliaDemo.java 10    12.02.25 17:58 Dbratko $
059    // $Revision: 10 $
060    //
061    
062    package demo.smime.basic;
063    
064    import iaik.asn1.structures.AlgorithmID;
065    import iaik.cms.CMSAlgorithmID;
066    import iaik.smime.SMimeBodyPart;
067    import iaik.smime.SMimeMultipart;
068    
069    import java.io.ByteArrayInputStream;
070    import java.io.ByteArrayOutputStream;
071    import java.io.IOException;
072    
073    import javax.activation.DataHandler;
074    import javax.activation.FileDataSource;
075    import javax.mail.Message;
076    import javax.mail.Multipart;
077    import javax.mail.Session;
078    import javax.mail.internet.MimeBodyPart;
079    import javax.mail.internet.MimeMessage;
080    
081    import demo.DemoSMimeUtil;
082    import demo.DemoUtil;
083    import demo.smime.DumpMessage;
084    
085    /**
086     * This class demonstrates the usage of the IAIK S/MIME implementation with the Camellia 
087     * encryption algorithm. It shows how to create encrypted S/MIMEv3 messages and how to parse
088     * them and decrypt the content.
089     * <p>
090     * To run this demo the following packages are required:
091     * <ul>
092     *    <li>
093     *       <code>iaik_cms.jar</code> (IAIK-CMS/SMIME)
094     *    </li>
095     *    <li>
096     *       <code>iaik_jce(_full).jar</code> (<a href="https://sic.tech/products/core-crypto-toolkits/jca-jce/" target="_blank">IAIK-JCE Core Crypto Library</a>).
097     *    </li>
098     *    <li>
099     *       <code>mail.jar</code> (<a href="http://www.oracle.com/technetwork/java/javamail/index.html" target="_blank">JavaMail API</a>).
100     *    </li>   
101     *    <li>
102     *       <code>activation.jar</code> (<a href="http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html" target="_blank">Java Activation Framework</a>; required for JDK versions &lt; 1.6).
103     *    </li> 
104     * </ul>
105     */
106    public class SMimeV3CamelliaDemo extends SMimeV3Demo {
107        
108      /**
109       * Default constructor. Reads certificates and keys from the demo keystore.
110       */
111      public SMimeV3CamelliaDemo() {
112        super();    
113      }
114      
115      /**
116       * Starts the demo.
117       *
118       * @throws IOException if an I/O related error occurs
119       */
120      public void start() throws IOException {
121    
122        // get the default Session
123            Session session = DemoSMimeUtil.getSession();
124    
125            try {
126          // Create a demo Multipart
127          MimeBodyPart mbp1 = new SMimeBodyPart();
128              mbp1.setText("This is a Test of the IAIK S/MIME implementation!\n\n");
129              // attachment
130          MimeBodyPart attachment = new SMimeBodyPart();
131          attachment.setDataHandler(new DataHandler(new FileDataSource("test.html")));
132          attachment.setFileName("test.html");
133    
134          Multipart mp = new SMimeMultipart();
135          mp.addBodyPart(mbp1);
136          mp.addBodyPart(attachment);
137     
138          Message msg;    // the message to send
139          ByteArrayOutputStream baos = new ByteArrayOutputStream(); // we write to a stream
140          ByteArrayInputStream bais;  // we read from a stream
141    
142          // Camellia-128  
143            
144          msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia128_CBC.clone(), 128,
145            (AlgorithmID)AlgorithmID.esdhKeyAgreement.clone(), (AlgorithmID)CMSAlgorithmID.cms_camellia128_wrap.clone(), 128);
146          System.out.println("creating encrypted message [Camellia/128]...");
147          baos.reset();
148          msg.saveChanges();
149          msg.writeTo(baos);
150          bais = new ByteArrayInputStream(baos.toByteArray());
151          msg = new MimeMessage(session, bais);
152          if (PRINT_MESSAGES) {
153            printMessage(msg);
154          }
155          DumpMessage.dumpMsg(msg);
156          
157          System.out.println("\n\n*****************************************\n\n");
158          
159          // Camellia-192
160          
161          msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia192_CBC.clone(), 192,
162            (AlgorithmID)AlgorithmID.esdhKeyAgreement.clone(), (AlgorithmID)CMSAlgorithmID.cms_camellia192_wrap.clone(), 192);
163          System.out.println("creating encrypted message [Camellia/192]...");
164          baos.reset();
165          msg.saveChanges();
166          msg.writeTo(baos);
167          bais = new ByteArrayInputStream(baos.toByteArray());
168          msg = new MimeMessage(session, bais);
169          if (PRINT_MESSAGES) {
170            printMessage(msg);
171          }
172          DumpMessage.dumpMsg(msg);
173            
174          System.out.println("\n\n*****************************************\n\n");
175          
176          // Camellia-256
177            
178          msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia256_CBC.clone(), 256,
179            (AlgorithmID)AlgorithmID.esdhKeyAgreement.clone(), (AlgorithmID)CMSAlgorithmID.cms_camellia256_wrap.clone(), 256);
180          System.out.println("creating encrypted message [Camellia/256]...");
181          baos.reset();
182          msg.saveChanges();
183          msg.writeTo(baos);
184          bais = new ByteArrayInputStream(baos.toByteArray());
185          msg = new MimeMessage(session, bais);
186          if (PRINT_MESSAGES) {
187            printMessage(msg);
188          }
189          DumpMessage.dumpMsg(msg);  
190          
191    
192            } catch (Exception ex) {
193          ex.printStackTrace();
194              throw new RuntimeException(ex.toString());
195            }
196      }
197    
198    
199      /**
200       * The main method.
201       */
202      public static void main(String[] argv) throws IOException {
203        double iaikProviderVersion = DemoUtil.getIaikProviderVersion(); 
204        if (iaikProviderVersion <= 3.18) {
205          System.err.println("This demo requires a IAIK provider version > 3.18! Your IAIK provider version is " + iaikProviderVersion + ".");
206        } else {
207          DemoSMimeUtil.initDemos();
208              (new SMimeV3CamelliaDemo()).start();
209          System.out.println("\nReady!");
210        }  
211        DemoUtil.waitKey();
212      }
213    }