825f5f7385139234b96d1a6993f1a01d556d072a
[shibboleth/cpp-opensaml.git] / saml / binding / impl / MessageEncoder.cpp
1 /*
2  *  Copyright 2001-2006 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
26 #include <xmltooling/signature/KeyInfo.h>
27 #include <xmltooling/signature/Signature.h>
28
29 using namespace opensaml;
30 using namespace xmlsignature;
31 using namespace xmltooling;
32 using namespace std;
33
34 namespace opensaml {
35     namespace saml1p {
36         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML1ArtifactEncoderFactory;
37         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML1POSTEncoderFactory;
38     }; 
39
40     namespace saml2p {
41         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML2ArtifactEncoderFactory;
42         SAML_DLLLOCAL PluginManager<MessageEncoder,const DOMElement*>::Factory SAML2POSTEncoderFactory;
43     };
44 };
45
46 void SAML_API opensaml::registerMessageEncoders()
47 {
48     SAMLConfig& conf=SAMLConfig::getConfig();
49     conf.MessageEncoderManager.registerFactory(SAML1_ARTIFACT_ENCODER, saml1p::SAML1ArtifactEncoderFactory);
50     conf.MessageEncoderManager.registerFactory(SAML1_POST_ENCODER, saml1p::SAML1POSTEncoderFactory);
51     conf.MessageEncoderManager.registerFactory(SAML2_ARTIFACT_ENCODER, saml2p::SAML2ArtifactEncoderFactory);
52     conf.MessageEncoderManager.registerFactory(SAML2_POST_ENCODER, saml2p::SAML2POSTEncoderFactory);
53 }
54
55 namespace {
56     class SAML_DLLLOCAL _addcert : public binary_function<X509Data*,XSECCryptoX509*,void> {
57     public:
58         void operator()(X509Data* bag, XSECCryptoX509* cert) const {
59             safeBuffer& buf=cert->getDEREncodingSB();
60             X509Certificate* x=X509CertificateBuilder::buildX509Certificate();
61             x->setValue(buf.sbStrToXMLCh());
62             bag->getX509Certificates().push_back(x);
63         }
64     };
65 };
66
67 Signature* MessageEncoder::buildSignature(const CredentialResolver* credResolver, const XMLCh* sigAlgorithm) const
68 {
69     // Build a Signature.
70     Signature* sig = SignatureBuilder::buildSignature();
71     if (sigAlgorithm)
72         sig->setSignatureAlgorithm(sigAlgorithm);
73     sig->setSigningKey(credResolver->getKey());
74
75     // Build KeyInfo.
76     const vector<XSECCryptoX509*>& certs = credResolver->getCertificates();
77     if (!certs.empty()) {
78         KeyInfo* keyInfo=KeyInfoBuilder::buildKeyInfo();
79         X509Data* x509Data=X509DataBuilder::buildX509Data();
80         keyInfo->getX509Datas().push_back(x509Data);
81         for_each(certs.begin(),certs.end(),bind1st(_addcert(),x509Data));
82         sig->setKeyInfo(keyInfo);
83     }
84     
85     return sig;
86 }