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/ExplicitSignedMailDemo.java 17    12.02.25 17:59 Dbratko $
059    // $Revision: 17 $
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 (explicit signed,
069     * the content data is included; content type multipart/signed) 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 &lt; 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.ExplicitSignedMailDemo  &lt;pkcs11Module&gt;.dll
112     * </pre>
113     */
114    public class ExplicitSignedMailDemo extends SignedMailDemo {
115      
116      /**
117       * Creates a ExplicitSignedDataStreamDemo object for the given module name.
118       * 
119       * @param moduleName the name of the module
120       * @param userPin the user-pin (password) for the TokenKeyStore
121       *                (may be <code>null</code> to pou-up a dialog asking for the pin)
122       */
123      public ExplicitSignedMailDemo(String moduleName, char[] userPin) {
124        // install provider in super class    
125        super(moduleName, userPin);
126        System.out.println();
127        System.out.println("***********************************************************************************************************************");
128        System.out.println("*                                        PKCS#11  ExplicitSignedMailDemo                                              *");
129        System.out.println("* (shows how to create application/pkcs7mime messages using the IAIK-PKCS11 provider for accessing the key on a card) *");
130        System.out.println("***********************************************************************************************************************");
131        System.out.println();
132      }
133    
134      /**
135       * Starts the demo.
136       */
137      public void start() {
138        try {
139          getKeyStore();
140          getSignatureKey();
141          start(false);
142        } catch (Throwable ex) {
143          ex.printStackTrace();
144          throw new RuntimeException(ex.toString());
145        }
146      }
147      
148      /**
149       * This is the main method that is called by the JVM during startup.
150       *
151       * @param args These are the command line arguments.
152       */
153      public static void main(String[] args) {
154        
155        if (args.length == 0) {
156          System.out.println("Missing pkcs11 module name.\n");
157          printUsage();
158        }
159        
160        String moduleName = args[0];
161        char[] userPin = (args.length == 2) ? args[1].toCharArray() : null;
162        
163        if (args.length > 2) {
164          System.out.println("Too many arguments.\n");
165          printUsage();
166        }
167        
168        DemoSMimeUtil.initDemos();
169        
170        (new ExplicitSignedMailDemo(moduleName, userPin)).start();
171        System.out.println("Ready!");
172        DemoUtil.waitKey();
173      }
174      
175      /**
176       * Print usage information.
177       */
178      private final static void printUsage() {
179        System.out.println("Usage:\n");
180        System.out.println("java ExplicitSignedMailDemo <pkcs11 module name> [<user-pin>]\n");
181        System.out.println("e.g.:");
182        System.out.println("java ExplicitSignedMailDemo aetpkss1.dll");
183        System.out.println("java ExplicitSignedMailDemo aetpkss1.so");
184        DemoUtil.waitKey();
185        System.exit(0);
186      }
187    
188    
189    }