4d1eeb26132783df3144f9108b59c5ac39cb3bfe
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / ExplicitKeyTrustEngine.cpp
1 /*\r
2  *  Copyright 2001-2005 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * ExplicitKeyTrustEngine.cpp\r
19  * \r
20  * TrustEngine based on explicit knowledge of peer key information.\r
21  */\r
22 \r
23 #include "internal.h"\r
24 #include "security/X509TrustEngine.h"\r
25 #include "signature/SignatureValidator.h"\r
26 #include "util/NDC.h"\r
27 \r
28 #include <log4cpp/Category.hh>\r
29 #include <xercesc/util/XMLUniDefs.hpp>\r
30 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>\r
31 \r
32 using namespace xmlsignature;\r
33 using namespace xmltooling;\r
34 using namespace log4cpp;\r
35 using namespace std;\r
36 \r
37 namespace xmltooling {\r
38     class XMLTOOL_DLLLOCAL ExplicitKeyTrustEngine : public X509TrustEngine\r
39     {\r
40     public:\r
41         ExplicitKeyTrustEngine(const DOMElement* e) : X509TrustEngine(e) {}\r
42         virtual ~ExplicitKeyTrustEngine() {}\r
43 \r
44         virtual bool validate(\r
45             Signature& sig,\r
46             TrustEngine::KeyInfoIterator& keyInfoSource,\r
47             const KeyResolver* keyResolver=NULL\r
48             ) const;\r
49         virtual bool validate(\r
50             XSECCryptoX509* certEE,\r
51             const vector<XSECCryptoX509*>& certChain,\r
52             TrustEngine::KeyInfoIterator& keyInfoSource,\r
53             bool checkName=true,\r
54             const KeyResolver* keyResolver=NULL\r
55             ) const;\r
56     };\r
57 \r
58     TrustEngine* XMLTOOL_DLLLOCAL ExplicitKeyTrustEngineFactory(const DOMElement* const & e)\r
59     {\r
60         return new ExplicitKeyTrustEngine(e);\r
61     }\r
62 };\r
63 \r
64 bool ExplicitKeyTrustEngine::validate(\r
65     Signature& sig,\r
66     TrustEngine::KeyInfoIterator& keyInfoSource,\r
67     const KeyResolver* keyResolver\r
68     ) const\r
69 {\r
70 #ifdef _DEBUG\r
71     NDC ndc("validate");\r
72 #endif\r
73     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");\r
74     \r
75     if (!keyInfoSource.hasNext()) {\r
76         log.warn("unable to validate signature, no key information available for peer");\r
77         return false;\r
78     }\r
79     \r
80     log.debug("attempting to validate signature with the key information for peer");\r
81     SignatureValidator sigValidator;\r
82     while (keyInfoSource.hasNext()) {\r
83         XSECCryptoKey* key = (keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoSource.next());\r
84         if (key) {\r
85             log.debug("attempting to validate signature with public key...");\r
86             try {\r
87                 sigValidator.setKey(key);   // key now owned by validator\r
88                 sigValidator.validate(&sig);\r
89                 log.info("signature validated with public key");\r
90                 return true;\r
91             }\r
92             catch (ValidationException& e) {\r
93                 if (log.isDebugEnabled()) {\r
94                     log.debug("public key did not validate signature: %s", e.what());\r
95                 }\r
96             }\r
97         }\r
98         else {\r
99             log.debug("key information does not resolve to a public key, skipping it");\r
100         }\r
101     }\r
102 \r
103     log.error("no peer key information validated the signature");\r
104     return false;\r
105 }\r
106 \r
107 bool ExplicitKeyTrustEngine::validate(\r
108     XSECCryptoX509* certEE,\r
109     const vector<XSECCryptoX509*>& certChain,\r
110     TrustEngine::KeyInfoIterator& keyInfoSource,\r
111     bool checkName,\r
112     const KeyResolver* keyResolver\r
113     ) const\r
114 {\r
115 #ifdef _DEBUG\r
116     NDC ndc("validate");\r
117 #endif\r
118     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");\r
119     \r
120     if (!certEE) {\r
121         log.error("unable to validate, end-entity certificate was null");\r
122         return false;\r
123     }\r
124     else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {\r
125         log.error("only the OpenSSL XSEC provider is supported");\r
126         return false;\r
127     }\r
128     else if (!keyInfoSource.hasNext()) {\r
129         log.warn("unable to validate, no key information available for peer");\r
130         return false;\r
131     }\r
132 \r
133     // The new "basic" trust implementation relies solely on certificates living within the\r
134     // role interface to verify the EE certificate.\r
135 \r
136     log.debug("attempting to match key information from peer with end-entity certificate");\r
137     while (keyInfoSource.hasNext()) {\r
138         KeyResolver::ResolvedCertificates resolvedCerts;\r
139         if (0 == (keyResolver ? keyResolver : m_keyResolver)->resolveCertificates(keyInfoSource.next(),resolvedCerts)) {\r
140             log.debug("key information does not resolve to a certificate, skipping it");\r
141             continue;\r
142         }\r
143 \r
144         log.debug("checking if certificates contained within key information match end-entity certificate");\r
145         if (resolvedCerts.v().front()->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {\r
146             log.error("only the OpenSSL XSEC provider is supported");\r
147             continue;\r
148         }\r
149         else if (!X509_cmp(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(),static_cast<OpenSSLCryptoX509*>(resolvedCerts.v().front())->getOpenSSLX509())) {\r
150             log.info("end-entity certificate matches certificate from peer key information");\r
151             return true;\r
152         }\r
153     }\r
154 \r
155     log.debug("no certificates within this peer's key information matched the given end-entity certificate");\r
156     return false;\r
157 }\r