Fix log messages.
[shibboleth/cpp-sp.git] / shibsp / attribute / KeyInfoAttributeDecoder.cpp
1 /*\r
2  *  Copyright 2009 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  * KeyInfoAttributeDecoder.cpp\r
19  *\r
20  * Decodes KeyInfo information into a SimpleAttribute.\r
21  */\r
22 \r
23 #include "internal.h"\r
24 #include "attribute/AttributeDecoder.h"\r
25 #include "attribute/SimpleAttribute.h"\r
26 \r
27 #include <saml/saml1/core/Assertions.h>\r
28 #include <saml/saml2/core/Assertions.h>\r
29 #include <xmltooling/security/SecurityHelper.h>\r
30 #include <xmltooling/signature/KeyInfo.h>\r
31 \r
32 using namespace shibsp;\r
33 using namespace opensaml;\r
34 using namespace xmlsignature;\r
35 using namespace xmltooling;\r
36 using namespace std;\r
37 \r
38 namespace shibsp {\r
39     class SHIBSP_DLLLOCAL KeyInfoAttributeDecoder : virtual public AttributeDecoder\r
40     {\r
41     public:\r
42         KeyInfoAttributeDecoder(const DOMElement* e);\r
43         ~KeyInfoAttributeDecoder() {\r
44             delete m_keyInfoResolver;\r
45         }\r
46 \r
47         Attribute* decode(\r
48             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=NULL, const char* relyingParty=NULL\r
49             ) const;\r
50 \r
51     private:\r
52         void extract(const KeyInfo* k, vector<string>& dest) const {\r
53             auto_ptr<Credential> cred (getKeyInfoResolver()->resolve(k, Credential::RESOLVE_KEYS));\r
54             if (cred.get()) {\r
55                 dest.push_back(string());\r
56                 dest.back() = SecurityHelper::getDEREncoding(*cred.get());\r
57                 if (dest.back().empty())\r
58                     dest.pop_back();\r
59             }\r
60         }\r
61 \r
62         const KeyInfoResolver* getKeyInfoResolver() const {\r
63             return m_keyInfoResolver ? m_keyInfoResolver : XMLToolingConfig::getConfig().getKeyInfoResolver();\r
64         }\r
65 \r
66         KeyInfoResolver* m_keyInfoResolver;\r
67     };\r
68 \r
69     AttributeDecoder* SHIBSP_DLLLOCAL KeyInfoAttributeDecoderFactory(const DOMElement* const & e)\r
70     {\r
71         return new KeyInfoAttributeDecoder(e);\r
72     }\r
73 \r
74     static const XMLCh _KeyInfoResolver[] = UNICODE_LITERAL_15(K,e,y,I,n,f,o,R,e,s,o,l,v,e,r);\r
75     static const XMLCh type[] =             UNICODE_LITERAL_4(t,y,p,e);\r
76 };\r
77 \r
78 KeyInfoAttributeDecoder::KeyInfoAttributeDecoder(const DOMElement* e) : AttributeDecoder(e), m_keyInfoResolver(NULL) {\r
79     e = e ? XMLHelper::getFirstChildElement(e,_KeyInfoResolver) : NULL;\r
80     if (e) {\r
81         auto_ptr_char t(e->getAttributeNS(NULL,type));\r
82         if (t.get() && *t.get())\r
83             m_keyInfoResolver = XMLToolingConfig::getConfig().KeyInfoResolverManager.newPlugin(t.get(), e);\r
84         else\r
85             throw UnknownExtensionException("<KeyInfoResolver> element found with no type attribute");\r
86     }\r
87 }\r
88 \r
89 Attribute* KeyInfoAttributeDecoder::decode(\r
90     const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty\r
91     ) const\r
92 {\r
93     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.KeyInfo");\r
94 \r
95     if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {\r
96         log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");\r
97         return NULL;\r
98     }\r
99 \r
100     auto_ptr<SimpleAttribute> attr(new SimpleAttribute(ids));\r
101     vector<string>& dest = attr->getValues();\r
102     vector<XMLObject*>::const_iterator v,stop;\r
103 \r
104     const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);\r
105     if (saml2attr) {\r
106         const vector<XMLObject*>& values = saml2attr->getAttributeValues();\r
107         v = values.begin();\r
108         stop = values.end();\r
109         if (log.isDebugEnabled()) {\r
110             auto_ptr_char n(saml2attr->getName());\r
111             log.debug(\r
112                 "decoding KeyInfo information (%s) from SAML 2 Attribute (%s) with %lu value(s)",\r
113                 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()\r
114                 );\r
115         }\r
116     }\r
117     else {\r
118         const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);\r
119         if (saml1attr) {\r
120             const vector<XMLObject*>& values = saml1attr->getAttributeValues();\r
121             v = values.begin();\r
122             stop = values.end();\r
123             if (log.isDebugEnabled()) {\r
124                 auto_ptr_char n(saml1attr->getAttributeName());\r
125                 log.debug(\r
126                     "decoding KeyInfo information (%s) from SAML 1 Attribute (%s) with %lu value(s)",\r
127                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()\r
128                     );\r
129             }\r
130         }\r
131         else {\r
132             log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");\r
133             return NULL;\r
134         }\r
135     }\r
136 \r
137     for (; v!=stop; ++v) {\r
138         const KeyInfo* k = dynamic_cast<const KeyInfo*>(*v);\r
139         if (k)\r
140             extract(k, dest);\r
141         else if ((*v)->hasChildren()) {\r
142             const list<XMLObject*>& children = (*v)->getOrderedChildren();\r
143             for (list<XMLObject*>::const_iterator vv = children.begin(); vv!=children.end(); ++vv) {\r
144                 if (k=dynamic_cast<const KeyInfo*>(*vv))\r
145                     extract(k, dest);\r
146                 else\r
147                     log.warn("skipping AttributeValue without a recognizable KeyInfo");\r
148             }\r
149         }\r
150     }\r
151 \r
152     return dest.empty() ? NULL : attr.release();\r
153 }\r