TruncateHMAC.java revision 1515:ffb8e36b668c
1/*
2 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test %I% %E%
26 * @bug 6824440
27 * @summary Check that Apache XMLSec APIs will not accept HMAC truncation
28 *    lengths less than minimum bound
29 * @compile -XDignore.symbol.file TruncateHMAC.java
30 * @run main TruncateHMAC
31 */
32
33import java.io.File;
34import javax.crypto.SecretKey;
35import javax.xml.parsers.DocumentBuilderFactory;
36import org.w3c.dom.Document;
37import org.w3c.dom.Element;
38import org.w3c.dom.NodeList;
39
40import com.sun.org.apache.xml.internal.security.Init;
41import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
42import com.sun.org.apache.xml.internal.security.signature.XMLSignature;
43import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
44import com.sun.org.apache.xml.internal.security.utils.Constants;
45
46
47public class TruncateHMAC {
48
49    private final static String DIR = System.getProperty("test.src", ".");
50    private static DocumentBuilderFactory dbf = null;
51    private static boolean atLeastOneFailed = false;
52
53    public static void main(String[] args) throws Exception {
54
55        Init.init();
56        dbf = DocumentBuilderFactory.newInstance();
57        dbf.setNamespaceAware(true);
58        dbf.setValidating(false);
59        validate("signature-enveloping-hmac-sha1-trunclen-0-attack.xml");
60        validate("signature-enveloping-hmac-sha1-trunclen-8-attack.xml");
61        generate_hmac_sha1_40();
62
63        if (atLeastOneFailed) {
64            throw new Exception
65                ("At least one signature did not validate as expected");
66        }
67    }
68
69    private static void validate(String data) throws Exception {
70        System.out.println("Validating " + data);
71        File file = new File(DIR, data);
72
73        Document doc = dbf.newDocumentBuilder().parse(file);
74        NodeList nl =
75            doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature");
76        if (nl.getLength() == 0) {
77            throw new Exception("Couldn't find signature Element");
78        }
79        Element sigElement = (Element) nl.item(0);
80        XMLSignature signature = new XMLSignature
81            (sigElement, file.toURI().toString());
82        SecretKey sk = signature.createSecretKey("secret".getBytes("ASCII"));
83        try {
84            System.out.println
85                ("Validation status: " + signature.checkSignatureValue(sk));
86            System.out.println("FAILED");
87            atLeastOneFailed = true;
88        } catch (XMLSignatureException xse) {
89            System.out.println(xse.getMessage());
90            System.out.println("PASSED");
91        }
92    }
93
94    private static void generate_hmac_sha1_40() throws Exception {
95        System.out.println("Generating ");
96
97        Document doc = dbf.newDocumentBuilder().newDocument();
98        XMLSignature sig = new XMLSignature
99            (doc, null, XMLSignature.ALGO_ID_MAC_HMAC_SHA1, 40,
100             Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
101        try {
102            sig.sign(getSecretKey("secret".getBytes("ASCII")));
103            System.out.println("FAILED");
104            atLeastOneFailed = true;
105        } catch (XMLSignatureException xse) {
106            System.out.println(xse.getMessage());
107            System.out.println("PASSED");
108        }
109    }
110
111    private static SecretKey getSecretKey(final byte[] secret) {
112        return new SecretKey() {
113            public String getFormat()   { return "RAW"; }
114            public byte[] getEncoded()  { return secret; }
115            public String getAlgorithm(){ return "SECRET"; }
116        };
117    }
118}
119