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.ByteArrayOutputStream;
048 import java.io.FileInputStream;
049 import java.io.InputStream;
050 import java.io.InputStreamReader;
051 import java.io.PrintWriter;
052
053 import iaik.pkcs.pkcs11.Module;
054 import iaik.pkcs.pkcs11.Session;
055 import iaik.pkcs.pkcs11.Token;
056 import iaik.pkcs.pkcs11.TokenInfo;
057 import iaik.pkcs.pkcs11.objects.Data;
058
059
060
061 /**
062 * This demo program can be used to download data to the card.
063 *
064 * @author <a href="mailto:Karl.Scheibelhofer@iaik.at"> Karl Scheibelhofer </a>
065 * @version 0.1
066 * @invariants
067 */
068 public class DownloadData {
069
070 static BufferedReader input_;
071
072 static PrintWriter output_;
073
074 static {
075 try {
076 //output_ = new PrintWriter(new FileWriter("SignAndVerify_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
094 Module pkcs11Module = Module.getInstance(args[0]);
095 pkcs11Module.initialize(null);
096
097 Token token = Util.selectToken(pkcs11Module, output_, input_);
098 if (token == null) {
099 output_.println("We have no token to proceed. Finished.");
100 output_.flush();
101 System.exit(0);
102 }
103 TokenInfo tokenInfo = token.getTokenInfo();
104
105 output_.println("################################################################################");
106 output_.println("Information of Token:");
107 output_.println(tokenInfo);
108 output_.println("################################################################################");
109
110 output_.println("################################################################################");
111 output_.println("Reading data from: " + args[1]);
112 InputStream dataInputStream = new FileInputStream(args[1]);
113 ByteArrayOutputStream bufferStream = new ByteArrayOutputStream(256);
114
115 // read the data from the file
116 byte[] buffer = new byte[4096];
117 int bytesRead;
118 while ((bytesRead = dataInputStream.read(buffer)) >= 0) {
119 bufferStream.write(buffer, 0, bytesRead);
120 }
121 dataInputStream.close();
122
123 byte[] data = bufferStream.toByteArray();
124 output_.println("################################################################################");
125
126 // open an read-write user session
127 Session session = Util.openAuthorizedSession(token, Token.SessionReadWriteBehavior.RW_SESSION, output_, input_);
128
129 output_.println("################################################################################");
130 output_.println("creating data object on the card... ");
131 output_.flush();
132
133 // create certificate object template
134 Data dataObjectTemplate = new Data();
135
136 // we could also set the name that manages this data object
137 //dataObjectTemplate.getApplication().setCharArrayValue("Application Name");
138
139 // set the data object's label
140 dataObjectTemplate.getLabel().setCharArrayValue(args[2].toCharArray());
141
142 // set the object's data content
143 dataObjectTemplate.getValue().setByteArrayValue(data);
144
145 // ensure that it is stored on the token and not just in this session
146 dataObjectTemplate.getToken().setBooleanValue(Boolean.TRUE);
147
148 // print template
149 output_.println(dataObjectTemplate);
150
151 // create object
152 session.createObject(dataObjectTemplate);
153
154 output_.println("################################################################################");
155
156 session.closeSession();
157 pkcs11Module.finalize(null);
158
159 } catch (Throwable thr) {
160 thr.printStackTrace();
161 }
162 }
163
164 public static void printUsage() {
165 output_.println("Usage: DownloadData <PKCS#11 module> <data file> <data object label>");
166 output_.println(" e.g.: DownloadData gclib.dll data.dat \"Student Data\"");
167 output_.println("The given DLL must be in the search path of the system.");
168 }
169
170 }