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.FileInputStream;
048 import java.io.FileOutputStream;
049 import java.io.InputStream;
050 import java.io.InputStreamReader;
051 import java.io.OutputStream;
052 import java.io.PrintWriter;
053 import java.security.Signature;
054 import java.util.Arrays;
055 import java.util.List;
056
057 import iaik.asn1.structures.AlgorithmID;
058 import iaik.pkcs.pkcs11.Mechanism;
059 import iaik.pkcs.pkcs11.MechanismInfo;
060 import iaik.pkcs.pkcs11.Module;
061 import iaik.pkcs.pkcs11.Session;
062 import iaik.pkcs.pkcs11.Token;
063 import iaik.pkcs.pkcs11.objects.PrivateKey;
064 import iaik.pkcs.pkcs11.objects.RSAPrivateKey;
065 import iaik.x509.X509Certificate;
066
067
068
069 /**
070 * Signs an X.509 certificate using a token. The actual certificate specific
071 * operations are in the last section of this demo.
072 * The hash is calculated outside the token. This implementation just uses raw
073 * RSA.
074 *
075 * @author <a href="mailto:Karl.Scheibelhofer@iaik.at"> Karl Scheibelhofer </a>
076 * @version 0.1
077 * @invariants
078 */
079 public class SignCertificate {
080
081 static PrintWriter output_;
082
083 static BufferedReader input_;
084
085 static {
086 try {
087 //output_ = new PrintWriter(new FileWriter("GetInfo_output.txt"), true);
088 output_ = new PrintWriter(System.out, true);
089 input_ = new BufferedReader(new InputStreamReader(System.in));
090 } catch (Throwable thr) {
091 thr.printStackTrace();
092 output_ = new PrintWriter(System.out, true);
093 input_ = new BufferedReader(new InputStreamReader(System.in));
094 }
095 }
096
097 public static void main(String[] args) {
098 if (args.length != 3) {
099 printUsage();
100 System.exit(1);
101 }
102
103 try {
104
105 Module pkcs11Module = Module.getInstance(args[0]);
106 pkcs11Module.initialize(null);
107
108 Token token = Util.selectToken(pkcs11Module, output_, input_);
109 if (token == null) {
110 output_.println("We have no token to proceed. Finished.");
111 output_.flush();
112 System.exit(0);
113 }
114
115 List supportedMechanisms = Arrays.asList(token.getMechanismList());
116 if (!supportedMechanisms.contains(Mechanism.RSA_PKCS)) {
117 output_.print("This token does not support raw RSA signing!");
118 output_.flush();
119 System.exit(0);
120 } else {
121 MechanismInfo rsaMechanismInfo = token.getMechanismInfo(Mechanism.RSA_PKCS);
122 if (!rsaMechanismInfo.isSign()) {
123 output_.print("This token does not support RSA signing according to PKCS!");
124 output_.flush();
125 System.exit(0);
126 }
127 }
128
129 Session session = Util.openAuthorizedSession(token, Token.SessionReadWriteBehavior.RO_SESSION, output_, input_);
130
131 // first we search for private RSA keys that we can use for signing
132 RSAPrivateKey privateSignatureKeyTemplate = new RSAPrivateKey();
133 privateSignatureKeyTemplate.getSign().setBooleanValue(Boolean.TRUE);
134
135 KeyAndCertificate selectedSignatureKeyAndCertificate =
136 Util.selectKeyAndCertificate(session, privateSignatureKeyTemplate, output_, input_);
137 if (selectedSignatureKeyAndCertificate == null) {
138 output_.println("We have no signature key to proceed. Finished.");
139 output_.flush();
140 System.exit(0);
141 }
142
143 PrivateKey selectedSignatureKey = (PrivateKey) selectedSignatureKeyAndCertificate.getKey();
144
145 // here the interesting code starts
146
147 output_.println("################################################################################");
148 output_.println("signing demo certificate");
149
150 Signature tokenSignatureEngine =
151 new PKCS11SignatureEngine("SHA1withRSA", session, Mechanism.RSA_PKCS, AlgorithmID.sha1);
152 AlgorithmIDAdapter pkcs11Sha1RSASignatureAlgorithmID = new AlgorithmIDAdapter(AlgorithmID.sha1WithRSAEncryption);
153 pkcs11Sha1RSASignatureAlgorithmID.setSignatureInstance(tokenSignatureEngine);
154
155 InputStream templateCertificateStream = new FileInputStream(args[1]);
156 X509Certificate certificate = new X509Certificate(templateCertificateStream);
157
158 java.security.PrivateKey tokenSignatureKey = new TokenPrivateKey(selectedSignatureKey);
159
160 output_.print("signing certificate... ");
161 certificate.sign(pkcs11Sha1RSASignatureAlgorithmID , tokenSignatureKey);
162 output_.println("finished");
163
164 output_.print("writing certificate to file \"");
165 output_.print(args[2]);
166 output_.print("\"... ");
167 OutputStream certificateStream = new FileOutputStream(args[2]);
168 certificate.writeTo(certificateStream);
169 output_.println("finished");
170
171 output_.println("################################################################################");
172
173 session.closeSession();
174 pkcs11Module.finalize(null);
175
176 } catch (Throwable thr) {
177 thr.printStackTrace();
178 } finally {
179 output_.close();
180 }
181 }
182
183 public static void printUsage() {
184 output_.println("Usage: SignCertificate <PKCS#11 module> <DER-encoded X.509 template certificate> <DER-encoded certificate output file>");
185 output_.println(" e.g.: SignCertificate pk2priv.dll templateCert.der signedCert.der");
186 output_.println("The given DLL must be in the search path of the system.");
187 }
188
189 }