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/SMimeCamelliaDemo.java 12 12.02.25 17:58 Dbratko $
059 // $Revision: 12 $
060 //
061
062 package demo.smime.basic;
063
064 import iaik.asn1.structures.AlgorithmID;
065 import iaik.smime.SMimeBodyPart;
066 import iaik.smime.SMimeMultipart;
067
068 import java.io.ByteArrayInputStream;
069 import java.io.ByteArrayOutputStream;
070 import java.io.IOException;
071
072 import javax.activation.DataHandler;
073 import javax.activation.FileDataSource;
074 import javax.mail.Message;
075 import javax.mail.Multipart;
076 import javax.mail.Session;
077 import javax.mail.internet.MimeBodyPart;
078 import javax.mail.internet.MimeMessage;
079
080 import demo.DemoSMimeUtil;
081 import demo.DemoUtil;
082 import demo.smime.DumpMessage;
083
084 /**
085 * This class demonstrates the usage of the IAIK S/MIME implementation with the Camellia
086 * encryption algorithm. It shows how to create encrypted S/MIME messages and how to parse
087 * them and decrypt the content.
088 * <p>
089 * To run this demo the following packages are required:
090 * <ul>
091 * <li>
092 * <code>iaik_cms.jar</code> (IAIK-CMS/SMIME)
093 * </li>
094 * <li>
095 * <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>).
096 * </li>
097 * <li>
098 * <code>mail.jar</code> (<a href="http://www.oracle.com/technetwork/java/javamail/index.html" target="_blank">JavaMail API</a>).
099 * </li>
100 * <li>
101 * <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 < 1.6).
102 * </li>
103 * </ul>
104 */
105 public class SMimeCamelliaDemo extends SMimeDemo {
106
107 /**
108 * Default constructor. Reads certificates and keys from the demo keystore.
109 */
110 public SMimeCamelliaDemo() {
111
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
143 // Create encrypted message using Camellia for content encryption
144
145 msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia128_CBC.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 msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia192_CBC.clone(), 192);
160 System.out.println("creating encrypted message [Camellia/192]...");
161 baos.reset();
162 msg.saveChanges();
163 msg.writeTo(baos);
164 bais = new ByteArrayInputStream(baos.toByteArray());
165 msg = new MimeMessage(session, bais);
166 if (PRINT_MESSAGES) {
167 printMessage(msg);
168 }
169 DumpMessage.dumpMsg(msg);
170
171 System.out.println("\n\n*****************************************\n\n");
172
173 msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia256_CBC.clone(), 256);
174 System.out.println("creating encrypted message [Camellia/256]...");
175 baos.reset();
176 msg.saveChanges();
177 msg.writeTo(baos);
178 bais = new ByteArrayInputStream(baos.toByteArray());
179 msg = new MimeMessage(session, bais);
180 if (PRINT_MESSAGES) {
181 printMessage(msg);
182 }
183 DumpMessage.dumpMsg(msg);
184
185 System.out.println("\n\n*****************************************\n\n");
186
187
188
189 } catch (Exception ex) {
190 ex.printStackTrace();
191 throw new RuntimeException(ex.toString());
192 }
193 }
194
195
196 /**
197 * The main method.
198 */
199 public static void main(String[] argv) throws IOException {
200
201 DemoSMimeUtil.initDemos();
202 (new SMimeCamelliaDemo()).start();
203 System.out.println("\nReady!");
204 DemoUtil.waitKey();
205 }
206 }