Reducing header overuse, non-inlining selected methods (CPPOST-35).
[shibboleth/cpp-opensaml.git] / saml / signature / ContentReference.cpp
1 /*
2  *  Copyright 2001-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  * ContentReference.cpp
19  * 
20  * SAML-specific signature reference profile 
21  */
22  
23 #include "internal.h"
24 #include "signature/ContentReference.h"
25 #include "signature/SignableObject.h"
26
27 #include <xmltooling/signature/Signature.h>
28 #include <xercesc/util/XMLUniDefs.hpp>
29 #include <xsec/dsig/DSIGReference.hpp>
30 #include <xsec/dsig/DSIGSignature.hpp>
31 #include <xsec/dsig/DSIGTransformC14n.hpp>
32
33 using namespace opensaml;
34 using namespace xmltooling;
35 using namespace std;
36
37 void ContentReference::createReferences(DSIGSignature* sig)
38 {
39     DSIGReference* ref=NULL;
40     const XMLCh* id=m_signableObject.getXMLID();
41     if (!id || !*id)
42         ref=sig->createReference(&chNull);  // whole doc reference
43     else {
44         XMLCh* buf=new XMLCh[XMLString::stringLen(id) + 2];
45         buf[0]=chPound;
46         buf[1]=chNull;
47         XMLString::catString(buf,id);
48         try {
49             ref=sig->createReference(buf, m_digest ? m_digest : DSIGConstants::s_unicodeStrURISHA1);
50             delete[] buf;
51         }
52         catch(...) {
53             delete[] buf;
54             throw;
55         }
56     }
57     
58     ref->appendEnvelopedSignatureTransform();
59     DSIGTransformC14n* c14n=ref->appendCanonicalizationTransform(m_c14n ? m_c14n : DSIGConstants::s_unicodeStrURIEXC_C14N_NOC);
60     if (!m_c14n || m_c14n == DSIGConstants::s_unicodeStrURIEXC_C14N_NOC || m_c14n == DSIGConstants::s_unicodeStrURIEXC_C14N_COM) {
61         addPrefixes(m_signableObject);
62 #ifdef HAVE_GOOD_STL
63         xstring prefixes;
64         for (set<xstring>::const_iterator p = m_prefixes.begin(); p!=m_prefixes.end(); ++p)
65             prefixes += *p + chSpace;
66         if (!prefixes.empty()) {
67             prefixes.erase(prefixes.begin() + prefixes.size() - 1);
68             c14n->setInclusiveNamespaces(XMLString::replicate(prefixes.c_str()));
69         }
70 #else
71         for (set<string>::const_iterator p = m_prefixes.begin(); p!=m_prefixes.end(); ++p)
72             c14n->addInclusiveNamespace(p->c_str());
73 #endif
74     }
75 }
76
77 void ContentReference::addInclusivePrefix(const XMLCh* prefix)
78 {
79     static const XMLCh _default[] = { chPound, chLatin_d, chLatin_e, chLatin_f, chLatin_a, chLatin_u, chLatin_l, chLatin_t, chNull };
80
81 #ifdef HAVE_GOOD_STL
82     if (prefix && *prefix)
83         m_prefixes.insert(prefix);
84     else
85         m_prefixes.insert(_default);
86 #else
87     if (prefix && *prefix) {
88         auto_ptr_char p(prefix);
89         m_prefixes.insert(p.get());
90     }
91     else
92         m_prefixes.insert("#default");
93 #endif
94 }
95
96 void ContentReference::addPrefixes(const std::set<Namespace>& namespaces)
97 {
98     for (set<Namespace>::const_iterator n = namespaces.begin(); n!=namespaces.end(); ++n) {
99         // Check for xmlns:xml.
100         if (!XMLString::equals(n->getNamespacePrefix(), xmlconstants::XML_PREFIX) || !XMLString::equals(n->getNamespaceURI(), xmlconstants::XML_NS))
101             addInclusivePrefix(n->getNamespacePrefix());
102     }
103 }
104
105 void ContentReference::addPrefixes(const XMLObject& xmlObject)
106 {
107     addPrefixes(xmlObject.getNamespaces());
108     const list<XMLObject*>& children = xmlObject.getOrderedChildren();
109     for (list<XMLObject*>::const_iterator child = children.begin(); child!=children.end(); ++child) {
110         if (*child)
111             addPrefixes(*(*child));
112     }
113 }