DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (409f3966645a)

VCS Links

Line Code
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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FunctionBroker.h"
#include "FunctionBrokerParent.h"
#include "PluginQuirks.h"

#if defined(XP_WIN)
#include <commdlg.h>
#include <schannel.h>
#include <sddl.h>
#endif // defined(XP_WIN)

using namespace mozilla;
using namespace mozilla::ipc;
using namespace mozilla::plugins;

namespace mozilla {
namespace plugins {

template <int QuirkFlag>
static bool CheckQuirks(int aQuirks)
{
  return static_cast<bool>(aQuirks & QuirkFlag);
}

void FreeDestructor(void* aObj) { free(aObj); }

#if defined(XP_WIN)

// Specialization of EndpointHandlers for Flash file dialog brokering.
struct FileDlgEHContainer
{
  template<Endpoint e> struct EndpointHandler;
};

template<>
struct FileDlgEHContainer::EndpointHandler<CLIENT> :
  public BaseEndpointHandler<CLIENT, FileDlgEHContainer::EndpointHandler<CLIENT>>
{
  using BaseEndpointHandler<CLIENT, EndpointHandler<CLIENT>>::Copy;

  inline static void Copy(OpenFileNameIPC& aDest, const LPOPENFILENAMEW& aSrc)
  {
    aDest.CopyFromOfn(aSrc);
  }
  inline static void Copy(LPOPENFILENAMEW& aDest, const OpenFileNameRetIPC& aSrc)
  {
    aSrc.AddToOfn(aDest);
  }
};

template<>
struct FileDlgEHContainer::EndpointHandler<SERVER> :
  public BaseEndpointHandler<SERVER, FileDlgEHContainer::EndpointHandler<SERVER>>
{
  using BaseEndpointHandler<SERVER, EndpointHandler<SERVER>>::Copy;

  inline static void Copy(OpenFileNameRetIPC& aDest, const LPOPENFILENAMEW& aSrc)
  {
    aDest.CopyFromOfn(aSrc);
  }
  inline static void Copy(ServerCallData* aScd, LPOPENFILENAMEW& aDest, const OpenFileNameIPC& aSrc)
  {
    MOZ_ASSERT(!aDest);
    ServerCallData::DestructorType* destructor =
      [](void* aObj) {
      OpenFileNameIPC::FreeOfnStrings(static_cast<LPOPENFILENAMEW>(aObj));
      DeleteDestructor<OPENFILENAMEW>(aObj);
    };
    aDest = aScd->Allocate<OPENFILENAMEW>(destructor);
    aSrc.AllocateOfnStrings(aDest);
    aSrc.AddToOfn(aDest);
  }
};

// FunctionBroker type that uses FileDlgEHContainer
template <FunctionHookId functionId, typename FunctionType>
using FileDlgFunctionBroker = FunctionBroker<functionId, FunctionType, FileDlgEHContainer>;

// Specialization of EndpointHandlers for Flash SSL brokering.
struct SslEHContainer {
  template<Endpoint e> struct EndpointHandler;
};

template<>
struct SslEHContainer::EndpointHandler<CLIENT> :
  public BaseEndpointHandler<CLIENT, SslEHContainer::EndpointHandler<CLIENT>>
{
  using BaseEndpointHandler<CLIENT, EndpointHandler<CLIENT>>::Copy;

  inline static void Copy(uint64_t& aDest, const PSecHandle& aSrc)
  {
    MOZ_ASSERT((aSrc->dwLower == aSrc->dwUpper) && IsOdd(aSrc->dwLower));
    aDest = static_cast<uint64_t>(aSrc->dwLower);
  }
  inline static void Copy(PSecHandle& aDest, const uint64_t& aSrc)
  {
    MOZ_ASSERT(IsOdd(aSrc));
    aDest->dwLower = static_cast<ULONG_PTR>(aSrc);
    aDest->dwUpper = static_cast<ULONG_PTR>(aSrc);
  }
  inline static void Copy(IPCSchannelCred& aDest, const PSCHANNEL_CRED& aSrc)
  {
    if (aSrc) {
      aDest.CopyFrom(aSrc);
    }
  }
  inline static void Copy(IPCInternetBuffers& aDest, const LPINTERNET_BUFFERSA& aSrc)
  {
    aDest.CopyFrom(aSrc);
  }
};

template<>
struct SslEHContainer::EndpointHandler<SERVER> :
  public BaseEndpointHandler<SERVER, SslEHContainer::EndpointHandler<SERVER>>
{
  using BaseEndpointHandler<SERVER, EndpointHandler<SERVER>>::Copy;

  // PSecHandle is the same thing as PCtxtHandle and PCredHandle.
  inline static void Copy(uint64_t& aDest, const PSecHandle& aSrc)
  {
    // If the SecHandle was an error then don't store it.
    if (!aSrc) {
      aDest = 0;
      return;
    }

    static uint64_t sNextVal = 1;
    UlongPair key(aSrc->dwLower, aSrc->dwUpper);
    // Fetch val by reference to update the value in the map
    uint64_t& val = sPairToIdMap[key];
    if (val == 0) {
      MOZ_ASSERT(IsOdd(sNextVal));
      val = sNextVal;
      sIdToPairMap[val] = key;
      sNextVal += 2;
    }
    aDest = val;
  }

  // HANDLEs and HINTERNETs marshal with obfuscation (for return values)
  inline static void Copy(uint64_t& aDest, void* const & aSrc)
  {
    // If the HANDLE/HINTERNET was an error then don't store it.
    if (!aSrc) {
      aDest = 0;
      return;
    }

    static uint64_t sNextVal = 1;
    // Fetch val by reference to update the value in the map
    uint64_t& val = sPtrToIdMap[aSrc];
    if (val == 0) {
      MOZ_ASSERT(IsOdd(sNextVal));
      val = sNextVal;
      sIdToPtrMap[val] = aSrc;
      sNextVal += 2;
    }
    aDest = val;
  }

  // HANDLEs and HINTERNETs unmarshal with obfuscation
  inline static void Copy(void*& aDest, const uint64_t& aSrc)
  {
    aDest = nullptr;
    MOZ_RELEASE_ASSERT(IsOdd(aSrc));

    // If the src is not found in the map then we get aDest == 0
    void* ptr = sIdToPtrMap[aSrc];
    aDest = reinterpret_cast<void*>(ptr);
    MOZ_RELEASE_ASSERT(aDest);
  }

  inline static void Copy(PSCHANNEL_CRED& aDest, const IPCSchannelCred& aSrc)
  {
    if (aDest) {
      aSrc.CopyTo(aDest);
    }
  }

  inline static void Copy(ServerCallData* aScd, PSecHandle& aDest, const uint64_t& aSrc)
  {
    MOZ_ASSERT(!aDest);
    MOZ_RELEASE_ASSERT(IsOdd(aSrc));

    // If the src is not found in the map then we get the pair { 0, 0 }
    aDest = aScd->Allocate<SecHandle>();
    const UlongPair& pair = sIdToPairMap[aSrc];
    MOZ_RELEASE_ASSERT(pair.first || pair.second);
    aDest->dwLower = pair.first;
    aDest->dwUpper = pair.second;
  }

  inline static void Copy(ServerCallData* aScd, PSCHANNEL_CRED& aDest, const IPCSchannelCred& aSrc)
  {
    MOZ_ASSERT(!aDest);
    aDest = aScd->Allocate<SCHANNEL_CRED>();
    Copy(aDest, aSrc);
  }

  inline static void Copy(ServerCallData* aScd, LPINTERNET_BUFFERSA& aDest, const IPCInternetBuffers& aSrc)
  {
    MOZ_ASSERT(!aDest);
    aSrc.CopyTo(aDest);
    ServerCallData::DestructorType* destructor =
      [](void* aObj) {
      LPINTERNET_BUFFERSA inetBuf = static_cast<LPINTERNET_BUFFERSA>(aObj);
      IPCInternetBuffers::FreeBuffers(inetBuf);
      FreeDestructor(inetBuf);
    };
    aScd->PostDestructor(aDest, destructor);
  }
};

// FunctionBroker type that uses SslEHContainer
template <FunctionHookId functionId, typename FunctionType>
using SslFunctionBroker = FunctionBroker<functionId, FunctionType, SslEHContainer>;

/* GetKeyState */

typedef FunctionBroker<ID_GetKeyState, decltype(GetKeyState)> GetKeyStateFB;

template<>
ShouldHookFunc* const
GetKeyStateFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_GETKEYSTATE>;

/* SetCursorPos */

typedef FunctionBroker<ID_SetCursorPos,
                       decltype(SetCursorPos)> SetCursorPosFB;

/* GetSaveFileNameW */

typedef FileDlgFunctionBroker<ID_GetSaveFileNameW,
                              decltype(GetSaveFileNameW)> GetSaveFileNameWFB;

// Remember files granted access in the chrome process
static void GrantFileAccess(base::ProcessId aClientId, LPOPENFILENAME& aLpofn,
                            bool isSave)
{
#if defined(MOZ_SANDBOX)
  if (aLpofn->Flags & OFN_ALLOWMULTISELECT) {
    // We only support multiselect with the OFN_EXPLORER flag.
    // This guarantees that ofn.lpstrFile follows the pattern below.
    MOZ_ASSERT(aLpofn->Flags & OFN_EXPLORER);

    // lpstrFile is one of two things:
    // 1. A null terminated full path to a file, or
    // 2. A path to a folder, followed by a NULL, followed by a
    // list of file names, each NULL terminated, followed by an
    // additional NULL (so it is also double-NULL terminated).
    std::wstring path = std::wstring(aLpofn->lpstrFile);
    MOZ_ASSERT(aLpofn->nFileOffset > 0);
    // For condition #1, nFileOffset points to the file name in the path.
    // It will be preceeded by a non-NULL character from the path.
    if (aLpofn->lpstrFile[aLpofn->nFileOffset-1] != L'\0') {
      FunctionBrokerParent::GetSandboxPermissions()->GrantFileAccess(aClientId, path.c_str(), isSave);
    }
    else {
      // This is condition #2
      wchar_t* nextFile = aLpofn->lpstrFile + path.size() + 1;
      while (*nextFile != L'\0') {
        std::wstring nextFileStr(nextFile);
        std::wstring fullPath =
          path + std::wstring(L"\\") + nextFileStr;
        FunctionBrokerParent::GetSandboxPermissions()->GrantFileAccess(aClientId, fullPath.c_str(), isSave);
        nextFile += nextFileStr.size() + 1;
      }
    }
  }
  else {
    FunctionBrokerParent::GetSandboxPermissions()->GrantFileAccess(aClientId, aLpofn->lpstrFile, isSave);
  }
#else
  MOZ_ASSERT_UNREACHABLE("GetFileName IPC message is only available on "
                         "Windows builds with sandbox.");
#endif
}

template<> template<>
BOOL GetSaveFileNameWFB::RunFunction(GetSaveFileNameWFB::FunctionType* aOrigFunction,
                                    base::ProcessId aClientId,
                                    LPOPENFILENAMEW& aLpofn) const
{
  BOOL result = aOrigFunction(aLpofn);
  if (result) {
    // Record any file access permission that was just granted.
    GrantFileAccess(aClientId, aLpofn, true);
  }
  return result;
}

template<> template<>
struct GetSaveFileNameWFB::Response::Info::ShouldMarshal<0> { static const bool value = true; };

/* GetOpenFileNameW */

typedef FileDlgFunctionBroker<ID_GetOpenFileNameW,
                              decltype(GetOpenFileNameW)> GetOpenFileNameWFB;

template<> template<>
BOOL GetOpenFileNameWFB::RunFunction(GetOpenFileNameWFB::FunctionType* aOrigFunction,
                                    base::ProcessId aClientId,
                                    LPOPENFILENAMEW& aLpofn) const
{
  BOOL result = aOrigFunction(aLpofn);
  if (result) {
    // Record any file access permission that was just granted.
    GrantFileAccess(aClientId, aLpofn, false);
  }
  return result;
}

template<> template<>
struct GetOpenFileNameWFB::Response::Info::ShouldMarshal<0> { static const bool value = true; };

/* InternetOpenA */

typedef SslFunctionBroker<ID_InternetOpenA,
                          decltype(InternetOpenA)> InternetOpenAFB;

template<>
ShouldHookFunc* const
InternetOpenAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

/* InternetConnectA */

typedef SslFunctionBroker<ID_InternetConnectA,
                          decltype(InternetConnectA)> InternetConnectAFB;

template<>
ShouldHookFunc* const
InternetConnectAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef InternetConnectAFB::Request ICAReqHandler;

template<>
bool ICAReqHandler::ShouldBroker(Endpoint endpoint, const HINTERNET& h,
                                  const LPCSTR& srv, const INTERNET_PORT& port,
                                  const LPCSTR& user, const LPCSTR& pass,
                                  const DWORD& svc, const DWORD& flags,
                                  const DWORD_PTR& cxt)
{
  return (endpoint == SERVER) || IsOdd(reinterpret_cast<uint64_t>(h));
}

/* InternetCloseHandle */

typedef SslFunctionBroker<ID_InternetCloseHandle,
                          decltype(InternetCloseHandle)> InternetCloseHandleFB;

template<>
ShouldHookFunc* const
InternetCloseHandleFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef InternetCloseHandleFB::Request ICHReqHandler;

template<>
bool ICHReqHandler::ShouldBroker(Endpoint endpoint, const HINTERNET& h)
{
  // If we are server side then we were already validated since we had to be
  // looked up in the "uint64_t <-> HINTERNET" hashtable.
  // In the client, we check that this is a dummy handle.
  return (endpoint == SERVER) || IsOdd(reinterpret_cast<uint64_t>(h));
}

/* InternetQueryDataAvailable */

typedef SslFunctionBroker<ID_InternetQueryDataAvailable,
                          decltype(InternetQueryDataAvailable)> InternetQueryDataAvailableFB;

template<>
ShouldHookFunc* const
InternetQueryDataAvailableFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef InternetQueryDataAvailableFB::Request IQDAReq;
typedef InternetQueryDataAvailableFB::RequestDelegate<BOOL HOOK_CALL (HINTERNET)> IQDADelegateReq;

template<>
void IQDAReq::Marshal(IpdlTuple& aTuple, const HINTERNET& file,
                      const LPDWORD& nBytes, const DWORD& flags,
                      const DWORD_PTR& cxt)
{
  IQDADelegateReq::Marshal(aTuple, file);
}

template<>
bool IQDAReq::Unmarshal(ServerCallData& aScd, const IpdlTuple& aTuple, HINTERNET& file,
                        LPDWORD& nBytes, DWORD& flags, DWORD_PTR& cxt)
{
  bool success = IQDADelegateReq::Unmarshal(aScd, aTuple, file);
  if (!success) {
    return false;
  }
  flags = 0;
  cxt = 0;
  nBytes = aScd.Allocate<DWORD>();
  return true;
}

template<>
bool IQDAReq::ShouldBroker(Endpoint endpoint, const HINTERNET& file,
                           const LPDWORD& nBytes, const DWORD& flags,
                           const DWORD_PTR& cxt)
{
  // If we are server side then we were already validated since we had to be
  // looked up in the "uint64_t <-> HINTERNET" hashtable.
  // In the client, we check that this is a dummy handle.
  return (endpoint == SERVER) ||
         ((flags == 0) && (cxt == 0) &&
          IsOdd(reinterpret_cast<uint64_t>(file)));
}

template<> template<>
struct InternetQueryDataAvailableFB::Response::Info::ShouldMarshal<1> { static const bool value = true; };

/* InternetReadFile */

typedef SslFunctionBroker<ID_InternetReadFile,
                          decltype(InternetReadFile)> InternetReadFileFB;

template<>
ShouldHookFunc* const
InternetReadFileFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef InternetReadFileFB::Request IRFRequestHandler;
typedef InternetReadFileFB::RequestDelegate<BOOL HOOK_CALL (HINTERNET, DWORD)> IRFDelegateReq;

template<>
void IRFRequestHandler::Marshal(IpdlTuple& aTuple, const HINTERNET& h,
                                const LPVOID& buf, const DWORD& nBytesToRead,
                                const LPDWORD& nBytesRead)
{
  IRFDelegateReq::Marshal(aTuple, h, nBytesToRead);
}


template<>
bool IRFRequestHandler::Unmarshal(ServerCallData& aScd, const IpdlTuple& aTuple, HINTERNET& h,
                                  LPVOID& buf, DWORD& nBytesToRead,
                                  LPDWORD& nBytesRead)
{
  bool ret = IRFDelegateReq::Unmarshal(aScd, aTuple, h, nBytesToRead);
  if (!ret) {
    return false;
  }

  nBytesRead = aScd.Allocate<DWORD>();
  MOZ_ASSERT(nBytesToRead > 0);
  aScd.AllocateMemory(nBytesToRead, buf);
  return true;
}

template<>
bool IRFRequestHandler::ShouldBroker(Endpoint endpoint, const HINTERNET& h,
                                     const LPVOID& buf, const DWORD& nBytesToRead,
                                     const LPDWORD& nBytesRead)
{
  // For server-side validation, the HINTERNET deserialization will have
  // required it to already be looked up in the IdToPtrMap.  At that point,
  // any call is valid.
  return (endpoint == SERVER) || IsOdd(reinterpret_cast<uint64_t>(h));
}

typedef InternetReadFileFB::Response IRFResponseHandler;
typedef InternetReadFileFB::ResponseDelegate<BOOL HOOK_CALL (nsDependentCSubstring)> IRFDelegateResponseHandler;

// Marshal the output parameter that we sent to the response delegate.
template<> template<>
struct IRFResponseHandler::Info::ShouldMarshal<0> { static const bool value = true; };

template<>
void IRFResponseHandler::Marshal(IpdlTuple& aTuple, const BOOL& ret, const HINTERNET& h,
                                 const LPVOID& buf, const DWORD& nBytesToRead,
                                 const LPDWORD& nBytesRead)
{
  nsDependentCSubstring str;
  if (*nBytesRead) {
    str.Assign(static_cast<const char*>(buf), *nBytesRead);
  }
  IRFDelegateResponseHandler::Marshal(aTuple, ret, str);
}

template<>
bool IRFResponseHandler::Unmarshal(const IpdlTuple& aTuple, BOOL& ret, HINTERNET& h,
                                   LPVOID& buf, DWORD& nBytesToRead,
                                   LPDWORD& nBytesRead)
{
  nsDependentCSubstring str;
  bool success =
    IRFDelegateResponseHandler::Unmarshal(aTuple, ret, str);
  if (!success) {
    return false;
  }

  if (str.Length()) {
    memcpy(buf, str.Data(), str.Length());
    *nBytesRead = str.Length();
  }
  return true;
}

/* InternetWriteFile */

typedef SslFunctionBroker<ID_InternetWriteFile,
                          decltype(InternetWriteFile)> InternetWriteFileFB;

template<>
ShouldHookFunc* const
InternetWriteFileFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef InternetWriteFileFB::Request IWFReqHandler;
typedef InternetWriteFileFB::RequestDelegate<int HOOK_CALL (HINTERNET, nsDependentCSubstring)> IWFDelegateReqHandler;

template<>
void IWFReqHandler::Marshal(IpdlTuple& aTuple, const HINTERNET& file, const LPCVOID& buf,
                            const DWORD& nToWrite, const LPDWORD& nWritten)
{
  MOZ_ASSERT(nWritten);
  IWFDelegateReqHandler::Marshal(aTuple, file,
                                  nsDependentCSubstring(static_cast<const char*>(buf), nToWrite));
}

template<>
bool IWFReqHandler::Unmarshal(ServerCallData& aScd, const IpdlTuple& aTuple, HINTERNET& file,
                              LPCVOID& buf, DWORD& nToWrite, LPDWORD& nWritten)
{
  nsDependentCSubstring str;
  if (!IWFDelegateReqHandler::Unmarshal(aScd, aTuple, file, str)) {
    return false;
  }

  aScd.AllocateString(str, buf, false);
  nToWrite = str.Length();
  nWritten = aScd.Allocate<DWORD>();
  return true;
}

template<>
bool IWFReqHandler::ShouldBroker(Endpoint endpoint, const HINTERNET& file,
                                 const LPCVOID& buf, const DWORD& nToWrite,
                                 const LPDWORD& nWritten)
{
  // For server-side validation, the HINTERNET deserialization will have
  // required it to already be looked up in the IdToPtrMap.  At that point,
  // any call is valid.
  return (endpoint == SERVER) || IsOdd(reinterpret_cast<uint64_t>(file));
}

template<> template<>
struct InternetWriteFileFB::Response::Info::ShouldMarshal<3> { static const bool value = true; };

/* InternetSetOptionA */

typedef SslFunctionBroker<ID_InternetSetOptionA,
                          decltype(InternetSetOptionA)> InternetSetOptionAFB;

template<>
ShouldHookFunc* const
InternetSetOptionAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef InternetSetOptionAFB::Request ISOAReqHandler;
typedef InternetSetOptionAFB::RequestDelegate<BOOL HOOK_CALL (HINTERNET, DWORD, nsDependentCSubstring)> ISOADelegateReqHandler;

template<>
void ISOAReqHandler::Marshal(IpdlTuple& aTuple, const HINTERNET& h, const DWORD& opt,
                             const LPVOID& buf, const DWORD& bufLen)
{
  ISOADelegateReqHandler::Marshal(aTuple, h, opt,
                                  nsDependentCSubstring(static_cast<const char*>(buf), bufLen));
}

template<>
bool ISOAReqHandler::Unmarshal(ServerCallData& aScd, const IpdlTuple& aTuple, HINTERNET& h,
                               DWORD& opt, LPVOID& buf, DWORD& bufLen)
{
  nsDependentCSubstring str;
  if (!ISOADelegateReqHandler::Unmarshal(aScd, aTuple, h, opt, str)) {
    return false;
  }

  aScd.AllocateString(str, buf, false);
  bufLen = str.Length();
  return true;
}

template<>
bool ISOAReqHandler::ShouldBroker(Endpoint endpoint, const HINTERNET& h, const DWORD& opt,
                                  const LPVOID& buf, const DWORD& bufLen)
{
  // For server-side validation, the HINTERNET deserialization will have
  // required it to already be looked up in the IdToPtrMap.  At that point,
  // any call is valid.
  return (endpoint == SERVER) || IsOdd(reinterpret_cast<uint64_t>(h));
}

/* HttpAddRequestHeadersA */

typedef SslFunctionBroker<ID_HttpAddRequestHeadersA,
                          decltype(HttpAddRequestHeadersA)> HttpAddRequestHeadersAFB;

template<>
ShouldHookFunc* const
HttpAddRequestHeadersAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef HttpAddRequestHeadersAFB::Request HARHAReqHandler;

template<>
bool HARHAReqHandler::ShouldBroker(Endpoint endpoint, const HINTERNET& h,
                                   const LPCSTR& head, const DWORD& headLen,
                                   const DWORD& mods)
{
  // For server-side validation, the HINTERNET deserialization will have
  // required it to already be looked up in the IdToPtrMap.  At that point,
  // any call is valid.
  return (endpoint == SERVER) || IsOdd(reinterpret_cast<uint64_t>(h));
}

/* HttpOpenRequestA */

typedef SslFunctionBroker<ID_HttpOpenRequestA,
                          decltype(HttpOpenRequestA)> HttpOpenRequestAFB;

template<>
ShouldHookFunc* const
HttpOpenRequestAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef HttpOpenRequestAFB::Request HORAReqHandler;
typedef HttpOpenRequestAFB::RequestDelegate<HINTERNET HOOK_CALL (HINTERNET, LPCSTR, LPCSTR, LPCSTR, LPCSTR, nsTArray<nsCString>, DWORD, DWORD_PTR)> HORADelegateReqHandler;

template<>
void HORAReqHandler::Marshal(IpdlTuple& aTuple, const HINTERNET& h,
                             const LPCSTR& verb, const LPCSTR& obj,
                             const LPCSTR& ver, const LPCSTR& ref,
                             LPCSTR * const & acceptTypes, const DWORD& flags,
                             const DWORD_PTR& cxt)
{
  nsTArray<nsCString> arrayAcceptTypes;
  LPCSTR * curAcceptType = acceptTypes;
  if (curAcceptType) {
    while (*curAcceptType) {
      arrayAcceptTypes.AppendElement(nsCString(*curAcceptType));
      ++curAcceptType;
    }
  }
  HORADelegateReqHandler::Marshal(aTuple, h, verb, obj, ver, ref,
                                  arrayAcceptTypes, flags, cxt);
}

template<>
bool HORAReqHandler::Unmarshal(ServerCallData& aScd, const IpdlTuple& aTuple, HINTERNET& h,
                               LPCSTR& verb, LPCSTR& obj, LPCSTR& ver,
                               LPCSTR& ref, LPCSTR *& acceptTypes,
                               DWORD& flags, DWORD_PTR& cxt)
{
  nsTArray<nsCString> arrayAcceptTypes;
  if (!HORADelegateReqHandler::Unmarshal(aScd, aTuple, h, verb, obj, ver, ref, arrayAcceptTypes, flags, cxt)) {
    return false;
  }
  if (arrayAcceptTypes.Length() == 0) {
    acceptTypes = nullptr;
  } else {
    aScd.AllocateMemory((arrayAcceptTypes.Length() + 1) * sizeof(LPCSTR), acceptTypes);
    for (size_t i = 0; i < arrayAcceptTypes.Length(); ++i) {
      aScd.AllocateString(arrayAcceptTypes[i], acceptTypes[i]);
    }
    acceptTypes[arrayAcceptTypes.Length()] = nullptr;
  }
  return true;
}

template<>
bool HORAReqHandler::ShouldBroker(Endpoint endpoint, const HINTERNET& h,
                                  const LPCSTR& verb, const LPCSTR& obj,
                                  const LPCSTR& ver, const LPCSTR& ref,
                                  LPCSTR * const & acceptTypes,
                                  const DWORD& flags, const DWORD_PTR& cxt)
{
  // For the server-side test, the HINTERNET deserialization will have
  // required it to already be looked up in the IdToPtrMap.  At that point,
  // any call is valid.
  return (endpoint == SERVER) || IsOdd(reinterpret_cast<uint64_t>(h));
}

/* HttpQueryInfoA */

typedef SslFunctionBroker<ID_HttpQueryInfoA,
                          decltype(HttpQueryInfoA)> HttpQueryInfoAFB;

template<>
ShouldHookFunc* const
HttpQueryInfoAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef HttpQueryInfoAFB::Request HQIARequestHandler;
typedef HttpQueryInfoAFB::RequestDelegate<BOOL HOOK_CALL (HINTERNET, DWORD, BOOL, DWORD, BOOL, DWORD)> HQIADelegateRequestHandler;

template<>
void HQIARequestHandler::Marshal(IpdlTuple& aTuple, const HINTERNET& h,
                                 const DWORD& lvl, const LPVOID& buf, const LPDWORD& bufLen,
                                 const LPDWORD& idx)
{
  HQIADelegateRequestHandler::Marshal(aTuple, h, lvl,
                                      bufLen != nullptr, bufLen ? *bufLen : 0,
                                      idx != nullptr, idx ? *idx : 0);
}

template<>
bool HQIARequestHandler::Unmarshal(ServerCallData& aScd, const IpdlTuple& aTuple, HINTERNET& h,
                                   DWORD& lvl, LPVOID& buf, LPDWORD& bufLen,
                                   LPDWORD& idx)
{
  BOOL hasBufLen, hasIdx;
  DWORD tempBufLen, tempIdx;
  bool success =
    HQIADelegateRequestHandler::Unmarshal(aScd, aTuple, h, lvl, hasBufLen, tempBufLen, hasIdx, tempIdx);
  if (!success) {
    return false;
  }

  bufLen = nullptr;
  if (hasBufLen) {
    aScd.AllocateMemory(tempBufLen, buf, bufLen);
  }

  idx = nullptr;
  if (hasIdx) {
    idx = aScd.Allocate<DWORD>(tempIdx);
  }

  return true;
}

template<>
bool HQIARequestHandler::ShouldBroker(Endpoint endpoint, const HINTERNET& h,
                                      const DWORD& lvl, const LPVOID& buf,
                                      const LPDWORD& bufLen, const LPDWORD& idx)
{
  return (endpoint == SERVER) || IsOdd(reinterpret_cast<uint64_t>(h));
}

// Marshal all of the output parameters that we sent to the response delegate.
template<> template<>
struct HttpQueryInfoAFB::Response::Info::ShouldMarshal<0> { static const bool value = true; };
template<> template<>
struct HttpQueryInfoAFB::Response::Info::ShouldMarshal<1> { static const bool value = true; };
template<> template<>
struct HttpQueryInfoAFB::Response::Info::ShouldMarshal<2> { static const bool value = true; };

typedef HttpQueryInfoAFB::Response HQIAResponseHandler;
typedef HttpQueryInfoAFB::ResponseDelegate<BOOL HOOK_CALL (nsDependentCSubstring, DWORD, DWORD)> HQIADelegateResponseHandler;

template<>
void HQIAResponseHandler::Marshal(IpdlTuple& aTuple, const BOOL& ret, const HINTERNET& h,
                                 const DWORD& lvl, const LPVOID& buf, const LPDWORD& bufLen,
                                 const LPDWORD& idx)
{
  nsDependentCSubstring str;
  if (buf && ret) {
    MOZ_ASSERT(bufLen);
    str.Assign(static_cast<const char*>(buf), *bufLen);
  }
  // Note that we send the bufLen separately to handle the case where buf wasn't
  // allocated or large enough to hold the entire return value.  bufLen is then
  // the required buffer size.
  HQIADelegateResponseHandler::Marshal(aTuple, ret, str,
                                       bufLen ? *bufLen : 0, idx ? *idx : 0);
}

template<>
bool HQIAResponseHandler::Unmarshal(const IpdlTuple& aTuple, BOOL& ret, HINTERNET& h,
                                   DWORD& lvl, LPVOID& buf, LPDWORD& bufLen,
                                   LPDWORD& idx)
{
  DWORD totalBufLen = *bufLen;
  nsDependentCSubstring str;
  DWORD tempBufLen, tempIdx;
  bool success =
    HQIADelegateResponseHandler::Unmarshal(aTuple, ret, str, tempBufLen, tempIdx);
  if (!success) {
    return false;
  }

  if (bufLen) {
    *bufLen = tempBufLen;
  }
  if (idx) {
    *idx = tempIdx;
  }

  if (buf && ret) {
    // When HttpQueryInfo returns strings, the buffer length will not include
    // the null terminator.  Rather than (brittle-y) trying to determine if the
    // return buffer is a string, we always tack on a null terminator if the
    // buffer has room for it.
    MOZ_ASSERT(str.Length() == *bufLen);
    memcpy(buf, str.Data(), str.Length());
    if (str.Length() < totalBufLen) {
      char* cbuf = static_cast<char*>(buf);
      cbuf[str.Length()] = '\0';
    }
  }
  return true;
}

/* HttpSendRequestA */

typedef SslFunctionBroker<ID_HttpSendRequestA,
                          decltype(HttpSendRequestA)> HttpSendRequestAFB;

template<>
ShouldHookFunc* const
HttpSendRequestAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef HttpSendRequestAFB::Request HSRARequestHandler;
typedef HttpSendRequestAFB::RequestDelegate<BOOL HOOK_CALL (HINTERNET, nsDependentCSubstring, nsDependentCSubstring)> HSRADelegateRequestHandler;

template<>
void HSRARequestHandler::Marshal(IpdlTuple& aTuple, const HINTERNET& h,
                                 const LPCSTR& head, const DWORD& headLen,
                                 const LPVOID& opt, const DWORD& optLen)
{
  nsDependentCSubstring headStr;
  headStr.SetIsVoid(head == nullptr);
  if (head) {
    // HttpSendRequest allows headLen == -1L for length of a null terminated string.
    DWORD ncHeadLen = headLen;
    if (ncHeadLen == -1L) {
      ncHeadLen = strlen(head);
    }
    headStr.Rebind(head, ncHeadLen);
  }
  nsDependentCSubstring optStr;
  optStr.SetIsVoid(opt == nullptr);
  if (opt) {
    optStr.Rebind(static_cast<const char*>(opt), optLen);
  }
  HSRADelegateRequestHandler::Marshal(aTuple, h, headStr, optStr);
}

template<>
bool HSRARequestHandler::Unmarshal(ServerCallData& aScd, const IpdlTuple& aTuple, HINTERNET& h,
                                   LPCSTR& head, DWORD& headLen, LPVOID& opt,
                                   DWORD& optLen)
{
  nsDependentCSubstring headStr;
  nsDependentCSubstring optStr;
  bool success = HSRADelegateRequestHandler::Unmarshal(aScd, aTuple, h, headStr, optStr);
  if (!success) {
    return false;
  }

  if (headStr.IsVoid()) {
    head = nullptr;
    MOZ_ASSERT(headLen == 0);
  } else {
    aScd.AllocateString(headStr, head, false);
    headLen = headStr.Length();
  }

  if (optStr.IsVoid()) {
    opt = nullptr;
    MOZ_ASSERT(optLen == 0);
  } else {
    aScd.AllocateString(optStr, opt, false);
    optLen = optStr.Length();
  }
  return true;
}

template<>
bool HSRARequestHandler::ShouldBroker(Endpoint endpoint, const HINTERNET& h,
                                       const LPCSTR& head, const DWORD& headLen,
                                       const LPVOID& opt, const DWORD& optLen)
{
  // If we are server side then we were already validated since we had to be
  // looked up in the "uint64_t <-> HINTERNET" hashtable.
  // In the client, we check that this is a dummy handle.
  return (endpoint == SERVER) || IsOdd(reinterpret_cast<uint64_t>(h));
}

/* HttpSendRequestExA */

typedef SslFunctionBroker<ID_HttpSendRequestExA,
                          decltype(HttpSendRequestExA)> HttpSendRequestExAFB;

template<>
ShouldHookFunc* const
HttpSendRequestExAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef RequestInfo<ID_HttpSendRequestExA> HSRExAReqInfo;

template<> template<>
struct HSRExAReqInfo::FixedValue<2> { static const LPINTERNET_BUFFERSA value; };
const LPINTERNET_BUFFERSA HSRExAReqInfo::FixedValue<2>::value = nullptr;

// Docs for HttpSendRequestExA say this parameter 'must' be zero but Flash
// passes other values.
// template<> template<>
// struct HSRExAReqInfo::FixedValue<3> { static const DWORD value = 0; };

template<> template<>
struct HSRExAReqInfo::FixedValue<4> { static const DWORD_PTR value; };
const DWORD_PTR HSRExAReqInfo::FixedValue<4>::value = 0;

/* HttpEndRequestA */

typedef SslFunctionBroker<ID_HttpEndRequestA,
  decltype(HttpEndRequestA)> HttpEndRequestAFB;

template<>
ShouldHookFunc* const
HttpEndRequestAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef RequestInfo<ID_HttpEndRequestA> HERAReqInfo;

template<> template<>
struct HERAReqInfo::FixedValue<1> { static const LPINTERNET_BUFFERSA value; };
const LPINTERNET_BUFFERSA HERAReqInfo::FixedValue<1>::value = nullptr;

template<> template<>
struct HERAReqInfo::FixedValue<2> { static const DWORD value; };
const DWORD HERAReqInfo::FixedValue<2>::value = 0;

template<> template<>
struct HERAReqInfo::FixedValue<3> { static const DWORD_PTR value; };
const DWORD_PTR HERAReqInfo::FixedValue<3>::value = 0;

/* InternetQueryOptionA */

typedef SslFunctionBroker<ID_InternetQueryOptionA,
                          decltype(InternetQueryOptionA)> InternetQueryOptionAFB;

template<>
ShouldHookFunc* const
InternetQueryOptionAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef InternetQueryOptionAFB::Request IQOARequestHandler;
typedef InternetQueryOptionAFB::RequestDelegate<BOOL HOOK_CALL (HINTERNET, DWORD, DWORD)> IQOADelegateRequestHandler;

template<>
void IQOARequestHandler::Marshal(IpdlTuple& aTuple, const HINTERNET& h,
                                 const DWORD& opt, const LPVOID& buf, const LPDWORD& bufLen)
{
  MOZ_ASSERT(bufLen);
  IQOADelegateRequestHandler::Marshal(aTuple, h, opt, buf ? *bufLen : 0);
}

template<>
bool IQOARequestHandler::Unmarshal(ServerCallData& aScd, const IpdlTuple& aTuple, HINTERNET& h,
                                   DWORD& opt, LPVOID& buf, LPDWORD& bufLen)
{
  DWORD tempBufLen;
  bool success =
    IQOADelegateRequestHandler::Unmarshal(aScd, aTuple, h, opt, tempBufLen);
  if (!success) {
    return false;
  }

  aScd.AllocateMemory(tempBufLen, buf, bufLen);
  return true;
}

template<>
bool IQOARequestHandler::ShouldBroker(Endpoint endpoint, const HINTERNET& h,
                                       const DWORD& opt, const LPVOID& buf,
                                       const LPDWORD& bufLen)
{
  // If we are server side then we were already validated since we had to be
  // looked up in the "uint64_t <-> HINTERNET" hashtable.
  // In the client, we check that this is a dummy handle.
  return (endpoint == SERVER) || IsOdd(reinterpret_cast<uint64_t>(h));
}

// Marshal all of the output parameters that we sent to the response delegate.
template<> template<>
struct InternetQueryOptionAFB::Response::Info::ShouldMarshal<0> { static const bool value = true; };
template<> template<>
struct InternetQueryOptionAFB::Response::Info::ShouldMarshal<1> { static const bool value = true; };

typedef InternetQueryOptionAFB::Response IQOAResponseHandler;
typedef InternetQueryOptionAFB::ResponseDelegate<BOOL HOOK_CALL (nsDependentCSubstring, DWORD)> IQOADelegateResponseHandler;

template<>
void IQOAResponseHandler::Marshal(IpdlTuple& aTuple, const BOOL& ret, const HINTERNET& h,
                                  const DWORD& opt, const LPVOID& buf, const LPDWORD& bufLen)
{
  nsDependentCSubstring str;
  if (buf && ret) {
    MOZ_ASSERT(*bufLen);
    str.Assign(static_cast<const char*>(buf), *bufLen);
  }
  IQOADelegateResponseHandler::Marshal(aTuple, ret, str, *bufLen);
}

template<>
bool IQOAResponseHandler::Unmarshal(const IpdlTuple& aTuple, BOOL& ret, HINTERNET& h,
                                   DWORD& opt, LPVOID& buf, LPDWORD& bufLen)
{
  nsDependentCSubstring str;
  bool success =
    IQOADelegateResponseHandler::Unmarshal(aTuple, ret, str, *bufLen);
  if (!success) {
    return false;
  }

  if (buf && ret) {
    MOZ_ASSERT(str.Length() == *bufLen);
    memcpy(buf, str.Data(), str.Length());
  }
  return true;
}

/* InternetErrorDlg */

typedef SslFunctionBroker<ID_InternetErrorDlg,
                          decltype(InternetErrorDlg)> InternetErrorDlgFB;

template<>
ShouldHookFunc* const
InternetErrorDlgFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef RequestInfo<ID_InternetErrorDlg> IEDReqInfo;

template<> template<>
struct IEDReqInfo::FixedValue<4> { static LPVOID* const value; };
LPVOID* const IEDReqInfo::FixedValue<4>::value = nullptr;

typedef InternetErrorDlgFB::Request IEDReqHandler;

template<>
bool IEDReqHandler::ShouldBroker(Endpoint endpoint, const HWND& hwnd,
                                 const HINTERNET& h, const DWORD& err,
                                 const DWORD& flags, LPVOID* const & data)
{
  const DWORD SUPPORTED_FLAGS =
    FLAGS_ERROR_UI_FILTER_FOR_ERRORS | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS |
    FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_NO_UI;

  // We broker if (1) the handle h is brokered (odd in client),
  // (2) we support the requested action flags and (3) there is no user
  // data, which wouldn't make sense for our supported flags anyway.
  return ((endpoint == SERVER) || IsOdd(reinterpret_cast<uint64_t>(h))) &&
         (!(flags & ~SUPPORTED_FLAGS)) && (data == nullptr);
}

/* AcquireCredentialsHandleA */

typedef SslFunctionBroker<ID_AcquireCredentialsHandleA,
                          decltype(AcquireCredentialsHandleA)> AcquireCredentialsHandleAFB;

template<>
ShouldHookFunc* const
AcquireCredentialsHandleAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef RequestInfo<ID_AcquireCredentialsHandleA> ACHAReqInfo;

template<> template<>
struct ACHAReqInfo::FixedValue<0> { static const LPSTR value; };
const LPSTR ACHAReqInfo::FixedValue<0>::value = nullptr;

template<> template<>
struct ACHAReqInfo::FixedValue<1> { static const LPSTR value; };
const LPSTR ACHAReqInfo::FixedValue<1>::value = UNISP_NAME_A;

template<> template<>
struct ACHAReqInfo::FixedValue<2> { static const unsigned long value; };
const unsigned long ACHAReqInfo::FixedValue<2>::value = SECPKG_CRED_OUTBOUND;

template<> template<>
struct ACHAReqInfo::FixedValue<3> { static void* const value; };
void* const ACHAReqInfo::FixedValue<3>::value = nullptr;

template<> template<>
struct ACHAReqInfo::FixedValue<5> { static const SEC_GET_KEY_FN value; };
const SEC_GET_KEY_FN ACHAReqInfo::FixedValue<5>::value = nullptr;

template<> template<>
struct ACHAReqInfo::FixedValue<6> { static void* const value; };
void* const ACHAReqInfo::FixedValue<6>::value = nullptr;

typedef AcquireCredentialsHandleAFB::Request ACHARequestHandler;
typedef AcquireCredentialsHandleAFB::RequestDelegate<SECURITY_STATUS HOOK_CALL (LPSTR, LPSTR, unsigned long, void*, PSCHANNEL_CRED, SEC_GET_KEY_FN, void*)> ACHADelegateRequestHandler;

template<>
void ACHARequestHandler::Marshal(IpdlTuple& aTuple, const LPSTR& principal,
                                 const LPSTR& pkg, const unsigned long& credUse,
                                 const PVOID& logonId, const PVOID& auth,
                                 const SEC_GET_KEY_FN& getKeyFn,
                                 const PVOID& getKeyArg, const PCredHandle& cred,
                                 const PTimeStamp& expiry)
{
  const PSCHANNEL_CRED& scCred = reinterpret_cast<const PSCHANNEL_CRED&>(auth);
  ACHADelegateRequestHandler::Marshal(aTuple, principal, pkg, credUse, logonId,
                                      scCred, getKeyFn, getKeyArg);
}

template<>
bool ACHARequestHandler::Unmarshal(ServerCallData& aScd, const IpdlTuple& aTuple, LPSTR& principal,
                                   LPSTR& pkg, unsigned long& credUse,
                                   PVOID& logonId, PVOID& auth, SEC_GET_KEY_FN& getKeyFn,
                                   PVOID& getKeyArg, PCredHandle& cred, PTimeStamp& expiry)
{
  PSCHANNEL_CRED& scCred = reinterpret_cast<PSCHANNEL_CRED&>(auth);
  if (!ACHADelegateRequestHandler::Unmarshal(aScd, aTuple, principal, pkg, credUse,
                                             logonId, scCred, getKeyFn, getKeyArg)) {
    return false;
  }

  cred = aScd.Allocate<CredHandle>();
  expiry = aScd.Allocate<::TimeStamp>();
  return true;
}

typedef ResponseInfo<ID_AcquireCredentialsHandleA> ACHARspInfo;

// Response phase must send output parameters
template<> template<>
struct ACHARspInfo::ShouldMarshal<7> { static const bool value = true; };
template<> template<>
struct ACHARspInfo::ShouldMarshal<8> { static const bool value = true; };

/* QueryCredentialsAttributesA */

typedef SslFunctionBroker<ID_QueryCredentialsAttributesA,
                          decltype(QueryCredentialsAttributesA)> QueryCredentialsAttributesAFB;

template<>
ShouldHookFunc* const
QueryCredentialsAttributesAFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

/* FreeCredentialsHandle */

typedef SslFunctionBroker<ID_FreeCredentialsHandle,
                          decltype(FreeCredentialsHandle)> FreeCredentialsHandleFB;

template<>
ShouldHookFunc* const
FreeCredentialsHandleFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_SSL>;

typedef FreeCredentialsHandleFB::Request FCHReq;

template<>
bool FCHReq::ShouldBroker(Endpoint endpoint, const PCredHandle& h)
{
  // If we are server side then we were already validated since we had to be
  // looked up in the "uint64_t <-> CredHandle" hashtable.
  // In the client, we check that this is a dummy handle.
  return (endpoint == SERVER) ||
         ((h->dwLower == h->dwUpper) && IsOdd(static_cast<uint64_t>(h->dwLower)));
}

/* CreateMutexW */

// Get the user's SID as a string.  Returns an empty string on failure.
static std::wstring GetUserSid()
{
  std::wstring ret;
  // Get user SID from process token information
  HANDLE token;
  BOOL success = ::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token);
  if (!success) {
    return ret;
  }
  DWORD bufLen;
  success = ::GetTokenInformation(token, TokenUser, nullptr, 0, &bufLen);
  if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
    return ret;
  }
  void* buf = malloc(bufLen);
  success = ::GetTokenInformation(token, TokenUser, buf, bufLen, &bufLen);
  MOZ_ASSERT(success);
  if (success) {
    TOKEN_USER* tokenUser = static_cast<TOKEN_USER*>(buf);
    PSID sid = tokenUser->User.Sid;
    LPWSTR sidStr;
    success = ::ConvertSidToStringSid(sid, &sidStr);
    if (success) {
      ret = sidStr;
      ::LocalFree(sidStr);
    }
  }
  free(buf);
  ::CloseHandle(token);
  return ret;
}

