Update copyright.
[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 std;
34
35 class _addprefix : public binary_function<DSIGTransformC14n*,string,void> {
36 public:
37     void operator()(DSIGTransformC14n* t, const string& s) const {
38         if (s.empty())
39             t->addInclusiveNamespace("#default");
40         else 
41             t->addInclusiveNamespace(s.c_str());
42     }
43 };
44
45 void ContentReference::createReferences(DSIGSignature* sig)
46 {
47     DSIGReference* ref=NULL;
48     const XMLCh* id=m_signableObject.getXMLID();
49     if (!id || !*id)
50         ref=sig->createReference(&chNull);  // whole doc reference
51     else {
52         XMLCh* buf=new XMLCh[XMLString::stringLen(id) + 2];
53         buf[0]=chPound;
54         buf[1]=chNull;
55         XMLString::catString(buf,id);
56         try {
57             ref=sig->createReference(buf);
58             delete[] buf;
59         }
60         catch(...) {
61             delete[] buf;
62             throw;
63         }
64     }
65     ref->appendEnvelopedSignatureTransform();
66     DSIGTransformC14n* c14n=ref->appendCanonicalizationTransform(CANON_C14NE_NOC);
67     for_each(m_prefixes.begin(), m_prefixes.end(), bind1st(_addprefix(),c14n));
68 }