gcc const fix, converted linefeeds
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / ExplicitKeyTrustEngine.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  * ExplicitKeyTrustEngine.cpp
19  * 
20  * TrustEngine based on explicit knowledge of peer key information.
21  */
22
23 #include "internal.h"
24 #include "security/X509TrustEngine.h"
25 #include "signature/SignatureValidator.h"
26 #include "util/NDC.h"
27
28 #include <log4cpp/Category.hh>
29 #include <xercesc/util/XMLUniDefs.hpp>
30 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
31
32 using namespace xmlsignature;
33 using namespace xmltooling;
34 using namespace log4cpp;
35 using namespace std;
36
37 namespace xmltooling {
38     class XMLTOOL_DLLLOCAL ExplicitKeyTrustEngine : public X509TrustEngine
39     {
40     public:
41         ExplicitKeyTrustEngine(const DOMElement* e) : X509TrustEngine(e) {}
42         virtual ~ExplicitKeyTrustEngine() {}
43
44         virtual bool validate(
45             Signature& sig,
46             TrustEngine::KeyInfoIterator& keyInfoSource,
47             const KeyResolver* keyResolver=NULL
48             ) const;
49         virtual bool validate(
50             XSECCryptoX509* certEE,
51             const vector<XSECCryptoX509*>& certChain,
52             TrustEngine::KeyInfoIterator& keyInfoSource,
53             bool checkName=true,
54             const KeyResolver* keyResolver=NULL
55             ) const;
56     };
57
58     TrustEngine* XMLTOOL_DLLLOCAL ExplicitKeyTrustEngineFactory(const DOMElement* const & e)
59     {
60         return new ExplicitKeyTrustEngine(e);
61     }
62 };
63
64 bool ExplicitKeyTrustEngine::validate(
65     Signature& sig,
66     TrustEngine::KeyInfoIterator& keyInfoSource,
67     const KeyResolver* keyResolver
68     ) const
69 {
70 #ifdef _DEBUG
71     NDC ndc("validate");
72 #endif
73     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
74     
75     if (!keyInfoSource.hasNext()) {
76         log.warn("unable to validate signature, no key information available for peer");
77         return false;
78     }
79     
80     log.debug("attempting to validate signature with the key information for peer");
81     SignatureValidator sigValidator;
82     while (keyInfoSource.hasNext()) {
83         XSECCryptoKey* key = (keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoSource.next());
84         if (key) {
85             log.debug("attempting to validate signature with public key...");
86             try {
87                 sigValidator.setKey(key);   // key now owned by validator
88                 sigValidator.validate(&sig);
89                 log.info("signature validated with public key");
90                 return true;
91             }
92             catch (ValidationException& e) {
93                 if (log.isDebugEnabled()) {
94                     log.debug("public key did not validate signature: %s", e.what());
95                 }
96             }
97         }
98         else {
99             log.debug("key information does not resolve to a public key, skipping it");
100         }
101     }
102
103     log.error("no peer key information validated the signature");
104     return false;
105 }
106
107 bool ExplicitKeyTrustEngine::validate(
108     XSECCryptoX509* certEE,
109     const vector<XSECCryptoX509*>& certChain,
110     TrustEngine::KeyInfoIterator& keyInfoSource,
111     bool checkName,
112     const KeyResolver* keyResolver
113     ) const
114 {
115 #ifdef _DEBUG
116     NDC ndc("validate");
117 #endif
118     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
119     
120     if (!certEE) {
121         log.error("unable to validate, end-entity certificate was null");
122         return false;
123     }
124     else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
125         log.error("only the OpenSSL XSEC provider is supported");
126         return false;
127     }
128     else if (!keyInfoSource.hasNext()) {
129         log.warn("unable to validate, no key information available for peer");
130         return false;
131     }
132
133     // The new "basic" trust implementation relies solely on certificates living within the
134     // role interface to verify the EE certificate.
135
136     log.debug("attempting to match key information from peer with end-entity certificate");
137     while (keyInfoSource.hasNext()) {
138         KeyResolver::ResolvedCertificates resolvedCerts;
139         if (0 == (keyResolver ? keyResolver : m_keyResolver)->resolveCertificates(keyInfoSource.next(),resolvedCerts)) {
140             log.debug("key information does not resolve to a certificate, skipping it");
141             continue;
142         }
143
144         log.debug("checking if certificates contained within key information match end-entity certificate");
145         if (resolvedCerts.v().front()->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
146             log.error("only the OpenSSL XSEC provider is supported");
147             continue;
148         }
149         else if (!X509_cmp(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(),static_cast<OpenSSLCryptoX509*>(resolvedCerts.v().front())->getOpenSSLX509())) {
150             log.info("end-entity certificate matches certificate from peer key information");
151             return true;
152         }
153     }
154
155     log.debug("no certificates within this peer's key information matched the given end-entity certificate");
156     return false;
157 }