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/DemoPkcs11All.java 11    12.02.25 17:58 Dbratko $
029// $Revision: 11 $
030//
031
032package demo;
033
034import java.io.IOException;
035
036import demo.keystore.CMSKeyStoreConstants;
037
038/**
039 * This class runs all  CMS/SMIME PKCS#11 demos.
040 * <p>
041 * For running this demo the following packages are required:
042 * <ul>
043 *    <li>
044 *       <code>iaik_cms.jar</code>
045 *    </li>
046 *    <li>
047 *       <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>).
048 *    </li>
049 *    <li>
050 *       <code>iaikPkcs11Provider.jar</code> (<a href="https://sic.tech/products/core-crypto-toolkits/pkcs11-provider/" target="_blank">IAIK PKCS#11 Provider</a>).
051 *    </li>
052 *    <li>
053 *       <code>iaikPkcs11Wrapper.jar</code> (<a href="https://sic.tech/products/core-crypto-toolkits/pkcs11-wrapper/" target="_blank">IAIK PKCS#11 Wrapper</a>).
054 *    </li>
055 *    <li>
056 *       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.
057 *    </li>
058 *    <li>
059 *       <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).
060 *    </li>   
061 *    <li>
062 *       <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
063 *    </li>   
064 *    <li>
065 *       <a href="https://jakarta.ee/specifications/activation/" target="_blank">Jakarta Activation Framework</a>
066 *    </li> 
067 * </ul>
068 * <code>iaik_cms.jar</code>, <code>iaik_cms_demo.jar</code>, <code>iaik_jce(full).jar</code>,
069 * <code>iaikPkcs11Wrapper.jar</code> and <code>iaikPkcs11Provider.jar</code> (and 
070 * <code>iaik_eccelerate.jar</code>, <code>mail.jar</code>, <code>activation.jar</code>) have 
071 * to be put into the classpath, the shared library (<code>pkcs11wrapper.dll</code> or 
072 * <code>libpkcs11wrapper.so</code>) has to be in your system library search path or in your VM
073 * library path, e.g. (on Windows, assuming that all jar files are located in a lib sub-directory
074 * and the dll is in a lib/win64 sub-directory):
075 * <pre>
076 * java -Djava.library.path=lib/win64 
077 *      -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
078 *      demo.DemoPkcs11All &lt;pkcs11Module&gt;.dll
079 * </pre>
080 */
081public class DemoPkcs11All implements CMSKeyStoreConstants {
082
083  /**
084   * Start all tests.
085   * 
086   * @param moduleName the name of the PKCS#11 module to be used
087   * @param userPin the user-pin (password) for the TokenKeyStore
088   *                (may be <code>null</code> to pou-up a dialog asking for the pin)
089   */
090  public void start(String moduleName, char[] userPin) throws Exception {
091    
092    // CMS
093    (new demo.cms.pkcs11.EnvelopedDataStreamDemo(moduleName, userPin)).start();
094    (new demo.cms.pkcs11.ExplicitSignedDataStreamDemo(moduleName, userPin)).start(false);
095    (new demo.cms.pkcs11.ImplicitSignedDataStreamDemo(moduleName, userPin)).start(true);
096    (new demo.cms.pkcs11.ExplicitRSAPssSignedDataStreamDemo(moduleName, userPin)).start(false);
097    (new demo.cms.pkcs11.ImplicitRSAPssSignedDataStreamDemo(moduleName, userPin)).start(true);
098    // S/MIME
099    (new demo.smime.pkcs11.EncryptedMailDemo(moduleName, userPin)).start();
100    (new demo.smime.pkcs11.ExplicitSignedMailDemo(moduleName, userPin)).start();
101    (new demo.smime.pkcs11.ImplicitSignedMailDemo(moduleName, userPin)).start();
102    
103    
104  }
105
106  /**
107   * Performs all tests.
108   * 
109   * @param args the PKCS#11 module name
110   */
111  public static void main(String args[]) throws IOException {
112
113    DemoSMimeUtil.initDemos();
114    
115    if (args.length == 0) {
116      System.out.println("Missing pkcs11 module name.\n");
117      printUsage();
118    }
119    
120    String moduleName = args[0];
121    char[] userPin = (args.length == 2) ? args[1].toCharArray() : null;
122    
123    if (args.length > 2) {
124      System.out.println("Too many arguments.\n");
125      printUsage();
126    }
127    
128    DemoSMimeUtil.initDemos();
129
130    try {
131      (new DemoPkcs11All()).start(moduleName, userPin);
132    } catch(Exception ex) {
133      ex.printStackTrace();
134      throw new RuntimeException(ex.toString());      
135    }
136    System.out.println("All tests O.K.!!!");
137    DemoUtil.waitKey();
138    
139  }
140  
141  /**
142   * Print usage information.
143   */
144  private final static void printUsage() {
145    System.out.println("Usage:\n");
146    System.out.println("java DemoPkcs11All <pkcs11 module name> [<user-pin>]\n");
147    System.out.println("e.g.:");
148    System.out.println("java DemoPkcs11All aetpkss1.dll");
149    System.out.println("java DemoPkcs11All aetpkss1.so");
150    DemoUtil.waitKey();
151    System.exit(0);
152  }
153}