Add const cast for older xmlsec.
[shibboleth/cpp-opensaml.git] / saml / signature / ContentReference.cpp
1 /*
2  *  Copyright 2001-2010 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 SignableObject::declareNonVisibleNamespaces() const
38 {
39     ContentReference* cr = getSignature() ? dynamic_cast<ContentReference*>(getSignature()->getContentReference()) : nullptr;
40
41     // Compute inclusive prefix set.
42     map<xstring,xstring> decls;
43     XMLHelper::getNonVisiblyUsedPrefixes(*this, decls);
44
45     for (map<xstring,xstring>::const_iterator decl = decls.begin(); decl != decls.end(); ++decl) {
46
47         // Pin it to the object root. An existing copy of the prefix on the root will take precedence.
48         addNamespace(Namespace(decl->second.c_str(), decl->first.c_str(), true, Namespace::NonVisiblyUsed));
49
50         // Add to content reference, if any.
51         if (cr)
52             cr->addInclusivePrefix(decl->first.c_str());
53     }
54 }
55
56 ContentReference::ContentReference(const SignableObject& signableObject)
57     : m_signableObject(signableObject), m_digest(nullptr), m_c14n(nullptr)
58 {
59 }
60
61 ContentReference::~ContentReference()
62 {
63 }
64
65 void ContentReference::createReferences(DSIGSignature* sig)
66 {
67     DSIGReference* ref = nullptr;
68     const XMLCh* id=m_signableObject.getXMLID();
69     if (!id || !*id)
70         ref=sig->createReference(&chNull, m_digest ? m_digest : DSIGConstants::s_unicodeStrURISHA1);  // whole doc reference
71     else {
72         XMLCh* buf=new XMLCh[XMLString::stringLen(id) + 2];
73         buf[0]=chPound;
74         buf[1]=chNull;
75         XMLString::catString(buf,id);
76         try {
77             ref=sig->createReference(buf, m_digest ? m_digest : DSIGConstants::s_unicodeStrURISHA1);
78             delete[] buf;
79         }
80         catch(...) {
81             delete[] buf;
82             throw;
83         }
84     }
85     
86     ref->appendEnvelopedSignatureTransform();
87     DSIGTransformC14n* c14n=ref->appendCanonicalizationTransform(m_c14n ? m_c14n : DSIGConstants::s_unicodeStrURIEXC_C14N_NOC);
88
89     if (!m_c14n || m_c14n == DSIGConstants::s_unicodeStrURIEXC_C14N_NOC || m_c14n == DSIGConstants::s_unicodeStrURIEXC_C14N_COM) {
90         // Build up the string of prefixes.
91         xstring prefixes;
92         static const XMLCh _default[] = { chPound, chLatin_d, chLatin_e, chLatin_f, chLatin_a, chLatin_u, chLatin_l, chLatin_t, chNull };
93         for (set<xstring>::const_iterator p = m_prefixes.begin(); p != m_prefixes.end(); ++p) {
94             prefixes += (p->empty() ? _default : p->c_str());
95             prefixes += chSpace;
96         }
97         if (!prefixes.empty()) {
98             prefixes.erase(prefixes.begin() + prefixes.size() - 1);
99             c14n->setInclusiveNamespaces(const_cast<XMLCh*>(prefixes.c_str()));
100         }
101     }
102 }
103
104 void ContentReference::addInclusivePrefix(const XMLCh* prefix)
105 {
106     m_prefixes.insert(prefix ? prefix : &chNull);
107 }
108
109 void ContentReference::setDigestAlgorithm(const XMLCh* digest)
110 {
111     m_digest = digest;
112 }
113
114 void ContentReference::setCanonicalizationMethod(const XMLCh* c14n)
115 {
116     m_c14n = c14n;
117 }