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.BufferedReader;
047 import java.io.FileOutputStream;
048 import java.io.InputStreamReader;
049 import java.io.OutputStream;
050 import java.io.PrintWriter;
051
052 import iaik.pkcs.pkcs11.Module;
053 import iaik.pkcs.pkcs11.Session;
054 import iaik.pkcs.pkcs11.Token;
055 import iaik.pkcs.pkcs11.TokenInfo;
056 import iaik.pkcs.pkcs11.objects.Data;
057 import iaik.pkcs.pkcs11.objects.Object;
058
059
060
061 /**
062 * This demo program read a data object with a specific label from the token.
063 *
064 * @author <a href="mailto:Karl.Scheibelhofer@iaik.at"> Karl Scheibelhofer </a>
065 * @version 0.1
066 * @invariants
067 */
068 public class ReadDataObject {
069
070 static PrintWriter output_;
071
072 static BufferedReader input_;
073
074 static {
075 try {
076 //output_ = new PrintWriter(new FileWriter("GetInfo_output.txt"), true);
077 output_ = new PrintWriter(System.out, true);
078 input_ = new BufferedReader(new InputStreamReader(System.in));
079 } catch (Throwable thr) {
080 thr.printStackTrace();
081 output_ = new PrintWriter(System.out, true);
082 input_ = new BufferedReader(new InputStreamReader(System.in));
083 }
084 }
085
086 public static void main(String[] args) {
087 if (args.length != 3) {
088 printUsage();
089 System.exit(1);
090 }
091
092 try {
093 Module pkcs11Module = Module.getInstance(args[0]);
094 pkcs11Module.initialize(null);
095
096 Token token = Util.selectToken(pkcs11Module, output_, input_);
097 if (token == null) {
098 output_.println("We have no token to proceed. Finished.");
099 output_.flush();
100 System.exit(0);
101 }
102 TokenInfo tokenInfo = token.getTokenInfo();
103
104 output_.println("################################################################################");
105 output_.println("Information of Token:");
106 output_.println(tokenInfo);
107 output_.println("################################################################################");
108
109 // open an read-write user session
110 Session session = Util.openAuthorizedSession(token, Token.SessionReadWriteBehavior.RW_SESSION, output_, input_);
111
112 output_.println("################################################################################");
113 output_.println("searching for data object on the card using this search template... ");
114 output_.flush();
115
116 // create certificate object template
117 Data dataObjectTemplate = new Data();
118
119 // we could also set the name that manages this data object
120 //dataObjectTemplate.getApplication().setCharArrayValue("Application Name");
121
122 // set the data object's label
123 dataObjectTemplate.getLabel().setCharArrayValue(args[1].toCharArray());
124
125 // print template
126 output_.println(dataObjectTemplate);
127
128 // start find operation
129 session.findObjectsInit(dataObjectTemplate);
130
131 Object[] foundDataObjects = session.findObjects(1); // find first
132
133 Data dataObject;
134 if (foundDataObjects.length > 0) {
135 dataObject = (Data) foundDataObjects[0];
136 output_.println("________________________________________________________________________________");
137 output_.print("found this data object with handle: ");
138 output_.println(dataObject.getObjectHandle());
139 output_.println(dataObject);
140 output_.println("________________________________________________________________________________");
141 // FIXME, there may be more than one that matches the given template, the label is not unique in general
142 // foundDataObjects = session.findObjects(1); //find next
143 } else {
144 dataObject = null;
145 }
146
147 session.findObjectsFinal();
148
149 output_.println("################################################################################");
150
151 output_.println("################################################################################");
152 output_.println("Writing content data (value attribute) to: " + args[2]);
153
154 OutputStream dataOutputStream = new FileOutputStream(args[2]);
155
156 byte[] data = dataObject.getValue().getByteArrayValue();
157 if (data != null) {
158 dataOutputStream.write(data);
159 }
160 dataOutputStream.flush();
161 dataOutputStream.close();
162
163 output_.println("################################################################################");
164
165 session.closeSession();
166 pkcs11Module.finalize(null);
167
168 } catch (Throwable ex) {
169 ex.printStackTrace();
170 }
171 }
172
173 protected static void printUsage() {
174 output_.println("ReadDataObject <PKCS#11 module name> <data object label> <output file>");
175 output_.println("e.g.: ReadDataObject gclib.dll \"Student Data\" data.dat");
176 }
177
178 }