1 // Copyright (c) 2002 Graz University of Technology. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without modification,
4 // are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice, this
7 //    list of conditions and the following disclaimer.
8 //
9 // 2. Redistributions in binary form must reproduce the above copyright notice,
10 //    this list of conditions and the following disclaimer in the documentation
11 //    and/or other materials provided with the distribution.
12 //
13 // 3. The end-user documentation included with the redistribution, if any, must
14 //    include the following acknowledgment:
15 //
16 //    "This product includes software developed by IAIK of Graz University of
17 //     Technology."
18 //
19 //    Alternately, this acknowledgment may appear in the software itself, if and
20 //    wherever such third-party acknowledgments normally appear.
21 //
22 // 4. The names "Graz University of Technology" and "IAIK of Graz University of
23 //    Technology" must not be used to endorse or promote products derived from this
24 //    software without prior written permission.
25 //
26 // 5. Products derived from this software may not be called "IAIK PKCS Wrapper",
27 //    nor may "IAIK" appear in their name, without prior written permission of
28 //    Graz University of Technology.
29 //
30 // THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
31 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
34 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
35 // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
37 // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
38 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
39 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 // POSSIBILITY OF SUCH DAMAGE.
42 
43 package demo.pkcs.pkcs11.wrapper.basics;
44 
45 import iaik.pkcs.pkcs11.Module;
46 import iaik.pkcs.pkcs11.Session;
47 import iaik.pkcs.pkcs11.SessionInfo;
48 import iaik.pkcs.pkcs11.State;
49 import iaik.pkcs.pkcs11.Token;
50 import iaik.pkcs.pkcs11.TokenException;
51 import iaik.pkcs.pkcs11.objects.Object;
52 
53 import java.io.BufferedReader;
54 import java.io.IOException;
55 import java.io.InputStreamReader;
56 import java.io.PrintWriter;
57 import java.util.Hashtable;
58 
59 import demo.pkcs.pkcs11.wrapper.util.Util;
60 
61 /**
62  * This demo program allows to delete certain objects on a certain token. It
63  * allows the user to select a token. Thereafter, it displays the objects on
64  * that token and lets the user select one of them to delete it.
65  *
66  * @author <a href="mailto:Karl.Scheibelhofer@iaik.at"> Karl Scheibelhofer </a>
67  * @version 0.1
68  * @invariants
69  */
70 public class DeleteObject {
71 
72 	static PrintWriter output_;
73 
74 	static BufferedReader input_;
75 
76 	static {
77 		try {
78 			//output_ = new PrintWriter(new FileWriter("GetInfo_output.txt"), true);
79 			output_ = new PrintWriter(System.out, true);
80 			input_ = new BufferedReader(new InputStreamReader(System.in));
81 		} catch (Throwable thr) {
82 			thr.printStackTrace();
83 			output_ = new PrintWriter(System.out, true);
84 			input_ = new BufferedReader(new InputStreamReader(System.in));
85 		}
86 	}
87 
main(String[] args)88 	public static void main(String[] args)
89 	    throws TokenException, IOException
90 	{
91 		if (args.length < 1) {
92 			printUsage();
93 			System.exit(1);
94 		}
95 
96 		Module pkcs11Module = Module.getInstance(args[0]);
97 		pkcs11Module.initialize(null);
98 
99 		Token token;
100 		if (1 < args.length) token = Util.selectToken(pkcs11Module, output_, input_, args[1]);
101 		else token = Util.selectToken(pkcs11Module, output_, input_);
102 		if (token == null) {
103 			output_.println("We have no token to proceed. Finished.");
104 			output_.flush();
105 			System.exit(0);
106 		}
107 
108 		Session session;
109 		if (2 < args.length) session = Util.openAuthorizedSession(token,
110 		    Token.SessionReadWriteBehavior.RW_SESSION, output_, input_, args[2]);
111 		else session = Util.openAuthorizedSession(token,
112 		    Token.SessionReadWriteBehavior.RW_SESSION, output_, input_, null);
113 
114 		SessionInfo sessionInfo = session.getSessionInfo();
115 		output_.println("using session:");
116 		output_.println(sessionInfo);
117 
118 		output_.println("listing all"
119 		    + (((sessionInfo.getState() == State.RO_USER_FUNCTIONS) || (sessionInfo
120 		        .getState() == State.RW_SO_FUNCTIONS)) ? "" : " public")
121 		    + " objects on token with ID " + token.getTokenID());
122 		output_
123 		    .println("________________________________________________________________________________");
124 
125 		// limits the automatic deletion loop to one if argument "auto" is given
126 		boolean bot_state = true;
127 
128 		deleteLoop: while (true) {
129 			session.findObjectsInit(null);
130 			Object[] objects = session.findObjects(1);
131 			Hashtable objectHandleToObject = new Hashtable(10);
132 
133 			int limit = 0, counter = 0;
134 			if (3 < args.length) limit = 2;
135 
136 			while (objects.length > 0 && (0 == limit || counter < limit)) {
137 				output_
138 				    .println("--------------------------------------------------------------------------------");
139 				long objectHandle = objects[0].getObjectHandle();
140 				objectHandleToObject.put(new Long(objectHandle), objects[0]);
141 				output_.println("Object with handle: " + objectHandle);
142 				output_.println(objects[0]);
143 				output_
144 				    .println("--------------------------------------------------------------------------------");
145 				objects = session.findObjects(1);
146 				counter++;
147 			}
148 			session.findObjectsFinal();
149 
150 			output_
151 			    .println("________________________________________________________________________________");
152 			output_
153 			    .println("################################################################################");
154 
155 			Object selectedObject = null;
156 			Long selectedObjectHandle;
157 			if (objectHandleToObject.isEmpty()) {
158 				output_.println("There are no objects on the token.");
159 				break deleteLoop;
160 			} else {
161 				boolean gotObjectHandle = false;
162 				while (!gotObjectHandle) {
163 					output_.print("Enter the handle of the object to delete or 'x' to exit: ");
164 					output_.flush();
165 					String objectHandleString;
166 					if (3 < args.length) {
167 						if (bot_state) {
168 							objectHandleString = objectHandleToObject.keys().nextElement().toString();
169 							bot_state = false;
170 						} else {
171 							objectHandleString = "x";
172 						}
173 						output_.println(objectHandleString);
174 					} else {
175 						objectHandleString = input_.readLine();
176 					}
177 					if (objectHandleString.equalsIgnoreCase("x")) {
178 						break deleteLoop;
179 					}
180 					try {
181 						selectedObjectHandle = new Long(objectHandleString);
182 						selectedObject = (Object) objectHandleToObject.get(selectedObjectHandle);
183 						if (selectedObject != null) {
184 							gotObjectHandle = true;
185 						} else {
186 							output_.println("An object with the handle \"" + objectHandleString
187 							    + "\" does not exist. Try again.");
188 						}
189 					} catch (NumberFormatException ex) {
190 						output_.println("The entered handle \"" + objectHandleString
191 						    + "\" is invalid. Try again.");
192 					}
193 				}
194 			}
195 
196 			output_.println("Going to delete this object: ");
197 			output_.println(selectedObject);
198 			output_.println();
199 			output_
200 			    .print("Are you sure that you want to DELETE this object permanently? [yes/no] ");
201 			output_.flush();
202 			String answer;
203 			if (3 < args.length) {
204 				answer = "yes";
205 				output_.println(answer);
206 			} else answer = input_.readLine();
207 			if (answer.equalsIgnoreCase("yes")) {
208 				session.destroyObject(selectedObject);
209 				output_.println("Object deleted.");
210 			}
211 		}
212 		session.closeSession();
213 		pkcs11Module.finalize(null);
214 	}
215 
printUsage()216 	protected static void printUsage() {
217 		output_.println("DeleteObject <PKCS#11 module name> [<slot>] [<pin>] [auto]");
218 		output_.println("e.g.: DeleteObject pk2priv.dll");
219 	}
220 
221 }
222