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/DemoSMimeUtil.java 8 12.02.25 17:58 Dbratko $ 059 // $Revision: 8 $ 060 // 061 062 package demo; 063 064 import java.util.Properties; 065 066 import javax.activation.CommandMap; 067 import javax.activation.MailcapCommandMap; 068 import javax.mail.Session; 069 070 071 /** 072 * Some basic utility methods used by the S/MIME demos. 073 */ 074 public class DemoSMimeUtil extends DemoUtil { 075 076 /** 077 * Mail session. 078 */ 079 private static Session session_; 080 081 /** 082 * Default constructor. 083 */ 084 DemoSMimeUtil() { 085 // empty 086 } 087 088 /** Perform a some initial setup to allow the demos to work */ 089 public synchronized static void initDemos() { 090 initDemos(true); 091 } 092 093 /** 094 * Perform a some initial setup to allow the demos to work 095 * 096 * @param quickStart whether to init the random generator with a 097 * (not strong) seed for quick start (ONLY FOR 098 * DEMO PURPOSES; NOT FOR PRODUCTION ENVIRONMENT!) 099 */ 100 public synchronized static void initDemos(boolean quickStart) { 101 102 if( initialized_ ) { 103 return; 104 } 105 initialized_ = true; 106 for( int i=0; i<GREETING.length; i++ ) { 107 System.out.println(GREETING[i]); 108 } 109 110 /* 111 * Before installing the IAIK provider load classes from activation.jar and mail.jar 112 * to avoid problems due to a bug in the jar file verification mechanism of 113 * some JDKs (some version of activation.jar and mail.jar may be signed which 114 * may cause a CastException during jar file verification by some buggy JDKs). 115 */ 116 registerMailCapEntries(); 117 118 // create Session object 119 session_ = getSession(); 120 121 initRandom(quickStart); 122 addIaikProvider(); 123 } 124 125 /** 126 * Registers the IAIK content handlers for the mailcap command map. 127 */ 128 public static void registerMailCapEntries() { 129 MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap(); 130 mc.addMailcap("multipart/signed;; x-java-content-handler=iaik.smime.signed_content"); 131 mc.addMailcap("application/x-pkcs7-signature;; x-java-content-handler=iaik.smime.signed_content"); 132 mc.addMailcap("application/x-pkcs7-mime;; x-java-content-handler=iaik.smime.encrypted_content"); 133 mc.addMailcap("application/pkcs7-signature;; x-java-content-handler=iaik.smime.signed_content"); 134 mc.addMailcap("application/pkcs7-mime;; x-java-content-handler=iaik.smime.encrypted_content"); 135 mc.addMailcap("application/x-pkcs10;; x-java-content-handler=iaik.smime.pkcs10_content"); 136 mc.addMailcap("application/pkcs10;; x-java-content-handler=iaik.smime.pkcs10_content"); 137 CommandMap.setDefaultCommandMap(mc); 138 } 139 140 /** 141 * Adds the IAIK S/MIME content handlers to the given MailcapCommandMap. 142 * 143 * @param mc the MailcapCommandMap to which to add the content handlers 144 * 145 * @return the MailcapCommandMap to which the content handlers have been added 146 */ 147 public static CommandMap addContentHandlers(MailcapCommandMap mc) { 148 mc.addMailcap("multipart/signed;; x-java-content-handler=iaik.smime.signed_content"); 149 mc.addMailcap("application/x-pkcs7-signature;; x-java-content-handler=iaik.smime.signed_content"); 150 mc.addMailcap("application/x-pkcs7-mime;; x-java-content-handler=iaik.smime.encrypted_content"); 151 mc.addMailcap("application/pkcs7-signature;; x-java-content-handler=iaik.smime.signed_content"); 152 mc.addMailcap("application/pkcs7-mime;; x-java-content-handler=iaik.smime.encrypted_content"); 153 mc.addMailcap("application/x-pkcs10;; x-java-content-handler=iaik.smime.pkcs10_content"); 154 mc.addMailcap("application/pkcs10;; x-java-content-handler=iaik.smime.pkcs10_content"); 155 return mc; 156 } 157 158 159 /** 160 * Returns (the default) mail session object. 161 * 162 * @return (the default) session object. 163 */ 164 public static Session getSession() { 165 if (session_ == null) { 166 // create some properties and get the default Session 167 Properties props = new Properties(); 168 props.put("mail.smtp.host", "mailhost"); 169 //props.put("mail.debug", "true"); 170 session_ = Session.getDefaultInstance(props, null); 171 } 172 return session_; 173 } 174 175 /** 176 * Returns a mail session object for the given mailhost. 177 * 178 * @param mailhost the mailhost to be used 179 * 180 * @return a mail session object for the given mailhost. 181 */ 182 public static Session getSession(String mailhost) { 183 // create some properties and get the default Session 184 Properties props = new Properties(); 185 props.put("mail.smtp.host", mailhost); 186 //props.put("mail.debug", "true"); 187 Session session = Session.getInstance(props, null); 188 return session; 189 } 190 191 192 193 }