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