2d1b23cefed698401bd0d1aa20d4e0ab8ca2bb1b
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1ArtifactDecoder.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  * SAML1ArtifactDecoder.cpp
19  * 
20  * SAML 1.x Artifact binding/profile message decoder
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "saml/binding/SAMLArtifact.h"
26 #include "saml/binding/ReplayCache.h"
27 #include "saml1/binding/SAML1ArtifactDecoder.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
30 #include "security/X509TrustEngine.h"
31
32 #include <log4cpp/Category.hh>
33 #include <xmltooling/util/NDC.h>
34
35 using namespace opensaml::saml2md;
36 using namespace opensaml::saml1p;
37 using namespace opensaml::saml1;
38 using namespace opensaml;
39 using namespace xmlsignature;
40 using namespace xmltooling;
41 using namespace log4cpp;
42 using namespace std;
43
44 namespace opensaml {
45     namespace saml1p {              
46         MessageDecoder* SAML_DLLLOCAL SAML1ArtifactDecoderFactory(const DOMElement* const & e)
47         {
48             return new SAML1ArtifactDecoder(e);
49         }
50     };
51 };
52
53 SAML1ArtifactDecoder::SAML1ArtifactDecoder(const DOMElement* e) {}
54
55 SAML1ArtifactDecoder::~SAML1ArtifactDecoder() {}
56
57 Response* SAML1ArtifactDecoder::decode(
58     string& relayState,
59     const RoleDescriptor*& issuer,
60     bool& issuerTrusted,
61     const HTTPRequest& httpRequest,
62     const MetadataProvider* metadataProvider,
63     const QName* role,
64     const opensaml::TrustEngine* trustEngine
65     ) const
66 {
67 #ifdef _DEBUG
68     xmltooling::NDC ndc("decode");
69 #endif
70     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1Artifact");
71
72     log.debug("validating input");
73     if (strcmp(httpRequest.getMethod(),"GET"))
74         return NULL;
75     vector<const char*> SAMLart;
76     const char* TARGET = httpRequest.getParameter("TARGET");
77     if (httpRequest.getParameters("SAMLart", SAMLart)==0 || !TARGET)
78         return NULL;
79     relayState = TARGET;
80
81     if (!m_artifactResolver || !metadataProvider)
82         throw BindingException("Artifact binding requires ArtifactResolver and MetadataProvider implementations be supplied.");
83
84     // Import the artifacts.
85     vector<SAMLArtifact*> artifacts;
86     for (vector<const char*>::const_iterator raw=SAMLart.begin(); raw!=SAMLart.end(); ++raw) {
87         try {
88             log.debug("processing encoded artifact (%s)", *raw);
89             
90             // Check replay.
91             ReplayCache* replayCache = SAMLConfig::getConfig().getReplayCache();
92             if (replayCache) {
93                 if (!replayCache->check("SAML1Artifact", *raw, time(NULL) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
94                     log.error("replay detected of artifact (%s)", *raw);
95                     throw BindingException("Rejecting replayed artifact ($1).", params(1,*raw));
96                 }
97             }
98             else
99                 log.warn("replay cache was not provided, this is a serious security risk!");
100
101             artifacts.push_back(SAMLArtifact::parse(*raw));
102         }
103         catch (ArtifactException&) {
104             log.error("error parsing artifact (%s)", *raw);
105             for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
106             throw;
107         }
108         catch (XMLToolingException&) {
109             for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
110             throw;
111         }
112     }
113     
114     issuer = NULL;
115     issuerTrusted = false;
116     log.debug("attempting to determine source of artifact(s)...");
117     const EntityDescriptor* provider=metadataProvider->getEntityDescriptor(artifacts.front());
118     if (!provider) {
119         log.error(
120             "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
121             SAMLArtifact::toHex(artifacts.front()->getBytes()).c_str()
122             );
123         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
124         throw BindingException("Metadata lookup failed, unable to determine artifact issuer");
125     }
126     
127     if (log.isDebugEnabled()) {
128         auto_ptr_char issuer(provider->getEntityID());
129         log.debug("lookup succeeded, artifact issued by (%s)", issuer.get());
130     }
131     
132     log.debug("attempting to find artifact issuing role...");
133     issuer=provider->getRoleDescriptor(*role, SAMLConstants::SAML11_PROTOCOL_ENUM);
134     if (!issuer)
135         issuer=provider->getRoleDescriptor(*role, SAMLConstants::SAML10_PROTOCOL_ENUM);
136     if (!issuer || !dynamic_cast<const IDPSSODescriptor*>(issuer)) {
137         log.error("unable to find compatible SAML role (%s) in metadata", role->toString().c_str());
138         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
139         BindingException ex("Unable to find compatible metadata role for artifact issuer.");
140         annotateException(&ex,provider); // throws it
141     }
142     
143     try {
144         auto_ptr<Response> response(
145             m_artifactResolver->resolve(
146                 issuerTrusted,
147                 artifacts,
148                 dynamic_cast<const IDPSSODescriptor&>(*issuer),
149                 dynamic_cast<const X509TrustEngine*>(trustEngine)
150                 )
151             );
152         
153         if (trustEngine && response->getSignature()) {
154             issuerTrusted = trustEngine->validate(*(response->getSignature()), *issuer, metadataProvider->getKeyResolver());
155             if (!issuerTrusted) {
156                 log.error("unable to verify signature on message with supplied trust engine");
157                 throw BindingException("Message signature failed verification.");
158             }
159         }
160         else if (!issuerTrusted) {
161             log.warn("unable to verify integrity of the message, leaving untrusted");
162         }
163         
164         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
165         return response.release();
166     }
167     catch (XMLToolingException& ex) {
168         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
169         annotateException(&ex,issuer,false);
170         throw;
171     }
172 }