21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 /** |
24 /** |
25 * @test |
25 * @test |
26 * @bug 4635230 6283345 6303830 6824440 6867348 7094155 8038184 8038349 |
26 * @bug 4635230 6283345 6303830 6824440 6867348 7094155 8038184 8038349 8046949 |
27 * @summary Basic unit tests for generating XML Signatures with JSR 105 |
27 * @summary Basic unit tests for generating XML Signatures with JSR 105 |
28 * @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java |
28 * @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java |
29 * X509KeySelector.java GenerationTests.java |
29 * X509KeySelector.java GenerationTests.java |
30 * @run main/othervm GenerationTests |
30 * @run main/othervm GenerationTests |
31 * @author Sean Mullan |
31 * @author Sean Mullan |
375 System.out.println(); |
375 System.out.println(); |
376 } |
376 } |
377 |
377 |
378 static void test_create_signature_x509_crt_crl() throws Exception { |
378 static void test_create_signature_x509_crt_crl() throws Exception { |
379 System.out.println("* Generating signature-x509-crt-crl.xml"); |
379 System.out.println("* Generating signature-x509-crt-crl.xml"); |
380 List<Object> xds = new ArrayList<Object>(); |
380 List<Object> xds = new ArrayList<>(); |
381 CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
381 CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
382 xds.add(signingCert); |
382 xds.add(signingCert); |
383 FileInputStream fis = new FileInputStream(CRL); |
383 FileInputStream fis = new FileInputStream(CRL); |
384 X509CRL crl = (X509CRL) cf.generateCRL(fis); |
384 X509CRL crl = (X509CRL) cf.generateCRL(fis); |
385 fis.close(); |
385 fis.close(); |
442 |
442 |
443 // create SignedInfo |
443 // create SignedInfo |
444 SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs); |
444 SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs); |
445 |
445 |
446 // create objects |
446 // create objects |
447 List<XMLStructure> objs = new ArrayList<XMLStructure>(); |
447 List<XMLObject> objs = new ArrayList<>(); |
448 |
448 |
449 // Object 1 |
449 // Object 1 |
450 List<Reference> manRefs = Collections.singletonList |
450 List<Reference> manRefs = Collections.singletonList |
451 (fac.newReference("#object-2", sha1)); |
451 (fac.newReference("#object-2", sha1)); |
452 objs.add(fac.newXMLObject(Collections.singletonList |
452 objs.add(fac.newXMLObject(Collections.singletonList |
608 // Reference 7 |
608 // Reference 7 |
609 refs.add(fac.newReference("#signature-properties-1", sha1, null, |
609 refs.add(fac.newReference("#signature-properties-1", sha1, null, |
610 SignatureProperties.TYPE, null)); |
610 SignatureProperties.TYPE, null)); |
611 |
611 |
612 // Reference 8 |
612 // Reference 8 |
613 List<Transform> transforms = new ArrayList<Transform>(); |
613 List<Transform> transforms = new ArrayList<>(); |
614 transforms.add(fac.newTransform |
614 transforms.add(fac.newTransform |
615 (Transform.ENVELOPED, (TransformParameterSpec) null)); |
615 (Transform.ENVELOPED, (TransformParameterSpec) null)); |
616 refs.add(fac.newReference("", sha1, transforms, null, null)); |
616 refs.add(fac.newReference("", sha1, transforms, null, null)); |
617 |
617 |
618 // Reference 9 |
618 // Reference 9 |
683 KeyInfo ki = kifac.newKeyInfo(Collections.singletonList(rm), null); |
683 KeyInfo ki = kifac.newKeyInfo(Collections.singletonList(rm), null); |
684 |
684 |
685 Document doc = db.newDocument(); |
685 Document doc = db.newDocument(); |
686 |
686 |
687 // create objects |
687 // create objects |
688 List<XMLStructure> objs = new ArrayList<XMLStructure>(); |
688 List<XMLObject> objs = new ArrayList<>(); |
689 |
689 |
690 // Object 1 |
690 // Object 1 |
691 objs.add(fac.newXMLObject(Collections.singletonList |
691 objs.add(fac.newXMLObject(Collections.singletonList |
692 (new DOMStructure(doc.createTextNode("I am the text."))), |
692 (new DOMStructure(doc.createTextNode("I am the text."))), |
693 "object-1", "text/plain", null)); |
693 "object-1", "text/plain", null)); |
703 nc.appendChild(doc.createComment(" Commentandum ")); |
703 nc.appendChild(doc.createComment(" Commentandum ")); |
704 objs.add(fac.newXMLObject(Collections.singletonList |
704 objs.add(fac.newXMLObject(Collections.singletonList |
705 (new DOMStructure(nc)), "object-3", null, null)); |
705 (new DOMStructure(nc)), "object-3", null, null)); |
706 |
706 |
707 // Manifest |
707 // Manifest |
708 List<Reference> manRefs = new ArrayList<Reference>(); |
708 List<Reference> manRefs = new ArrayList<>(); |
709 |
709 |
710 // Manifest Reference 1 |
710 // Manifest Reference 1 |
711 manRefs.add(fac.newReference(STYLESHEET, |
711 manRefs.add(fac.newReference(STYLESHEET, |
712 sha1, null, null, "manifest-reference-1")); |
712 sha1, null, null, "manifest-reference-1")); |
713 |
713 |
714 // Manifest Reference 2 |
714 // Manifest Reference 2 |
715 manRefs.add(fac.newReference("#reference-1", sha1)); |
715 manRefs.add(fac.newReference("#reference-1", sha1)); |
716 |
716 |
717 // Manifest Reference 3 |
717 // Manifest Reference 3 |
718 List<Transform> manTrans = new ArrayList<Transform>(); |
718 List<Transform> manTrans = new ArrayList<>(); |
719 String xslt = "" |
719 String xslt = "" |
720 + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n" |
720 + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n" |
721 + " xmlns='http://www.w3.org/TR/xhtml1/strict' \n" |
721 + " xmlns='http://www.w3.org/TR/xhtml1/strict' \n" |
722 + " exclude-result-prefixes='foo' \n" |
722 + " exclude-result-prefixes='foo' \n" |
723 + " version='1.0'>\n" |
723 + " version='1.0'>\n" |
768 (Collections.singletonList(sp), "signature-properties-1"); |
768 (Collections.singletonList(sp), "signature-properties-1"); |
769 objs.add(fac.newXMLObject(Collections.singletonList(sps), null, |
769 objs.add(fac.newXMLObject(Collections.singletonList(sps), null, |
770 null, null)); |
770 null, null)); |
771 |
771 |
772 // Object 4 |
772 // Object 4 |
773 List<Object> xds = new ArrayList<Object>(); |
773 List<Object> xds = new ArrayList<>(); |
774 xds.add("CN=User"); |
774 xds.add("CN=User"); |
775 xds.add(kifac.newX509IssuerSerial |
775 xds.add(kifac.newX509IssuerSerial |
776 ("CN=User", new BigInteger("45ef2729", 16))); |
776 ("CN=User", new BigInteger("45ef2729", 16))); |
777 xds.add(signingCert); |
777 xds.add(signingCert); |
778 objs.add(fac.newXMLObject(Collections.singletonList |
778 objs.add(fac.newXMLObject(Collections.singletonList |
928 } |
928 } |
929 } |
929 } |
930 |
930 |
931 static void test_create_exc_signature() throws Exception { |
931 static void test_create_exc_signature() throws Exception { |
932 System.out.println("* Generating exc_signature.xml"); |
932 System.out.println("* Generating exc_signature.xml"); |
933 List<Reference> refs = new ArrayList<Reference>(4); |
933 List<Reference> refs = new ArrayList<>(4); |
934 |
934 |
935 // create reference 1 |
935 // create reference 1 |
936 refs.add(fac.newReference |
936 refs.add(fac.newReference |
937 ("#xpointer(id('to-be-signed'))", |
937 ("#xpointer(id('to-be-signed'))", |
938 fac.newDigestMethod(DigestMethod.SHA1, null), |
938 fac.newDigestMethod(DigestMethod.SHA1, null), |
940 (fac.newTransform(CanonicalizationMethod.EXCLUSIVE, |
940 (fac.newTransform(CanonicalizationMethod.EXCLUSIVE, |
941 (TransformParameterSpec) null)), |
941 (TransformParameterSpec) null)), |
942 null, null)); |
942 null, null)); |
943 |
943 |
944 // create reference 2 |
944 // create reference 2 |
945 List<String> prefixList = new ArrayList<String>(2); |
945 List<String> prefixList = new ArrayList<>(2); |
946 prefixList.add("bar"); |
946 prefixList.add("bar"); |
947 prefixList.add("#default"); |
947 prefixList.add("#default"); |
948 ExcC14NParameterSpec params = new ExcC14NParameterSpec(prefixList); |
948 ExcC14NParameterSpec params = new ExcC14NParameterSpec(prefixList); |
949 refs.add(fac.newReference |
949 refs.add(fac.newReference |
950 ("#xpointer(id('to-be-signed'))", |
950 ("#xpointer(id('to-be-signed'))", |
961 (CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, |
961 (CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, |
962 (TransformParameterSpec) null)), |
962 (TransformParameterSpec) null)), |
963 null, null)); |
963 null, null)); |
964 |
964 |
965 // create reference 4 |
965 // create reference 4 |
966 prefixList = new ArrayList<String>(2); |
966 prefixList = new ArrayList<>(2); |
967 prefixList.add("bar"); |
967 prefixList.add("bar"); |
968 prefixList.add("#default"); |
968 prefixList.add("#default"); |
969 params = new ExcC14NParameterSpec(prefixList); |
969 params = new ExcC14NParameterSpec(prefixList); |
970 refs.add(fac.newReference |
970 refs.add(fac.newReference |
971 ("#xpointer(id('to-be-signed'))", |
971 ("#xpointer(id('to-be-signed'))", |
980 (CanonicalizationMethod.EXCLUSIVE, |
980 (CanonicalizationMethod.EXCLUSIVE, |
981 (C14NMethodParameterSpec) null), |
981 (C14NMethodParameterSpec) null), |
982 fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs); |
982 fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs); |
983 |
983 |
984 // create KeyInfo |
984 // create KeyInfo |
985 List<XMLStructure> kits = new ArrayList<XMLStructure>(2); |
985 List<XMLStructure> kits = new ArrayList<>(2); |
986 kits.add(kifac.newKeyValue(validatingKey)); |
986 kits.add(kifac.newKeyValue(validatingKey)); |
987 KeyInfo ki = kifac.newKeyInfo(kits); |
987 KeyInfo ki = kifac.newKeyInfo(kits); |
988 |
988 |
989 // create Objects |
989 // create Objects |
990 Document doc = db.newDocument(); |
990 Document doc = db.newDocument(); |
1025 System.out.println(); |
1025 System.out.println(); |
1026 } |
1026 } |
1027 |
1027 |
1028 static void test_create_sign_spec() throws Exception { |
1028 static void test_create_sign_spec() throws Exception { |
1029 System.out.println("* Generating sign-spec.xml"); |
1029 System.out.println("* Generating sign-spec.xml"); |
1030 List<Reference> refs = new ArrayList<Reference>(2); |
1030 List<Reference> refs = new ArrayList<>(2); |
1031 |
1031 |
1032 // create reference 1 |
1032 // create reference 1 |
1033 List<XPathType> types = new ArrayList<XPathType>(3); |
1033 List<XPathType> types = new ArrayList<>(3); |
1034 types.add(new XPathType(" //ToBeSigned ", XPathType.Filter.INTERSECT)); |
1034 types.add(new XPathType(" //ToBeSigned ", XPathType.Filter.INTERSECT)); |
1035 types.add(new XPathType(" //NotToBeSigned ", |
1035 types.add(new XPathType(" //NotToBeSigned ", |
1036 XPathType.Filter.SUBTRACT)); |
1036 XPathType.Filter.SUBTRACT)); |
1037 types.add(new XPathType(" //ReallyToBeSigned ", |
1037 types.add(new XPathType(" //ReallyToBeSigned ", |
1038 XPathType.Filter.UNION)); |
1038 XPathType.Filter.UNION)); |
1041 ("", fac.newDigestMethod(DigestMethod.SHA1, null), |
1041 ("", fac.newDigestMethod(DigestMethod.SHA1, null), |
1042 Collections.singletonList(fac.newTransform(Transform.XPATH2, xp1)), |
1042 Collections.singletonList(fac.newTransform(Transform.XPATH2, xp1)), |
1043 null, null)); |
1043 null, null)); |
1044 |
1044 |
1045 // create reference 2 |
1045 // create reference 2 |
1046 List<Transform> trans2 = new ArrayList<Transform>(2); |
1046 List<Transform> trans2 = new ArrayList<>(2); |
1047 trans2.add(fac.newTransform(Transform.ENVELOPED, |
1047 trans2.add(fac.newTransform(Transform.ENVELOPED, |
1048 (TransformParameterSpec) null)); |
1048 (TransformParameterSpec) null)); |
1049 XPathFilter2ParameterSpec xp2 = new XPathFilter2ParameterSpec |
1049 XPathFilter2ParameterSpec xp2 = new XPathFilter2ParameterSpec |
1050 (Collections.singletonList |
1050 (Collections.singletonList |
1051 (new XPathType(" / ", XPathType.Filter.UNION))); |
1051 (new XPathType(" / ", XPathType.Filter.UNION))); |
1059 (CanonicalizationMethod.INCLUSIVE, |
1059 (CanonicalizationMethod.INCLUSIVE, |
1060 (C14NMethodParameterSpec) null), |
1060 (C14NMethodParameterSpec) null), |
1061 fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs); |
1061 fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs); |
1062 |
1062 |
1063 // create KeyInfo |
1063 // create KeyInfo |
1064 List<XMLStructure> kits = new ArrayList<XMLStructure>(2); |
1064 List<XMLStructure> kits = new ArrayList<>(2); |
1065 kits.add(kifac.newKeyValue(validatingKey)); |
1065 kits.add(kifac.newKeyValue(validatingKey)); |
1066 List<Object> xds = new ArrayList<Object>(2); |
1066 List<Object> xds = new ArrayList<>(2); |
1067 xds.add("CN=User"); |
1067 xds.add("CN=User"); |
1068 xds.add(signingCert); |
1068 xds.add(signingCert); |
1069 kits.add(kifac.newX509Data(xds)); |
1069 kits.add(kifac.newX509Data(xds)); |
1070 KeyInfo ki = kifac.newKeyInfo(kits); |
1070 KeyInfo ki = kifac.newKeyInfo(kits); |
1071 |
1071 |