Modified STUN verification functions

The new verification makes verification a function on a message.
It also stores the password used in the request message, so that
it is easily accessible when verifying the response.

Bug: chromium:1177125
Change-Id: I505df4b54214643a28a6b292c4e2262b9d97b097
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/209060
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33366}
diff --git a/p2p/base/stun_request.cc b/p2p/base/stun_request.cc
index 44376ce..2870dcd 100644
--- a/p2p/base/stun_request.cc
+++ b/p2p/base/stun_request.cc
@@ -120,6 +120,18 @@
   }
 
   StunRequest* request = iter->second;
+
+  // Now that we know the request, we can see if the response is
+  // integrity-protected or not.
+  // For some tests, the message integrity is not set in the request.
+  // Complain, and then don't check.
+  bool skip_integrity_checking = false;
+  if (request->msg()->integrity() == StunMessage::IntegrityStatus::kNotSet) {
+    skip_integrity_checking = true;
+  } else {
+    msg->ValidateMessageIntegrity(request->msg()->password());
+  }
+
   if (!msg->GetNonComprehendedAttributes().empty()) {
     // If a response contains unknown comprehension-required attributes, it's
     // simply discarded and the transaction is considered failed. See RFC5389
@@ -129,6 +141,9 @@
     delete request;
     return false;
   } else if (msg->type() == GetStunSuccessResponseType(request->type())) {
+    if (!msg->IntegrityOk() && !skip_integrity_checking) {
+      return false;
+    }
     request->OnResponse(msg);
   } else if (msg->type() == GetStunErrorResponseType(request->type())) {
     request->OnErrorResponse(msg);