Update copyright.
[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 "security/ChainingTrustEngine.h"
26
27 #include <xercesc/util/XMLUniDefs.hpp>
28
29 using namespace xmlsignature;
30 using namespace xmltooling;
31 using namespace std;
32
33 namespace xmltooling {
34     TrustEngine* XMLTOOL_DLLLOCAL ChainingTrustEngineFactory(const DOMElement* const & e)
35     {
36         return new ChainingTrustEngine(e);
37     }
38 };
39
40 static const XMLCh GenericTrustEngine[] =           UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
41 static const XMLCh type[] =                         UNICODE_LITERAL_4(t,y,p,e);
42
43 ChainingTrustEngine::ChainingTrustEngine(const DOMElement* e) : OpenSSLTrustEngine(e) {
44     try {
45         e = e ? xmltooling::XMLHelper::getFirstChildElement(e, GenericTrustEngine) : NULL;
46         while (e) {
47             auto_ptr_char temp(e->getAttributeNS(NULL,type));
48             if (temp.get())
49                 m_engines.push_back(XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(temp.get(), e));
50             e = xmltooling::XMLHelper::getNextSiblingElement(e, GenericTrustEngine);
51         }
52     }
53     catch (xmltooling::XMLToolingException&) {
54         for_each(m_engines.begin(), m_engines.end(), xmltooling::cleanup<TrustEngine>());
55         throw;
56     }
57 }
58
59 ChainingTrustEngine::~ChainingTrustEngine() {
60     for_each(m_engines.begin(), m_engines.end(), xmltooling::cleanup<TrustEngine>());
61 }
62
63 bool ChainingTrustEngine::validate(
64     Signature& sig,
65     const KeyInfoSource& keyInfoSource,
66     const KeyResolver* keyResolver
67     ) const
68 {
69     for (vector<TrustEngine*>::const_iterator i=m_engines.begin(); i!=m_engines.end(); ++i) {
70         if ((*i)->validate(sig,keyInfoSource,keyResolver))
71             return true;
72     }
73     return false;
74 }
75
76 bool ChainingTrustEngine::validate(
77     const XMLCh* sigAlgorithm,
78     const char* sig,
79     KeyInfo* keyInfo,
80     const char* in,
81     unsigned int in_len,
82     const KeyInfoSource& keyInfoSource,
83     const KeyResolver* keyResolver
84     ) const
85 {
86     for (vector<TrustEngine*>::const_iterator i=m_engines.begin(); i!=m_engines.end(); ++i) {
87         if ((*i)->validate(sigAlgorithm, sig, keyInfo, in, in_len, keyInfoSource, keyResolver))
88             return true;
89     }
90     return false;
91 }
92
93 bool ChainingTrustEngine::validate(
94     XSECCryptoX509* certEE,
95     const vector<XSECCryptoX509*>& certChain,
96     const KeyInfoSource& keyInfoSource,
97     bool checkName,
98     const KeyResolver* keyResolver
99     ) const
100 {
101     X509TrustEngine* down;
102     for (vector<TrustEngine*>::const_iterator i=m_engines.begin(); i!=m_engines.end(); ++i) {
103         if ((down = dynamic_cast<X509TrustEngine*>(*i)) &&
104                 down->validate(certEE,certChain,keyInfoSource,checkName,keyResolver))
105             return true;
106     }
107     return false;
108 }
109
110 bool ChainingTrustEngine::validate(
111     X509* certEE,
112     STACK_OF(X509)* certChain,
113     const KeyInfoSource& keyInfoSource,
114     bool checkName,
115     const xmlsignature::KeyResolver* keyResolver
116     ) const
117 {
118     OpenSSLTrustEngine* down;
119     for (vector<TrustEngine*>::const_iterator i=m_engines.begin(); i!=m_engines.end(); ++i) {
120         if ((down = dynamic_cast<OpenSSLTrustEngine*>(*i)) &&
121                 down->validate(certEE,certChain,keyInfoSource,checkName,keyResolver))
122             return true;
123     }
124     return false;
125 }