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