2 * Copyright 2009 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * KeyInfoAttributeDecoder.cpp
20 * Decodes KeyInfo information into a SimpleAttribute.
24 #include "attribute/AttributeDecoder.h"
25 #include "attribute/SimpleAttribute.h"
27 #include <saml/saml1/core/Assertions.h>
28 #include <saml/saml2/core/Assertions.h>
29 #include <xmltooling/XMLToolingConfig.h>
30 #include <xmltooling/security/Credential.h>
31 #include <xmltooling/security/KeyInfoResolver.h>
32 #include <xmltooling/security/SecurityHelper.h>
33 #include <xmltooling/signature/KeyInfo.h>
35 using namespace shibsp;
36 using namespace opensaml;
37 using namespace xmlsignature;
38 using namespace xmltooling;
42 class SHIBSP_DLLLOCAL KeyInfoAttributeDecoder : virtual public AttributeDecoder
45 KeyInfoAttributeDecoder(const DOMElement* e);
46 ~KeyInfoAttributeDecoder() {
47 delete m_keyInfoResolver;
51 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=NULL, const char* relyingParty=NULL
55 void extract(const KeyInfo* k, vector<string>& dest) const {
56 auto_ptr<Credential> cred (getKeyInfoResolver()->resolve(k, Credential::RESOLVE_KEYS));
58 const char* alg = m_hashAlg.get();
61 dest.push_back(string());
62 dest.back() = SecurityHelper::getDEREncoding(*cred.get(), m_hash, true, alg);
63 if (dest.back().empty())
68 const KeyInfoResolver* getKeyInfoResolver() const {
69 return m_keyInfoResolver ? m_keyInfoResolver : XMLToolingConfig::getConfig().getKeyInfoResolver();
73 auto_ptr_char m_hashAlg;
74 KeyInfoResolver* m_keyInfoResolver;
77 AttributeDecoder* SHIBSP_DLLLOCAL KeyInfoAttributeDecoderFactory(const DOMElement* const & e)
79 return new KeyInfoAttributeDecoder(e);
82 static const XMLCh _KeyInfoResolver[] = UNICODE_LITERAL_15(K,e,y,I,n,f,o,R,e,s,o,l,v,e,r);
83 static const XMLCh _hash[] = UNICODE_LITERAL_4(h,a,s,h);
84 static const XMLCh _hashAlg[] = UNICODE_LITERAL_7(h,a,s,h,A,l,g);
85 static const XMLCh _type[] = UNICODE_LITERAL_4(t,y,p,e);
88 KeyInfoAttributeDecoder::KeyInfoAttributeDecoder(const DOMElement* e)
89 : AttributeDecoder(e), m_hash(false), m_hashAlg(e ? e->getAttributeNS(NULL, _hashAlg) : NULL), m_keyInfoResolver(NULL) {
90 const XMLCh* flag = e ? e->getAttributeNS(NULL, _hash) : NULL;
91 m_hash = (flag && (*flag == chLatin_t || *flag == chDigit_1));
92 e = e ? XMLHelper::getFirstChildElement(e,_KeyInfoResolver) : NULL;
94 auto_ptr_char t(e->getAttributeNS(NULL, _type));
95 if (t.get() && *t.get())
96 m_keyInfoResolver = XMLToolingConfig::getConfig().KeyInfoResolverManager.newPlugin(t.get(), e);
98 throw UnknownExtensionException("<KeyInfoResolver> element found with no type attribute");
102 Attribute* KeyInfoAttributeDecoder::decode(
103 const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
106 Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.KeyInfo");
108 if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {
109 log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");
113 auto_ptr<SimpleAttribute> attr(new SimpleAttribute(ids));
114 vector<string>& dest = attr->getValues();
115 vector<XMLObject*>::const_iterator v,stop;
117 const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);
119 const vector<XMLObject*>& values = saml2attr->getAttributeValues();
122 if (log.isDebugEnabled()) {
123 auto_ptr_char n(saml2attr->getName());
125 "decoding KeyInfo information (%s) from SAML 2 Attribute (%s) with %lu value(s)",
126 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
131 const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);
133 const vector<XMLObject*>& values = saml1attr->getAttributeValues();
136 if (log.isDebugEnabled()) {
137 auto_ptr_char n(saml1attr->getAttributeName());
139 "decoding KeyInfo information (%s) from SAML 1 Attribute (%s) with %lu value(s)",
140 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
145 log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");
150 for (; v!=stop; ++v) {
151 const KeyInfo* k = dynamic_cast<const KeyInfo*>(*v);
154 else if ((*v)->hasChildren()) {
155 const list<XMLObject*>& children = (*v)->getOrderedChildren();
156 for (list<XMLObject*>::const_iterator vv = children.begin(); vv!=children.end(); ++vv) {
157 if (k=dynamic_cast<const KeyInfo*>(*vv))
160 log.warn("skipping AttributeValue without a recognizable KeyInfo");
165 return dest.empty() ? NULL : _decode(attr.release());