📄 helper.cxx
字号:
#if defined(USE_SSL)#include <openssl/blowfish.h>static const Data sep("[]");static const Data pad("\0\0\0\0\0\0\0", 7);static const Data GRUU("_GRUU");static const int saltBytes(16);DataHelper::gruuUserPart(const Data& instanceId, const Data& aor, const Data& key){ unsigned char ivec[8]; ivec[0] = '\x6E'; ivec[1] = '\xE7'; ivec[2] = '\xB0'; ivec[3] = '\x4A'; ivec[4] = '\x45'; ivec[5] = '\x93'; ivec[6] = '\x7D'; ivec[7] = '\x51'; BF_KEY fish; BF_set_key(&fish, key.size(), (const unsigned char*)key.data()); const Data salt(resip::Random::getRandomHex(saltBytes)); const Data token(salt + instanceId + sep + aor + '\0' + pad.substr(0, (8 - ((salt.size() + instanceId.size() + sep.size() + 1 + aor.size() ) % 8)) % 8)); auto_ptr <unsigned char> out(new unsigned char[token.size()]); BF_cbc_encrypt((const unsigned char*)token.data(), out.get(), token.size(), &fish, ivec, BF_ENCRYPT); return GRUU + Data(out.get(),token.size()).base64encode(true/*safe URL*/);}std::pair<Data,Data> Helper::fromGruuUserPart(const Data& gruuUserPart, const Data& key){ unsigned char ivec[8]; ivec[0] = '\x6E'; ivec[1] = '\xE7'; ivec[2] = '\xB0'; ivec[3] = '\x4A'; ivec[4] = '\x45'; ivec[5] = '\x93'; ivec[6] = '\x7D'; ivec[7] = '\x51'; static const std::pair<Data, Data> empty; if (gruuUserPart.size() < GRUU.size()) { return empty; } const Data gruu = gruuUserPart.substr(GRUU.size()); BF_KEY fish; BF_set_key(&fish, key.size(), (const unsigned char*)key.data()); const Data decoded = gruu.base64decode(); auto_ptr <unsigned char> out(new unsigned char[gruuUserPart.size()+1]); BF_cbc_encrypt((const unsigned char*)decoded.data(), out.get(), decoded.size(), &fish, ivec, BF_DECRYPT); const Data pair(out.get(), decoded.size()); Data::size_type pos = pair.find(sep); if (pos == Data::npos) { return empty; } return std::make_pair(pair.substr(2*saltBytes, pos), // strip out the salt pair.substr(pos+sep.size()));}#endifHelper::ContentsSecAttrs::ContentsSecAttrs() : mContents(0), mAttributes(0){}Helper::ContentsSecAttrs::ContentsSecAttrs(std::auto_ptr<Contents> contents, std::auto_ptr<SecurityAttributes> attributes) : mContents(contents), mAttributes(attributes){}Helper::ContentsSecAttrs::ContentsSecAttrs(const ContentsSecAttrs& rhs) : mContents(rhs.mContents), mAttributes(rhs.mAttributes){}Helper::ContentsSecAttrs& Helper::ContentsSecAttrs::operator=(const ContentsSecAttrs& rhs){ if (&rhs != this) { mContents = rhs.mContents; mAttributes = rhs.mAttributes; } return *this;}Contents*extractFromPkcs7Recurse(Contents* tree, const Data& signerAor, const Data& receiverAor, SecurityAttributes* attributes, Security& security){ Pkcs7Contents* pk; if ((pk = dynamic_cast<Pkcs7Contents*>(tree))) { InfoLog( << "GREG1: " << *pk );#if defined(USE_SSL) Contents* contents = security.decrypt(receiverAor, pk); if (contents) { attributes->setEncrypted(); } return contents;#else return 0;#endif } MultipartSignedContents* mps; if ((mps = dynamic_cast<MultipartSignedContents*>(tree))) { InfoLog( << "GREG2: " << *mps );#if defined(USE_SSL) Data signer; SignatureStatus sigStatus; Contents* b = extractFromPkcs7Recurse(security.checkSignature(mps, &signer, &sigStatus), signerAor, receiverAor, attributes, security); attributes->setSigner(signer); attributes->setSignatureStatus(sigStatus); return b->clone();#else return mps->parts().front()->clone();#endif } MultipartAlternativeContents* alt; if ((alt = dynamic_cast<MultipartAlternativeContents*>(tree))) { InfoLog( << "GREG3: " << *alt ); for (MultipartAlternativeContents::Parts::reverse_iterator i = alt->parts().rbegin(); i != alt->parts().rend(); ++i) { Contents* b = extractFromPkcs7Recurse(*i, signerAor, receiverAor, attributes, security); if (b) { return b; } } } MultipartMixedContents* mult; if ((mult = dynamic_cast<MultipartMixedContents*>(tree))) { InfoLog( << "GREG4: " << *mult ); for (MultipartMixedContents::Parts::iterator i = mult->parts().begin(); i != mult->parts().end(); ++i) { Contents* b = extractFromPkcs7Recurse(*i, signerAor, receiverAor, attributes, security); if (b) { return b; } }; return 0; } return tree->clone();}Helper::ContentsSecAttrsHelper::extractFromPkcs7(const SipMessage& message, Security& security){ SecurityAttributes* attr = new SecurityAttributes; // .dlb. currently flattening SecurityAttributes? //attr->setIdentity(message.getIdentity()); attr->setIdentity(message.header(h_From).uri().getAor()); Contents *b = message.getContents(); if (b) { Data fromAor(message.header(h_From).uri().getAor()); Data toAor(message.header(h_To).uri().getAor()); if (message.isRequest()) { b = extractFromPkcs7Recurse(b, fromAor, toAor, attr, security); } else // its a response { b = extractFromPkcs7Recurse(b, toAor, fromAor, attr, security); } } std::auto_ptr<Contents> c(b); std::auto_ptr<SecurityAttributes> a(attr); return ContentsSecAttrs(c, a);}Helper::FailureMessageEffect Helper::determineFailureMessageEffect(const SipMessage& response){ assert(response.isResponse()); int code = response.header(h_StatusLine).statusCode(); assert(code >= 400); switch(code) { case 404: case 410: case 416: case 480: // but maybe not, still not quite decided: case 481: case 482: // but maybe not, still not quite decided: case 484: case 485: case 502: case 604: return DialogTermination; case 403: case 489: //only for only subscription case 408: //again, maybe not. This seems best. return UsageTermination; case 400: case 401: case 402: case 405: //doesn't agree w/ -00 of dialogusage case 406: case 412: case 413: case 414: case 415: case 420: case 421: case 423: case 429: // but if this the refer creating the Subscription, no sub will be created. case 486: case 487: case 488: case 491: case 493: case 494: case 505: case 513: case 603: case 606: return TransactionTermination; case 483: // who knows, gravefully terminate or just destroy dialog case 501: return ApplicationDependant; default: if (code < 600) { if (response.exists(h_RetryAfter)) { return RetryAfter; } else { return OptionalRetryAfter; } } else { if (response.exists(h_RetryAfter)) { return RetryAfter; } else { return ApplicationDependant; } } }}SdpContents* getSdpRecurse(Contents* tree){ if (dynamic_cast<SdpContents*>(tree)) { return static_cast<SdpContents*>(tree); } MultipartSignedContents* mps; if ((mps = dynamic_cast<MultipartSignedContents*>(tree))) { try { MultipartSignedContents::Parts::const_iterator it = mps->parts().begin(); Contents* contents = getSdpRecurse(*it); return static_cast<SdpContents*>(contents); } catch (ParseException& e) { ErrLog(<< e.name() << endl << e.getMessage()); } catch (BaseException& e) { ErrLog(<< e.name() << endl << e.getMessage()); } return 0; } MultipartAlternativeContents* alt; if ((alt = dynamic_cast<MultipartAlternativeContents*>(tree))) { try { for (MultipartAlternativeContents::Parts::reverse_iterator i = alt->parts().rbegin(); i != alt->parts().rend(); ++i) { Contents* contents = getSdpRecurse(*i); if (contents) { return static_cast<SdpContents*>(contents); } } } catch (ParseException& e) { ErrLog(<< e.name() << endl << e.getMessage()); } catch (BaseException& e) { ErrLog(<< e.name() << endl << e.getMessage()); } return 0; } MultipartMixedContents* mult; if ((mult = dynamic_cast<MultipartMixedContents*>(tree))) { try { for (MultipartMixedContents::Parts::iterator i = mult->parts().begin(); i != mult->parts().end(); ++i) { Contents* contents = getSdpRecurse(*i); if (contents) { return static_cast<SdpContents*>(contents); } } } catch (ParseException& e) { ErrLog(<< e.name() << endl << e.getMessage()); } catch (BaseException& e) { ErrLog(<< e.name() << endl << e.getMessage()); } return 0; } return 0;}auto_ptr<SdpContents> Helper::getSdp(Contents* tree){ static std::auto_ptr<SdpContents> empty; if (tree) { SdpContents* sdp = getSdpRecurse(tree); if (sdp) { DebugLog(<< "Got sdp" << endl); return auto_ptr<SdpContents>(static_cast<SdpContents*>(sdp->clone())); } } //DebugLog(<< "No sdp" << endl); return empty;}/* ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -