jdk/test/com/sun/org/apache/xml/internal/security/TruncateHMAC.java
author jwilhelm
Thu, 04 Jun 2015 14:19:51 +0200
changeset 31034 6d34754bd5e0
parent 23010 6dadb192ad81
child 30820 0d4717a011d3
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3462
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18266
diff changeset
     2
 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
3462
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3466
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3466
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3466
diff changeset
    21
 * questions.
3462
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%
3466
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    26
 * @bug 6824440 6858484
3462
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);
3466
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    59
        validate("signature-enveloping-hmac-sha1-trunclen-0-attack.xml", false);
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    60
        validate("signature-enveloping-hmac-sha1-trunclen-8-attack.xml", false);
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    61
        // this one should pass
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    62
        validate("signature-enveloping-hmac-sha1.xml", true);
3462
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    63
        generate_hmac_sha1_40();
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    64
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    65
        if (atLeastOneFailed) {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    66
            throw new Exception
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    67
                ("At least one signature did not validate as expected");
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    68
        }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    69
    }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    70
3466
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    71
    private static void validate(String data, boolean pass) throws Exception {
3462
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    72
        System.out.println("Validating " + data);
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    73
        File file = new File(DIR, data);
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    74
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    75
        Document doc = dbf.newDocumentBuilder().parse(file);
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    76
        NodeList nl =
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    77
            doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature");
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    78
        if (nl.getLength() == 0) {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    79
            throw new Exception("Couldn't find signature Element");
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    80
        }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    81
        Element sigElement = (Element) nl.item(0);
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    82
        XMLSignature signature = new XMLSignature
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    83
            (sigElement, file.toURI().toString());
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    84
        SecretKey sk = signature.createSecretKey("secret".getBytes("ASCII"));
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    85
        try {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    86
            System.out.println
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    87
                ("Validation status: " + signature.checkSignatureValue(sk));
3466
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    88
            if (!pass) {
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    89
                System.out.println("FAILED");
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    90
                atLeastOneFailed = true;
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    91
            } else {
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    92
                System.out.println("PASSED");
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    93
            }
3462
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    94
        } catch (XMLSignatureException xse) {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
    95
            System.out.println(xse.getMessage());
3466
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    96
            if (!pass) {
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    97
                System.out.println("PASSED");
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    98
            } else {
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
    99
                System.out.println("FAILED");
18266
26e69da689b9 8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents: 5506
diff changeset
   100
                atLeastOneFailed = true;
3466
2200fa4b79ff 6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents: 3462
diff changeset
   101
            }
3462
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   102
        }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   103
    }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   104
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   105
    private static void generate_hmac_sha1_40() throws Exception {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   106
        System.out.println("Generating ");
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   107
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   108
        Document doc = dbf.newDocumentBuilder().newDocument();
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   109
        XMLSignature sig = new XMLSignature
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   110
            (doc, null, XMLSignature.ALGO_ID_MAC_HMAC_SHA1, 40,
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   111
             Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   112
        try {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   113
            sig.sign(getSecretKey("secret".getBytes("ASCII")));
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   114
            System.out.println("FAILED");
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   115
            atLeastOneFailed = true;
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   116
        } catch (XMLSignatureException xse) {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   117
            System.out.println(xse.getMessage());
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   118
            System.out.println("PASSED");
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   119
        }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   120
    }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   121
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   122
    private static SecretKey getSecretKey(final byte[] secret) {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   123
        return new SecretKey() {
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   124
            public String getFormat()   { return "RAW"; }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   125
            public byte[] getEncoded()  { return secret; }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   126
            public String getAlgorithm(){ return "SECRET"; }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   127
        };
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   128
    }
4477fb399895 6824440: XML Signature HMAC issue
mullan
parents:
diff changeset
   129
}