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/pkcs11/ExplicitSignedMailDemo.java 17    12.02.25 17:59 Dbratko $
029// $Revision: 17 $
030//
031
032package demo.smime.pkcs11;
033
034import demo.DemoSMimeUtil;
035import demo.DemoUtil;
036
037/**
038 * This class shows how to create a signed message (explicit signed,
039 * the content data is included; content type multipart/signed) according
040 * to S/MIME using the IAIK PKCS#11 provider for accessing the private key
041 * on a smart card. This implementation uses the <code>SecurityProvider</code> 
042 * feature of the IAIK-CMS toolkit.
043 * <p>
044 * To run this demo the following packages are required:
045 * <ul>
046 *    <li>
047 *       <code>iaik_cms.jar</code>
048 *    </li>
049 *    <li>
050 *       <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>).
051 *    </li>
052 *    <li>
053 *       <code>iaikPkcs11Provider.jar</code> (<a href="https://sic.tech/products/core-crypto-toolkits/pkcs11-provider/" target="_blank">IAIK PKCS#11 Provider</a>).
054 *    </li>
055 *    <li>
056 *       <code>iaikPkcs11Wrapper.jar</code> (<a href="https://sic.tech/products/core-crypto-toolkits/pkcs11-wrapper/" target="_blank">IAIK PKCS#11 Wrapper</a>).
057 *    </li>
058 *    <li>
059 *       The shared PKCS#11 library (<code>pkcs11wrapper.dll</code> for Windows, <code>libpkcs11wrapper.so</code> for Unix); contained in the IAIK PKCS#11 Wrapper library.
060 *    </li>
061 *    <li>
062 *       <code>iaik_eccelerate.jar</code> (<a href="https://sic.tech/products/core-crypto-toolkits/eccelerate/" target="_blank">IAIK ECC Library</a>, if you want to use Elliptic Curve Cryptography).
063 *    </li>   
064 *    <li>
065 *       <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
066 *    </li>   
067 *    <li>
068 *       <a href="https://jakarta.ee/specifications/activation/" target="_blank">Jakarta Activation Framework</a>
069 *    </li> 
070 * </ul>
071 * <code>iaik_cms.jar</code>, <code>iaik_cms_demo.jar</code>, <code>iaik_jce(full).jar</code>,
072 * <code>iaikPkcs11Wrapper.jar</code> and <code>iaikPkcs11Provider.jar</code> (and
073 * <code>iaik_eccelerate.jar</code>, <code>mail.jar</code>, <code>activation.jar</code>) have to
074 * be put into the classpath, the shared library (<code>pkcs11wrapper.dll</code> or 
075 * <code>libpkcs11wrapper.so</code>) has to be in your system library search path or in your VM 
076 * library path, e.g. (on Windows, assuming that all jar files are located in a lib sub-directory 
077 * and the dll is in a lib/win64 sub-directory):
078 * <pre>
079 * java -Djava.library.path=lib/win64 
080 *      -cp lib/iaik_jce.jar;lib/iaikPkcs11Wrapper.jar;lib/iaikPkcs11Provider.jar;lib/iaik_cms.jar;lib/iaik_cms_demo.jar;lib/mail.jar;lib/activation.jar
081 *      demo.pkcs11.ExplicitSignedMailDemo  &lt;pkcs11Module&gt;.dll
082 * </pre>
083 */
084public class ExplicitSignedMailDemo extends SignedMailDemo {
085  
086  /**
087   * Creates a ExplicitSignedDataStreamDemo object for the given module name.
088   * 
089   * @param moduleName the name of the module
090   * @param userPin the user-pin (password) for the TokenKeyStore
091   *                (may be <code>null</code> to pou-up a dialog asking for the pin)
092   */
093  public ExplicitSignedMailDemo(String moduleName, char[] userPin) {
094    // install provider in super class    
095    super(moduleName, userPin);
096    System.out.println();
097    System.out.println("***********************************************************************************************************************");
098    System.out.println("*                                        PKCS#11  ExplicitSignedMailDemo                                              *");
099    System.out.println("* (shows how to create application/pkcs7mime messages using the IAIK-PKCS11 provider for accessing the key on a card) *");
100    System.out.println("***********************************************************************************************************************");
101    System.out.println();
102  }
103
104  /**
105   * Starts the demo.
106   */
107  public void start() {
108    try {
109      getKeyStore();
110      getSignatureKey();
111      start(false);
112    } catch (Throwable ex) {
113      ex.printStackTrace();
114      throw new RuntimeException(ex.toString());
115    }
116  }
117  
118  /**
119   * This is the main method that is called by the JVM during startup.
120   *
121   * @param args These are the command line arguments.
122   */
123  public static void main(String[] args) {
124    
125    if (args.length == 0) {
126      System.out.println("Missing pkcs11 module name.\n");
127      printUsage();
128    }
129    
130    String moduleName = args[0];
131    char[] userPin = (args.length == 2) ? args[1].toCharArray() : null;
132    
133    if (args.length > 2) {
134      System.out.println("Too many arguments.\n");
135      printUsage();
136    }
137    
138    DemoSMimeUtil.initDemos();
139    
140    (new ExplicitSignedMailDemo(moduleName, userPin)).start();
141    System.out.println("Ready!");
142    DemoUtil.waitKey();
143  }
144  
145  /**
146   * Print usage information.
147   */
148  private final static void printUsage() {
149    System.out.println("Usage:\n");
150    System.out.println("java ExplicitSignedMailDemo <pkcs11 module name> [<user-pin>]\n");
151    System.out.println("e.g.:");
152    System.out.println("java ExplicitSignedMailDemo aetpkss1.dll");
153    System.out.println("java ExplicitSignedMailDemo aetpkss1.so");
154    DemoUtil.waitKey();
155    System.exit(0);
156  }
157
158
159}