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