SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / StaticPKIXTrustEngine.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * PKIXTrustEngine.cpp
23  * 
24  * Shibboleth-specific PKIX-validation TrustEngine.
25  */
26
27 #include "internal.h"
28
29 #include "logging.h"
30 #include "XMLToolingConfig.h"
31 #include "security/AbstractPKIXTrustEngine.h"
32 #include "security/CredentialResolver.h"
33 #include "security/X509Credential.h"
34 #include "util/XMLHelper.h"
35
36 #include <xercesc/util/XMLUniDefs.hpp>
37
38 using namespace xmlsignature;
39 using namespace xmltooling;
40 using namespace xercesc;
41 using namespace std;
42
43 namespace xmltooling {
44
45     static const XMLCh _CredentialResolver[] =  UNICODE_LITERAL_18(C,r,e,d,e,n,t,i,a,l,R,e,s,o,l,v,e,r);
46     static const XMLCh type[] =                 UNICODE_LITERAL_4(t,y,p,e);
47     static const XMLCh certificate[] =          UNICODE_LITERAL_11(c,e,r,t,i,f,i,c,a,t,e);
48     static const XMLCh Certificate[] =          UNICODE_LITERAL_11(C,e,r,t,i,f,i,c,a,t,e);
49     static const XMLCh Path[] =                 UNICODE_LITERAL_4(P,a,t,h);
50     static const XMLCh verifyDepth[] =          UNICODE_LITERAL_11(v,e,r,i,f,y,D,e,p,t,h);
51
52     class XMLTOOL_DLLLOCAL StaticPKIXTrustEngine : public AbstractPKIXTrustEngine
53     {
54     public:
55         StaticPKIXTrustEngine(const DOMElement* e=nullptr);
56
57         virtual ~StaticPKIXTrustEngine() {}
58         
59         AbstractPKIXTrustEngine::PKIXValidationInfoIterator* getPKIXValidationInfoIterator(
60             const CredentialResolver& pkixSource, CredentialCriteria* criteria=nullptr
61             ) const;
62
63         const KeyInfoResolver* getKeyInfoResolver() const {
64             return m_keyInfoResolver ? m_keyInfoResolver : XMLToolingConfig::getConfig().getKeyInfoResolver();
65         }
66
67     private:
68         int m_depth;
69         auto_ptr<CredentialResolver> m_credResolver;
70         friend class XMLTOOL_DLLLOCAL StaticPKIXIterator;
71     };
72     
73     TrustEngine* XMLTOOL_DLLLOCAL StaticPKIXTrustEngineFactory(const DOMElement* const & e)
74     {
75         return new StaticPKIXTrustEngine(e);
76     }
77
78     class XMLTOOL_DLLLOCAL StaticPKIXIterator : public AbstractPKIXTrustEngine::PKIXValidationInfoIterator
79     {
80     public:
81         StaticPKIXIterator(const StaticPKIXTrustEngine& engine) : m_engine(engine), m_done(false) {
82             // Merge together all X509Credentials we can resolve.
83             m_engine.m_credResolver->lock();
84             try {
85                 vector<const Credential*> creds;
86                 m_engine.m_credResolver->resolve(creds);
87                 for (vector<const Credential*>::const_iterator i = creds.begin(); i != creds.end(); ++i) {
88                     const X509Credential* xcred = dynamic_cast<const X509Credential*>(*i);
89                     if (xcred) {
90                         m_certs.insert(m_certs.end(), xcred->getEntityCertificateChain().begin(), xcred->getEntityCertificateChain().end());
91                         m_crls.insert(m_crls.end(), xcred->getCRLs().begin(), xcred->getCRLs().end());
92                     }
93                 }
94             }
95             catch (exception& ex) {
96                 logging::Category::getInstance(XMLTOOLING_LOGCAT ".TrustEngine.StaticPKIX").error(ex.what());
97             }
98         }
99
100         virtual ~StaticPKIXIterator() {
101             m_engine.m_credResolver->unlock();
102         }
103
104         bool next() {
105             if (m_done)
106                 return false;
107             m_done = true;
108             return true;
109         }
110
111         int getVerificationDepth() const {
112             return m_engine.m_depth;
113         }
114         
115         const vector<XSECCryptoX509*>& getTrustAnchors() const {
116             return m_certs;
117         }
118
119         const vector<XSECCryptoX509CRL*>& getCRLs() const {
120             return m_crls;
121         }
122     
123     private:
124         const StaticPKIXTrustEngine& m_engine;
125         vector<XSECCryptoX509*> m_certs;
126         vector<XSECCryptoX509CRL*> m_crls;
127         bool m_done;
128     };
129 };
130
131 StaticPKIXTrustEngine::StaticPKIXTrustEngine(const DOMElement* e)
132     : AbstractPKIXTrustEngine(e), m_depth(XMLHelper::getAttrInt(e, 1, verifyDepth))
133 {
134     if (e && e->hasAttributeNS(nullptr, certificate)) {
135         // Simple File resolver config rooted here.
136         m_credResolver.reset(XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(FILESYSTEM_CREDENTIAL_RESOLVER, e));
137     }
138     else {
139         e = e ? XMLHelper::getFirstChildElement(e, _CredentialResolver) : nullptr;
140         string t = XMLHelper::getAttrString(e, nullptr, type);
141         if (!t.empty())
142             m_credResolver.reset(XMLToolingConfig::getConfig().CredentialResolverManager.newPlugin(t.c_str(), e));
143         else
144             throw XMLSecurityException("Missing <CredentialResolver> element, or no type attribute found");
145     }
146 }
147
148 AbstractPKIXTrustEngine::PKIXValidationInfoIterator* StaticPKIXTrustEngine::getPKIXValidationInfoIterator(
149     const CredentialResolver& pkixSource, CredentialCriteria* criteria
150     ) const
151 {
152     return new StaticPKIXIterator(*this);
153 }