|
From: Pelle B. <pe...@us...> - 2004-03-18 21:41:14
|
Update of /cvsroot/neuclear/neuclear-xmlsig/src/java/org/neuclear/xml/xmlsec In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12467/src/java/org/neuclear/xml/xmlsec Modified Files: SignedInfo.java Log Message: Some fixups in SignedInfo Index: SignedInfo.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-xmlsig/src/java/org/neuclear/xml/xmlsec/SignedInfo.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** SignedInfo.java 8 Mar 2004 23:51:03 -0000 1.3 --- SignedInfo.java 18 Mar 2004 21:31:33 -0000 1.4 *************** *** 1,4 **** --- 1,7 ---- /* $Id$ * $Log$ + * Revision 1.4 2004/03/18 21:31:33 pelle + * Some fixups in SignedInfo + * * Revision 1.3 2004/03/08 23:51:03 pelle * More improvements on the XMLSignature. Now uses the Transforms properly, References properly. *************** *** 108,111 **** --- 111,115 ---- import org.dom4j.Element; + import org.neuclear.commons.Utility; import org.neuclear.commons.crypto.CryptoException; import org.neuclear.commons.crypto.CryptoTools; *************** *** 115,120 **** import org.neuclear.xml.XMLException; import org.neuclear.xml.c14.Canonicalizer; ! import java.security.*; import java.util.ArrayList; import java.util.Collections; --- 119,128 ---- import org.neuclear.xml.XMLException; import org.neuclear.xml.c14.Canonicalizer; + import org.neuclear.xml.c14.CanonicalizerWithComments; ! import java.security.NoSuchAlgorithmException; ! import java.security.PrivateKey; ! import java.security.PublicKey; ! import java.security.Signature; import java.util.ArrayList; import java.util.Collections; *************** *** 122,126 **** public final class SignedInfo extends AbstractXMLSigElement { ! public SignedInfo(Reference references[], final int sigalg) { this(sigalg, references.length); for (int i = 0; i < references.length; i++) { --- 130,134 ---- public final class SignedInfo extends AbstractXMLSigElement { ! public SignedInfo(Reference references[], final int sigalg) throws XMLSecurityException { this(sigalg, references.length); for (int i = 0; i < references.length; i++) { *************** *** 130,136 **** } ! public SignedInfo(final int sigalg, final int refcount) { super(SignedInfo.TAG_NAME); - this.algType = sigalg; refs = new ArrayList(refcount); --- 138,143 ---- } ! public SignedInfo(final int sigalg, final int refcount) throws XMLSecurityException { super(SignedInfo.TAG_NAME); refs = new ArrayList(refcount); *************** *** 141,147 **** final Element sm = XMLSecTools.createElementInSignatureSpace("SignatureMethod"); if (sigalg == SignedInfo.SIG_ALG_RSA) ! sm.addAttribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1"); else ! sm.addAttribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#dsa-sha1"); addElement(sm); --- 148,156 ---- final Element sm = XMLSecTools.createElementInSignatureSpace("SignatureMethod"); if (sigalg == SignedInfo.SIG_ALG_RSA) ! sm.addAttribute("Algorithm", DSIG_ALG_RSA); ! else if (sigalg == SignedInfo.SIG_ALG_DSA) ! sm.addAttribute("Algorithm", DSIG_ALG_DSA); else ! throw new XMLSecurityException("Unsupported Signature algorithm"); addElement(sm); *************** *** 151,166 **** this(sigalg, 1); - final Element cm = XMLSecTools.createElementInSignatureSpace("CanonicalizationMethod"); - cm.addAttribute("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"); try { - addElement(cm); - - final Element sm = XMLSecTools.createElementInSignatureSpace("SignatureMethod"); - if (sigalg == SignedInfo.SIG_ALG_RSA) - sm.addAttribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1"); - else - sm.addAttribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#dsa-sha1"); - - addElement(sm); Reference ref = new Reference(root, enveloped); refs.add(ref); --- 160,164 ---- *************** *** 175,181 **** if (!elem.getQName().equals(XMLSecTools.createQName(TAG_NAME))) throw new XMLSecurityException("Element: " + elem.getQualifiedName() + " is not a valid: " + XMLSecTools.NS_DS.getPrefix() + ":" + TAG_NAME); - final Element c14elem = elem.element(XMLSecTools.createQName("CanonicalizationMethod")); - if (c14elem != null && c14elem.attributeValue("Algorithm").equals("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments")) - c14nType = Canonicalizer.C14NTYPE_WITH_COMMENTS; final List list = elem.elements(XMLSecTools.createQName("Reference")); refs = new ArrayList(list.size()); --- 173,176 ---- *************** *** 229,249 **** final Canonicalizer getCanonicalizer() { ! // if (ref.getSigType() == Reference.XMLSIGTYPE_ENVELOPED) ! // return new CanonicalizerWithoutSignature(); ! // else if (c14nType == Canonicalizer.C14NTYPE_WITH_COMMENTS) ! // return new CanonicalizerWithComments(); return new Canonicalizer(); } ! //TODO Ignore this bit for now final Signature getSignatureAlgorithm() throws XMLSecurityException { try { ! return Signature.getInstance("SHA1withRSA", "BC"); } catch (NoSuchAlgorithmException e) { ! XMLSecTools.rethrowException(e); ! } catch (NoSuchProviderException e) { ! XMLSecTools.rethrowException(e); } - return null; } --- 224,252 ---- final Canonicalizer getCanonicalizer() { ! final Element c14elem = getElement().element(XMLSecTools.createQName("CanonicalizationMethod")); ! if (c14elem != null && c14elem.attributeValue("Algorithm").equals("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments")) ! return new CanonicalizerWithComments(); return new Canonicalizer(); } ! // Returns JCE Signature Cipher for SignedInfo final Signature getSignatureAlgorithm() throws XMLSecurityException { + final Element sigElem = getElement().element(XMLSecTools.createQName("SignatureMethod")); + if (sigElem == null) + throw new XMLSecurityException("No SignatureMethod element found"); + + String algname = sigElem.attributeValue("Algorithm"); + if (Utility.isEmpty(algname)) + throw new XMLSecurityException("No algorithm found in SignatureMethod element"); + try { ! if (algname.equals(DSIG_ALG_RSA)) ! return Signature.getInstance(JCE_ALG_RSA); ! if (algname.equals(DSIG_ALG_DSA)) ! return Signature.getInstance(JCE_ALG_DSA); ! throw new XMLSecurityException("Unsupported Signature algorithm: " + algname); } catch (NoSuchAlgorithmException e) { ! throw new XMLSecurityException(e); } } *************** *** 285,294 **** private static final String TAG_NAME = "SignedInfo"; private final List refs; - private int c14nType = 0; - private int algType = 0; public final static int SIG_ALG_RSA = Signer.KEY_RSA; public final static int SIG_ALG_DSA = Signer.KEY_DSA; // private PublicKey pub; } --- 288,300 ---- private static final String TAG_NAME = "SignedInfo"; private final List refs; public final static int SIG_ALG_RSA = Signer.KEY_RSA; public final static int SIG_ALG_DSA = Signer.KEY_DSA; + public final static String DSIG_ALG_RSA = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"; + public final static String DSIG_ALG_DSA = "http://www.w3.org/2000/09/xmldsig#dsa-sha1"; + public final static String JCE_ALG_RSA = "SHA1withRSA"; + public final static String JCE_ALG_DSA = "SHA1withDSA"; + // private PublicKey pub; } |