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.*;
59 import org.apache.log4j.ConsoleAppender;
60 import org.apache.log4j.Level;
61 import org.apache.log4j.Logger;
62 import org.apache.log4j.PatternLayout;
63 import org.apache.xml.security.c14n.*;
64 import org.apache.xml.security.signature.*;
65 import org.apache.xml.security.transforms.*;
68 import edu.internet2.middleware.shibboleth.common.XML;
69 import edu.internet2.middleware.shibboleth.xml.Parser;
72 * Signs/verifies/maintains Shibboleth metadata files
74 * @author Scott Cantor
75 * @created June 11, 2002
77 public class MetadataTool
80 * Signs/verifies/maintains Shibboleth metadata files
82 * @param argv The command line arguments
83 * @exception Exception One of about fifty different kinds of possible errors
85 public static void main(String args[]) throws Exception {
86 // Process the command line.
87 CmdLineParser parser = new CmdLineParser();
88 CmdLineParser.Option helpOption = parser.addBooleanOption('h', "help");
89 CmdLineParser.Option signOption = parser.addBooleanOption('s', "sign");
90 CmdLineParser.Option noverifyOption = parser.addBooleanOption('N', "noverify");
91 CmdLineParser.Option inOption = parser.addStringOption('i', "in");
92 CmdLineParser.Option outOption = parser.addStringOption('o', "out");
93 CmdLineParser.Option keystoreOption = parser.addStringOption('k', "keystore");
94 CmdLineParser.Option aliasOption = parser.addStringOption('a', "alias");
95 CmdLineParser.Option pwOption = parser.addStringOption('p', "password");
96 CmdLineParser.Option nsOption = parser.addStringOption('x', "ns");
97 CmdLineParser.Option nameOption = parser.addStringOption('n', "name");
98 CmdLineParser.Option debugOption = parser.addBooleanOption('d', "debug");
100 Boolean debugEnabled = ((Boolean) parser.getOptionValue(debugOption));
101 boolean debug = false;
102 if (debugEnabled != null) {
103 debug = debugEnabled.booleanValue();
105 configureLogging(debug);
110 catch (CmdLineParser.OptionException e) {
111 System.err.println(e.getMessage());
113 Thread.sleep(100); //silliness to get error to print first
115 catch (InterruptedException ie) {
118 printUsage(System.out);
122 Boolean helpEnabled = (Boolean)parser.getOptionValue(helpOption);
123 if (helpEnabled != null && helpEnabled.booleanValue()) {
124 printUsage(System.out);
128 Boolean sign = (Boolean)parser.getOptionValue(signOption);
129 Boolean noverify = (Boolean)parser.getOptionValue(noverifyOption);
130 String keystore = (String)parser.getOptionValue(keystoreOption);
131 String pw = (String)parser.getOptionValue(pwOption);
132 String alias = (String)parser.getOptionValue(aliasOption);
133 String infile = (String)parser.getOptionValue(inOption);
134 String outfile = (String)parser.getOptionValue(outOption);
135 String ns = (String)parser.getOptionValue(nsOption);
136 String name = (String)parser.getOptionValue(nameOption);
138 if (infile == null || infile.length() == 0) {
139 printUsage(System.out);
143 if (keystore != null && keystore.length() > 0) {
144 if (alias == null || alias.length() == 0) {
145 printUsage(System.out);
150 PrivateKey privateKey = null;
151 Certificate chain[] = null;
152 X509Certificate cert = null;
154 if (sign != null && sign.booleanValue()) {
155 if (keystore == null || keystore.length() == 0 || pw == null || pw.length() == 0) {
156 printUsage(System.out);
159 KeyStore ks = KeyStore.getInstance("JKS");
160 FileInputStream fis = new FileInputStream(keystore);
161 ks.load(fis, pw.toCharArray());
162 privateKey = (PrivateKey)ks.getKey(alias, pw.toCharArray());
163 chain = ks.getCertificateChain(alias);
164 if (privateKey == null || chain == null) {
165 System.err.println("error: couldn't load key or certificate chain from keystore");
169 else if (keystore != null && keystore.length() > 0){
170 KeyStore ks = KeyStore.getInstance("JKS");
171 FileInputStream fis = new FileInputStream(keystore);
173 cert = (X509Certificate)ks.getCertificate(alias);
175 System.err.println("error: couldn't load certificate from keystore");
179 else if (noverify == null || !noverify.booleanValue()) {
180 printUsage(System.out);
185 // Parse file and verify root element.
186 Document doc = Parser.loadDom(infile,true);
187 Element e = doc.getDocumentElement();
188 if (ns != null && name != null && !org.opensaml.XML.isElementNamed(e,ns,name)) {
189 System.err.println("error: root element did not match ns and name parameters");
192 else if (!org.opensaml.XML.isElementNamed(e,XML.SHIB_NS,"SiteGroup") &&
193 !org.opensaml.XML.isElementNamed(e,XML.SHIB_NS,"Trust") &&
194 !org.opensaml.XML.isElementNamed(e,XML.TRUST_NS,"Trust")) {
195 System.err.println("error: root element must be shib:SiteGroup, shib:Trust, or trust:Trust");
199 if (sign != null && sign.booleanValue()) {
200 // Remove any existing signature.
201 Element old = org.opensaml.XML.getLastChildElement(e, org.opensaml.XML.XMLSIG_NS, "Signature");
205 // Create new signature.
206 XMLSignature sig = new XMLSignature(doc, null, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);
207 Transforms transforms = new Transforms(doc);
208 transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
209 transforms.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
210 sig.addDocument("", transforms, org.apache.xml.security.utils.Constants.ALGO_ID_DIGEST_SHA1);
211 for (int i=0; i < chain.length; i++)
212 sig.addKeyInfo((X509Certificate)chain[i]);
213 e.appendChild(sig.getElement());
214 sig.sign(privateKey);
217 Element sigElement = org.opensaml.XML.getLastChildElement(e, org.opensaml.XML.XMLSIG_NS, "Signature");
218 boolean v = (noverify == null || !noverify.booleanValue());
220 if (sigElement == null) {
221 System.err.println("error: file is not signed");
224 XMLSignature sig = new XMLSignature(sigElement, null);
225 if (!sig.checkSignatureValue(cert)) {
226 System.err.println("error: signature on file did not verify");
230 else if (sigElement != null) {
231 XMLSignature sig = new XMLSignature(sigElement, null);
232 System.err.println("verification of signer disabled, make sure you trust the source of this file!");
233 if (!sig.checkSignatureValue(sig.getKeyInfo().getPublicKey())) {
234 System.err.println("error: signature on file did not verify");
239 System.err.println("verification disabled, and file is unsigned!");
243 OutputStream out = null;
244 Canonicalizer c = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS);
245 if (outfile != null && outfile.length() > 0) {
246 out = new FileOutputStream(outfile);
247 out.write(c.canonicalizeSubtree(doc));
251 System.out.write(c.canonicalizeSubtree(doc));
255 private static void printUsage(PrintStream out)
258 out.println("usage: java edu.internet2.middleware.shibboleth.utils.MetadataTool");
260 out.println("when signing: -i <uri> -s -k <keystore> -a <alias> -p <pass> [-o <outfile>]");
261 out.println("when updating: -i <uri> [-k <keystore> -a <alias> OR -N ] [-o <outfile>]");
262 out.println(" -i,--in input file or url");
263 out.println(" -k,--keystore pathname of Java keystore file");
264 out.println(" -a,--alias alias of signing or verification key");
265 out.println(" -p,--password keystore/key password");
266 out.println(" -o,--outfile write signed copy to this file instead of stdout");
267 out.println(" -s,--sign sign the input file and write out a signed version");
268 out.println(" -N,--noverify allows update of file without signature check");
269 out.println(" -h,--help print this message");
270 out.println(" -x,--ns XML namespace of root element");
271 out.println(" -n,--name name of root element");
272 out.println(" -d, --debug run in debug mode");
277 private static void configureLogging(boolean debugEnabled)
279 ConsoleAppender rootAppender = new ConsoleAppender();
280 rootAppender.setWriter(new PrintWriter(System.out));
281 rootAppender.setName("stdout");
282 Logger.getRootLogger().addAppender(rootAppender);
285 Logger.getRootLogger().setLevel(Level.DEBUG);
286 rootAppender.setLayout(new PatternLayout("%-5p %-41X{serviceId} %d{ISO8601} (%c:%L) - %m%n"));
288 Logger.getRootLogger().setLevel(Level.INFO);
289 Logger.getLogger("edu.internet2.middleware.shibboleth.aa.attrresolv").setLevel(Level.WARN);
290 rootAppender.setLayout(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
292 Logger.getLogger("org.apache.xml.security").setLevel(Level.OFF);