2015-05-03 22:32:37 +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: */
|
2014-05-15 14:20:00 +04:00
|
|
|
/* 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 "CryptoBuffer.h"
|
2014-08-06 14:15:35 +04:00
|
|
|
#include "secitem.h"
|
2014-07-19 17:25:00 +04:00
|
|
|
#include "mozilla/Base64.h"
|
2014-05-15 14:20:00 +04:00
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-07-19 17:24:00 +04:00
|
|
|
uint8_t* CryptoBuffer::Assign(const CryptoBuffer& aData) {
|
|
|
|
// Same as in nsTArray_Impl::operator=, but return the value
|
|
|
|
// returned from ReplaceElementsAt to enable OOM detection
|
2015-05-18 23:50:35 +03:00
|
|
|
return ReplaceElementsAt(0, Length(), aData.Elements(), aData.Length(),
|
|
|
|
fallible);
|
2014-07-19 17:24:00 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 14:20:00 +04:00
|
|
|
uint8_t* CryptoBuffer::Assign(const uint8_t* aData, uint32_t aLength) {
|
2015-05-18 23:50:35 +03:00
|
|
|
return ReplaceElementsAt(0, Length(), aData, aLength, fallible);
|
2014-05-15 14:20:00 +04:00
|
|
|
}
|
|
|
|
|
2016-02-09 18:43:00 +03:00
|
|
|
uint8_t* CryptoBuffer::Assign(const nsACString& aString) {
|
|
|
|
return Assign(reinterpret_cast<uint8_t const*>(aString.BeginReading()),
|
|
|
|
aString.Length());
|
|
|
|
}
|
|
|
|
|
2014-05-15 14:20:00 +04:00
|
|
|
uint8_t* CryptoBuffer::Assign(const SECItem* aItem) {
|
2014-05-26 14:05:00 +04:00
|
|
|
MOZ_ASSERT(aItem);
|
2014-05-15 14:20:00 +04:00
|
|
|
return Assign(aItem->data, aItem->len);
|
|
|
|
}
|
|
|
|
|
2019-07-10 06:25:56 +03:00
|
|
|
uint8_t* CryptoBuffer::Assign(const nsTArray<uint8_t>& aData) {
|
2016-04-15 19:29:12 +03:00
|
|
|
return ReplaceElementsAt(0, Length(), aData.Elements(), aData.Length(),
|
|
|
|
fallible);
|
|
|
|
}
|
|
|
|
|
2014-05-15 14:20:00 +04:00
|
|
|
uint8_t* CryptoBuffer::Assign(const ArrayBuffer& aData) {
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
aData.ComputeLengthAndData();
|
2014-05-15 14:20:00 +04:00
|
|
|
return Assign(aData.Data(), aData.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* CryptoBuffer::Assign(const ArrayBufferView& aData) {
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
aData.ComputeLengthAndData();
|
2014-05-15 14:20:00 +04:00
|
|
|
return Assign(aData.Data(), aData.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* CryptoBuffer::Assign(const ArrayBufferViewOrArrayBuffer& aData) {
|
|
|
|
if (aData.IsArrayBufferView()) {
|
|
|
|
return Assign(aData.GetAsArrayBufferView());
|
|
|
|
} else if (aData.IsArrayBuffer()) {
|
|
|
|
return Assign(aData.GetAsArrayBuffer());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If your union is uninitialized, something's wrong
|
|
|
|
MOZ_ASSERT(false);
|
2015-05-28 21:07:43 +03:00
|
|
|
Clear();
|
2014-05-15 14:20:00 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* CryptoBuffer::Assign(const OwningArrayBufferViewOrArrayBuffer& aData) {
|
|
|
|
if (aData.IsArrayBufferView()) {
|
|
|
|
return Assign(aData.GetAsArrayBufferView());
|
|
|
|
} else if (aData.IsArrayBuffer()) {
|
|
|
|
return Assign(aData.GetAsArrayBuffer());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If your union is uninitialized, something's wrong
|
|
|
|
MOZ_ASSERT(false);
|
2015-05-28 21:07:43 +03:00
|
|
|
Clear();
|
2014-05-15 14:20:00 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-15 19:29:12 +03:00
|
|
|
uint8_t* CryptoBuffer::AppendSECItem(const SECItem* aItem) {
|
|
|
|
MOZ_ASSERT(aItem);
|
|
|
|
return AppendElements(aItem->data, aItem->len, fallible);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* CryptoBuffer::AppendSECItem(const SECItem& aItem) {
|
|
|
|
return AppendElements(aItem.data, aItem.len, fallible);
|
|
|
|
}
|
|
|
|
|
2014-07-19 17:25:00 +04:00
|
|
|
// Helpers to encode/decode JWK's special flavor of Base64
|
|
|
|
// * No whitespace
|
|
|
|
// * No padding
|
|
|
|
// * URL-safe character set
|
|
|
|
nsresult CryptoBuffer::FromJwkBase64(const nsString& aBase64) {
|
|
|
|
NS_ConvertUTF16toUTF8 temp(aBase64);
|
|
|
|
temp.StripWhitespace();
|
|
|
|
|
2016-03-18 00:13:09 +03:00
|
|
|
// JWK prohibits padding per RFC 7515, section 2.
|
2016-04-22 17:41:58 +03:00
|
|
|
nsresult rv =
|
|
|
|
Base64URLDecode(temp, Base64URLDecodePaddingPolicy::Reject, *this);
|
2014-07-19 17:25:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-09 23:22:49 +03:00
|
|
|
nsresult CryptoBuffer::ToJwkBase64(nsString& aBase64) const {
|
2014-07-19 17:25:00 +04:00
|
|
|
// Shortcut for the empty octet string
|
|
|
|
if (Length() == 0) {
|
|
|
|
aBase64.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-18 00:13:09 +03:00
|
|
|
nsAutoCString base64;
|
2016-04-22 17:41:58 +03:00
|
|
|
nsresult rv = Base64URLEncode(Length(), Elements(),
|
|
|
|
Base64URLEncodePaddingPolicy::Omit, base64);
|
2014-07-19 17:25:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
CopyASCIItoUTF16(base64, aBase64);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-20 08:38:44 +04:00
|
|
|
bool CryptoBuffer::ToSECItem(PLArenaPool* aArena, SECItem* aItem) const {
|
|
|
|
aItem->type = siBuffer;
|
|
|
|
aItem->data = nullptr;
|
|
|
|
|
|
|
|
if (!::SECITEM_AllocItem(aArena, aItem, Length())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(aItem->data, Elements(), Length());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-27 22:22:57 +04:00
|
|
|
JSObject* CryptoBuffer::ToUint8Array(JSContext* aCx) const {
|
|
|
|
return Uint8Array::Create(aCx, Length(), Elements());
|
|
|
|
}
|
|
|
|
|
2017-09-20 17:32:07 +03:00
|
|
|
JSObject* CryptoBuffer::ToArrayBuffer(JSContext* aCx) const {
|
|
|
|
return ArrayBuffer::Create(aCx, Length(), Elements());
|
|
|
|
}
|
|
|
|
|
2016-04-13 20:20:37 +03:00
|
|
|
bool CryptoBuffer::ToNewUnsignedBuffer(uint8_t** aBuf,
|
|
|
|
uint32_t* aBufLen) const {
|
|
|
|
MOZ_ASSERT(aBuf);
|
|
|
|
MOZ_ASSERT(aBufLen);
|
|
|
|
|
|
|
|
uint32_t dataLen = Length();
|
|
|
|
uint8_t* tmp = reinterpret_cast<uint8_t*>(moz_xmalloc(dataLen));
|
2016-04-15 19:29:12 +03:00
|
|
|
|
2016-04-13 20:20:37 +03:00
|
|
|
memcpy(tmp, Elements(), dataLen);
|
|
|
|
*aBuf = tmp;
|
|
|
|
*aBufLen = dataLen;
|
2016-04-15 19:29:12 +03:00
|
|
|
return true;
|
|
|
|
}
|
2014-09-27 22:22:57 +04:00
|
|
|
|
2014-05-15 14:20:00 +04:00
|
|
|
// "BigInt" comes from the WebCrypto spec
|
|
|
|
// ("unsigned long" isn't very "big", of course)
|
|
|
|
// Likewise, the spec calls for big-endian ints
|
|
|
|
bool CryptoBuffer::GetBigIntValue(unsigned long& aRetVal) {
|
|
|
|
if (Length() > sizeof(aRetVal)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRetVal = 0;
|
|
|
|
for (size_t i = 0; i < Length(); ++i) {
|
|
|
|
aRetVal = (aRetVal << 8) + ElementAt(i);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|