c0d2a8d65e489e5dc5c5a87d8060a250752356de
[shibboleth/cpp-opensaml.git] / saml / binding / impl / MessageEncoder.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  * MessageEncoder.cpp
19  * 
20  * Interface to SAML protocol binding message encoders. 
21  */
22
23 #include "internal.h"
24 #include "binding/MessageEncoder.h"
25 #include "util/SAMLConstants.h"
26
27 #include <xmltooling/signature/KeyInfo.h>
28 #include <xmltooling/signature/Signature.h>
29
30 using namespace opensaml;
31 using namespace xmlsignature;
32 using namespace xmltooling;
33 using namespace std;
34
35 namespace opensaml {
36     namespace saml1p {
37         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML1ArtifactEncoderFactory;
38         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML1POSTEncoderFactory;
39         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML1SOAPEncoderFactory;
40     }; 
41
42     namespace saml2p {
43         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML2ArtifactEncoderFactory;
44         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML2POSTEncoderFactory;
45         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML2POSTSimpleSignEncoderFactory;
46         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML2RedirectEncoderFactory;
47         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML2SOAPEncoderFactory;
48     };
49 };
50
51 void SAML_API opensaml::registerMessageEncoders()
52 {
53     SAMLConfig& conf=SAMLConfig::getConfig();
54     conf.MessageEncoderManager.registerFactory(samlconstants::SAML1_PROFILE_BROWSER_ARTIFACT, saml1p::SAML1ArtifactEncoderFactory);
55     conf.MessageEncoderManager.registerFactory(samlconstants::SAML1_PROFILE_BROWSER_POST, saml1p::SAML1POSTEncoderFactory);
56     conf.MessageEncoderManager.registerFactory(samlconstants::SAML1_BINDING_SOAP, saml1p::SAML1SOAPEncoderFactory);
57     conf.MessageEncoderManager.registerFactory(samlconstants::SAML20_BINDING_HTTP_ARTIFACT, saml2p::SAML2ArtifactEncoderFactory);
58     conf.MessageEncoderManager.registerFactory(samlconstants::SAML20_BINDING_HTTP_POST, saml2p::SAML2POSTEncoderFactory);
59     conf.MessageEncoderManager.registerFactory(samlconstants::SAML20_BINDING_HTTP_POST_SIMPLESIGN, saml2p::SAML2POSTSimpleSignEncoderFactory);
60     conf.MessageEncoderManager.registerFactory(samlconstants::SAML20_BINDING_HTTP_REDIRECT, saml2p::SAML2RedirectEncoderFactory);
61     conf.MessageEncoderManager.registerFactory(samlconstants::SAML20_BINDING_SOAP, saml2p::SAML2SOAPEncoderFactory);
62 }
63
64 namespace {
65     class SAML_DLLLOCAL _addcert : public binary_function<X509Data*,XSECCryptoX509*,void> {
66     public:
67         void operator()(X509Data* bag, XSECCryptoX509* cert) const {
68             safeBuffer& buf=cert->getDEREncodingSB();
69             X509Certificate* x=X509CertificateBuilder::buildX509Certificate();
70             x->setValue(buf.sbStrToXMLCh());
71             bag->getX509Certificates().push_back(x);
72         }
73     };
74 };
75
76 Signature* MessageEncoder::buildSignature(const CredentialResolver* credResolver, const XMLCh* sigAlgorithm) const
77 {
78     // Build a Signature.
79     Signature* sig = SignatureBuilder::buildSignature();
80     if (sigAlgorithm)
81         sig->setSignatureAlgorithm(sigAlgorithm);
82     sig->setSigningKey(credResolver->getKey());
83
84     // Build KeyInfo.
85     const vector<XSECCryptoX509*>& certs = credResolver->getCertificates();
86     if (!certs.empty()) {
87         KeyInfo* keyInfo=KeyInfoBuilder::buildKeyInfo();
88         X509Data* x509Data=X509DataBuilder::buildX509Data();
89         keyInfo->getX509Datas().push_back(x509Data);
90         for_each(certs.begin(),certs.end(),bind1st(_addcert(),x509Data));
91         sig->setKeyInfo(keyInfo);
92     }
93     
94     return sig;
95 }