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.ByteArrayOutputStream; 047 import java.io.FileInputStream; 048 import java.io.IOException; 049 import java.io.InputStream; 050 import java.io.PrintWriter; 051 052 import iaik.pkcs.pkcs11.Info; 053 import iaik.pkcs.pkcs11.InitializeArgs; 054 import iaik.pkcs.pkcs11.Module; 055 import iaik.pkcs.pkcs11.MutexHandler; 056 057 058 059 /** 060 * This demo program tries to call initialize with some arguments. 061 * 062 * @author <a href="mailto:Karl.Scheibelhofer@iaik.at"> Karl Scheibelhofer </a> 063 * @version 0.1 064 * @invariants 065 */ 066 public class InitArgs implements InitializeArgs { 067 068 static PrintWriter output_; 069 070 static { 071 try { 072 //output_ = new PrintWriter(new FileWriter("GetInfo_output.txt"), true); 073 output_ = new PrintWriter(System.out, true); 074 } catch (Throwable thr) { 075 thr.printStackTrace(); 076 output_ = new PrintWriter(System.out, true); 077 } 078 } 079 080 public static void main(String[] args) { 081 if ((args.length == 1) || (args.length == 2)){ 082 try { 083 output_.println("################################################################################"); 084 output_.println("load and initialize module \"" + args[0] + "\" using InitializeArgs"); 085 output_.flush(); 086 Module pkcs11Module = Module.getInstance(args[0]); 087 byte[] reservedParameter = (args.length >= 2) ? readStream(new FileInputStream(args[1])) : null; 088 InitializeArgs initArgs = new InitArgs(reservedParameter); 089 pkcs11Module.initialize(initArgs); 090 091 Info info = pkcs11Module.getInfo(); 092 output_.println(info); 093 output_.println("################################################################################"); 094 } catch (Throwable ex) { 095 ex.printStackTrace(); 096 } 097 } else { 098 printUsage(); 099 } 100 System.gc(); // to finalize and disconnect the pkcs11Module 101 } 102 103 protected static void printUsage() { 104 output_.println("InitArgs <PKCS#11 module name> [<file providing reserved parameter>]"); 105 output_.println("e.g.: InitArgs slbck.dll"); 106 } 107 108 /** 109 * Read the contents of the stream into a byte array. The stream is read 110 * until it returns EOF. 111 */ 112 protected static byte[] readStream(InputStream in) 113 throws IOException 114 { 115 ByteArrayOutputStream bufferStream = new ByteArrayOutputStream(256); // initial size 116 int bytesRead; 117 byte[] buffer = new byte[256]; 118 while((bytesRead = in.read(buffer)) >= 0) { 119 bufferStream.write(buffer, 0, bytesRead); 120 } 121 return bufferStream.toByteArray() ; 122 } 123 124 protected byte[] reservedParameter_; 125 126 public InitArgs(byte[] reservedParameter) { 127 reservedParameter_ = reservedParameter; 128 } 129 130 /** 131 * Get the handler object that handes mutex objects. 132 * 133 * @return The mutex handler object or null, if there is none set. 134 * @preconditions 135 * @postconditions 136 */ 137 public MutexHandler getMutexHandler() { 138 output_.println("getMutexHandler() called"); 139 return null ; 140 } 141 142 /** 143 * Checks, if the library is not allowed to create operating system threads. 144 * 145 * @return True, if the library is not allowed to create operating system 146 * threads; false, otherwise. 147 * @preconditions 148 * @postconditions 149 */ 150 public boolean isLibraryCantCreateOsThreads() { 151 output_.println("isLibraryCantCreateOsThreads() called"); 152 return false ; 153 } 154 155 /** 156 * Checks, if the library is allowed to use locking mechanisms of the 157 * operating system. 158 * 159 * @return True, if the library is allowed to use locking mechanisms of the 160 * operating system. 161 * @preconditions 162 * @postconditions 163 */ 164 public boolean isOsLockingOk() { 165 output_.println("isOsLockingOk() called"); 166 return true ; 167 } 168 169 /** 170 * Get the reserved parameter. This is always null as of version 2.11 of 171 * PKCS#11. 172 * 173 * @return null as of version 2.11 of PKCS#11. 174 * @preconditions 175 * @postconditions (result == null) 176 */ 177 public java.lang.Object getReserved() { 178 output_.println("getReserved() called"); 179 return reservedParameter_ ; 180 } 181 182 }