2 * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package edu.internet2.middleware.shibboleth.runner;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.security.KeyStore;
24 import java.security.KeyStoreException;
25 import java.security.NoSuchAlgorithmException;
26 import java.security.cert.CertificateException;
27 import java.util.Arrays;
28 import java.util.Date;
29 import java.util.Iterator;
31 import org.apache.xml.security.signature.XMLSignature;
32 import org.opensaml.SAMLAssertion;
33 import org.opensaml.SAMLResponse;
36 * Class that signs and resets the timestamps on SAML objects.
38 * <p>SAML Responses and Assertions must be signed and they have
39 * expiration times that are very short. This makes static files
40 * of test cases hard to use. This class promiscuously signs
41 * any static assertion in an XML file with credentials supplied
42 * in a JKS and it resets the timestamps. It is used to support
43 * JUnit testing where signed input is required.</p>
48 public class MadSignertest {
50 private KeyStore ks = null;
51 private char[] passwd;
54 * Create a signer associated with a JKS file
55 * @param path The JKS file path
56 * @param password The password of the JKS file and all its Keys.
58 public MadSignertest(String path, String password)
59 throws KeyStoreException,
60 NoSuchAlgorithmException,
62 FileNotFoundException,
64 passwd = password.toCharArray();
65 ks = KeyStore.getInstance("JKS");
66 ks.load(new FileInputStream(path), passwd);
70 * Sign the SAMLResponse in a test data xml file.
71 * @param path Path to the input XML file.
72 * @param alias Alias in the JKS of the signing key.
73 * @param now Date to use for timestamps
74 * @return SAMLResponse now signed
76 public SAMLResponse signResponseFile(String path, String alias, Date now)
78 InputStream in = new FileInputStream(path);
83 SAMLResponse r = new SAMLResponse(in);
85 Iterator assertions = r.getAssertions();
86 while (assertions.hasNext()) {
87 SAMLAssertion assertion = (SAMLAssertion) assertions.next();
88 assertion.setIssueInstant(now);
89 assertion.setNotBefore(now);
90 assertion.setNotOnOrAfter(new Date(now.getTime() + 60000));
92 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1,
93 ks.getKey(alias,passwd),
94 Arrays.asList(ks.getCertificateChain(alias))
99 XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1,
100 ks.getKey(alias,passwd),
101 Arrays.asList(ks.getCertificateChain(alias))