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/ImplicitSignedMailDemo.java 18    12.02.25 17:59 Dbratko $
029// $Revision: 18 $
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 (implicit signed,
039 * the content data is included; content type application/pkcs7-mime) 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.ImplicitSignedMailDemo  &lt;pkcs11Module&gt;.dll
082 * </pre>
083
084 */
085public class ImplicitSignedMailDemo extends SignedMailDemo {
086  
087  /**
088   * Creates a ImplicitSignedDataStreamDemo object for the given module name.
089   * 
090   * @param moduleName the name of the module
091   * @param userPin the user-pin (password) for the TokenKeyStore
092   *                (may be <code>null</code> to pou-up a dialog asking for the pin)
093   */
094  public ImplicitSignedMailDemo(String moduleName, char[] userPin) {
095    // install provider in super class    
096    super(moduleName, userPin);
097    System.out.println();
098    System.out.println("******************************************************************************************************************");
099    System.out.println("*                                        PKCS#11  ImplicitSignedMailDemo                                         *");
100    System.out.println("* (shows how to create multipart/signed messages using the IAIK-PKCS11 provider for accessing the key on a card) *");
101    System.out.println("******************************************************************************************************************");
102    System.out.println();
103  }
104
105  /**
106   * Starts the demo.
107   */
108  public void start() {
109    try {
110      getKeyStore();
111      getSignatureKey();
112      start(true);
113    } catch (Throwable ex) {
114      ex.printStackTrace();
115      throw new RuntimeException(ex.toString());
116    }
117  }
118  
119  /**
120   * This is the main method that is called by the JVM during startup.
121   *
122   * @param args These are the command line arguments.
123   */
124  public static void main(String[] args) {
125
126    if (args.length == 0) {
127      System.out.println("Missing pkcs11 module name.\n");
128      printUsage();
129    }
130    
131    String moduleName = args[0];
132    char[] userPin = (args.length == 2) ? args[1].toCharArray() : null;
133    
134    if (args.length > 2) {
135      System.out.println("Too many arguments.\n");
136      printUsage();
137    }
138    
139    DemoSMimeUtil.initDemos();
140    
141    (new ImplicitSignedMailDemo(moduleName, userPin)).start();
142    System.out.println("Ready!");
143    DemoUtil.waitKey();
144  }
145  
146  /**
147   * Print usage information.
148   */
149  private final static void printUsage() {
150    System.out.println("Usage:\n");
151    System.out.println("java ImplicitSignedMailDemo <pkcs11 module name> [<user-pin>]\n");
152    System.out.println("e.g.:");
153    System.out.println("java ImplicitSignedMailDemo aetpkss1.dll");
154    System.out.println("java ImplicitSignedMailDemo aetpkss1.so");
155    DemoUtil.waitKey();
156    System.exit(0);
157  }
158
159
160}