2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * ContentReference.cpp
24 * SAML-specific signature reference profile.
28 #include "signature/ContentReference.h"
29 #include "signature/SignableObject.h"
31 #include <xmltooling/signature/Signature.h>
32 #include <xercesc/util/XMLUniDefs.hpp>
33 #include <xsec/dsig/DSIGReference.hpp>
34 #include <xsec/dsig/DSIGSignature.hpp>
35 #include <xsec/dsig/DSIGTransformC14n.hpp>
37 using namespace opensaml;
38 using namespace xmltooling;
41 void SignableObject::declareNonVisibleNamespaces() const
43 ContentReference* cr = getSignature() ? dynamic_cast<ContentReference*>(getSignature()->getContentReference()) : nullptr;
45 // Compute inclusive prefix set.
46 map<xstring,xstring> decls;
47 XMLHelper::getNonVisiblyUsedPrefixes(*this, decls);
49 for (map<xstring,xstring>::const_iterator decl = decls.begin(); decl != decls.end(); ++decl) {
51 // Pin it to the object root. An existing copy of the prefix on the root will take precedence.
52 addNamespace(Namespace(decl->second.c_str(), decl->first.c_str(), true, Namespace::NonVisiblyUsed));
54 // Add to content reference, if any.
56 cr->addInclusivePrefix(decl->first.c_str());
60 ContentReference::ContentReference(const SignableObject& signableObject)
61 : m_signableObject(signableObject), m_digest(nullptr), m_c14n(nullptr)
65 ContentReference::~ContentReference()
69 void ContentReference::createReferences(DSIGSignature* sig)
71 DSIGReference* ref = nullptr;
72 sig->setIdByAttributeName(false);
73 const XMLCh* id=m_signableObject.getXMLID();
75 ref=sig->createReference(&chNull, m_digest ? m_digest : DSIGConstants::s_unicodeStrURISHA1); // whole doc reference
77 XMLCh* buf=new XMLCh[XMLString::stringLen(id) + 2];
80 XMLString::catString(buf,id);
82 ref=sig->createReference(buf, m_digest ? m_digest : DSIGConstants::s_unicodeStrURISHA1);
91 ref->appendEnvelopedSignatureTransform();
92 DSIGTransformC14n* c14n=ref->appendCanonicalizationTransform(m_c14n ? m_c14n : DSIGConstants::s_unicodeStrURIEXC_C14N_NOC);
94 if (!m_c14n || m_c14n == DSIGConstants::s_unicodeStrURIEXC_C14N_NOC || m_c14n == DSIGConstants::s_unicodeStrURIEXC_C14N_COM) {
95 // Build up the string of prefixes.
97 static const XMLCh _default[] = { chPound, chLatin_d, chLatin_e, chLatin_f, chLatin_a, chLatin_u, chLatin_l, chLatin_t, chNull };
98 for (set<xstring>::const_iterator p = m_prefixes.begin(); p != m_prefixes.end(); ++p) {
99 prefixes += (p->empty() ? _default : p->c_str());
102 if (!prefixes.empty()) {
103 prefixes.erase(prefixes.begin() + prefixes.size() - 1);
104 c14n->setInclusiveNamespaces(const_cast<XMLCh*>(prefixes.c_str()));
109 void ContentReference::addInclusivePrefix(const XMLCh* prefix)
111 m_prefixes.insert(prefix ? prefix : &chNull);
114 void ContentReference::setDigestAlgorithm(const XMLCh* digest)
119 void ContentReference::setCanonicalizationMethod(const XMLCh* c14n)