Xerces 3 revisions.
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / ChainingTrustEngine.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  * ChainingTrustEngine.cpp
19  * 
20  * TrustEngine that uses multiple engines in sequence.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "logging.h"
26 #include "security/ChainingTrustEngine.h"
27 #include "util/XMLHelper.h"
28
29 #include <xercesc/util/XMLUniDefs.hpp>
30
31 using namespace xmlsignature;
32 using namespace xmltooling::logging;
33 using namespace xmltooling;
34 using namespace std;
35
36 using xercesc::DOMElement;
37
38 namespace xmltooling {
39     TrustEngine* XMLTOOL_DLLLOCAL ChainingTrustEngineFactory(const DOMElement* const & e)
40     {
41         return new ChainingTrustEngine(e);
42     }
43 };
44
45 static const XMLCh _TrustEngine[] =                 UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
46 static const XMLCh type[] =                         UNICODE_LITERAL_4(t,y,p,e);
47
48 ChainingTrustEngine::ChainingTrustEngine(const DOMElement* e) : TrustEngine(e) {
49     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine."CHAINING_TRUSTENGINE);
50     e = e ? XMLHelper::getFirstChildElement(e, _TrustEngine) : NULL;
51     while (e) {
52         try {
53             auto_ptr_char temp(e->getAttributeNS(NULL,type));
54             if (temp.get() && *temp.get()) {
55                 log.info("building TrustEngine of type %s", temp.get());
56                 TrustEngine* engine = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(temp.get(), e);
57                 m_engines.push_back(engine);
58                 SignatureTrustEngine* sig = dynamic_cast<SignatureTrustEngine*>(engine);
59                 if (sig)
60                     m_sigEngines.push_back(sig);
61                 X509TrustEngine* x509 = dynamic_cast<X509TrustEngine*>(engine);
62                 if (x509)
63                     m_x509Engines.push_back(x509);
64                 OpenSSLTrustEngine* ossl = dynamic_cast<OpenSSLTrustEngine*>(engine);
65                 if (ossl)
66                     m_osslEngines.push_back(ossl);
67             }
68         }
69         catch (exception& ex) {
70             log.error("error building TrustEngine: %s", ex.what());
71         }
72         e = XMLHelper::getNextSiblingElement(e, _TrustEngine);
73     }
74 }
75
76 ChainingTrustEngine::~ChainingTrustEngine() {
77     for_each(m_engines.begin(), m_engines.end(), xmltooling::cleanup<TrustEngine>());
78 }
79
80 bool ChainingTrustEngine::validate(Signature& sig, const CredentialResolver& credResolver, CredentialCriteria* criteria) const
81 {
82     for (vector<SignatureTrustEngine*>::const_iterator i=m_sigEngines.begin(); i!=m_sigEngines.end(); ++i) {
83         if ((*i)->validate(sig,credResolver,criteria))
84             return true;
85     }
86     return false;
87 }
88
89 bool ChainingTrustEngine::validate(
90     const XMLCh* sigAlgorithm,
91     const char* sig,
92     KeyInfo* keyInfo,
93     const char* in,
94     unsigned int in_len,
95     const CredentialResolver& credResolver,
96     CredentialCriteria* criteria
97     ) const
98 {
99     for (vector<SignatureTrustEngine*>::const_iterator i=m_sigEngines.begin(); i!=m_sigEngines.end(); ++i) {
100         if ((*i)->validate(sigAlgorithm, sig, keyInfo, in, in_len, credResolver, criteria))
101             return true;
102     }
103     return false;
104 }
105
106 bool ChainingTrustEngine::validate(
107     XSECCryptoX509* certEE,
108     const vector<XSECCryptoX509*>& certChain,
109     const CredentialResolver& credResolver,
110     CredentialCriteria* criteria
111     ) const
112 {
113     for (vector<X509TrustEngine*>::const_iterator i=m_x509Engines.begin(); i!=m_x509Engines.end(); ++i) {
114         if ((*i)->validate(certEE,certChain,credResolver,criteria))
115             return true;
116     }
117     return false;
118 }
119
120 bool ChainingTrustEngine::validate(
121     X509* certEE,
122     STACK_OF(X509)* certChain,
123     const CredentialResolver& credResolver,
124     CredentialCriteria* criteria
125     ) const
126 {
127     for (vector<OpenSSLTrustEngine*>::const_iterator i=m_osslEngines.begin(); i!=m_osslEngines.end(); ++i) {
128         if ((*i)->validate(certEE,certChain,credResolver,criteria))
129             return true;
130     }
131     return false;
132 }