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