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/smime/pkcs11/ImplicitSignedMailDemo.java 18 12.02.25 17:59 Dbratko $
059 // $Revision: 18 $
060 //
061
062 package demo.smime.pkcs11;
063
064 import demo.DemoSMimeUtil;
065 import demo.DemoUtil;
066
067 /**
068 * This class shows how to create a signed message (implicit signed,
069 * the content data is included; content type application/pkcs7-mime) according
070 * to S/MIME using the IAIK PKCS#11 provider for accessing the private key
071 * on a smart card. This implementation uses the <code>SecurityProvider</code>
072 * feature of the IAIK-CMS toolkit.
073 * <p>
074 * To run this demo the following packages are required:
075 * <ul>
076 * <li>
077 * <code>iaik_cms.jar</code>
078 * </li>
079 * <li>
080 * <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>).
081 * </li>
082 * <li>
083 * <code>iaikPkcs11Provider.jar</code> (<a href="https://sic.tech/products/core-crypto-toolkits/pkcs11-provider/" target="_blank">IAIK PKCS#11 Provider</a>).
084 * </li>
085 * <li>
086 * <code>iaikPkcs11Wrapper.jar</code> (<a href="https://sic.tech/products/core-crypto-toolkits/pkcs11-wrapper/" target="_blank">IAIK PKCS#11 Wrapper</a>).
087 * </li>
088 * <li>
089 * 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.
090 * </li>
091 * <li>
092 * <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).
093 * </li>
094 * <li>
095 * <code>mail.jar</code> (<a href="http://www.oracle.com/technetwork/java/javamail/index.html" target="_blank">JavaMail API</a>).
096 * </li>
097 * <li>
098 * <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).
099 * </li>
100 * </ul>
101 * <code>iaik_cms.jar</code>, <code>iaik_cms_demo.jar</code>, <code>iaik_jce(full).jar</code>,
102 * <code>iaikPkcs11Wrapper.jar</code> and <code>iaikPkcs11Provider.jar</code> (and
103 * <code>iaik_eccelerate.jar</code>, <code>mail.jar</code>, <code>activation.jar</code>) have to
104 * be put into the classpath, the shared library (<code>pkcs11wrapper.dll</code> or
105 * <code>libpkcs11wrapper.so</code>) has to be in your system library search path or in your VM
106 * library path, e.g. (on Windows, assuming that all jar files are located in a lib sub-directory
107 * and the dll is in a lib/win64 sub-directory):
108 * <pre>
109 * java -Djava.library.path=lib/win64
110 * -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
111 * demo.pkcs11.ImplicitSignedMailDemo <pkcs11Module>.dll
112 * </pre>
113
114 */
115 public class ImplicitSignedMailDemo extends SignedMailDemo {
116
117 /**
118 * Creates a ImplicitSignedDataStreamDemo object for the given module name.
119 *
120 * @param moduleName the name of the module
121 * @param userPin the user-pin (password) for the TokenKeyStore
122 * (may be <code>null</code> to pou-up a dialog asking for the pin)
123 */
124 public ImplicitSignedMailDemo(String moduleName, char[] userPin) {
125 // install provider in super class
126 super(moduleName, userPin);
127 System.out.println();
128 System.out.println("******************************************************************************************************************");
129 System.out.println("* PKCS#11 ImplicitSignedMailDemo *");
130 System.out.println("* (shows how to create multipart/signed messages using the IAIK-PKCS11 provider for accessing the key on a card) *");
131 System.out.println("******************************************************************************************************************");
132 System.out.println();
133 }
134
135 /**
136 * Starts the demo.
137 */
138 public void start() {
139 try {
140 getKeyStore();
141 getSignatureKey();
142 start(true);
143 } catch (Throwable ex) {
144 ex.printStackTrace();
145 throw new RuntimeException(ex.toString());
146 }
147 }
148
149 /**
150 * This is the main method that is called by the JVM during startup.
151 *
152 * @param args These are the command line arguments.
153 */
154 public static void main(String[] args) {
155
156 if (args.length == 0) {
157 System.out.println("Missing pkcs11 module name.\n");
158 printUsage();
159 }
160
161 String moduleName = args[0];
162 char[] userPin = (args.length == 2) ? args[1].toCharArray() : null;
163
164 if (args.length > 2) {
165 System.out.println("Too many arguments.\n");
166 printUsage();
167 }
168
169 DemoSMimeUtil.initDemos();
170
171 (new ImplicitSignedMailDemo(moduleName, userPin)).start();
172 System.out.println("Ready!");
173 DemoUtil.waitKey();
174 }
175
176 /**
177 * Print usage information.
178 */
179 private final static void printUsage() {
180 System.out.println("Usage:\n");
181 System.out.println("java ImplicitSignedMailDemo <pkcs11 module name> [<user-pin>]\n");
182 System.out.println("e.g.:");
183 System.out.println("java ImplicitSignedMailDemo aetpkss1.dll");
184 System.out.println("java ImplicitSignedMailDemo aetpkss1.so");
185 DemoUtil.waitKey();
186 System.exit(0);
187 }
188
189
190 }