View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.PGPData;
26  import org.opensaml.xml.signature.PGPKeyID;
27  import org.opensaml.xml.signature.PGPKeyPacket;
28  import org.opensaml.xml.signature.X509CRL;
29  import org.opensaml.xml.signature.X509Certificate;
30  import org.opensaml.xml.signature.X509Data;
31  import org.opensaml.xml.signature.X509IssuerSerial;
32  import org.opensaml.xml.signature.X509SKI;
33  import org.opensaml.xml.signature.X509SubjectName;
34  import org.opensaml.xml.util.XMLConstants;
35  import org.opensaml.xml.validation.ValidationException;
36  import org.opensaml.xml.validation.Validator;
37  
38  /**
39   * Checks {@link org.opensaml.xml.signature.PGPData} for Schema compliance. 
40   */
41  public class PGPDataSchemaValidator implements Validator<PGPData> {
42      
43      /** QNames corresponding to the valid children. */
44      private static final Set<QName> VALID_DS_CHILD_NAMES;
45  
46      /** {@inheritDoc} */
47      public void validate(PGPData xmlObject) throws ValidationException {
48          validateChildrenPresence(xmlObject);
49          validateChildrenNamespaces(xmlObject);
50      }
51      
52      /**
53       * Get the QNames corresponding to the valid children
54       * defined in the XML Signature namespace.
55       * 
56       * @return list of valid child QNames
57       */
58      protected static Set<QName> getValidDSChildNames() {
59          return VALID_DS_CHILD_NAMES;
60      }
61  
62      /**
63       * Validate that at least one mandatory child is present.
64       * 
65       * @param xmlObject the object to validate
66       * @throws ValidationException  thrown if the object is invalid
67       */
68      protected void validateChildrenPresence(PGPData xmlObject) throws ValidationException {
69          if (xmlObject.getPGPKeyID() == null && xmlObject.getPGPKeyPacket() == null) {
70              throw new ValidationException("PGPData must contain at least one of PGPKeyID or PGPKeyPacket");
71          }
72      }
73      
74      /**
75       * Validate that all children are either ones defined within the XML Signature schema,
76       * or are from another namespace.
77       * 
78       * @param xmlObject the object to validate
79       * @throws ValidationException thrown if the object is invalid
80       */
81      protected void validateChildrenNamespaces(PGPData xmlObject) throws ValidationException {
82          // Validate that any unknown children are from another namespace.
83          for (XMLObject child : xmlObject.getUnknownXMLObjects()) {
84              QName childName = child.getElementQName();
85              if (! getValidDSChildNames().contains(childName) 
86                      && XMLConstants.XMLSIG_NS.equals(childName.getNamespaceURI())) {
87                  throw new ValidationException("PGPData contains an illegal child extension element: " + childName);
88              }
89          }
90      }
91      
92      static {
93          VALID_DS_CHILD_NAMES = new HashSet<QName>(5);
94          VALID_DS_CHILD_NAMES.add(PGPKeyID.DEFAULT_ELEMENT_NAME);
95          VALID_DS_CHILD_NAMES.add(PGPKeyPacket.DEFAULT_ELEMENT_NAME);
96      }
97  }