2.0 SOAP Encoder
[shibboleth/cpp-opensaml.git] / saml / security / impl / ChainingTrustEngine.cpp
1 /*
2  *  Copyright 2001-2005 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  * ChainingTrustEngine.cpp
19  * 
20  * TrustEngine that uses multiple engines in sequence.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "security/ChainingTrustEngine.h"
26
27 using namespace opensaml::saml2md;
28 using namespace opensaml;
29 using namespace xmlsignature;
30 using namespace std;
31
32 namespace opensaml {
33     TrustEngine* SAML_DLLLOCAL ChainingTrustEngineFactory(const DOMElement* const & e)
34     {
35         return new ChainingTrustEngine(e);
36     }
37 };
38
39 static const XMLCh GenericTrustEngine[] =           UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
40 static const XMLCh type[] =                         UNICODE_LITERAL_4(t,y,p,e);
41
42 ChainingTrustEngine::ChainingTrustEngine(const DOMElement* e) {
43     try {
44         e = e ? xmltooling::XMLHelper::getFirstChildElement(e, GenericTrustEngine) : NULL;
45         while (e) {
46             xmltooling::auto_ptr_char temp(e->getAttributeNS(NULL,type));
47             if (temp.get()) {
48                 auto_ptr<TrustEngine> engine(
49                     SAMLConfig::getConfig().TrustEngineManager.newPlugin(temp.get(), e)
50                     );
51                 X509TrustEngine* x509 = dynamic_cast<X509TrustEngine*>(engine.get());
52                 if (x509) {
53                     m_engines.push_back(x509);
54                     engine.release();
55                 }
56                 else {
57                     throw xmltooling::UnknownExtensionException("Embedded trust engine does not support required interface.");
58                 }
59             }
60             e = xmltooling::XMLHelper::getNextSiblingElement(e, GenericTrustEngine);
61         }
62     }
63     catch (xmltooling::XMLToolingException&) {
64         for_each(m_engines.begin(), m_engines.end(), xmltooling::cleanup<X509TrustEngine>());
65         throw;
66     }
67 }
68
69 ChainingTrustEngine::~ChainingTrustEngine() {
70     for_each(m_engines.begin(), m_engines.end(), xmltooling::cleanup<X509TrustEngine>());
71 }
72
73 bool ChainingTrustEngine::validate(
74     Signature& sig,
75     const RoleDescriptor& role,
76     const KeyResolver* keyResolver
77     ) const
78 {
79     for (vector<X509TrustEngine*>::const_iterator i=m_engines.begin(); i!=m_engines.end(); ++i) {
80         if (static_cast<TrustEngine*>(*i)->validate(sig,role,keyResolver))
81             return true;
82     }
83     return false;
84 }
85
86 bool ChainingTrustEngine::validate(
87     const XMLCh* sigAlgorithm,
88     const char* sig,
89     KeyInfo* keyInfo,
90     const char* in,
91     unsigned int in_len,
92     const RoleDescriptor& role,
93     const KeyResolver* keyResolver
94     ) const
95 {
96     for (vector<X509TrustEngine*>::const_iterator i=m_engines.begin(); i!=m_engines.end(); ++i) {
97         if (static_cast<TrustEngine*>(*i)->validate(sigAlgorithm, sig, keyInfo, in, in_len, role, keyResolver))
98             return true;
99     }
100     return false;
101 }
102
103 bool ChainingTrustEngine::validate(
104     XSECCryptoX509* certEE,
105     const vector<XSECCryptoX509*>& certChain,
106     const RoleDescriptor& role,
107     bool checkName,
108     const KeyResolver* keyResolver
109     ) const
110 {
111     for (vector<X509TrustEngine*>::const_iterator i=m_engines.begin(); i!=m_engines.end(); ++i) {
112         if ((*i)->validate(certEE,certChain,role,checkName,keyResolver))
113             return true;
114     }
115     return false;
116 }