2 * The Shibboleth License, Version 1.
4 * University Corporation for Advanced Internet Development, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution, if any, must include
17 * the following acknowledgment: "This product includes software developed by
18 * the University Corporation for Advanced Internet Development
19 * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
20 * may appear in the software itself, if and wherever such third-party
21 * acknowledgments normally appear.
23 * Neither the name of Shibboleth nor the names of its contributors, nor
24 * Internet2, nor the University Corporation for Advanced Internet Development,
25 * Inc., nor UCAID may be used to endorse or promote products derived from this
26 * software without specific prior written permission. For written permission,
27 * please contact shibboleth@shibboleth.org
29 * Products derived from this software may not be called Shibboleth, Internet2,
30 * UCAID, or the University Corporation for Advanced Internet Development, nor
31 * may Shibboleth appear in their name, without prior written permission of the
32 * University Corporation for Advanced Internet Development.
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36 * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
38 * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
39 * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
40 * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
41 * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
42 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 package edu.internet2.middleware.shibboleth.utils;
52 import jargs.gnu.CmdLineParser;
55 import java.security.*;
56 import java.security.cert.Certificate;
57 import java.security.cert.*;
58 import org.apache.xml.security.c14n.*;
59 import org.apache.xml.security.signature.*;
60 import org.apache.xml.security.transforms.*;
63 import edu.internet2.middleware.shibboleth.common.XML;
66 * Signs/verifies/maintains Shibboleth metadata files
68 * @author Scott Cantor
69 * @created June 11, 2002
71 public class MetadataTool
74 * Signs/verifies/maintains Shibboleth metadata files
76 * @param argv The command line arguments
77 * @exception Exception One of about fifty different kinds of possible errors
79 public static void main(String args[]) throws Exception {
80 // Process the command line.
81 CmdLineParser parser = new CmdLineParser();
82 CmdLineParser.Option helpOption = parser.addBooleanOption('h', "help");
83 CmdLineParser.Option signOption = parser.addBooleanOption('s', "sign");
84 CmdLineParser.Option noverifyOption = parser.addBooleanOption('N', "noverify");
85 CmdLineParser.Option inOption = parser.addStringOption('i', "in");
86 CmdLineParser.Option outOption = parser.addStringOption('o', "out");
87 CmdLineParser.Option keystoreOption = parser.addStringOption('k', "keystore");
88 CmdLineParser.Option aliasOption = parser.addStringOption('a', "alias");
89 CmdLineParser.Option pwOption = parser.addStringOption('p', "password");
90 CmdLineParser.Option nsOption = parser.addStringOption('x', "ns");
91 CmdLineParser.Option nameOption = parser.addStringOption('n', "name");
96 catch (CmdLineParser.OptionException e) {
97 System.err.println(e.getMessage());
99 Thread.sleep(100); //silliness to get error to print first
101 catch (InterruptedException ie) {
104 printUsage(System.out);
108 Boolean helpEnabled = (Boolean)parser.getOptionValue(helpOption);
109 if (helpEnabled != null && helpEnabled.booleanValue()) {
110 printUsage(System.out);
114 Boolean sign = (Boolean)parser.getOptionValue(signOption);
115 Boolean noverify = (Boolean)parser.getOptionValue(noverifyOption);
116 String keystore = (String)parser.getOptionValue(keystoreOption);
117 String pw = (String)parser.getOptionValue(pwOption);
118 String alias = (String)parser.getOptionValue(aliasOption);
119 String infile = (String)parser.getOptionValue(inOption);
120 String outfile = (String)parser.getOptionValue(outOption);
121 String ns = (String)parser.getOptionValue(nsOption);
122 String name = (String)parser.getOptionValue(nameOption);
124 if (infile == null || infile.length() == 0) {
125 printUsage(System.out);
129 if (keystore != null && keystore.length() > 0) {
130 if (alias == null || alias.length() == 0) {
131 printUsage(System.out);
136 PrivateKey privateKey = null;
137 Certificate chain[] = null;
138 X509Certificate cert = null;
140 if (sign != null && sign.booleanValue()) {
141 if (keystore == null || keystore.length() == 0 || pw == null || pw.length() == 0) {
142 printUsage(System.out);
145 KeyStore ks = KeyStore.getInstance("JKS");
146 FileInputStream fis = new FileInputStream(keystore);
147 ks.load(fis, pw.toCharArray());
148 privateKey = (PrivateKey)ks.getKey(alias, pw.toCharArray());
149 chain = ks.getCertificateChain(alias);
150 if (privateKey == null || chain == null) {
151 System.err.println("error: couldn't load key or certificate chain from keystore");
155 else if (keystore != null && keystore.length() > 0){
156 KeyStore ks = KeyStore.getInstance("JKS");
157 FileInputStream fis = new FileInputStream(keystore);
159 cert = (X509Certificate)ks.getCertificate(alias);
161 System.err.println("error: couldn't load certificate from keystore");
165 else if (noverify == null || !noverify.booleanValue()) {
166 printUsage(System.out);
170 org.opensaml.XML.parserPool.registerSchema(XML.SHIB_NS, XML.SHIB_SCHEMA_ID, new XML.SchemaResolver());
171 org.opensaml.XML.parserPool.registerSchema(XML.TRUST_NS, XML.TRUST_SCHEMA_ID, new XML.SchemaResolver());
173 // Parse file and verify root element.
174 Document doc = org.opensaml.XML.parserPool.parse(infile);
175 Element e = doc.getDocumentElement();
176 if (ns != null && name != null && !org.opensaml.XML.isElementNamed(e,ns,name)) {
177 System.err.println("error: root element did not match ns and name parameters");
180 else if (!org.opensaml.XML.isElementNamed(e,XML.SHIB_NS,"SiteGroup") &&
181 !org.opensaml.XML.isElementNamed(e,XML.SHIB_NS,"Trust") &&
182 !org.opensaml.XML.isElementNamed(e,XML.TRUST_NS,"Trust")) {
183 System.err.println("error: root element must be shib:SiteGroup, shib:Trust, or trust:Trust");
187 if (sign != null && sign.booleanValue()) {
188 // Remove any existing signature.
189 Element old = org.opensaml.XML.getLastChildElement(e, org.opensaml.XML.XMLSIG_NS, "Signature");
193 // Create new signature.
194 XMLSignature sig = new XMLSignature(doc, null, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);
195 Transforms transforms = new Transforms(doc);
196 transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
197 transforms.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
198 sig.addDocument("", transforms, org.apache.xml.security.utils.Constants.ALGO_ID_DIGEST_SHA1);
199 for (int i=0; i < chain.length; i++)
200 sig.addKeyInfo((X509Certificate)chain[i]);
201 e.appendChild(sig.getElement());
202 sig.sign(privateKey);
205 Element sigElement = org.opensaml.XML.getLastChildElement(e, org.opensaml.XML.XMLSIG_NS, "Signature");
206 boolean v = (noverify == null || !noverify.booleanValue());
208 if (sigElement == null) {
209 System.err.println("error: file is not signed");
212 XMLSignature sig = new XMLSignature(sigElement, null);
213 if (!sig.checkSignatureValue(cert)) {
214 System.err.println("error: signature on file did not verify");
218 else if (sigElement != null) {
219 XMLSignature sig = new XMLSignature(sigElement, null);
220 System.err.println("verification of signer disabled, make sure you trust the source of this file!");
221 if (!sig.checkSignatureValue(sig.getKeyInfo().getPublicKey())) {
222 System.err.println("error: signature on file did not verify");
227 System.err.println("verification disabled, and file is unsigned!");
231 OutputStream out = null;
232 Canonicalizer c = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS);
233 if (outfile != null && outfile.length() > 0) {
234 out = new FileOutputStream(outfile);
235 out.write(c.canonicalizeSubtree(doc));
239 System.out.write(c.canonicalizeSubtree(doc));
243 private static void printUsage(PrintStream out)
246 out.println("usage: java edu.internet2.middleware.shibboleth.utils.MetadataTool");
248 out.println("when signing: -i <uri> -s -k <keystore> -a <alias> -p <pass> [-o <outfile>]");
249 out.println("when updating: -i <uri> [-k <keystore> -a <alias> OR -N ] [-o <outfile>]");
250 out.println(" -i,--in input file or url");
251 out.println(" -k,--keystore pathname of Java keystore file");
252 out.println(" -a,--alias alias of signing or verification key");
253 out.println(" -p,--password keystore/key password");
254 out.println(" -o,--outfile write signed copy to this file instead of stdout");
255 out.println(" -s,--sign sign the input file and write out a signed version");
256 out.println(" -N,--noverify allows update of file without signature check");
257 out.println(" -h,--help print this message");
258 out.println(" -x,--ns XML namespace of root element");
259 out.println(" -n,--name name of root element");