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