fc71f1ba48036b1158893f553d5d270e0ae4b929
[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 ContentReference::ContentReference(const SignableObject& signableObject)
38     : m_signableObject(signableObject), m_digest(NULL), m_c14n(NULL)
39 {
40 }
41
42 ContentReference::~ContentReference()
43 {
44 }
45
46 void ContentReference::createReferences(DSIGSignature* sig)
47 {
48     DSIGReference* ref=NULL;
49     const XMLCh* id=m_signableObject.getXMLID();
50     if (!id || !*id)
51         ref=sig->createReference(&chNull);  // whole doc reference
52     else {
53         XMLCh* buf=new XMLCh[XMLString::stringLen(id) + 2];
54         buf[0]=chPound;
55         buf[1]=chNull;
56         XMLString::catString(buf,id);
57         try {
58             ref=sig->createReference(buf, m_digest ? m_digest : DSIGConstants::s_unicodeStrURISHA1);
59             delete[] buf;
60         }
61         catch(...) {
62             delete[] buf;
63             throw;
64         }
65     }
66     
67     ref->appendEnvelopedSignatureTransform();
68     DSIGTransformC14n* c14n=ref->appendCanonicalizationTransform(m_c14n ? m_c14n : DSIGConstants::s_unicodeStrURIEXC_C14N_NOC);
69     if (!m_c14n || m_c14n == DSIGConstants::s_unicodeStrURIEXC_C14N_NOC || m_c14n == DSIGConstants::s_unicodeStrURIEXC_C14N_COM) {
70         addPrefixes(m_signableObject);
71 #ifdef HAVE_GOOD_STL
72         xstring prefixes;
73         for (set<xstring>::const_iterator p = m_prefixes.begin(); p!=m_prefixes.end(); ++p)
74             prefixes += *p + chSpace;
75         if (!prefixes.empty()) {
76             prefixes.erase(prefixes.begin() + prefixes.size() - 1);
77             c14n->setInclusiveNamespaces(XMLString::replicate(prefixes.c_str()));
78         }
79 #else
80         for (set<string>::const_iterator p = m_prefixes.begin(); p!=m_prefixes.end(); ++p)
81             c14n->addInclusiveNamespace(p->c_str());
82 #endif
83     }
84 }
85
86 void ContentReference::addInclusivePrefix(const XMLCh* prefix)
87 {
88     static const XMLCh _default[] = { chPound, chLatin_d, chLatin_e, chLatin_f, chLatin_a, chLatin_u, chLatin_l, chLatin_t, chNull };
89
90 #ifdef HAVE_GOOD_STL
91     if (prefix && *prefix)
92         m_prefixes.insert(prefix);
93     else
94         m_prefixes.insert(_default);
95 #else
96     if (prefix && *prefix) {
97         auto_ptr_char p(prefix);
98         m_prefixes.insert(p.get());
99     }
100     else
101         m_prefixes.insert("#default");
102 #endif
103 }
104
105 void ContentReference::setDigestAlgorithm(const XMLCh* digest)
106 {
107     m_digest = digest;
108 }
109
110 void ContentReference::setCanonicalizationMethod(const XMLCh* c14n)
111 {
112     m_c14n = c14n;
113 }
114
115 void ContentReference::addPrefixes(const std::set<Namespace>& namespaces)
116 {
117     for (set<Namespace>::const_iterator n = namespaces.begin(); n!=namespaces.end(); ++n) {
118         // Check for xmlns:xml.
119         if (!XMLString::equals(n->getNamespacePrefix(), xmlconstants::XML_PREFIX) || !XMLString::equals(n->getNamespaceURI(), xmlconstants::XML_NS))
120             addInclusivePrefix(n->getNamespacePrefix());
121     }
122 }
123
124 void ContentReference::addPrefixes(const XMLObject& xmlObject)
125 {
126     addPrefixes(xmlObject.getNamespaces());
127     const list<XMLObject*>& children = xmlObject.getOrderedChildren();
128     for (list<XMLObject*>::const_iterator child = children.begin(); child!=children.end(); ++child) {
129         if (*child)
130             addPrefixes(*(*child));
131     }
132 }