// Get the name Windows uses for the camera mutex.  Returns an empty string
// on failure.
// The camera mutex is identified in Windows code using a hard-coded GUID string,
// "eed3bd3a-a1ad-4e99-987b-d7cb3fcfa7f0", and the user's SID.  The GUID
// value was determined by investigating Windows code.  It is referenced in
// CCreateSwEnum::CCreateSwEnum(void) in devenum.dll.
static std::wstring GetCameraMutexName()
{
  std::wstring userSid = GetUserSid();
  if (userSid.empty()) {
    return userSid;
  }
  return std::wstring(L"eed3bd3a-a1ad-4e99-987b-d7cb3fcfa7f0 - ") + userSid;
}

typedef FunctionBroker<ID_CreateMutexW, decltype(CreateMutexW)> CreateMutexWFB;

template<>
ShouldHookFunc* const
CreateMutexWFB::BaseType::mShouldHook = &CheckQuirks<QUIRK_FLASH_HOOK_CREATEMUTEXW>;

typedef CreateMutexWFB::Request CMWReqHandler;
typedef CMWReqHandler::Info CMWReqInfo;
typedef CreateMutexWFB::Response CMWRspHandler;

template<>
bool CMWReqHandler::ShouldBroker(Endpoint endpoint,
                                 const LPSECURITY_ATTRIBUTES& aAttribs,
                                 const BOOL& aOwner,
                                 const LPCWSTR& aName)
{
  // Statically hold the camera mutex name so that we dont recompute it for
  // every CreateMutexW call in the client process.
  static std::wstring camMutexName = GetCameraMutexName();

  // Only broker if we are requesting the camera mutex.  Note that we only
  // need to check that the client is actually requesting the camera.  The
  // command is always valid on the server as long as we can construct the
  // mutex name.
  if (endpoint == SERVER) {
    return !camMutexName.empty();
  }

  return (!aOwner) && aName && (!camMutexName.empty()) && (camMutexName == aName);
}

