001// Copyright (C) 2002 IAIK 002// https://sic.tech/ 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// This source is provided for inspection purposes and recompilation only, 011// unless specified differently in a contract with IAIK. This source has to 012// be kept in strict confidence and must not be disclosed to any third party 013// under any circumstances. Redistribution in source and binary forms, with 014// or without modification, are <not> permitted in any case! 015// 016// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 017// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 018// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 019// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 020// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 021// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 022// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 023// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 024// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 025// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 026// SUCH DAMAGE. 027// 028// $Header: /IAIK-CMS/current/src/demo/smime/basic/SMimeCamelliaDemo.java 12 12.02.25 17:58 Dbratko $ 029// $Revision: 12 $ 030// 031 032package demo.smime.basic; 033 034import iaik.asn1.structures.AlgorithmID; 035import iaik.smime.SMimeBodyPart; 036import iaik.smime.SMimeMultipart; 037 038import java.io.ByteArrayInputStream; 039import java.io.ByteArrayOutputStream; 040import java.io.IOException; 041 042import jakarta.activation.DataHandler; 043import jakarta.activation.FileDataSource; 044import jakarta.mail.Message; 045import jakarta.mail.Multipart; 046import jakarta.mail.Session; 047import jakarta.mail.internet.MimeBodyPart; 048import jakarta.mail.internet.MimeMessage; 049 050import demo.DemoSMimeUtil; 051import demo.DemoUtil; 052import demo.smime.DumpMessage; 053 054/** 055 * This class demonstrates the usage of the IAIK S/MIME implementation with the Camellia 056 * encryption algorithm. It shows how to create encrypted S/MIME messages and how to parse 057 * them and decrypt the content. 058 * <p> 059 * To run this demo the following packages are required: 060 * <ul> 061 * <li> 062 * <code>iaik_cms.jar</code> (IAIK-CMS/SMIME) 063 * </li> 064 * <li> 065 * <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>). 066 * </li> 067 * <li> 068 * <a href="https://jakarta.ee/specifications/mail/" target="_blank">Jakarta</a>/<a href="https://eclipse-ee4j.github.io/angus-mail/" target="_blank">Angus</a> Mail 069 * </li> 070 * <li> 071 * <a href="https://jakarta.ee/specifications/activation/" target="_blank">Jakarta Activation Framework</a> 072 * </li> 073 * </ul> 074 */ 075public class SMimeCamelliaDemo extends SMimeDemo { 076 077 /** 078 * Default constructor. Reads certificates and keys from the demo keystore. 079 */ 080 public SMimeCamelliaDemo() { 081 082 super(); 083 } 084 085 /** 086 * Starts the demo. 087 * 088 * @throws IOException if an I/O related error occurs 089 */ 090 public void start() throws IOException { 091 092 // get the default Session 093 Session session = DemoSMimeUtil.getSession(); 094 095 try { 096 // Create a demo Multipart 097 MimeBodyPart mbp1 = new SMimeBodyPart(); 098 mbp1.setText("This is a Test of the IAIK S/MIME implementation!\n\n"); 099 // attachment 100 MimeBodyPart attachment = new SMimeBodyPart(); 101 attachment.setDataHandler(new DataHandler(new FileDataSource("test.html"))); 102 attachment.setFileName("test.html"); 103 104 Multipart mp = new SMimeMultipart(); 105 mp.addBodyPart(mbp1); 106 mp.addBodyPart(attachment); 107 108 Message msg; // the message to send 109 ByteArrayOutputStream baos = new ByteArrayOutputStream(); // we write to a stream 110 ByteArrayInputStream bais; // we read from a stream 111 112 113 // Create encrypted message using Camellia for content encryption 114 115 msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia128_CBC.clone(), 128); 116 System.out.println("creating encrypted message [Camellia/128]..."); 117 baos.reset(); 118 msg.saveChanges(); 119 msg.writeTo(baos); 120 bais = new ByteArrayInputStream(baos.toByteArray()); 121 msg = new MimeMessage(session, bais); 122 if (PRINT_MESSAGES) { 123 printMessage(msg); 124 } 125 DumpMessage.dumpMsg(msg); 126 127 System.out.println("\n\n*****************************************\n\n"); 128 129 msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia192_CBC.clone(), 192); 130 System.out.println("creating encrypted message [Camellia/192]..."); 131 baos.reset(); 132 msg.saveChanges(); 133 msg.writeTo(baos); 134 bais = new ByteArrayInputStream(baos.toByteArray()); 135 msg = new MimeMessage(session, bais); 136 if (PRINT_MESSAGES) { 137 printMessage(msg); 138 } 139 DumpMessage.dumpMsg(msg); 140 141 System.out.println("\n\n*****************************************\n\n"); 142 143 msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia256_CBC.clone(), 256); 144 System.out.println("creating encrypted message [Camellia/256]..."); 145 baos.reset(); 146 msg.saveChanges(); 147 msg.writeTo(baos); 148 bais = new ByteArrayInputStream(baos.toByteArray()); 149 msg = new MimeMessage(session, bais); 150 if (PRINT_MESSAGES) { 151 printMessage(msg); 152 } 153 DumpMessage.dumpMsg(msg); 154 155 System.out.println("\n\n*****************************************\n\n"); 156 157 158 159 } catch (Exception ex) { 160 ex.printStackTrace(); 161 throw new RuntimeException(ex.toString()); 162 } 163 } 164 165 166 /** 167 * The main method. 168 */ 169 public static void main(String[] argv) throws IOException { 170 171 DemoSMimeUtil.initDemos(); 172 (new SMimeCamelliaDemo()).start(); 173 System.out.println("\nReady!"); 174 DemoUtil.waitKey(); 175 } 176}