JavaSE 6基于JSR105的XML签名之实践篇(6)
http://www.sina.com.cn 2007年01月11日 08:56 天极yesky
四、 修改XML签名
为了表明该校验程序确实能够捕获对生成的XML签名的修改,我们可以在我们的示例中创建一个Tamper.java程序,允许我们修改清单中的信用卡号(或signature.xml文件中的SignatureValue元素)。
这个程序使用XML签名文档和一个布尔值作为参数。当该布尔参数为true时,程序改变信用卡号;否则,它修改签名值。
public class Tamper { public static void main(String[] args) throws Exception { String sigfile = "etc/signature.xml";
//决定要修改的标志-Reference或SignatureValue boolean tamperRef = true ;
if (args.length >= 2) { sigfile = args[0]; tamperRef = Boolean.parseBoolean(args[1]); } File file = new File(sigfile); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document signature = dbf.newDocumentBuilder().parse(file);
if (tamperRef){ //修改信用卡号 NodeList targets =signature.getDocumentElement().getElementsByTagName("number"); Node number = targets.item(0); if (!number.getTextContent().equals("987654321")){ number.setTextContent("987654321"); }else{ number.setTextContent("000000000"); } }else{ //修改SignatureValue(第一字节) BASE64Encoder en = new BASE64Encoder(); BASE64Decoder de = new BASE64Decoder(); NodeList sigValues =signature.getDocumentElement().getElementsByTagName("SignatureValue"); Node sigValue = sigValues.item(0); byte[] oldValue = de.decodeBuffer(sigValue.getTextContent()); if (oldValue[0]!= 111){ oldValue[0] = (byte)111; }else{ oldValue[0] = (byte)112; } sigValue.setTextContent(en.encode(oldValue)); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); trans.transform(new DOMSource(signature),new StreamResult(new FileOutputStream(file))); } } |
为了运行它,读者可以执行经修改的Ant目标。在运行这个修改的程序后,如果我们再次运行该校验程序,核心校验将失败,并且System.out将分别输出引用和签名校验的状态。随着第二个Boolean输入参数值的不同,引用与/或签名校验可能报告失败。
至此,我们已经讨论完本文中的示例应用程序。
五、 结论 XML签名和JSR-105中都包含了大量的内容,在一篇小小的文章中我们是无法全面涉及它们(例如transforms,canonicalization方法,还有Manifest和SignatureProperties元素)的。而且,我们也根本没有深入分析digest和签名算法。感兴趣的读者应该进一步参考W3C建议,JSR-105 API文档以及相关密码学信息。
W3C建议中仅要求实现基于SHA-1哈希函数支持digest和签名方法。注意,一个由王小云教授率领的中国数学研究小组已经攻克了一些包括SHA-1在内广泛应用的哈希函数中的安全问题。尽管他们的结果还不是立即意味着-现在SHA-1在XML签名方面的使用还不是不安全的;但是,密码学专家们确实推荐在新的应用程序和系统中应该考虑使用更为安全的算法。
读者还应该明白,尽管JCP计划随同JAVA SE 6一起发行JSR-105,但是JSR-105要求全面兼容实现对JDK1.4及其高版本的支持。因此,在使用JDK 1.4开发的应用程序中使用这一技术应该是没有问题的。
[上一页] [1] [2] [3] [4] [5] [6]