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