Address certificate object lifetime with wrapper class.
[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             );\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             );\r
56 \r
57     private:\r
58     };\r
59 \r
60     TrustEngine* XMLTOOL_DLLLOCAL ExplicitKeyTrustEngineFactory(const DOMElement* const & e)\r
61     {\r
62         return new ExplicitKeyTrustEngine(e);\r
63     }\r
64 };\r
65 \r
66 bool ExplicitKeyTrustEngine::validate(\r
67     Signature& sig,\r
68     TrustEngine::KeyInfoIterator& keyInfoSource,\r
69     const KeyResolver* keyResolver\r
70     )\r
71 {\r
72 #ifdef _DEBUG\r
73     NDC ndc("validate");\r
74 #endif\r
75     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");\r
76     \r
77     if (!keyInfoSource.hasNext()) {\r
78         log.warn("unable to validate signature, no key information available for peer");\r
79         return false;\r
80     }\r
81     \r
82     log.debug("attempting to validate signature with the key information for peer");\r
83     SignatureValidator sigValidator;\r
84     while (keyInfoSource.hasNext()) {\r
85         XSECCryptoKey* key = (keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoSource.next());\r
86         if (key) {\r
87             log.debug("attempting to validate signature with public key...");\r
88             try {\r
89                 sigValidator.setKey(key);   // key now owned by validator\r
90                 sigValidator.validate(&sig);\r
91                 log.info("signature validated with public key");\r
92                 return true;\r
93             }\r
94             catch (ValidationException& e) {\r
95                 if (log.isDebugEnabled()) {\r
96                     log.debug("public key did not validate signature: %s", e.what());\r
97                 }\r
98             }\r
99         }\r
100         else {\r
101             log.debug("key information does not resolve to a public key, skipping it");\r
102         }\r
103     }\r
104 \r
105     log.error("no peer key information validated the signature");\r
106     return false;\r
107 }\r
108 \r
109 bool ExplicitKeyTrustEngine::validate(\r
110     XSECCryptoX509* certEE,\r
111     const vector<XSECCryptoX509*>& certChain,\r
112     TrustEngine::KeyInfoIterator& keyInfoSource,\r
113     bool checkName,\r
114     const KeyResolver* keyResolver\r
115     )\r
116 {\r
117 #ifdef _DEBUG\r
118     NDC ndc("validate");\r
119 #endif\r
120     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");\r
121     \r
122     if (!certEE) {\r
123         log.error("unable to validate, end-entity certificate was null");\r
124         return false;\r
125     }\r
126     else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {\r
127         log.error("only the OpenSSL XSEC provider is supported");\r
128         return false;\r
129     }\r
130     else if (!keyInfoSource.hasNext()) {\r
131         log.warn("unable to validate, no key information available for peer");\r
132         return false;\r
133     }\r
134 \r
135     // The new "basic" trust implementation relies solely on certificates living within the\r
136     // role interface to verify the EE certificate.\r
137 \r
138     log.debug("attempting to match key information from peer with end-entity certificate");\r
139     while (keyInfoSource.hasNext()) {\r
140         KeyResolver::ResolvedCertificates resolvedCerts;\r
141         if (0 == (keyResolver ? keyResolver : m_keyResolver)->resolveCertificates(keyInfoSource.next(),resolvedCerts)) {\r
142             log.debug("key information does not resolve to a certificate, skipping it");\r
143             continue;\r
144         }\r
145 \r
146         log.debug("checking if certificates contained within key information match end-entity certificate");\r
147         if (resolvedCerts.v().front()->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {\r
148             log.error("only the OpenSSL XSEC provider is supported");\r
149             continue;\r
150         }\r
151         else if (!X509_cmp(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(),static_cast<OpenSSLCryptoX509*>(resolvedCerts.v().front())->getOpenSSLX509())) {\r
152             log.info("end-entity certificate matches certificate from peer key information");\r
153             return true;\r
154         }\r
155     }\r
156 \r
157     log.debug("no certificates within this peer's key information matched the given end-entity certificate");\r
158     return false;\r
159 }\r