Boost code changes
[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 <boost/scoped_ptr.hpp>
32 #include <saml/saml1/core/Assertions.h>
33 #include <saml/saml2/core/Assertions.h>
34 #include <xmltooling/XMLToolingConfig.h>
35 #include <xmltooling/security/Credential.h>
36 #include <xmltooling/security/KeyInfoResolver.h>
37 #include <xmltooling/security/SecurityHelper.h>
38 #include <xmltooling/signature/KeyInfo.h>
39
40 using namespace shibsp;
41 using namespace opensaml;
42 using namespace xmlsignature;
43 using namespace xmltooling;
44 using namespace boost;
45 using namespace std;
46
47 namespace shibsp {
48     class SHIBSP_DLLLOCAL KeyInfoAttributeDecoder : virtual public AttributeDecoder
49     {
50     public:
51         KeyInfoAttributeDecoder(const DOMElement* e);
52         ~KeyInfoAttributeDecoder() {}
53
54         Attribute* decode(
55             const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty=nullptr, const char* relyingParty=nullptr
56             ) const;
57
58     private:
59         void extract(const KeyInfo* k, vector<string>& dest) const {
60             scoped_ptr<Credential> cred(getKeyInfoResolver()->resolve(k, Credential::RESOLVE_KEYS));
61             if (cred) {
62                 dest.push_back(string());
63                 dest.back() = SecurityHelper::getDEREncoding(*cred, m_hash ? m_keyInfoHashAlg.c_str() : nullptr);
64                 if (dest.back().empty())
65                     dest.pop_back();
66             }
67         }
68
69         const KeyInfoResolver* getKeyInfoResolver() const {
70             return m_keyInfoResolver ? m_keyInfoResolver.get() : XMLToolingConfig::getConfig().getKeyInfoResolver();
71         }
72
73         bool m_hash;
74         string m_keyInfoHashAlg;
75         scoped_ptr<KeyInfoResolver> m_keyInfoResolver;
76     };
77
78     AttributeDecoder* SHIBSP_DLLLOCAL KeyInfoAttributeDecoderFactory(const DOMElement* const & e)
79     {
80         return new KeyInfoAttributeDecoder(e);
81     }
82
83     static const XMLCh _KeyInfoResolver[] = UNICODE_LITERAL_15(K,e,y,I,n,f,o,R,e,s,o,l,v,e,r);
84     static const XMLCh _hash[] =            UNICODE_LITERAL_4(h,a,s,h);
85     static const XMLCh keyInfoHashAlg[] =   UNICODE_LITERAL_14(k,e,y,I,n,f,o,H,a,s,h,A,l,g);
86     static const XMLCh _type[] =            UNICODE_LITERAL_4(t,y,p,e);
87 };
88
89 KeyInfoAttributeDecoder::KeyInfoAttributeDecoder(const DOMElement* e)
90     : AttributeDecoder(e),
91         m_hash(XMLHelper::getAttrBool(e, false, _hash)),
92         m_keyInfoHashAlg(XMLHelper::getAttrString(e, "SHA1", keyInfoHashAlg)) {
93     e = XMLHelper::getFirstChildElement(e, _KeyInfoResolver);
94     if (e) {
95         string t(XMLHelper::getAttrString(e, nullptr, _type));
96         if (t.empty())
97             throw UnknownExtensionException("<KeyInfoResolver> element found with no type attribute");
98         m_keyInfoResolver.reset(XMLToolingConfig::getConfig().KeyInfoResolverManager.newPlugin(t.c_str(), e));
99     }
100 }
101
102 Attribute* KeyInfoAttributeDecoder::decode(
103     const vector<string>& ids, const XMLObject* xmlObject, const char* assertingParty, const char* relyingParty
104     ) const
105 {
106     Category& log = Category::getInstance(SHIBSP_LOGCAT".AttributeDecoder.KeyInfo");
107
108     if (!xmlObject || !XMLString::equals(saml1::Attribute::LOCAL_NAME, xmlObject->getElementQName().getLocalPart())) {
109         log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");
110         return nullptr;
111     }
112
113     auto_ptr<SimpleAttribute> attr(new SimpleAttribute(ids));
114     vector<string>& dest = attr->getValues();
115     vector<XMLObject*>::const_iterator v,stop;
116
117     const saml2::Attribute* saml2attr = dynamic_cast<const saml2::Attribute*>(xmlObject);
118     if (saml2attr) {
119         const vector<XMLObject*>& values = saml2attr->getAttributeValues();
120         v = values.begin();
121         stop = values.end();
122         if (log.isDebugEnabled()) {
123             auto_ptr_char n(saml2attr->getName());
124             log.debug(
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()
127                 );
128         }
129     }
130     else {
131         const saml1::Attribute* saml1attr = dynamic_cast<const saml1::Attribute*>(xmlObject);
132         if (saml1attr) {
133             const vector<XMLObject*>& values = saml1attr->getAttributeValues();
134             v = values.begin();
135             stop = values.end();
136             if (log.isDebugEnabled()) {
137                 auto_ptr_char n(saml1attr->getAttributeName());
138                 log.debug(
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()
141                     );
142             }
143         }
144         else {
145             log.warn("XMLObject type not recognized by KeyInfoAttributeDecoder, no values returned");
146             return nullptr;
147         }
148     }
149
150     for (; v!=stop; ++v) {
151         const KeyInfo* k = dynamic_cast<const KeyInfo*>(*v);
152         if (k)
153             extract(k, dest);
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))
158                     extract(k, dest);
159                 else
160                     log.warn("skipping AttributeValue without a recognizable KeyInfo");
161             }
162         }
163     }
164
165     return dest.empty() ? nullptr : _decode(attr.release());
166 }