Change license header, remove stale pkg files.
[shibboleth/cpp-opensaml.git] / saml / signature / SignatureProfileValidator.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * SignatureProfileValidator.cpp
23  * 
24  * SAML-specific signature verification.
25  */
26  
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "signature/SignableObject.h"
30 #include "signature/SignatureProfileValidator.h"
31
32 #include <xmltooling/signature/Signature.h>
33
34 #include <xercesc/util/XMLUniDefs.hpp>
35 #include <xsec/dsig/DSIGReference.hpp>
36 #include <xsec/dsig/DSIGSignature.hpp>
37 #include <xsec/dsig/DSIGTransformList.hpp>
38
39 using namespace opensaml;
40 using namespace xmlsignature;
41 using namespace xmltooling;
42 using namespace std;
43
44 SignatureProfileValidator::SignatureProfileValidator()
45 {
46 }
47
48 SignatureProfileValidator::~SignatureProfileValidator()
49 {
50 }
51
52 void SignatureProfileValidator::validate(const XMLObject* xmlObject) const
53 {
54     const Signature* sigObj=dynamic_cast<const Signature*>(xmlObject);
55     if (!sigObj)
56         throw ValidationException("Validator only applies to Signature objects.");
57     validateSignature(*sigObj);
58 }
59
60 void SignatureProfileValidator::validateSignature(const Signature& sigObj) const
61 {
62     DSIGSignature* sig=sigObj.getXMLSignature();
63     if (!sig)
64         throw ValidationException("Signature does not exist yet.");
65
66     const SignableObject* signableObj=dynamic_cast<const SignableObject*>(sigObj.getParent());
67     if (!signableObj)
68         throw ValidationException("Signature is not a child of a signable SAML object.");
69     
70     bool valid=false;
71     DSIGReferenceList* refs=sig->getReferenceList();
72     if (refs && refs->getSize()==1) {
73         DSIGReference* ref=refs->item(0);
74         if (ref) {
75             const XMLCh* URI=ref->getURI();
76             const XMLCh* ID=signableObj->getXMLID();
77             if (URI==nullptr || *URI==0 || (*URI==chPound && ID && !XMLString::compareString(URI+1,ID))) {
78                 DSIGTransformList* tlist=ref->getTransforms();
79                 if (tlist->getSize() <= 2) { 
80                     for (unsigned int i=0; tlist && i<tlist->getSize(); i++) {
81                         if (tlist->item(i)->getTransformType()==TRANSFORM_ENVELOPED_SIGNATURE)
82                             valid=true;
83                         else if (tlist->item(i)->getTransformType()!=TRANSFORM_EXC_C14N &&
84                                  tlist->item(i)->getTransformType()!=TRANSFORM_C14N) {
85                             valid=false;
86                             break;
87                         }
88                     }
89                 }
90             }
91         }
92     }
93     
94     if (!valid)
95         throw ValidationException("Invalid signature profile for SAML object.");
96 }