end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

XMLデジタル署名検証 - java=OK, .net=OKそう, perl=NG

XML Security Library

2013/3/21追記
どうやら、 http://www.aleksey.com/xmlsec/ も利用できるらしい。
実際に試してはいませんが、「C library based on LibXML2」ということなので、javaperl等からでも利用できるかも

XMLデジタル署名とは?

www.w3.org の記載の通りです
XML Signature Syntax and Processing (Second Edition)

XMLデジタル署名のサンプルは、例えば、次のurlにあります。
XML::Sig - search.cpan.org
※このurlで記載されている署名は、javaでもperlでもvalidationできませんでしが

perlでは XML::Sig , Net::SAML2::XML::Sig ...

にある verify() が使えそうでしたが、xmlの問題か、XML::Sigの問題か、検証できなかったので、諦めました。

.netであれば、SignedXml.CheckSignature()

次のurlに Visual Basic, C#, C++ のサンプルコードがあります。
http://msdn.microsoft.com/ja-jp/library/system.security.cryptography.xml.signedxml%28v=vs.71%29.aspx

javaであれば、JSR 105 API

http://docs.oracle.com/javase/jp/6/technotes/guides/security/xmldsig/XMLDigitalSignature.html
http://code.google.com/p/javaeetutorial5/source/browse/trunk/java+api/jdk-6u10-docs/technotes/guides/security/xmldsig?r=159
↑ここにある通り、↓このままで動作します

import javax.xml.crypto.*;
import javax.xml.crypto.dsig.*;
import javax.xml.crypto.dom.*;
import javax.xml.crypto.dsig.dom.DOMValidateContext;
import javax.xml.crypto.dsig.keyinfo.*;
import java.io.FileInputStream;
import java.security.*;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

/**
 * This is a simple example of validating an XML
 * Signature using the JSR 105 API. It assumes the key needed to
 * validate the signature is contained in a KeyValue KeyInfo.
 */
public class Validate {

    //
    // Synopsis: java Validate [document]
    //
    //    where "document" is the name of a file containing the XML document
    //    to be validated.
    //
    public static void main(String[] args) throws Exception {

    	// Instantiate the document to be validated
    	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    	dbf.setNamespaceAware(true);
    	Document doc =
    			dbf.newDocumentBuilder().parse(
    					new FileInputStream("c:/home/endo/tmp/XXXX_SYS/ds/signed.xml"));
//  	dbf.newDocumentBuilder().parse(new FileInputStream(args[0]));

        // Find Signature element
        NodeList nl =
            doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
        if (nl.getLength() == 0) {
            throw new Exception("Cannot find Signature element");
        }

        // Create a DOM XMLSignatureFactory that will be used to unmarshal the
        // document containing the XMLSignature
        XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");

        // Create a DOMValidateContext and specify a KeyValue KeySelector
        // and document context
        DOMValidateContext valContext = new DOMValidateContext
            (new KeyValueKeySelector(), nl.item(0));
       
        // unmarshal the XMLSignature
        XMLSignature signature = fac.unmarshalXMLSignature(valContext);

        // Validate the XMLSignature (generated above)
        boolean coreValidity = signature.validate(valContext);

        // Check core validation status
        if (coreValidity == false) {
            System.err.println("Signature failed core validation");
            boolean sv = signature.getSignatureValue().validate(valContext);
            System.out.println("signature validation status: " + sv);
            // check the validation status of each Reference
            Iterator i = signature.getSignedInfo().getReferences().iterator();
            for (int j=0; i.hasNext(); j++) {
                boolean refValid =
                    ((Reference) i.next()).validate(valContext);
                System.out.println("ref["+j+"] validity status: " + refValid);
            }
        } else {
            System.out.println("Signature passed core validation");
        }
    }

    /**
     * KeySelector which retrieves the public key out of the
     * KeyValue element and returns it.
     * NOTE: If the key algorithm doesn't match signature algorithm,
     * then the public key will be ignored.
     */
    private static class KeyValueKeySelector extends KeySelector {
        public KeySelectorResult select(KeyInfo keyInfo,
                                        KeySelector.Purpose purpose,
                                        AlgorithmMethod method,
                                        XMLCryptoContext context)
            throws KeySelectorException {
            if (keyInfo == null) {
                throw new KeySelectorException("Null KeyInfo object!");
            }
            SignatureMethod sm = (SignatureMethod) method;
            List list = keyInfo.getContent();

            for (int i = 0; i < list.size(); i++) {
                XMLStructure xmlStructure = (XMLStructure) list.get(i);
                if (xmlStructure instanceof KeyValue) {
                    PublicKey pk = null;
                    try {
                        pk = ((KeyValue)xmlStructure).getPublicKey();
                    } catch (KeyException ke) {
                        throw new KeySelectorException(ke);
                    }
                    // make sure algorithm is compatible with method
                    if (algEquals(sm.getAlgorithm(), pk.getAlgorithm())) {
                        return new SimpleKeySelectorResult(pk);
                    }
                }
            }
            throw new KeySelectorException("No KeyValue element found!");
        }

        //@@@FIXME: this should also work for key types other than DSA/RSA
        static boolean algEquals(String algURI, String algName) {
            if (algName.equalsIgnoreCase("DSA") &&
                algURI.equalsIgnoreCase(SignatureMethod.DSA_SHA1)) {
                return true;
            } else if (algName.equalsIgnoreCase("RSA") &&
                       algURI.equalsIgnoreCase(SignatureMethod.RSA_SHA1)) {
                return true;
            } else {
                return false;
            }
        }
    }

    private static class SimpleKeySelectorResult implements KeySelectorResult {
        private PublicKey pk;
        SimpleKeySelectorResult(PublicKey pk) {
            this.pk = pk;
        }

        public Key getKey() { return pk; }
    }
}

その他、参考にさせて頂いたurl

Java で RSA 暗号を使う - daily dayflower
http://www.java-only.com/LoadTutorial.javaonly?id=67
X.509 の解説

自前で、署名をpublic keyでdecryptして digest value と比較しようともしましたが、写経で済ませました