// We dont need to marshal any parameters.  We construct all of them server-side.
template<> template<>
struct CMWReqInfo::ShouldMarshal<0> { static const bool value = false; };
template<> template<>
struct CMWReqInfo::ShouldMarshal<1> { static const bool value = false; };
template<> template<>
struct CMWReqInfo::ShouldMarshal<2> { static const bool value = false; };

template<> template<>
HANDLE CreateMutexWFB::RunFunction(CreateMutexWFB::FunctionType* aOrigFunction,
                                   base::ProcessId aClientId,
                                   LPSECURITY_ATTRIBUTES& aAttribs,
                                   BOOL& aOwner,
                                   LPCWSTR& aName) const
{
  // Use CreateMutexW to get the camera mutex and DuplicateHandle to open it
  // for use in the child process.
  // Recall that aAttribs, aOwner and aName are all unmarshaled so they are
  // unassigned garbage.
  SECURITY_ATTRIBUTES mutexAttrib =
    { sizeof(SECURITY_ATTRIBUTES), nullptr /* ignored */, TRUE };
  std::wstring camMutexName = GetCameraMutexName();
  if (camMutexName.empty()) {
    return 0;
  }
  HANDLE serverMutex = ::CreateMutexW(&mutexAttrib, FALSE, camMutexName.c_str());
  if (serverMutex == 0) {
    return 0;
  }
  ScopedProcessHandle clientProcHandle;
  if (!base::OpenProcessHandle(aClientId, &clientProcHandle.rwget())) {
    return 0;
  }
  HANDLE ret;
  if (!::DuplicateHandle(::GetCurrentProcess(), serverMutex, clientProcHandle,
                         &ret, SYNCHRONIZE, FALSE, DUPLICATE_CLOSE_SOURCE)) {
    return 0;
  }
  return ret;
}

