Imported Upstream version 2.2.1+dfsg
[shibboleth/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(), m_hash);\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         bool m_hash;\r
67         KeyInfoResolver* m_keyInfoResolver;\r
68     };\r
69 \r
70     AttributeDecoder* SHIBSP_DLLLOCAL KeyInfoAttributeDecoderFactory(const DOMElement* const & e)\r
71     {\r
72         return new KeyInfoAttributeDecoder(e);\r
73     }\r
74 \r
75     static const XMLCh _KeyInfoResolver[] = UNICODE_LITERAL_15(K,e,y,I,n,f,o,R,e,s,o,l,v,e,r);\r
76     static const XMLCh _hash[] =            UNICODE_LITERAL_4(h,a,s,h);\r
77     static const XMLCh _type[] =            UNICODE_LITERAL_4(t,y,p,e);\r
78 };\r
79 \r
80 KeyInfoAttributeDecoder::KeyInfoAttributeDecoder(const DOMElement* e) : AttributeDecoder(e), m_hash(false), m_keyInfoResolver(NULL) {\r
81     const XMLCh* flag = e ? e->getAttributeNS(NULL, _hash) : NULL;\r
82     m_hash = (flag && (*flag == chLatin_t || *flag == chDigit_1));\r
83     e = e ? XMLHelper::getFirstChildElement(e,_KeyInfoResolver) : NULL;\r
84     if (e) {\r
85         auto_ptr_char t(e->getAttributeNS(NULL, _type));\r
86         if (t.get() && *t.get())\r
87             m_keyInfoResolver = XMLToolingConfig::getConfig().KeyInfoResolverManager.newPlugin(t.get(), e);\r
88         else\r
89             throw UnknownExtensionException("<KeyInfoResolver> element found with no type attribute");\r
90     }\r
91 }\r
92 \r
93 Attribute* KeyInfoAttributeDecoder::decode(\r
94     const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty\r
95     ) const\r
96 {\r
97     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.KeyInfo");\r
98 \r
99     if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {\r
100         log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");\r
101         return NULL;\r
102     }\r
103 \r
104     auto_ptr<SimpleAttribute> attr(new SimpleAttribute(ids));\r
105     vector<string>& dest = attr->getValues();\r
106     vector<XMLObject*>::const_iterator v,stop;\r
107 \r
108     const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);\r
109     if (saml2attr) {\r
110         const vector<XMLObject*>& values = saml2attr->getAttributeValues();\r
111         v = values.begin();\r
112         stop = values.end();\r
113         if (log.isDebugEnabled()) {\r
114             auto_ptr_char n(saml2attr->getName());\r
115             log.debug(\r
116                 "decoding KeyInfo information (%s) from SAML 2 Attribute (%s) with %lu value(s)",\r
117                 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()\r
118                 );\r
119         }\r
120     }\r
121     else {\r
122         const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);\r
123         if (saml1attr) {\r
124             const vector<XMLObject*>& values = saml1attr->getAttributeValues();\r
125             v = values.begin();\r
126             stop = values.end();\r
127             if (log.isDebugEnabled()) {\r
128                 auto_ptr_char n(saml1attr->getAttributeName());\r
129                 log.debug(\r
130                     "decoding KeyInfo information (%s) from SAML 1 Attribute (%s) with %lu value(s)",\r
131                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()\r
132                     );\r
133             }\r
134         }\r
135         else {\r
136             log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");\r
137             return NULL;\r
138         }\r
139     }\r
140 \r
141     for (; v!=stop; ++v) {\r
142         const KeyInfo* k = dynamic_cast<const KeyInfo*>(*v);\r
143         if (k)\r
144             extract(k, dest);\r
145         else if ((*v)->hasChildren()) {\r
146             const list<XMLObject*>& children = (*v)->getOrderedChildren();\r
147             for (list<XMLObject*>::const_iterator vv = children.begin(); vv!=children.end(); ++vv) {\r
148                 if (k=dynamic_cast<const KeyInfo*>(*vv))\r
149                     extract(k, dest);\r
150                 else\r
151                     log.warn("skipping AttributeValue without a recognizable KeyInfo");\r
152             }\r
153         }\r
154     }\r
155 \r
156     return dest.empty() ? NULL : _decode(attr.release());\r
157 }\r