X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2Fsignature%2Fimpl%2FXMLSecSignatureImpl.cpp;h=cf3afd29b83855fc4caea9914e4f296e4e19b3ec;hb=6f8dbf9e8e5519805cf28f59060b06f585e03a84;hp=b56181475ab032948c766aaee2c2a19b3f2aced1;hpb=7c2636878325d3c99889f626a93dc876b5a77d65;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/signature/impl/XMLSecSignatureImpl.cpp b/xmltooling/signature/impl/XMLSecSignatureImpl.cpp index b561814..cf3afd2 100644 --- a/xmltooling/signature/impl/XMLSecSignatureImpl.cpp +++ b/xmltooling/signature/impl/XMLSecSignatureImpl.cpp @@ -36,6 +36,7 @@ #include #include +using namespace xmlsignature; using namespace xmltooling; using namespace log4cpp; using namespace std; @@ -45,7 +46,7 @@ using namespace std; #pragma warning( disable : 4250 4251 ) #endif -namespace xmltooling { +namespace xmlsignature { class XMLTOOL_DLLLOCAL XMLSecSignatureImpl : public UnknownElementImpl, public virtual Signature { @@ -56,6 +57,7 @@ namespace xmltooling { void releaseDOM(); XMLObject* clone() const; + Signature* cloneSignature() const; DOMElement* marshall(DOMDocument* document=NULL, MarshallingContext* ctx=NULL) const; DOMElement* marshall(DOMElement* parentElement, MarshallingContext* ctx=NULL) const; @@ -69,7 +71,7 @@ namespace xmltooling { void setCanonicalizationMethod(const XMLCh* c14n) { m_c14n = prepareForAssignment(m_c14n,c14n); } void setSignatureAlgorithm(const XMLCh* sm) { m_sm = prepareForAssignment(m_sm,sm); } - void sign(const SigningContext& ctx); + void sign(SigningContext& ctx); void verify(const VerifyingContext& ctx) const; private: @@ -108,6 +110,11 @@ void XMLSecSignatureImpl::releaseDOM() XMLObject* XMLSecSignatureImpl::clone() const { + return cloneSignature(); +} + +Signature* XMLSecSignatureImpl::cloneSignature() const +{ XMLSecSignatureImpl* ret=new XMLSecSignatureImpl(); ret->m_c14n=XMLString::replicate(m_c14n); @@ -130,7 +137,7 @@ public: } }; -void XMLSecSignatureImpl::sign(const SigningContext& ctx) +void XMLSecSignatureImpl::sign(SigningContext& ctx) { Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature"); log.debug("applying signature"); @@ -140,15 +147,29 @@ void XMLSecSignatureImpl::sign(const SigningContext& ctx) try { log.debug("creating signature content"); - ctx.createSignature(m_signature); - const std::vector& certs=ctx.getX509Certificates(); - if (!certs.empty()) { - DSIGKeyInfoX509* x509Data=m_signature->appendX509Data(); - for_each(certs.begin(),certs.end(),bind1st(_addcert(),x509Data)); + CredentialResolver& cr=ctx.getCredentialResolver(); + if (!ctx.createSignature(m_signature)) { + auto_ptr keyInfo(ctx.getKeyInfo()); + if (keyInfo.get()) { + DOMElement* domElement=keyInfo->marshall(m_signature->getParentDocument()); + getDOM()->appendChild(domElement); + } + else { + Locker locker1(cr); + const std::vector* certs=cr.getX509Certificates(); + if (certs && !certs->empty()) { + DSIGKeyInfoX509* x509Data=m_signature->appendX509Data(); + for_each(certs->begin(),certs->end(),bind1st(_addcert(),x509Data)); + } + } } log.debug("computing signature"); - m_signature->setSigningKey(ctx.getSigningKey()); + Locker locker2(cr); + XSECCryptoKey* key=cr.getPrivateKey(); + if (!key) + throw SignatureException(string("Unable to obtain signing key from CredentialResolver (") + cr.getId() + ")"); + m_signature->setSigningKey(key->clone()); m_signature->sign(); } catch(XSECException& e) { @@ -245,7 +266,7 @@ DOMElement* XMLSecSignatureImpl::marshall(DOMDocument* document, MarshallingCont MemBufInputSource src(reinterpret_cast(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl"); Wrapper4InputSource dsrc(&src,false); log.debug("parsing Signature XML back into DOM tree"); - DOMDocument* internalDoc=XMLToolingInternalConfig::getInternalConfig().m_parserPool->parse(dsrc); + DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc); if (document) { // The caller insists on using his own document, so we now have to import the thing // into it. Then we're just dumping the one we built. @@ -349,7 +370,7 @@ DOMElement* XMLSecSignatureImpl::marshall(DOMElement* parentElement, Marshalling MemBufInputSource src(reinterpret_cast(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl"); Wrapper4InputSource dsrc(&src,false); log.debug("parsing XML back into DOM tree"); - DOMDocument* internalDoc=XMLToolingInternalConfig::getInternalConfig().m_parserPool->parse(dsrc); + DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc); log.debug("reimporting new DOM into caller-supplied document"); cachedDOM=static_cast(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(),true));