Sync up ID attribute handling to latest tooling changes.
[shibboleth/cpp-opensaml.git] / saml / signature / SignatureProfileValidator.cpp
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * SignatureProfileValidator.cpp\r
19  * \r
20  * SAML-specific signature verification \r
21  */\r
22  \r
23 #include "internal.h"\r
24 #include "exceptions.h"\r
25 #include "signature/SignatureProfileValidator.h"\r
26 \r
27 #include <xmltooling/signature/Signature.h>\r
28 \r
29 #include <xercesc/util/XMLUniDefs.hpp>\r
30 #include <xsec/dsig/DSIGReference.hpp>\r
31 #include <xsec/dsig/DSIGTransformC14n.hpp>\r
32 #include <xsec/dsig/DSIGTransformList.hpp>\r
33 \r
34 using namespace opensaml;\r
35 using namespace xmlsignature;\r
36 using namespace xmltooling;\r
37 using namespace std;\r
38 \r
39 void SignatureProfileValidator::validate(const XMLObject* xmlObject) const\r
40 {\r
41     const Signature* sigObj=dynamic_cast<const Signature*>(xmlObject);\r
42     if (!sigObj)\r
43         throw ValidationException("Validator only applies to Signature objects.");\r
44     DSIGSignature* sig=sigObj->getXMLSignature();\r
45     if (!sig)\r
46         throw ValidationException("Signature does not exist yet.");\r
47 \r
48     const SignableObject* signableObj=dynamic_cast<const SignableObject*>(sigObj->getParent());\r
49     if (!signableObj)\r
50         throw ValidationException("Signature is not a child of a signable SAML object.");\r
51     \r
52     bool valid=false;\r
53     DSIGReferenceList* refs=sig->getReferenceList();\r
54     if (refs && refs->getSize()==1) {\r
55         DSIGReference* ref=refs->item(0);\r
56         if (ref) {\r
57             const XMLCh* URI=ref->getURI();\r
58             const XMLCh* ID=signableObj->getXMLID();\r
59             if (URI==NULL || *URI==0 || (*URI==chPound && ID && !XMLString::compareString(URI+1,ID))) {\r
60                 DSIGTransformList* tlist=ref->getTransforms();\r
61                 for (unsigned int i=0; tlist && i<tlist->getSize(); i++) {\r
62                     if (tlist->item(i)->getTransformType()==TRANSFORM_ENVELOPED_SIGNATURE)\r
63                         valid=true;\r
64                     else if (tlist->item(i)->getTransformType()!=TRANSFORM_EXC_C14N &&\r
65                              tlist->item(i)->getTransformType()!=TRANSFORM_C14N) {\r
66                         valid=false;\r
67                         break;\r
68                     }\r
69                 }\r
70             }\r
71         }\r
72     }\r
73     \r
74     if (!valid)\r
75         throw ValidationException("Invalid signature profile for SAML object.");\r
76 }\r