1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.xml.signature.validator;
18
19 import java.util.HashSet;
20 import java.util.Set;
21
22 import javax.xml.namespace.QName;
23
24 import org.opensaml.xml.XMLObject;
25 import org.opensaml.xml.signature.KeyInfoType;
26 import org.opensaml.xml.signature.KeyName;
27 import org.opensaml.xml.signature.KeyValue;
28 import org.opensaml.xml.signature.MgmtData;
29 import org.opensaml.xml.signature.PGPData;
30 import org.opensaml.xml.signature.RetrievalMethod;
31 import org.opensaml.xml.signature.SPKIData;
32 import org.opensaml.xml.signature.X509Data;
33 import org.opensaml.xml.util.XMLConstants;
34 import org.opensaml.xml.validation.ValidationException;
35 import org.opensaml.xml.validation.Validator;
36
37
38
39
40 public class KeyInfoTypeSchemaValidator implements Validator<KeyInfoType> {
41
42
43 private static final Set<QName> VALID_DS_CHILD_NAMES;
44
45
46 public void validate(KeyInfoType xmlObject) throws ValidationException {
47 validateChildrenPresence(xmlObject);
48 validateChildrenNamespaces(xmlObject);
49 }
50
51
52
53
54
55
56
57 protected static Set<QName> getValidDSChildNames() {
58 return VALID_DS_CHILD_NAMES;
59 }
60
61
62
63
64
65
66
67 protected void validateChildrenPresence(KeyInfoType xmlObject) throws ValidationException {
68 if (xmlObject.getXMLObjects().isEmpty()) {
69 throw new ValidationException("No children were present in the KeyInfoType object");
70 }
71 }
72
73
74
75
76
77
78
79
80 protected void validateChildrenNamespaces(KeyInfoType xmlObject) throws ValidationException {
81
82
83 for (XMLObject child : xmlObject.getXMLObjects()) {
84 QName childName = child.getElementQName();
85 if (! getValidDSChildNames().contains(childName)
86 && XMLConstants.XMLSIG_NS.equals(childName.getNamespaceURI())) {
87 throw new ValidationException("KeyInfoType contains an illegal child extension element: " + childName);
88 }
89 }
90 }
91
92 static {
93 VALID_DS_CHILD_NAMES = new HashSet<QName>(10);
94 VALID_DS_CHILD_NAMES.add(KeyName.DEFAULT_ELEMENT_NAME);
95 VALID_DS_CHILD_NAMES.add(KeyValue.DEFAULT_ELEMENT_NAME);
96 VALID_DS_CHILD_NAMES.add(RetrievalMethod.DEFAULT_ELEMENT_NAME);
97 VALID_DS_CHILD_NAMES.add(X509Data.DEFAULT_ELEMENT_NAME);
98 VALID_DS_CHILD_NAMES.add(PGPData.DEFAULT_ELEMENT_NAME);
99 VALID_DS_CHILD_NAMES.add(SPKIData.DEFAULT_ELEMENT_NAME);
100 VALID_DS_CHILD_NAMES.add(MgmtData.DEFAULT_ELEMENT_NAME);
101 }
102 }