1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
|
/*
* Copyright 2001-2005 Internet2
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* SAMLPOSTProfile.cpp - implements basics of the SAML POST profile
Scott Cantor
8/12/02
$History:$
*/
#include <xercesc/util/XMLChar.hpp>
#include <xercesc/util/Base64.hpp>
#include "internal.h"
#include <ctime>
#include <sstream>
using namespace saml;
using namespace std;
using namespace log4cpp;
class MemoryReplayCache : public IReplayCache
{
public:
MemoryReplayCache(const DOMElement*) {}
~MemoryReplayCache() {}
bool check(const XMLCh* str, time_t expires) {auto_ptr_XMLCh temp(str); return check(temp.get(),expires);}
bool check(const char* str, time_t expires);
void thread_init() {}
void thread_end() {}
private:
set<string> m_replayCache;
multimap<time_t,string> m_replayExpMap;
};
IPlugIn* MemoryReplayCacheFactory(const DOMElement* e)
{
return new MemoryReplayCache(e);
}
IReplayCache* IReplayCache::getInstance(const DOMElement* e)
{
return getInstance(DEFAULT_REPLAYCACHE_PROVIDER,e);
}
IReplayCache* IReplayCache::getInstance(const char* type, const DOMElement* e)
{
IPlugIn* p=SAMLConfig::getConfig().getPlugMgr().newPlugin(type,e);
IReplayCache* rc=dynamic_cast<IReplayCache*>(p);
if (rc)
return rc;
delete p;
throw UnsupportedExtensionException("factory returned plugin type other than IReplayCache");
}
bool MemoryReplayCache::check(const char* str, time_t expires)
{
SAMLConfig& config=SAMLConfig::getConfig();
// Garbage collect any expired entries.
time_t now=time(NULL)-config.clock_skew_secs;
config.saml_lock();
try
{
multimap<time_t,string>::iterator stop=m_replayExpMap.upper_bound(now);
for (multimap<time_t,string>::iterator i=m_replayExpMap.begin(); i!=stop; m_replayExpMap.erase(i++))
m_replayCache.erase(i->second);
// If it's already been seen, bail.
if (!m_replayCache.insert(str).second) {
config.saml_unlock();
return false;
}
// Add the pair to the expiration map.
m_replayExpMap.insert(multimap<time_t,string>::value_type(expires,str));
}
catch(...)
{
config.saml_unlock();
Category::getInstance(SAML_LOGCAT".MemoryReplayCache").error("check() caught an exception");
return false;
}
config.saml_unlock();
return true;
}
class BrowserProfile : virtual public SAMLBrowserProfile
{
public:
BrowserProfile(const DOMElement*) {}
~BrowserProfile() {}
BrowserProfileResponse receive(
const char* packet,
const XMLCh* recipient,
int supportedProfiles,
IReplayCache* replayCache=NULL,
ArtifactMapper* callback=NULL,
int minorVersion=1
) const;
private:
class CgiParse
{
public:
CgiParse(const char* data, unsigned int len);
~CgiParse();
typedef multimap<string,char*>::const_iterator walker;
pair<walker,walker> get_values(const char* name) const;
char* get_value(const char* name) const;
private:
char * fmakeword(char stop, unsigned int *cl, const char** ppch);
char * makeword(char *line, char stop);
void plustospace(char *str);
char x2c(char *what);
void url_decode(char *url);
multimap<string,char*> kvp_map;
};
};
IPlugIn* BrowserProfileFactory(const DOMElement* e)
{
return new BrowserProfile(e);
}
SAMLBrowserProfile* SAMLBrowserProfile::getInstance(const DOMElement* e)
{
return getInstance(DEFAULT_BROWSERPROFILE_PROVIDER,e);
}
SAMLBrowserProfile* SAMLBrowserProfile::getInstance(const char* type, const DOMElement* e)
{
IPlugIn* p=SAMLConfig::getConfig().getPlugMgr().newPlugin(type,e);
SAMLBrowserProfile* bp=dynamic_cast<SAMLBrowserProfile*>(p);
if (bp)
return bp;
delete p;
throw UnsupportedExtensionException("factory returned plugin type other than SAMLBrowserProfile");
}
SAMLBrowserProfile::BrowserProfileResponse BrowserProfile::receive(
const char* packet,
const XMLCh* recipient,
int supportedProfiles,
IReplayCache* replayCache,
ArtifactMapper* artifactMapper,
int minorVersion
) const
{
#ifdef _DEBUG
saml::NDC ndc("receive");
#endif
Category& log=Category::getInstance(SAML_LOGCAT".BrowserProfile");
SAMLConfig& config=SAMLConfig::getConfig();
time_t now=time(NULL);
SAMLResponse* response = NULL;
SAMLAssertion* assertion = NULL;
SAMLAuthenticationStatement* authnStatement = NULL;
// First we process the input packet. Technically artifact requires GET, but
// it doesn't really matter to the implementation of the profile how it comes in.
CgiParse parser(packet,strlen(packet));
char* bits=parser.get_value("SAMLResponse");
if (bits) {
// This is a POST profile packet.
if (!(supportedProfiles & SAMLBrowserProfile::Post)) {
auto_ptr_char recip(recipient);
log.error("attempted use of Browser/POST profile at unupported endpoint (%s)", recip.get());
throw UnsupportedProfileException("blocked use of Browser/POST profile");
}
supportedProfiles=SAMLBrowserProfile::Post;
// The value is writable, so we collapse any whitespace inline to save a buffer copy.
unsigned int x,y;
for (x=0,y=0; bits[y]; y++)
if (!XMLChar1_0::isWhitespace(bits[y]))
bits[x++]=bits[y];
bits[x]=0;
// Decode the base64 into SAML.
XMLByte* decoded=Base64::decode(reinterpret_cast<XMLByte*>(bits),&x);
if (!decoded)
throw FatalProfileException("unable to decode base64 in POST profile response");
log.debug("decoded SAML response:\n%s",decoded);
stringstream str(reinterpret_cast<char*>(decoded));
XMLString::release(&decoded);
response=new SAMLResponse(str,minorVersion);
try {
// Check security bits in the outer wrapper (Recipient and IssueInstant).
const XMLCh* recipient2=response->getRecipient();
if (!recipient || !*recipient || !recipient2 || !*recipient2 || XMLString::compareString(recipient,recipient2))
throw FatalProfileException("detected recipient mismatch in POST profile response");
if (response->getIssueInstant()->getEpoch() < now-(2*config.clock_skew_secs))
throw ExpiredAssertionException("detected expired POST profile response");
// We don't verify the signature, but at least check for one.
if (!response->isSigned())
throw FatalProfileException("detected unsigned POST profile response");
}
catch (SAMLException& e) {
// Save off issuer...
Iterator<SAMLAssertion*> assertions=response->getAssertions();
if (assertions.hasNext()) {
auto_ptr_char issuer(assertions.next()->getIssuer());
e.addProperty("issuer",issuer.get());
}
delete response;
throw;
}
#ifndef _DEBUG
catch (...) {
delete response;
throw;
}
#endif
}
else {
// Check for artifacts.
pair<CgiParse::walker,CgiParse::walker> raw_artifacts = parser.get_values("SAMLart");
if (raw_artifacts.first==raw_artifacts.second)
throw FatalProfileException("Unable to locate SAMLResponse or SAMLart parameters");
if ((supportedProfiles & SAMLBrowserProfile::Artifact) == 0) {
auto_ptr_char recip(recipient);
log.error("attempted use of Browser/Artifact profile at unupported endpoint (%s)", recip.get());
throw UnsupportedProfileException("blocked use of Browser/Artifact profile");
}
else if (!artifactMapper) {
throw FatalProfileException("Support of artifact profile requires ArtifactMapper interface object");
}
supportedProfiles=SAMLBrowserProfile::Artifact;
// Import the artifacts.
vector<SAMLArtifact*> artifacts;
for (; raw_artifacts.first!=raw_artifacts.second; raw_artifacts.first++) {
try {
log.debug("processing encoded artifact (%s)", raw_artifacts.first->second);
// If a replay cache was provided, check for replay.
if (replayCache) {
string key("A_");
key+=raw_artifacts.first->second;
if (!replayCache->check(key.c_str(),now + 2*config.clock_skew_secs))
throw ReplayedAssertionException(
string("rejecting replayed artifact (") + raw_artifacts.first->second + ")"
);
}
else
log.warn("replay cache was not provided, this is a potential security risk!");
artifacts.push_back(SAMLArtifact::parse(raw_artifacts.first->second));
}
catch (...) {
log.error("invalid artifact (%s)", raw_artifacts.first->second);
for (Iterator<SAMLArtifact*> arts=artifacts; arts.hasNext();)
delete arts.next();
throw;
}
}
auto_ptr<SAMLRequest> request(new SAMLRequest(artifacts));
request->setMinorVersion(minorVersion);
// The request object (housed in the smart ptr) owns the artifacts now.
artifacts.clear();
// That's actually the hard part. The rest of the work is mostly done by the caller.
// An exception might get tossed here, of course.
response = artifactMapper->resolve(request.get());
}
// At this point, we have a seemingly valid response, either via POST or from an artifact callback.
// This is messy. We have to basically guess as to where the authentication statement is, by finding
// one with an appropriate subject confirmation method. We go for the first match inside a valid assertion.
try {
bool bExpired=false;
for (Iterator<SAMLAssertion*> assertions=response->getAssertions(); !assertion && assertions.hasNext();) {
bExpired=false;
SAMLAssertion* a=assertions.next();
// The assertion must be bounded front and back.
const SAMLDateTime* notBefore=a->getNotBefore();
const SAMLDateTime* notOnOrAfter=a->getNotOnOrAfter();
if (!notBefore || !notOnOrAfter) {
log.debug("skipping assertion without time conditions...");
continue;
}
if (now+config.clock_skew_secs < notBefore->getEpoch()) {
bExpired=true;
log.debug("skipping assertion that's not yet valid...");
continue;
}
if (notOnOrAfter->getEpoch() <= now-config.clock_skew_secs) {
bExpired=true;
log.debug("skipping expired assertion...");
continue;
}
// Look for an authentication statement.
for (Iterator<SAMLStatement*> statements=a->getStatements(); !authnStatement && statements.hasNext();) {
SAMLStatement* s=statements.next();
SAMLAuthenticationStatement* as=dynamic_cast<SAMLAuthenticationStatement*>(s);
if (!as)
continue;
const SAMLSubject* subject=as->getSubject();
for (Iterator<const XMLCh*> methods=subject->getConfirmationMethods(); methods.hasNext();) {
const XMLCh* m=methods.next();
if ((supportedProfiles==SAMLBrowserProfile::Post &&
!XMLString::compareString(m,SAMLSubject::CONF_BEARER)) ||
!XMLString::compareString(m,SAMLSubject::CONF_ARTIFACT) ||
!XMLString::compareString(m,SAMLSubject::CONF_ARTIFACT01)) {
authnStatement=as;
assertion=a;
break;
}
}
}
}
if (!authnStatement) {
if (bExpired && response->getAssertions().size()==1)
throw ExpiredAssertionException("unable to accept assertion because of clock skew");
throw FatalProfileException("Unable to locate a valid authentication statement");
}
else if (supportedProfiles==SAMLBrowserProfile::Post) {
// Check for assertion replay. With artifact, the back-channel acts as a replay guard.
if (replayCache) {
auto_ptr_char id(assertion->getId());
string key(id.get());
key="P_" + key;
if (!replayCache->check(key.c_str(),assertion->getNotOnOrAfter()->getEpoch()))
throw ReplayedAssertionException(string("Rejecting replayed assertion ID (") + id.get() + ")");
}
else
log.warn("replay cache was not provided, this is a serious security risk!");
}
}
catch (SAMLException& ex) {
Iterator<SAMLAssertion*> assertions=response->getAssertions();
if (assertions.hasNext()) {
auto_ptr_char issuer(assertions.next()->getIssuer());
ex.addProperty("issuer",issuer.get());
}
delete response;
throw;
}
#ifndef _DEBUG
catch (...) {
delete response;
throw;
}
#endif
BrowserProfileResponse profileResponse;
profileResponse.profile = static_cast<profiles_t>(supportedProfiles);
profileResponse.response = response;
profileResponse.assertion = assertion;
profileResponse.authnStatement = authnStatement;
// Extract TARGET parameter, if any. Might be required in SAML, but this is more forgiving.
bits=parser.get_value("TARGET");
if (bits)
profileResponse.TARGET=bits;
return profileResponse;
}
/*************************************************************************
* CGI Parser implementation
*/
BrowserProfile::CgiParse::CgiParse(const char* data, unsigned int len)
{
const char* pch = data;
unsigned int cl = len;
while (cl && pch) {
char *name;
char *value;
value=fmakeword('&',&cl,&pch);
plustospace(value);
url_decode(value);
name=makeword(value,'=');
kvp_map.insert(pair<const string,char*>(name,value));
free(name);
}
}
BrowserProfile::CgiParse::~CgiParse()
{
for (multimap<string,char*>::iterator i=kvp_map.begin(); i!=kvp_map.end(); i++)
free(i->second);
}
char*
BrowserProfile::CgiParse::get_value(const char* name) const
{
if (kvp_map.count(name)!=1)
return NULL;
return kvp_map.lower_bound(name)->second;
}
pair<BrowserProfile::CgiParse::walker,BrowserProfile::CgiParse::walker>
BrowserProfile::CgiParse::get_values(const char* name) const
{
return kvp_map.equal_range(name);
}
/* Parsing routines modified from NCSA source. */
char *
BrowserProfile::CgiParse::makeword(char *line, char stop)
{
int x = 0,y;
char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
for(x=0;((line[x]) && (line[x] != stop));x++)
word[x] = line[x];
word[x] = '\0';
if(line[x])
++x;
y=0;
while(line[x])
line[y++] = line[x++];
line[y] = '\0';
return word;
}
char *
BrowserProfile::CgiParse::fmakeword(char stop, unsigned int *cl, const char** ppch)
{
int wsize;
char *word;
int ll;
wsize = 1024;
ll=0;
word = (char *) malloc(sizeof(char) * (wsize + 1));
while(1)
{
word[ll] = *((*ppch)++);
if(ll==wsize-1)
{
word[ll+1] = '\0';
wsize+=1024;
word = (char *)realloc(word,sizeof(char)*(wsize+1));
}
--(*cl);
if((word[ll] == stop) || word[ll] == EOF || (!(*cl)))
{
if(word[ll] != stop)
ll++;
word[ll] = '\0';
return word;
}
++ll;
}
}
void
BrowserProfile::CgiParse::plustospace(char *str)
{
register int x;
for(x=0;str[x];x++)
if(str[x] == '+') str[x] = ' ';
}
char
BrowserProfile::CgiParse::x2c(char *what)
{
register char digit;
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}
void
BrowserProfile::CgiParse::url_decode(char *url)
{
register int x,y;
for(x=0,y=0;url[y];++x,++y)
{
if((url[x] = url[y]) == '%')
{
url[x] = x2c(&url[y+1]);
y+=2;
}
}
url[x] = '\0';
}
const XMLCh SAMLBrowserProfile::BROWSER_ARTIFACT[] = // urn:oasis:names:tc:SAML:1.0:profiles:artifact-01
{ chLatin_u, chLatin_r, chLatin_n, chColon, chLatin_o, chLatin_a, chLatin_s, chLatin_i, chLatin_s, chColon,
chLatin_n, chLatin_a, chLatin_m, chLatin_e, chLatin_s, chColon, chLatin_t, chLatin_c, chColon,
chLatin_S, chLatin_A, chLatin_M, chLatin_L, chColon, chDigit_1, chPeriod, chDigit_0, chColon,
chLatin_p, chLatin_r, chLatin_o, chLatin_f, chLatin_i, chLatin_l, chLatin_e, chLatin_s, chColon,
chLatin_a, chLatin_r, chLatin_t, chLatin_i, chLatin_f, chLatin_a, chLatin_c, chLatin_t, chDash, chDigit_0, chDigit_1, chNull
};
const XMLCh SAMLBrowserProfile::BROWSER_POST[] = // urn:oasis:names:tc:SAML:1.0:profiles:browser-post
{ chLatin_u, chLatin_r, chLatin_n, chColon, chLatin_o, chLatin_a, chLatin_s, chLatin_i, chLatin_s, chColon,
chLatin_n, chLatin_a, chLatin_m, chLatin_e, chLatin_s, chColon, chLatin_t, chLatin_c, chColon,
chLatin_S, chLatin_A, chLatin_M, chLatin_L, chColon, chDigit_1, chPeriod, chDigit_0, chColon,
chLatin_p, chLatin_r, chLatin_o, chLatin_f, chLatin_i, chLatin_l, chLatin_e, chLatin_s, chColon,
chLatin_b, chLatin_r, chLatin_o, chLatin_w, chLatin_s, chLatin_e, chLatin_r, chDash,
chLatin_p, chLatin_o, chLatin_s, chLatin_t, chNull
};
|