SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-sp.git] / shibsp / attribute / KeyInfoAttributeDecoder.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * KeyInfoAttributeDecoder.cpp
23  *
24  * Decodes KeyInfo information into a SimpleAttribute.
25  */
26
27 #include "internal.h"
28 #include "attribute/AttributeDecoder.h"
29 #include "attribute/SimpleAttribute.h"
30
31 #include <saml/saml1/core/Assertions.h>
32 #include <saml/saml2/core/Assertions.h>
33 #include <xmltooling/XMLToolingConfig.h>
34 #include <xmltooling/security/Credential.h>
35 #include <xmltooling/security/KeyInfoResolver.h>
36 #include <xmltooling/security/SecurityHelper.h>
37 #include <xmltooling/signature/KeyInfo.h>
38
39 using namespace shibsp;
40 using namespace opensaml;
41 using namespace xmlsignature;
42 using namespace xmltooling;
43 using namespace boost;
44 using namespace std;
45
46 namespace shibsp {
47     class SHIBSP_DLLLOCAL KeyInfoAttributeDecoder : virtual public AttributeDecoder
48     {
49     public:
50         KeyInfoAttributeDecoder(const DOMElement* e);
51         ~KeyInfoAttributeDecoder() {}
52
53         // deprecated method
54         Attribute* decode(
55             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
56             ) const {
57             return decode(nullptr, ids, xmlObject, assertingParty, relyingParty);
58         }
59
60         Attribute* decode(
61             const GenericRequest*, const vector<string>&, const XMLObject*, const char* assertingParty=nullptr, const char* relyingParty=nullptr
62             ) const;
63
64     private:
65         void extract(const KeyInfo* k, vector<string>& dest) const {
66             scoped_ptr<Credential> cred(getKeyInfoResolver()->resolve(k, Credential::RESOLVE_KEYS));
67             if (cred) {
68                 dest.push_back(string());
69                 dest.back() = SecurityHelper::getDEREncoding(*cred, m_hash ? m_keyInfoHashAlg.c_str() : nullptr);
70                 if (dest.back().empty())
71                     dest.pop_back();
72             }
73         }
74
75         const KeyInfoResolver* getKeyInfoResolver() const {
76             return m_keyInfoResolver ? m_keyInfoResolver.get() : XMLToolingConfig::getConfig().getKeyInfoResolver();
77         }
78
79         bool m_hash;
80         string m_keyInfoHashAlg;
81         scoped_ptr<KeyInfoResolver> m_keyInfoResolver;
82     };
83
84     AttributeDecoder* SHIBSP_DLLLOCAL KeyInfoAttributeDecoderFactory(const DOMElement* const & e)
85     {
86         return new KeyInfoAttributeDecoder(e);
87     }
88
89     static const XMLCh _KeyInfoResolver[] = UNICODE_LITERAL_15(K,e,y,I,n,f,o,R,e,s,o,l,v,e,r);
90     static const XMLCh _hash[] =            UNICODE_LITERAL_4(h,a,s,h);
91     static const XMLCh keyInfoHashAlg[] =   UNICODE_LITERAL_14(k,e,y,I,n,f,o,H,a,s,h,A,l,g);
92     static const XMLCh _type[] =            UNICODE_LITERAL_4(t,y,p,e);
93 };
94
95 KeyInfoAttributeDecoder::KeyInfoAttributeDecoder(const DOMElement* e)
96     : AttributeDecoder(e),
97         m_hash(XMLHelper::getAttrBool(e, false, _hash)),
98         m_keyInfoHashAlg(XMLHelper::getAttrString(e, "SHA1", keyInfoHashAlg)) {
99     e = XMLHelper::getFirstChildElement(e, _KeyInfoResolver);
100     if (e) {
101         string t(XMLHelper::getAttrString(e, nullptr, _type));
102         if (t.empty())
103             throw UnknownExtensionException("<KeyInfoResolver> element found with no type attribute");
104         m_keyInfoResolver.reset(XMLToolingConfig::getConfig().KeyInfoResolverManager.newPlugin(t.c_str(), e));
105     }
106 }
107
108 Attribute* KeyInfoAttributeDecoder::decode(
109     const GenericRequest*, const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
110     ) const
111 {
112     Category& log = Category::getInstance(SHIBSP_LOGCAT ".AttributeDecoder.KeyInfo");
113
114     if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {
115         log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");
116         return nullptr;
117     }
118
119     auto_ptr<SimpleAttribute> attr(new SimpleAttribute(ids));
120     vector<string>& dest = attr->getValues();
121     vector<XMLObject*>::const_iterator v,stop;
122
123     const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);
124     if (saml2attr) {
125         const vector<XMLObject*>& values = saml2attr->getAttributeValues();
126         v = values.begin();
127         stop = values.end();
128         if (log.isDebugEnabled()) {
129             auto_ptr_char n(saml2attr->getName());
130             log.debug(
131                 "decoding KeyInfo information (%s) from SAML 2 Attribute (%s) with %lu value(s)",
132                 ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
133                 );
134         }
135     }
136     else {
137         const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);
138         if (saml1attr) {
139             const vector<XMLObject*>& values = saml1attr->getAttributeValues();
140             v = values.begin();
141             stop = values.end();
142             if (log.isDebugEnabled()) {
143                 auto_ptr_char n(saml1attr->getAttributeName());
144                 log.debug(
145                     "decoding KeyInfo information (%s) from SAML 1 Attribute (%s) with %lu value(s)",
146                     ids.front().c_str(), n.get() ? n.get() : "unnamed", values.size()
147                     );
148             }
149         }
150         else {
151             log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");
152             return nullptr;
153         }
154     }
155
156     for (; v != stop; ++v) {
157         const KeyInfo* k = dynamic_cast<const KeyInfo*>(*v);
158         if (k)
159             extract(k, dest);
160         else if ((*v)->hasChildren()) {
161             const list<XMLObject*>& children = (*v)->getOrderedChildren();
162             for (list<XMLObject*>::const_iterator vv = children.begin(); vv != children.end(); ++vv) {
163                 if (k=dynamic_cast<const KeyInfo*>(*vv))
164                     extract(k, dest);
165                 else
166                     log.warn("skipping AttributeValue without a recognizable KeyInfo");
167             }
168         }
169     }
170
171     return dest.empty() ? nullptr : _decode(attr.release());
172 }