--- /dev/null
+/*\r
+ * Copyright 2009 Internet2\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/**\r
+ * KeyInfoAttributeDecoder.cpp\r
+ *\r
+ * Decodes KeyInfo information into a SimpleAttribute.\r
+ */\r
+\r
+#include "internal.h"\r
+#include "attribute/AttributeDecoder.h"\r
+#include "attribute/SimpleAttribute.h"\r
+\r
+#include <saml/saml1/core/Assertions.h>\r
+#include <saml/saml2/core/Assertions.h>\r
+#include <xmltooling/security/SecurityHelper.h>\r
+#include <xmltooling/signature/KeyInfo.h>\r
+\r
+using namespace shibsp;\r
+using namespace opensaml;\r
+using namespace xmlsignature;\r
+using namespace xmltooling;\r
+using namespace std;\r
+\r
+namespace shibsp {\r
+ class SHIBSP_DLLLOCAL KeyInfoAttributeDecoder : virtual public AttributeDecoder\r
+ {\r
+ public:\r
+ KeyInfoAttributeDecoder(const DOMElement* e);\r
+ ~KeyInfoAttributeDecoder() {\r
+ delete m_keyInfoResolver;\r
+ }\r
+\r
+ Attribute* decode(\r
+ const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=NULL, const char* relyingParty=NULL\r
+ ) const;\r
+\r
+ private:\r
+ void extract(const KeyInfo* k, vector<string>& dest) const {\r
+ auto_ptr<Credential> cred (getKeyInfoResolver()->resolve(k, Credential::RESOLVE_KEYS));\r
+ if (cred.get()) {\r
+ dest.push_back(string());\r
+ dest.back() = SecurityHelper::getDEREncoding(*cred.get(), m_hash);\r
+ if (dest.back().empty())\r
+ dest.pop_back();\r
+ }\r
+ }\r
+\r
+ const KeyInfoResolver* getKeyInfoResolver() const {\r
+ return m_keyInfoResolver ? m_keyInfoResolver : XMLToolingConfig::getConfig().getKeyInfoResolver();\r
+ }\r
+\r
+ bool m_hash;\r
+ KeyInfoResolver* m_keyInfoResolver;\r
+ };\r
+\r
+ AttributeDecoder* SHIBSP_DLLLOCAL KeyInfoAttributeDecoderFactory(const DOMElement* const & e)\r
+ {\r
+ return new KeyInfoAttributeDecoder(e);\r
+ }\r
+\r
+ static const XMLCh _KeyInfoResolver[] = UNICODE_LITERAL_15(K,e,y,I,n,f,o,R,e,s,o,l,v,e,r);\r
+ static const XMLCh _hash[] = UNICODE_LITERAL_4(h,a,s,h);\r
+ static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);\r
+};\r
+\r
+KeyInfoAttributeDecoder::KeyInfoAttributeDecoder(const DOMElement* e) : AttributeDecoder(e), m_hash(false), m_keyInfoResolver(NULL) {\r
+ const XMLCh* flag = e ? e->getAttributeNS(NULL, _hash) : NULL;\r
+ m_hash = (flag && (*flag == chLatin_t || *flag == chDigit_1));\r
+ e = e ? XMLHelper::getFirstChildElement(e,_KeyInfoResolver) : NULL;\r
+ if (e) {\r
+ auto_ptr_char t(e->getAttributeNS(NULL, _type));\r
+ if (t.get() && *t.get())\r
+ m_keyInfoResolver = XMLToolingConfig::getConfig().KeyInfoResolverManager.newPlugin(t.get(), e);\r
+ else\r
+ throw UnknownExtensionException("<KeyInfoResolver> element found with no type attribute");\r
+ }\r
+}\r
+\r
+Attribute* KeyInfoAttributeDecoder::decode(\r
+ const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty\r
+ ) const\r
+{\r
+ Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.KeyInfo");\r
+\r
+ if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {\r
+ log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");\r
+ return NULL;\r
+ }\r
+\r
+ auto_ptr<SimpleAttribute> attr(new SimpleAttribute(ids));\r
+ vector<string>& dest = attr->getValues();\r
+ vector<XMLObject*>::const_iterator v,stop;\r
+\r
+ const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);\r
+ if (saml2attr) {\r
+ const vector<XMLObject*>& values = saml2attr->getAttributeValues();\r
+ v = values.begin();\r
+ stop = values.end();\r
+ if (log.isDebugEnabled()) {\r
+ auto_ptr_char n(saml2attr->getName());\r
+ log.debug(\r
+ "decoding KeyInfo information (%s) from SAML 2 Attribute (%s) with %lu value(s)",\r
+ ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()\r
+ );\r
+ }\r
+ }\r
+ else {\r
+ const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);\r
+ if (saml1attr) {\r
+ const vector<XMLObject*>& values = saml1attr->getAttributeValues();\r
+ v = values.begin();\r
+ stop = values.end();\r
+ if (log.isDebugEnabled()) {\r
+ auto_ptr_char n(saml1attr->getAttributeName());\r
+ log.debug(\r
+ "decoding KeyInfo information (%s) from SAML 1 Attribute (%s) with %lu value(s)",\r
+ ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()\r
+ );\r
+ }\r
+ }\r
+ else {\r
+ log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");\r
+ return NULL;\r
+ }\r
+ }\r
+\r
+ for (; v!=stop; ++v) {\r
+ const KeyInfo* k = dynamic_cast<const KeyInfo*>(*v);\r
+ if (k)\r
+ extract(k, dest);\r
+ else if ((*v)->hasChildren()) {\r
+ const list<XMLObject*>& children = (*v)->getOrderedChildren();\r
+ for (list<XMLObject*>::const_iterator vv = children.begin(); vv!=children.end(); ++vv) {\r
+ if (k=dynamic_cast<const KeyInfo*>(*vv))\r
+ extract(k, dest);\r
+ else\r
+ log.warn("skipping AttributeValue without a recognizable KeyInfo");\r
+ }\r
+ }\r
+ }\r
+\r
+ return dest.empty() ? NULL : _decode(attr.release());\r
+}\r