001 /* Copyright (c) 2002 Graz University of Technology. All rights reserved.
002 *
003 * Redistribution and use in source and binary forms, with or without
004 * modification, are permitted provided that the following conditions are met:
005 *
006 * 1. Redistributions of source code must retain the above copyright notice,
007 * this list of conditions and the following disclaimer.
008 *
009 * 2. Redistributions in binary form must reproduce the above copyright notice,
010 * this list of conditions and the following disclaimer in the documentation
011 * and/or other materials provided with the distribution.
012 *
013 * 3. The end-user documentation included with the redistribution, if any, must
014 * include the following acknowledgment:
015 *
016 * "This product includes software developed by IAIK of Graz University of
017 * Technology."
018 *
019 * Alternately, this acknowledgment may appear in the software itself, if
020 * and wherever such third-party acknowledgments normally appear.
021 *
022 * 4. The names "Graz University of Technology" and "IAIK of Graz University of
023 * Technology" must not be used to endorse or promote products derived from
024 * this software without prior written permission.
025 *
026 * 5. Products derived from this software may not be called
027 * "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
028 * written permission of Graz University of Technology.
029 *
030 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
031 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
032 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
033 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
034 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
035 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
036 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
037 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
038 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
039 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
040 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
041 * POSSIBILITY OF SUCH DAMAGE.
042 */
043
044 package demo.pkcs.pkcs11;
045
046 import java.io.FileOutputStream;
047
048 import iaik.pkcs.pkcs11.Module;
049 import iaik.pkcs.pkcs11.Session;
050 import iaik.pkcs.pkcs11.Slot;
051 import iaik.pkcs.pkcs11.Token;
052
053
054
055 /**
056 * This demo program uses a PKCS#11 module to produce random data.
057 * Optionally the random data can be written to file.
058 *
059 * @author <a href="mailto:Karl.Scheibelhofer@iaik.at"> Karl Scheibelhofer </a>
060 * @version 0.1
061 * @invariants
062 */
063 public class GenerateRandom {
064
065 public static void main(String[] args) {
066 if ((args.length != 2) && (args.length != 3)) {
067 printUsage();
068 System.exit(1);
069 }
070 int numberOfBytes = -1;
071 try {
072 numberOfBytes = Integer.parseInt(args[1]);
073 } catch (Exception ex) {
074 printUsage();
075 System.exit(1);
076 }
077
078 try {
079
080 Module pkcs11Module = Module.getInstance(args[0]);
081 pkcs11Module.initialize(null);
082
083 Slot[] slots = pkcs11Module.getSlotList(Module.SlotRequirement.TOKEN_PRESENT);
084
085 if (slots.length == 0) {
086 System.out.println("No slot with present token found!");
087 System.exit(0);
088 }
089
090 Slot selectedSlot = slots[0];
091 Token token = selectedSlot.getToken();
092
093 Session session =
094 token.openSession(Token.SessionType.SERIAL_SESSION, Token.SessionReadWriteBehavior.RO_SESSION, null, null);
095
096 System.out.println("################################################################################");
097 System.out.print("generating " + numberOfBytes + " bytes of random data... ");
098
099 byte[] dataBuffer = session.generateRandom(numberOfBytes);
100
101 System.out.println("finished");
102 System.out.println("################################################################################");
103
104
105 if (args.length == 3) {
106 System.out.println("################################################################################");
107 System.out.println("writing random data to file : " + args[2]);
108
109 FileOutputStream dataOutput = new FileOutputStream(args[2]);
110 dataOutput.write(dataBuffer);
111 dataOutput.flush();
112 dataOutput.close();
113 System.out.println("################################################################################");
114 }
115
116 session.closeSession();
117 pkcs11Module.finalize(null);
118
119 } catch (Throwable thr) {
120 thr.printStackTrace();
121 }
122 }
123
124 public static void printUsage() {
125 System.out.println("Usage: GenerateRandom <PKCS#11 module> <number of bytes> [<output file>]");
126 System.out.println(" e.g.: GenerateRandom pk2priv.dll 128 random.dat");
127 System.out.println("The given DLL must be in the search path of the system.");
128 }
129
130 }