2#if defined(HAVE_CONFIG_H)
44#define RESIPROCATE_SUBSYSTEM Subsystem::DUM
49 return (method == ACK || method == CANCEL || method == BYE);
64 for (list<Request*>::iterator it = mRequests.begin(); it != mRequests.end(); ++it)
73 ErrLog(<<
"Async currently is not supported");
88 DebugLog(<<
"Decrypted message:" << sipMsg << endl);
105 if (!event->message()->getContents())
110 if (!event->message()->getSecurityAttributes() ||
112 event->message()->getSecurityAttributes()->encryptionPerformed())
119 if (event->message()->isRequest())
121 senderAor =
event->message()->header(h_From).uri().getAor();
122 recipAor =
event->message()->header(h_To).uri().getAor();
126 senderAor =
event->message()->header(h_To).uri().getAor();
127 recipAor =
event->message()->header(h_From).uri().getAor();
131 bool setContents =
true;
132 bool noCerts =
false;
134 switch (event->message()->getSecurityAttributes()->getOutgoingEncryptionLevel())
140 contents = encrypt(event->message(), recipAor, &noCerts);
143 contents = sign(event->message(), senderAor, &noCerts);
146 contents = signAndEncrypt(event->message(), senderAor, recipAor, &noCerts);
154 event->message()->setContents(unique_ptr<Contents>(contents));
177 const EncryptionManager::Result result = processCertMessage(certMsg);
178 if (result == Complete)
192EncryptionManager::Result EncryptionManager::processCertMessage(
CertMessage* message)
194 InfoLog( <<
"Received a cert message: " << *message << endl);
195 Result ret = Pending;
196 list<Request*>::iterator it;
197 for (it = mRequests.begin(); it != mRequests.end(); ++it)
199 if ((*it)->getId() == message->
id().
mId)
break;
202 if (it != mRequests.end())
204 InfoLog ( <<
"Processing the cert message" << endl);
216Contents* EncryptionManager::sign(std::shared_ptr<SipMessage> msg,
217 const Data& senderAor,
220 Sign* request =
new Sign(
mDum, mRemoteCertStore.get(), msg, senderAor, *
this);
223 bool async = request->sign(&contents, noCerts);
226 InfoLog(<<
"Async sign" << endl);
228 mRequests.push_back(request);
237Contents* EncryptionManager::encrypt(std::shared_ptr<SipMessage> msg,
238 const Data& recipientAor,
241 Encrypt* request =
new Encrypt(
mDum, mRemoteCertStore.get(), msg, recipientAor, *
this);
244 bool async = request->encrypt(&contents, noCerts);
247 InfoLog(<<
"Async encrypt" << endl);
249 mRequests.push_back(request);
258Contents* EncryptionManager::signAndEncrypt(std::shared_ptr<SipMessage> msg,
259 const Data& senderAor,
260 const Data& recipAor,
263 SignAndEncrypt* request =
new SignAndEncrypt(
mDum, mRemoteCertStore.get(), msg, senderAor, recipAor, *
this);
266 bool async = request->signAndEncrypt(&contents, noCerts);
273 InfoLog(<<
"Async sign and encrypt" << endl);
275 mRequests.push_back(request);
280bool EncryptionManager::decrypt(
SipMessage* msg)
282 Decrypt* request =
new Decrypt(
mDum, mRemoteCertStore.get(), msg, *
this);
285 bool ret = request->decrypt(csa);
298 if(origSecurityAttributes)
308 request->handleInvalidContents();
319 InfoLog(<<
"Async decrypt" << endl);
320 mRequests.push_back(request);
328 std::shared_ptr<SipMessage> msg,
339EncryptionManager::Request::~Request()
349void EncryptionManager::Request::response415()
353 InfoLog(<<
"Generated 415" << endl);
358 std::shared_ptr<SipMessage> msg,
359 const Data& senderAor,
361 : Request(dum, store, msg, feature),
362 mSenderAor(senderAor)
366bool EncryptionManager::Sign::sign(
Contents** contents,
bool* noCerts)
373 bool missingCert = !mDum.getSecurity()->hasUserCert(mSenderAor);
374 bool missingKey = !mDum.getSecurity()->hasUserPrivateKey(mSenderAor);
376 if (!missingCert && !missingKey)
378 InfoLog(<<
"Signing message" << endl);
379 msc = mDum.getSecurity()->sign(mSenderAor, mMsgToEncrypt->getContents());
388 InfoLog(<<
"Fetching cert for " << mSenderAor << endl);
395 InfoLog(<<
"Fetching private key for " << mSenderAor << endl);
404 InfoLog(<<
"No remote cert store installed" << endl);
413EncryptionManager::Result EncryptionManager::Sign::received(
bool success,
426 InfoLog(<<
"Adding cert for: " << aor << endl);
427 mDum.getSecurity()->addUserCertDER(aor, data);
431 InfoLog(<<
"Adding private key for " << aor << endl);
432 mDum.getSecurity()->addUserPrivateKeyDER(aor, data);
434 if (--mPendingRequests == 0)
436 InfoLog(<<
"Signing message" << endl);
438 mMsgToEncrypt->setContents(unique_ptr<Contents>(msc));
442 mDum.post(
new TargetCommand(mDum.dumOutgoingTarget(), unique_ptr<Message>(event)));
457 std::shared_ptr<SipMessage> msg,
458 const Data& recipientAor,
460 : Request(dum, store, msg, feature),
461 mRecipientAor(recipientAor)
465bool EncryptionManager::Encrypt::encrypt(
Contents** contents,
bool* noCerts)
471 if (mDum.getSecurity()->hasUserCert(mRecipientAor))
473 InfoLog(<<
"Encrypting message" << endl);
479 Contents* last = mDum.getSecurity()->encrypt(parts.back(), mRecipientAor);
483 delete mac->
parts().back();
484 mac->
parts().pop_back();
485 mac->
parts().push_back(last);
491 *contents = mDum.getSecurity()->encrypt(mMsgToEncrypt->getContents(), mRecipientAor);
498 InfoLog(<<
"Fetching cert for " << mRecipientAor << endl);
506 InfoLog(<<
"No remote cert store installed" << endl);
514EncryptionManager::Result EncryptionManager::Encrypt::received(
bool success,
524 InfoLog(<<
"Adding user cert for " << aor << endl);
525 mDum.getSecurity()->addUserCertDER(aor, data);
527 InfoLog(<<
"Encrypting message" << endl);
528 Pkcs7Contents* encrypted = mDum.getSecurity()->encrypt(mMsgToEncrypt->getContents(), aor);
529 mMsgToEncrypt->setContents(unique_ptr<Contents>(encrypted));
533 mDum.post(
new TargetCommand(mDum.dumOutgoingTarget(), unique_ptr<Message>(event)));
537 InfoLog(<<
"Failed to fetch cert for " << aor << endl);
545 std::shared_ptr<SipMessage> msg,
546 const Data& senderAor,
547 const Data& recipientAor,
549 : Request(dum, store, msg, feature),
550 mSenderAor(senderAor),
551 mRecipientAor(recipientAor)
555bool EncryptionManager::SignAndEncrypt::signAndEncrypt(
Contents** contents,
bool* noCerts)
560 bool missingCert = !mDum.getSecurity()->hasUserCert(mSenderAor);
561 bool missingKey = !mDum.getSecurity()->hasUserPrivateKey(mSenderAor);
562 bool missingRecipCert = !mDum.getSecurity()->hasUserCert(mRecipientAor);
564 if (!missingCert && !missingKey && !missingRecipCert)
566 InfoLog(<<
"Encrypting and signing message" << endl);
567 *contents = doWork();
575 InfoLog(<<
"Fetching cert for " << mSenderAor << endl);
582 InfoLog(<<
"Fetching private key for " << mSenderAor << endl);
587 if (missingRecipCert)
589 InfoLog(<<
"Fetching cert for " << mRecipientAor << endl);
598 InfoLog(<<
"No remote cert store installed" << endl);
607EncryptionManager::Result EncryptionManager::SignAndEncrypt::received(
bool success,
619 InfoLog(<<
"Adding user cert for " << aor << endl);
620 mDum.getSecurity()->addUserCertDER(aor, data);
625 InfoLog(<<
"Adding private key for " << aor << endl);
626 mDum.getSecurity()->addUserPrivateKeyDER(aor, data);
629 if (--mPendingRequests == 0)
631 InfoLog(<<
"Encrypting and signing message" << endl);
633 mMsgToEncrypt->setContents(unique_ptr<Contents>(contents));
637 mDum.post(
new TargetCommand(mDum.dumOutgoingTarget(), unique_ptr<Message>(event)));
643 InfoLog(<<
"Failed to fetch cert for " << aor << endl);
650Contents* EncryptionManager::SignAndEncrypt::doWork()
657 Pkcs7Contents* pkcs7 = mDum.getSecurity()->encrypt(parts.back(), mRecipientAor);
661 delete alt->
parts().back();
662 alt->
parts().pop_back();
663 alt->
parts().push_back(pkcs7);
669 contents = mDum.getSecurity()->encrypt(mMsgToEncrypt->getContents() , mRecipientAor);
674 contents = mDum.getSecurity()->sign(mSenderAor, contents);
684 : Request(dum, store, nullptr, feature),
701EncryptionManager::Decrypt::~Decrypt()
705 delete mMsgToDecrypt;
711 bool noDecryptionKey =
false;
713 if (!
dynamic_cast<Pkcs7Contents*
>(mMsgToDecrypt->getContents()))
715 mOriginalMsgContents =
Data(mMsgToDecrypt->getContents()->getHeaderField().getBuffer(), mMsgToDecrypt->getContents()->getHeaderField().getLength());
716 mOriginalMsgContentsType = mMsgToDecrypt->getContents()->getType();
725 bool missingDecryptorCert = !mDum.getSecurity()->hasUserCert(mDecryptor);
726 bool missingDecryptorKey = !mDum.getSecurity()->hasUserPrivateKey(mDecryptor);
727 if (missingDecryptorCert || missingDecryptorKey)
731 if (missingDecryptorCert)
733 InfoLog(<<
"Fetching user cert for " << mDecryptor << endl);
739 if (missingDecryptorKey)
741 InfoLog(<<
"Fetching private key for " << mDecryptor << endl);
746 mMessageTaken =
true;
751 InfoLog(<<
"No remote cert store installed" << endl);
752 noDecryptionKey =
true;
757 if (isSigned(noDecryptionKey))
759 if (!mDum.getSecurity()->hasUserCert(mSigner))
763 InfoLog(<<
"Fetching user cert for " << mSigner << endl);
767 mMessageTaken =
true;
772 InfoLog(<<
"No remote cert store installed" << endl);
777 csa = getContents(mMsgToDecrypt, *mDum.getSecurity(), noDecryptionKey);
781EncryptionManager::Result EncryptionManager::Decrypt::received(
bool success,
786 Result result = Complete;
787 resip_assert(mPendingRequests>0 && mPendingRequests<=2);
795 InfoLog(<<
"Adding user cert for " << aor << endl);
796 mDum.getSecurity()->addUserCertDER(aor, data);
803 InfoLog(<<
"Adding user cert for " << aor << endl);
804 mDum.getSecurity()->addUserCertDER(aor, data);
808 InfoLog(<<
"Adding private key for " << aor << endl);
809 mDum.getSecurity()->addUserPrivateKeyDER(aor, data);
812 if (--mPendingRequests == 0)
816 if (!mDum.getSecurity()->hasUserCert(mSigner))
818 InfoLog(<<
"Fetching user cert for " << mSigner << endl);
834 InfoLog(<<
"Failed to fetch cert for " << aor << endl);
837 if (Complete == result)
840 csa = getContents(mMsgToDecrypt, *mDum.getSecurity(),
841 (!mDum.getSecurity()->hasUserCert(mDecryptor) || !mDum.getSecurity()->hasUserPrivateKey(mDecryptor)));
847 mMsgToDecrypt->setContents(std::move(csa.
mContents));
851 mMsgToDecrypt->setSecurityAttributes(std::move(csa.
mAttributes));
857 ErrLog(<<
"No valid contents in message received" << endl);
858 handleInvalidContents();
870 mDum.post(decrypted);
876bool EncryptionManager::Decrypt::isEncrypted()
879 return isEncryptedRecurse(&contents);
882bool EncryptionManager::Decrypt::isSigned(
bool noDecryptionKey)
885 return isSignedRecurse(&contents, mDecryptor, noDecryptionKey);
888bool EncryptionManager::Decrypt::isEncryptedRecurse(
Contents** contents)
921 if (*contents == mMsgToDecrypt->getContents())
923 mMsgToDecrypt->setContents(std::unique_ptr<Contents>(createInvalidContents(mps)));
927 *contents = createInvalidContents(mps);
934 if (constMps->
parts().empty())
944 Contents*
const beforeRecurse = firstPart;
945 const bool encrypted = isEncryptedRecurse(&firstPart);
946 if (firstPart != beforeRecurse)
948 mps->
parts().front() = firstPart;
963 if (*contents == mMsgToDecrypt->getContents())
965 mMsgToDecrypt->setContents(std::unique_ptr<Contents>(createInvalidContents(alt)));
969 *contents = createInvalidContents(alt);
975 for (MultipartAlternativeContents::Parts::reverse_iterator i = alt->
parts().rbegin();
976 i != alt->
parts().rend(); ++i)
978 if (isEncryptedRecurse(&(*i)))
995bool EncryptionManager::Decrypt::isSignedRecurse(
Contents** contents,
996 const Data& decryptorAor,
997 bool noDecryptionKey)
1008 if (noDecryptionKey)
1013 Contents* decrypted = mDum.getSecurity()->decrypt(decryptorAor, pk);
1018 if (*contents == mMsgToDecrypt->getContents())
1021 mOriginalMsgContentsType = decrypted->
getType();
1027 if (isMultipart(decrypted))
1035 if (*contents == mMsgToDecrypt->getContents())
1037 mMsgToDecrypt->setContents(unique_ptr<Contents>(decrypted));
1042 *contents = decrypted;
1045 return isSignedRecurse(contents, decryptorAor, noDecryptionKey);
1053 if (*contents == mMsgToDecrypt->getContents())
1055 mMsgToDecrypt->setContents(std::unique_ptr<Contents>(createInvalidContents(decrypted)));
1059 *contents = createInvalidContents(decrypted);
1089 if (*contents == mMsgToDecrypt->getContents())
1091 mMsgToDecrypt->setContents(unique_ptr<Contents>(createInvalidContents(alt)));
1095 *contents = createInvalidContents(alt);
1102 for (MultipartAlternativeContents::Parts::reverse_iterator i = alt->
parts().rbegin();
1103 i != alt->
parts().rend(); ++i)
1105 if (isSignedRecurse(&(*i), decryptorAor, noDecryptionKey))
1124 bool noDecryptionKey)
1131 contents = getContentsRecurse(&contents, security, noDecryptionKey, attr);
1142 std::unique_ptr<Contents> c(contents);
1143 std::unique_ptr<SecurityAttributes> a(attr);
1149 bool noDecryptionKey,
1161 if (noDecryptionKey)
1169 if (*tree == mMsgToDecrypt->getContents())
1172 mOriginalMsgContentsType = contents->
getType();
1178 if (isMultipart(contents))
1180 if (*tree == mMsgToDecrypt->getContents())
1182 mMsgToDecrypt->setContents(std::unique_ptr<Contents>(contents));
1191 return getContentsRecurse(tree, security, noDecryptionKey, attributes);
1202 if (*tree == mMsgToDecrypt->getContents())
1204 mMsgToDecrypt->setContents(std::unique_ptr<Contents>(createInvalidContents(contents)));
1208 *tree = createInvalidContents(contents);
1226 Contents* contents = getContentsRecurse(&tmp,
1227 security, noDecryptionKey, attributes);
1246 if (*tree == mMsgToDecrypt->getContents())
1248 mMsgToDecrypt->setContents(std::unique_ptr<Contents>(createInvalidContents(alt)));
1252 *tree = createInvalidContents(alt);
1259 for (MultipartAlternativeContents::Parts::reverse_iterator i = alt->
parts().rbegin();
1260 i != alt->
parts().rend(); ++i)
1262 Contents* contents = getContentsRecurse(&(*i), security, noDecryptionKey, attributes);
1285 if (*tree == mMsgToDecrypt->getContents())
1287 mMsgToDecrypt->setContents(std::unique_ptr<Contents>(createInvalidContents(mult)));
1291 *tree = createInvalidContents(mult);
1299 return mult->
clone();
1307 ret = (*tree)->
clone();
1313 if (*tree == mMsgToDecrypt->getContents())
1315 mMsgToDecrypt->setContents(std::unique_ptr<Contents>(createInvalidContents(*tree)));
1320 *tree = createInvalidContents(*tree);
1331EncryptionManager::Decrypt::createInvalidContents(
Contents* orig)
1338EncryptionManager::Decrypt::isMultipart(
Contents* contents)
1348EncryptionManager::Decrypt::handleInvalidContents()
1350 if (mMsgToDecrypt->isRequest())
1354 DebugLog(<<
"No valid contents in the request" << endl);
1356 mMsgToDecrypt->setContents(unique_ptr<Contents>(invalid));
1360 DebugLog(<<
"No valid contents in the request -- reject with 400" << endl);
1363 mDum.getSipStack().send(response);
1368 DebugLog(<<
"No valid contents in the response" << endl);
1370 mMsgToDecrypt->setContents(unique_ptr<Contents>(invalid));
static bool isAckOrCancelOrBye(const SipMessage &msg)
#define resip_assert(x)
This file needs to work for both C and C++ code.
The abstract base-class for all exceptions thrown by resip code.
const Data & getMessage() const noexcept
virtual const char * name() const noexcept=0
Contents * checkSignature(MultipartSignedContents *, Data *signedBy, SignatureStatus *sigStat)
Contents * decrypt(const Data &decryptorAor, const Pkcs7Contents *)
const MessageId & id() const
const Data & body() const
Base class for all SIP body types.
virtual Contents * getContents()
access to wrapped contents (signed, encrypted)
const Mime & getType() const
getter for mime type of message
virtual Contents * clone() const =0
returns a copy of a Contents object
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
static const Data Empty
Constant that represents a zero-length data.
DialogUsageManager & mDum
static void setEncryptionPerformed(SipMessage &message)
Exception(const Data &msg, const Data &file, int line)
virtual DumFeature::ProcessingResult process(Message *msg)
EncryptionManager(DialogUsageManager &dum, TargetCommand::Target &target)
void setRemoteCertStore(std::unique_ptr< RemoteCertStore > store)
virtual ~EncryptionManager()
static void makeResponse(SipMessage &response, const SipMessage &request, int responseCode, const Data &reason=Data::Empty, const Data &hostname=Data::Empty, const Data &warning=Data::Empty)
Make a response to a provided request.
SIP body type to represent an invalid content type (MIME content-type Invalid/Invalid).
HeaderFieldValue & getHeaderField()
The base-class used for message-passing.
SIP body type for holding Multipart-Alternative body contents (MIME content-type multipart/alternativ...
SIP body type for holding Multipart-Mixed body contents (MIME content-type multipart/mixed).
virtual Contents * clone() const
duplicate an MultipartMixedContents object
std::vector< Contents * > Parts
SIP body type for holding Multipart-Signed body contents (MIME content-type multipart/signed).
SIP body type for holding PKCS7 contents (MIME content-type application/pkcs7-mime).
MethodTypes getMethod() const
IdentityStrength getIdentityStrength() const
void setIdentity(const Data &identity)
void setSignatureStatus(SignatureStatus status)
void setSigner(const Data &signer)
Represents a SIP message.
void setContents(const Contents *contents)
Set the contents of the message.
bool isRequest() const
Returns true if message is a request, false otherwise.
Contents * getContents() const
Retrieves the body of a SIP message.
void setSecurityAttributes(std::unique_ptr< SecurityAttributes >) noexcept
const RequestLine & header(const RequestLineType &l) const
Returns the RequestLine. This is only valid for request messages.
bool isResponse() const
Returns true if message is a response, false otherwise.
const SecurityAttributes * getSecurityAttributes() const noexcept
dcm! – add UnusedChecking(_enum) below;
RequestLineType h_RequestLine
std::unique_ptr< Contents > mContents
std::unique_ptr< SecurityAttributes > mAttributes