Basic assertion validator.
[shibboleth/cpp-opensaml.git] / saml / signature / SignatureProfileValidator.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
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 /**
18  * SignatureProfileValidator.cpp
19  * 
20  * SAML-specific signature verification 
21  */
22  
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "signature/SignatureProfileValidator.h"
26
27 #include <xmltooling/signature/Signature.h>
28
29 #include <xercesc/util/XMLUniDefs.hpp>
30 #include <xsec/dsig/DSIGReference.hpp>
31 #include <xsec/dsig/DSIGTransformC14n.hpp>
32 #include <xsec/dsig/DSIGTransformList.hpp>
33
34 using namespace opensaml;
35 using namespace xmlsignature;
36 using namespace xmltooling;
37 using namespace std;
38
39 void SignatureProfileValidator::validate(const XMLObject* xmlObject) const
40 {
41     const Signature* sigObj=dynamic_cast<const Signature*>(xmlObject);
42     if (!sigObj)
43         throw ValidationException("Validator only applies to Signature objects.");
44     validateSignature(*sigObj);
45 }
46
47 void SignatureProfileValidator::validateSignature(const Signature& sigObj) const
48 {
49     DSIGSignature* sig=sigObj.getXMLSignature();
50     if (!sig)
51         throw ValidationException("Signature does not exist yet.");
52
53     const SignableObject* signableObj=dynamic_cast<const SignableObject*>(sigObj.getParent());
54     if (!signableObj)
55         throw ValidationException("Signature is not a child of a signable SAML object.");
56     
57     bool valid=false;
58     DSIGReferenceList* refs=sig->getReferenceList();
59     if (refs && refs->getSize()==1) {
60         DSIGReference* ref=refs->item(0);
61         if (ref) {
62             const XMLCh* URI=ref->getURI();
63             const XMLCh* ID=signableObj->getXMLID();
64             if (URI==NULL || *URI==0 || (*URI==chPound && ID && !XMLString::compareString(URI+1,ID))) {
65                 DSIGTransformList* tlist=ref->getTransforms();
66                 for (unsigned int i=0; tlist && i<tlist->getSize(); i++) {
67                     if (tlist->item(i)->getTransformType()==TRANSFORM_ENVELOPED_SIGNATURE)
68                         valid=true;
69                     else if (tlist->item(i)->getTransformType()!=TRANSFORM_EXC_C14N &&
70                              tlist->item(i)->getTransformType()!=TRANSFORM_C14N) {
71                         valid=false;
72                         break;
73                     }
74                 }
75             }
76         }
77     }
78     
79     if (!valid)
80         throw ValidationException("Invalid signature profile for SAML object.");
81 }