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/SMimeV3CamelliaDemo.java 10 12.02.25 17:58 Dbratko $ 029// $Revision: 10 $ 030// 031 032package demo.smime.basic; 033 034import iaik.asn1.structures.AlgorithmID; 035import iaik.cms.CMSAlgorithmID; 036import iaik.smime.SMimeBodyPart; 037import iaik.smime.SMimeMultipart; 038 039import java.io.ByteArrayInputStream; 040import java.io.ByteArrayOutputStream; 041import java.io.IOException; 042 043import jakarta.activation.DataHandler; 044import jakarta.activation.FileDataSource; 045import jakarta.mail.Message; 046import jakarta.mail.Multipart; 047import jakarta.mail.Session; 048import jakarta.mail.internet.MimeBodyPart; 049import jakarta.mail.internet.MimeMessage; 050 051import demo.DemoSMimeUtil; 052import demo.DemoUtil; 053import demo.smime.DumpMessage; 054 055/** 056 * This class demonstrates the usage of the IAIK S/MIME implementation with the Camellia 057 * encryption algorithm. It shows how to create encrypted S/MIMEv3 messages and how to parse 058 * them and decrypt the content. 059 * <p> 060 * To run this demo the following packages are required: 061 * <ul> 062 * <li> 063 * <code>iaik_cms.jar</code> (IAIK-CMS/SMIME) 064 * </li> 065 * <li> 066 * <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>). 067 * </li> 068 * <li> 069 * <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 070 * </li> 071 * <li> 072 * <a href="https://jakarta.ee/specifications/activation/" target="_blank">Jakarta Activation Framework</a> 073 * </li> 074 * </ul> 075 */ 076public class SMimeV3CamelliaDemo extends SMimeV3Demo { 077 078 /** 079 * Default constructor. Reads certificates and keys from the demo keystore. 080 */ 081 public SMimeV3CamelliaDemo() { 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 // Camellia-128 113 114 msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia128_CBC.clone(), 128, 115 (AlgorithmID)AlgorithmID.esdhKeyAgreement.clone(), (AlgorithmID)CMSAlgorithmID.cms_camellia128_wrap.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 // Camellia-192 130 131 msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia192_CBC.clone(), 192, 132 (AlgorithmID)AlgorithmID.esdhKeyAgreement.clone(), (AlgorithmID)CMSAlgorithmID.cms_camellia192_wrap.clone(), 192); 133 System.out.println("creating encrypted message [Camellia/192]..."); 134 baos.reset(); 135 msg.saveChanges(); 136 msg.writeTo(baos); 137 bais = new ByteArrayInputStream(baos.toByteArray()); 138 msg = new MimeMessage(session, bais); 139 if (PRINT_MESSAGES) { 140 printMessage(msg); 141 } 142 DumpMessage.dumpMsg(msg); 143 144 System.out.println("\n\n*****************************************\n\n"); 145 146 // Camellia-256 147 148 msg = createEncryptedMessage(session, (AlgorithmID)AlgorithmID.camellia256_CBC.clone(), 256, 149 (AlgorithmID)AlgorithmID.esdhKeyAgreement.clone(), (AlgorithmID)CMSAlgorithmID.cms_camellia256_wrap.clone(), 256); 150 System.out.println("creating encrypted message [Camellia/256]..."); 151 baos.reset(); 152 msg.saveChanges(); 153 msg.writeTo(baos); 154 bais = new ByteArrayInputStream(baos.toByteArray()); 155 msg = new MimeMessage(session, bais); 156 if (PRINT_MESSAGES) { 157 printMessage(msg); 158 } 159 DumpMessage.dumpMsg(msg); 160 161 162 } catch (Exception ex) { 163 ex.printStackTrace(); 164 throw new RuntimeException(ex.toString()); 165 } 166 } 167 168 169 /** 170 * The main method. 171 */ 172 public static void main(String[] argv) throws IOException { 173 double iaikProviderVersion = DemoUtil.getIaikProviderVersion(); 174 if (iaikProviderVersion <= 3.18) { 175 System.err.println("This demo requires a IAIK provider version > 3.18! Your IAIK provider version is " + iaikProviderVersion + "."); 176 } else { 177 DemoSMimeUtil.initDemos(); 178 (new SMimeV3CamelliaDemo()).start(); 179 System.out.println("\nReady!"); 180 } 181 DemoUtil.waitKey(); 182 } 183}