1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 package org.apache.xml.security.samples;
20 
21 import java.io.File;
22 import java.io.FileInputStream;
23 
24 import javax.xml.xpath.XPath;
25 import javax.xml.xpath.XPathConstants;
26 import javax.xml.xpath.XPathFactory;
27 
28 import org.apache.xml.security.signature.XMLSignature;
29 import org.apache.xml.security.utils.Constants;
30 import org.w3c.dom.Element;
31 
32 /**
33  * @author $Author: coheigea $
34  */
35 public class AxisVerifier {
36 
37     /**
38      * Method main
39      *
40      * @param unused
41      * @throws Exception
42      */
main(String unused[])43     public static void main(String unused[]) throws Exception {
44 
45         org.apache.xml.security.Init.init();
46 
47         File signatureFile = new File(AxisSigner.AXIS_SIGNATURE_FILENAME);
48         javax.xml.parsers.DocumentBuilderFactory dbf =
49             javax.xml.parsers.DocumentBuilderFactory.newInstance();
50 
51         dbf.setNamespaceAware(true);
52 
53         javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
54         org.w3c.dom.Document doc = db.parse(new FileInputStream(signatureFile));
55         String BaseURI = signatureFile.toURI().toURL().toString();
56 
57         XPathFactory xpf = XPathFactory.newInstance();
58         XPath xpath = xpf.newXPath();
59         DSNamespaceContext context = new DSNamespaceContext();
60         xpath.setNamespaceContext(context);
61 
62         String expression = "//ds:Signature[1]";
63         Element sigElement =
64             (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
65 
66         expression = "//env:Body[1]";
67         context.putPrefix("env", "http://www.w3.org/2001/12/soap-envelope");
68         Element bodyElement =
69             (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
70         bodyElement.setIdAttributeNS("http://schemas.xmlsoap.org/soap/security/2000-12", "id", true);
71 
72         XMLSignature sig = new XMLSignature(sigElement, BaseURI);
73         boolean verify = sig.checkSignatureValue(sig.getKeyInfo().getPublicKey());
74 
75         System.out.println("The signature is" + (verify ? " " : " not ") + "valid");
76 
77         for (int i = 0; i < sig.getSignedInfo().getSignedContentLength(); i++) {
78             boolean thisOneWasSigned =
79                 sig.getSignedInfo().getVerificationResult(i);
80 
81             if (thisOneWasSigned) {
82                 System.out.println("--- Signed Content follows ---");
83                 System.out.println(new String(sig.getSignedInfo().getSignedContentItem(i)));
84             }
85         }
86 
87         System.out.println("");
88         System.out.println("Prior transforms");
89         System.out.println(
90             new String(sig.getSignedInfo().getReferencedContentBeforeTransformsItem(0).getBytes())
91         );
92     }
93 }
94