2018-06-21 03:21:17 +03:00
|
|
|
/* -*- 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 "mozilla/ClearOnShutdown.h"
|
2018-09-09 03:23:52 +03:00
|
|
|
#include "mozilla/ScopeExit.h"
|
2018-06-21 03:21:17 +03:00
|
|
|
#include "mozilla/TextUtils.h"
|
|
|
|
|
2018-09-09 03:23:52 +03:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
|
|
|
|
2019-03-22 15:51:22 +03:00
|
|
|
#include "mprio.h"
|
|
|
|
|
2018-06-21 03:21:17 +03:00
|
|
|
#include "PrioEncoder.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
StaticRefPtr<PrioEncoder> PrioEncoder::sSingleton;
|
2018-06-21 03:21:17 +03:00
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
PublicKey PrioEncoder::sPublicKeyA = nullptr;
|
|
|
|
/* static */
|
|
|
|
PublicKey PrioEncoder::sPublicKeyB = nullptr;
|
2018-06-21 03:21:17 +03:00
|
|
|
|
2019-05-23 21:13:53 +03:00
|
|
|
// Production keys from bug 1552315 comment#3
|
|
|
|
/* static */
|
|
|
|
const char* kDefaultKeyA =
|
|
|
|
"E780C1A9C50E3FC5A9B39469FCC92D62D2527BAE6AF76BBDEF128883FA400846";
|
|
|
|
/* static */
|
|
|
|
const char* kDefaultKeyB =
|
|
|
|
"F992B575840AEC202289FBF99D6C04FB2A37B1DA1CDEB1DF8036E1340D46C561";
|
|
|
|
|
2018-06-21 03:21:17 +03:00
|
|
|
PrioEncoder::PrioEncoder() = default;
|
|
|
|
PrioEncoder::~PrioEncoder() {
|
|
|
|
if (sPublicKeyA) {
|
|
|
|
PublicKey_clear(sPublicKeyA);
|
|
|
|
sPublicKeyA = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sPublicKeyB) {
|
|
|
|
PublicKey_clear(sPublicKeyB);
|
|
|
|
sPublicKeyB = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Prio_clear();
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void PrioEncoder::Encode(GlobalObject& aGlobal, const nsCString& aBatchID,
|
|
|
|
const PrioParams& aPrioParams,
|
|
|
|
RootedDictionary<PrioEncodedData>& aData,
|
|
|
|
ErrorResult& aRv) {
|
2018-06-21 03:21:17 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2018-09-09 03:23:52 +03:00
|
|
|
return;
|
2018-06-21 03:21:17 +03:00
|
|
|
}
|
|
|
|
|
2019-03-22 15:51:22 +03:00
|
|
|
nsCString aResult;
|
|
|
|
nsCString bResult;
|
|
|
|
nsresult rv = PrioEncoder::EncodeNative(aBatchID, aPrioParams.mBooleans,
|
|
|
|
aResult, bResult);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2019-03-21 22:02:52 +03:00
|
|
|
return;
|
2019-03-20 23:51:40 +03:00
|
|
|
}
|
|
|
|
|
2019-03-21 22:02:52 +03:00
|
|
|
nsTArray<uint8_t> arrayForServerA;
|
|
|
|
nsTArray<uint8_t> arrayForServerB;
|
|
|
|
|
2019-03-22 15:51:22 +03:00
|
|
|
if (!arrayForServerA.AppendElements(
|
|
|
|
reinterpret_cast<const uint8_t*>(aResult.BeginReading()),
|
|
|
|
aResult.Length(), fallible)) {
|
2019-03-21 22:02:52 +03:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-22 15:51:22 +03:00
|
|
|
if (!arrayForServerB.AppendElements(
|
|
|
|
reinterpret_cast<const uint8_t*>(bResult.BeginReading()),
|
|
|
|
bResult.Length(), fallible)) {
|
2019-03-21 22:02:52 +03:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> valueA(aGlobal.Context());
|
|
|
|
if (!ToJSValue(aGlobal.Context(),
|
|
|
|
TypedArrayCreator<Uint8Array>(arrayForServerA), &valueA)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
2019-03-20 23:51:40 +03:00
|
|
|
}
|
2019-03-21 22:02:52 +03:00
|
|
|
|
|
|
|
aData.mA.Construct().Init(&valueA.toObject());
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> valueB(aGlobal.Context());
|
|
|
|
if (!ToJSValue(aGlobal.Context(),
|
|
|
|
TypedArrayCreator<Uint8Array>(arrayForServerB), &valueB)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aData.mB.Construct().Init(&valueB.toObject());
|
2019-03-20 23:51:40 +03:00
|
|
|
}
|
|
|
|
|
2019-03-22 15:51:22 +03:00
|
|
|
/* static */
|
|
|
|
nsresult PrioEncoder::EncodeNative(const nsCString& aBatchID,
|
|
|
|
const nsTArray<bool>& aData,
|
|
|
|
nsCString& aResult, nsCString& bResult) {
|
|
|
|
SECStatus prio_rv = SECSuccess;
|
|
|
|
|
|
|
|
nsresult rv = PrioEncoder::LazyInitSingleton();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aData.Length() > gNumBooleans) {
|
|
|
|
return NS_ERROR_RANGE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrioConfig prioConfig = PrioConfig_new(
|
|
|
|
aData.Length(), sPublicKeyA, sPublicKeyB,
|
|
|
|
reinterpret_cast<const unsigned char*>(aBatchID.BeginReading()),
|
|
|
|
aBatchID.Length());
|
|
|
|
|
|
|
|
if (!prioConfig) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto configGuard = MakeScopeExit([&] { PrioConfig_clear(prioConfig); });
|
|
|
|
|
|
|
|
unsigned char* forServerA = nullptr;
|
|
|
|
unsigned int lenA = 0;
|
|
|
|
unsigned char* forServerB = nullptr;
|
|
|
|
unsigned int lenB = 0;
|
|
|
|
|
|
|
|
prio_rv = PrioClient_encode(prioConfig, aData.Elements(), &forServerA, &lenA,
|
|
|
|
&forServerB, &lenB);
|
|
|
|
|
|
|
|
aResult.Adopt(reinterpret_cast<char*>(forServerA), lenA);
|
|
|
|
bResult.Adopt(reinterpret_cast<char*>(forServerB), lenB);
|
|
|
|
|
|
|
|
if (prio_rv != SECSuccess) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-06-21 03:21:17 +03:00
|
|
|
bool PrioEncoder::IsValidHexPublicKey(mozilla::Span<const char> aStr) {
|
|
|
|
if (aStr.Length() != CURVE25519_KEY_LEN_HEX) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto c : aStr) {
|
|
|
|
if (!IsAsciiHexDigit(c)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-22 15:51:22 +03:00
|
|
|
/* static */
|
2019-05-23 21:13:53 +03:00
|
|
|
nsresult PrioEncoder::SetKeys(const char* aKeyA, const char* aKeyB) {
|
|
|
|
nsAutoCStringN<CURVE25519_KEY_LEN_HEX + 1> prioKeyA;
|
|
|
|
if (aKeyA == nullptr) {
|
|
|
|
prioKeyA = kDefaultKeyA;
|
|
|
|
} else {
|
|
|
|
prioKeyA = aKeyA;
|
|
|
|
}
|
2019-03-22 15:51:22 +03:00
|
|
|
|
2019-05-23 21:13:53 +03:00
|
|
|
nsAutoCStringN<CURVE25519_KEY_LEN_HEX + 1> prioKeyB;
|
|
|
|
if (aKeyB == nullptr) {
|
|
|
|
prioKeyB = kDefaultKeyB;
|
|
|
|
} else {
|
|
|
|
prioKeyB = aKeyB;
|
|
|
|
}
|
2019-03-22 15:51:22 +03:00
|
|
|
|
2019-05-23 21:13:53 +03:00
|
|
|
// Check that both public keys are of the right length
|
|
|
|
// and contain only hex digits 0-9a-fA-f
|
|
|
|
if (!PrioEncoder::IsValidHexPublicKey(prioKeyA) ||
|
|
|
|
!PrioEncoder::IsValidHexPublicKey(prioKeyB)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2019-03-22 15:51:22 +03:00
|
|
|
|
2019-05-23 21:13:53 +03:00
|
|
|
SECStatus prio_rv = SECSuccess;
|
|
|
|
prio_rv = Prio_init();
|
2019-03-22 15:51:22 +03:00
|
|
|
|
2019-05-23 21:13:53 +03:00
|
|
|
if (prio_rv != SECSuccess) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2019-03-22 15:51:22 +03:00
|
|
|
|
2019-05-23 21:13:53 +03:00
|
|
|
prio_rv = PublicKey_import_hex(
|
|
|
|
&sPublicKeyA,
|
|
|
|
reinterpret_cast<const unsigned char*>(prioKeyA.BeginReading()),
|
|
|
|
CURVE25519_KEY_LEN_HEX);
|
|
|
|
if (prio_rv != SECSuccess) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2019-03-22 15:51:22 +03:00
|
|
|
|
2019-05-23 21:13:53 +03:00
|
|
|
prio_rv = PublicKey_import_hex(
|
|
|
|
&sPublicKeyB,
|
|
|
|
reinterpret_cast<const unsigned char*>(prioKeyB.BeginReading()),
|
|
|
|
CURVE25519_KEY_LEN_HEX);
|
|
|
|
if (prio_rv != SECSuccess) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2019-03-22 15:51:22 +03:00
|
|
|
|
2019-05-23 21:13:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult PrioEncoder::LazyInitSingleton() {
|
|
|
|
if (!sSingleton) {
|
|
|
|
// Init to the default keys.
|
|
|
|
nsresult rv = PrioEncoder::SetKeys();
|
|
|
|
if (!NS_SUCCEEDED(rv)) {
|
|
|
|
return rv;
|
2019-03-22 15:51:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sSingleton = new PrioEncoder();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-06-21 03:21:17 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|