#endif // defined(XP_WIN)

/*****************************************************************************/

#define FUN_HOOK(x) static_cast<FunctionHook*>(x)
void
AddBrokeredFunctionHooks(FunctionHookArray& aHooks)
{
  // We transfer ownership of the FunctionHook objects to the array.
#if defined(XP_WIN)
  aHooks[ID_GetKeyState] =
    FUN_HOOK(new GetKeyStateFB("user32.dll", "GetKeyState", &GetKeyState));
  aHooks[ID_SetCursorPos] =
    FUN_HOOK(new SetCursorPosFB("user32.dll", "SetCursorPos", &SetCursorPos));
  aHooks[ID_GetSaveFileNameW] =
    FUN_HOOK(new GetSaveFileNameWFB("comdlg32.dll", "GetSaveFileNameW",
                                    &GetSaveFileNameW));
  aHooks[ID_GetOpenFileNameW] =
    FUN_HOOK(new GetOpenFileNameWFB("comdlg32.dll", "GetOpenFileNameW",
                                    &GetOpenFileNameW));
  aHooks[ID_InternetOpenA] =
    FUN_HOOK(new InternetOpenAFB("wininet.dll", "InternetOpenA", &InternetOpenA));
  aHooks[ID_InternetConnectA] =
    FUN_HOOK(new InternetConnectAFB("wininet.dll", "InternetConnectA",
                                    &InternetConnectA));
  aHooks[ID_InternetCloseHandle] =
    FUN_HOOK(new InternetCloseHandleFB("wininet.dll", "InternetCloseHandle",
                                       &InternetCloseHandle));
  aHooks[ID_InternetQueryDataAvailable] =
    FUN_HOOK(new InternetQueryDataAvailableFB("wininet.dll",
                                              "InternetQueryDataAvailable",
                                              &InternetQueryDataAvailable));
  aHooks[ID_InternetReadFile] =
    FUN_HOOK(new InternetReadFileFB("wininet.dll", "InternetReadFile",
                                    &InternetReadFile));
  aHooks[ID_InternetWriteFile] =
    FUN_HOOK(new InternetWriteFileFB("wininet.dll", "InternetWriteFile",
                                     &InternetWriteFile));
  aHooks[ID_InternetSetOptionA] =
    FUN_HOOK(new InternetSetOptionAFB("wininet.dll", "InternetSetOptionA",
                                      &InternetSetOptionA));
  aHooks[ID_HttpAddRequestHeadersA] =
    FUN_HOOK(new HttpAddRequestHeadersAFB("wininet.dll",
                                          "HttpAddRequestHeadersA",
                                          &HttpAddRequestHeadersA));
  aHooks[ID_HttpOpenRequestA] =
    FUN_HOOK(new HttpOpenRequestAFB("wininet.dll", "HttpOpenRequestA",
                                    &HttpOpenRequestA));
  aHooks[ID_HttpQueryInfoA] =
    FUN_HOOK(new HttpQueryInfoAFB("wininet.dll", "HttpQueryInfoA",
                                  &HttpQueryInfoA));
  aHooks[ID_HttpSendRequestA] =
    FUN_HOOK(new HttpSendRequestAFB("wininet.dll", "HttpSendRequestA",
                                    &HttpSendRequestA));
  aHooks[ID_HttpSendRequestExA] =
    FUN_HOOK(new HttpSendRequestExAFB("wininet.dll", "HttpSendRequestExA",
                                      &HttpSendRequestExA));
  aHooks[ID_HttpEndRequestA] =
    FUN_HOOK(new HttpEndRequestAFB("wininet.dll", "HttpEndRequestA",
      &HttpEndRequestA));
  aHooks[ID_InternetQueryOptionA] =
    FUN_HOOK(new InternetQueryOptionAFB("wininet.dll", "InternetQueryOptionA",
                                        &InternetQueryOptionA));
  aHooks[ID_InternetErrorDlg] =
    FUN_HOOK(new InternetErrorDlgFB("wininet.dll", "InternetErrorDlg",
                                    InternetErrorDlg));
  aHooks[ID_AcquireCredentialsHandleA] =
    FUN_HOOK(new AcquireCredentialsHandleAFB("sspicli.dll",
                                             "AcquireCredentialsHandleA",
                                             &AcquireCredentialsHandleA));
  aHooks[ID_QueryCredentialsAttributesA] =
    FUN_HOOK(new QueryCredentialsAttributesAFB("sspicli.dll",
                                               "QueryCredentialsAttributesA",
                                               &QueryCredentialsAttributesA));
  aHooks[ID_FreeCredentialsHandle] =
    FUN_HOOK(new FreeCredentialsHandleFB("sspicli.dll",
                                         "FreeCredentialsHandle",
                                         &FreeCredentialsHandle));
  aHooks[ID_CreateMutexW] =
    FUN_HOOK(new CreateMutexWFB("kernel32.dll", "CreateMutexW", &CreateMutexW));
#endif // defined(XP_WIN)
}

#undef FUN_HOOK

} // namespace plugins
} // namespace mozilla