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