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/DemoPkcs11All.java 11 12.02.25 17:58 Dbratko $
059 // $Revision: 11 $
060 //
061
062 package demo;
063
064 import java.io.IOException;
065
066 import demo.keystore.CMSKeyStoreConstants;
067
068 /**
069 * This class runs all CMS/SMIME PKCS#11 demos.
070 * <p>
071 * For running this demo the following packages are required:
072 * <ul>
073 * <li>
074 * <code>iaik_cms.jar</code>
075 * </li>
076 * <li>
077 * <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>).
078 * </li>
079 * <li>
080 * <code>iaikPkcs11Provider.jar</code> (<a href="https://sic.tech/products/core-crypto-toolkits/pkcs11-provider/" target="_blank">IAIK PKCS#11 Provider</a>).
081 * </li>
082 * <li>
083 * <code>iaikPkcs11Wrapper.jar</code> (<a href="https://sic.tech/products/core-crypto-toolkits/pkcs11-wrapper/" target="_blank">IAIK PKCS#11 Wrapper</a>).
084 * </li>
085 * <li>
086 * 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.
087 * </li>
088 * <li>
089 * <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).
090 * </li>
091 * <li>
092 * <code>mail.jar</code> (<a href="http://www.oracle.com/technetwork/java/javamail/index.html" target="_blank">JavaMail API</a>).
093 * </li>
094 * <li>
095 * <code>activation.jar</code> (<a href="http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html" target="_blank">Java Activation Framework</a>; required for JDK versions < 1.6).
096 * </li>
097 * </ul>
098 * <code>iaik_cms.jar</code>, <code>iaik_cms_demo.jar</code>, <code>iaik_jce(full).jar</code>,
099 * <code>iaikPkcs11Wrapper.jar</code> and <code>iaikPkcs11Provider.jar</code> (and
100 * <code>iaik_eccelerate.jar</code>, <code>mail.jar</code>, <code>activation.jar</code>) have
101 * to be put into the classpath, the shared library (<code>pkcs11wrapper.dll</code> or
102 * <code>libpkcs11wrapper.so</code>) has to be in your system library search path or in your VM
103 * library path, e.g. (on Windows, assuming that all jar files are located in a lib sub-directory
104 * and the dll is in a lib/win64 sub-directory):
105 * <pre>
106 * java -Djava.library.path=lib/win64
107 * -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
108 * demo.DemoPkcs11All <pkcs11Module>.dll
109 * </pre>
110 */
111 public class DemoPkcs11All implements CMSKeyStoreConstants {
112
113 /**
114 * Start all tests.
115 *
116 * @param moduleName the name of the PKCS#11 module to be used
117 * @param userPin the user-pin (password) for the TokenKeyStore
118 * (may be <code>null</code> to pou-up a dialog asking for the pin)
119 */
120 public void start(String moduleName, char[] userPin) throws Exception {
121
122 // CMS
123 (new demo.cms.pkcs11.EnvelopedDataStreamDemo(moduleName, userPin)).start();
124 (new demo.cms.pkcs11.ExplicitSignedDataStreamDemo(moduleName, userPin)).start(false);
125 (new demo.cms.pkcs11.ImplicitSignedDataStreamDemo(moduleName, userPin)).start(true);
126 (new demo.cms.pkcs11.ExplicitRSAPssSignedDataStreamDemo(moduleName, userPin)).start(false);
127 (new demo.cms.pkcs11.ImplicitRSAPssSignedDataStreamDemo(moduleName, userPin)).start(true);
128 // S/MIME
129 (new demo.smime.pkcs11.EncryptedMailDemo(moduleName, userPin)).start();
130 (new demo.smime.pkcs11.ExplicitSignedMailDemo(moduleName, userPin)).start();
131 (new demo.smime.pkcs11.ImplicitSignedMailDemo(moduleName, userPin)).start();
132
133
134 }
135
136 /**
137 * Performs all tests.
138 *
139 * @param args the PKCS#11 module name
140 */
141 public static void main(String args[]) throws IOException {
142
143 DemoSMimeUtil.initDemos();
144
145 if (args.length == 0) {
146 System.out.println("Missing pkcs11 module name.\n");
147 printUsage();
148 }
149
150 String moduleName = args[0];
151 char[] userPin = (args.length == 2) ? args[1].toCharArray() : null;
152
153 if (args.length > 2) {
154 System.out.println("Too many arguments.\n");
155 printUsage();
156 }
157
158 DemoSMimeUtil.initDemos();
159
160 try {
161 (new DemoPkcs11All()).start(moduleName, userPin);
162 } catch(Exception ex) {
163 ex.printStackTrace();
164 throw new RuntimeException(ex.toString());
165 }
166 System.out.println("All tests O.K.!!!");
167 DemoUtil.waitKey();
168
169 }
170
171 /**
172 * Print usage information.
173 */
174 private final static void printUsage() {
175 System.out.println("Usage:\n");
176 System.out.println("java DemoPkcs11All <pkcs11 module name> [<user-pin>]\n");
177 System.out.println("e.g.:");
178 System.out.println("java DemoPkcs11All aetpkss1.dll");
179 System.out.println("java DemoPkcs11All aetpkss1.so");
180 DemoUtil.waitKey();
181 System.exit(0);
182 }
183 }