|
From: Pelle B. <pe...@us...> - 2004-04-16 23:54:11
|
Update of /cvsroot/neuclear/neuclear-xmlsig/src/java/org/neuclear/xml/xmlsec In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6037/src/java/org/neuclear/xml/xmlsec Modified Files: XMLSignature.java Added Files: HTMLSignature.java Log Message: Added HTMLSignature with tests and associated changes in StandaloneSigner --- NEW FILE: HTMLSignature.java --- package org.neuclear.xml.xmlsec; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.DOMReader; import org.neuclear.commons.Utility; import org.neuclear.commons.crypto.passphraseagents.UserCancellationException; import org.neuclear.commons.crypto.signers.BrowsableSigner; import org.neuclear.commons.crypto.signers.NonExistingSignerException; import org.neuclear.commons.crypto.signers.Signer; import org.w3c.tidy.Tidy; import java.io.InputStream; import java.security.KeyPair; import java.util.List; /** * This is a standard Enveloped Signature with only one Reference object. */ public class HTMLSignature extends XMLSignature { /** * Creates a standard Enveloped Signature within the given Element. * Uses the provided Signer and Alias to sign it. * * @param name * @param signer * @param is * @throws XMLSecurityException * @throws org.neuclear.commons.crypto.passphraseagents.UserCancellationException * * @throws org.neuclear.commons.crypto.signers.NonExistingSignerException * * @see org.neuclear.commons.crypto.signers.Signer */ public HTMLSignature(String name, Signer signer, InputStream is) throws XMLSecurityException, UserCancellationException, NonExistingSignerException { super(name, signer); Tidy tidy = new Tidy(); tidy.setXmlOut(true); org.w3c.dom.Document dom = tidy.parseDOM(is, System.out); DOMReader reader = new DOMReader(); Document doc = reader.read(dom); Element html = doc.getRootElement(); createHTMLBadge(html); si.getElement().addAttribute("id", "dsdigestvalue"); si.setEnvelopedReference(html); html.add(getElement()); sign(name, signer); sigval.addAttribute("id", "dssigvalue"); final Element ki = getElement().element("KeyInfo"); if (ki != null) ki.addAttribute("id", "dskeyinfo"); } private void createHTMLBadge(Element elem) { Element head = elem.element("head"); Element css = head.addElement("link"); css.addAttribute("rel", "stylesheet"); css.addAttribute("href", "http://neuclear.org/sig.css"); css.addAttribute("type", "text/css"); Element body = elem.element("body"); // body.addElement("hr"); Element title = body.addElement("p"); title.addAttribute("id", "dsigtitle"); title.setText("This page has been digitally signed."); body.addText("\n"); } /** * Creates a standard Enveloped Signature within the given Element. * Uses the provided Signer and Alias to sign it. * * @param signer * @param is * @throws XMLSecurityException * @throws org.neuclear.commons.crypto.passphraseagents.UserCancellationException * * @see org.neuclear.commons.crypto.signers.Signer */ public HTMLSignature(BrowsableSigner signer, InputStream is) throws XMLSecurityException, UserCancellationException { super(new SignedInfo(SignedInfo.SIG_ALG_RSA, 1)); Tidy tidy = new Tidy(); tidy.setXmlOut(true); org.w3c.dom.Document dom = tidy.parseDOM(is, null); DOMReader reader = new DOMReader(); Document doc = reader.read(dom); Element html = doc.getRootElement(); createHTMLBadge(html); si.getElement().addAttribute("id", "dsdigestvalue"); si.setEnvelopedReference(html); html.add(getElement()); sign(signer); sigval.addAttribute("id", "dssigvalue"); final Element ki = getElement().element("KeyInfo"); if (ki != null) ki.addAttribute("id", "dskeyinfo"); } /** * Creates a standard Enveloped Signature within the given Element. * Uses the provided KeyPair to sign it. * * @param kp * @param elem * @throws XMLSecurityException */ public HTMLSignature(KeyPair kp, Element elem) throws XMLSecurityException { super(kp.getPublic()); si.setEnvelopedReference(elem); elem.add(getElement()); sign(kp); } protected void verifyReferencesStructure() throws InvalidReferencesException { List refs = si.getReferences(); if (refs.size() != 1) throw new InvalidReferencesException(refs.size()); if (!Utility.isEmpty(si.getPrimaryReference().getUri())) throw new InvalidReferencesException(); } } Index: XMLSignature.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-xmlsig/src/java/org/neuclear/xml/xmlsec/XMLSignature.java,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** XMLSignature.java 12 Apr 2004 15:00:42 -0000 1.21 --- XMLSignature.java 16 Apr 2004 23:54:03 -0000 1.22 *************** *** 1,4 **** --- 1,7 ---- /* $Id$ * $Log$ + * Revision 1.22 2004/04/16 23:54:03 pelle + * Added HTMLSignature with tests and associated changes in StandaloneSigner + * * Revision 1.21 2004/04/12 15:00:42 pelle * Now have a slightly better way of handling the waiting for input using the WaitForInput class. *************** *** 364,368 **** */ protected void sign(final KeyPair kp) throws XMLSecurityException { ! sigval.setText(Base64.encode(si.sign(kp.getPrivate()))); } --- 367,371 ---- */ protected void sign(final KeyPair kp) throws XMLSecurityException { ! sigval.setText(Base64.encodeClean(si.sign(kp.getPrivate()))); } *************** *** 378,387 **** */ protected void sign(final String alias, final Signer signer) throws XMLSecurityException, NonExistingSignerException, UserCancellationException { ! sigval.setText(Base64.encode(si.sign(alias, signer))); } protected void sign(BrowsableSigner signer) throws XMLSecurityException, UserCancellationException { KeyInfo.CreateKeyInfoCallBack cb = new KeyInfo.CreateKeyInfoCallBack(); ! sigval.setText(Base64.encode(si.sign(signer, cb))); addElement(cb.createKeyInfo()); } --- 381,394 ---- */ protected void sign(final String alias, final Signer signer) throws XMLSecurityException, NonExistingSignerException, UserCancellationException { ! sigval.setText(Base64.encodeClean(si.sign(alias, signer))); ! // if (signer instanceof PublicKeySource) { ! // ki=new KeyInfo(((PublicKeySource)signer).getPublicKey(alias)); ! // addElement(ki); ! // } } protected void sign(BrowsableSigner signer) throws XMLSecurityException, UserCancellationException { KeyInfo.CreateKeyInfoCallBack cb = new KeyInfo.CreateKeyInfoCallBack(); ! sigval.setText(Base64.encodeClean(si.sign(signer, cb))); addElement(cb.createKeyInfo()); } *************** *** 454,459 **** protected SignedInfo si; ! private Element sigval; ! private KeyInfo ki; private static final String TAG_NAME = "Signature"; // private PublicKey pub; --- 461,466 ---- protected SignedInfo si; ! protected Element sigval; ! protected KeyInfo ki; private static final String TAG_NAME = "Signature"; // private PublicKey pub; |