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
|
/* -*- 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/. */
#ifndef mozilla_dom_quota_EncryptingOutputStream_impl_h
#define mozilla_dom_quota_EncryptingOutputStream_impl_h
#include <algorithm>
#include <utility>
#include "CipherStrategy.h"
#include "EncryptingOutputStream.h"
#include "mozilla/Assertions.h"
#include "mozilla/Span.h"
#include "mozilla/fallible.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsIAsyncOutputStream.h"
#include "nsIRandomGenerator.h"
#include "nsServiceManagerUtils.h"
namespace mozilla::dom::quota {
template <typename CipherStrategy>
EncryptingOutputStream<CipherStrategy>::EncryptingOutputStream(
nsCOMPtr<nsIOutputStream> aBaseStream, size_t aBlockSize,
typename CipherStrategy::KeyType aKey)
: EncryptingOutputStreamBase(std::move(aBaseStream), aBlockSize) {
// XXX Move this to a fallible init function.
MOZ_ALWAYS_SUCCEEDS(mCipherStrategy.Init(CipherMode::Encrypt,
CipherStrategy::SerializeKey(aKey),
CipherStrategy::MakeBlockPrefix()));
MOZ_ASSERT(mBlockSize > 0);
MOZ_ASSERT(mBlockSize % CipherStrategy::BasicBlockSize == 0);
static_assert(
CipherStrategy::BlockPrefixLength % CipherStrategy::BasicBlockSize == 0);
// This implementation only supports sync base streams. Verify this in debug
// builds. Note, this is a bit complicated because the streams we support
// advertise different capabilities:
// - nsFileOutputStream - blocking and sync
// - FixedBufferOutputStream - non-blocking and sync
// - nsPipeOutputStream - can be blocking, but provides async interface
#ifdef DEBUG
bool baseNonBlocking;
nsresult rv = (*mBaseStream)->IsNonBlocking(&baseNonBlocking);
MOZ_ASSERT(NS_SUCCEEDED(rv));
if (baseNonBlocking) {
nsCOMPtr<nsIAsyncOutputStream> async =
do_QueryInterface((*mBaseStream).get());
MOZ_ASSERT(!async);
}
#endif
}
template <typename CipherStrategy>
EncryptingOutputStream<CipherStrategy>::~EncryptingOutputStream() {
Close();
}
template <typename CipherStrategy>
NS_IMETHODIMP EncryptingOutputStream<CipherStrategy>::Close() {
if (!mBaseStream) {
return NS_OK;
}
// When closing, flush to the base stream unconditionally, i.e. even if the
// buffer is not completely full.
nsresult rv = FlushToBaseStream();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// XXX Maybe this Flush call can be removed, since the base stream is closed
// afterwards anyway.
rv = (*mBaseStream)->Flush();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// XXX What if closing the base stream failed? Fail this method, or at least
// log a warning?
(*mBaseStream)->Close();
mBaseStream.destroy();
mBuffer.Clear();
mEncryptedBlock.reset();
return NS_OK;
}
template <typename CipherStrategy>
NS_IMETHODIMP EncryptingOutputStream<CipherStrategy>::Flush() {
if (!mBaseStream) {
return NS_BASE_STREAM_CLOSED;
}
if (!EnsureBuffers()) {
return NS_ERROR_OUT_OF_MEMORY;
}
// We cannot call FlushBaseStream() here if the buffer is not completely
// full, we would write an incomplete page, which might be read sequentially,
// but we want to support random accesses in DecryptingInputStream, which
// would no longer be feasible.
if (mNextByte && mNextByte == mEncryptedBlock->MaxPayloadLength()) {
nsresult rv = FlushToBaseStream();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
return (*mBaseStream)->Flush();
}
template <typename CipherStrategy>
NS_IMETHODIMP EncryptingOutputStream<CipherStrategy>::StreamStatus() {
if (!mBaseStream) {
return NS_BASE_STREAM_CLOSED;
}
return (*mBaseStream)->StreamStatus();
}
template <typename CipherStrategy>
NS_IMETHODIMP EncryptingOutputStream<CipherStrategy>::WriteSegments(
nsReadSegmentFun aReader, void* aClosure, uint32_t aCount,
uint32_t* aBytesWrittenOut) {
*aBytesWrittenOut = 0;
if (!mBaseStream) {
return NS_BASE_STREAM_CLOSED;
}
if (!EnsureBuffers()) {
return NS_ERROR_OUT_OF_MEMORY;
}
const size_t plainBufferSize = mEncryptedBlock->MaxPayloadLength();
while (aCount > 0) {
// Determine how much space is left in our flat, plain buffer.
MOZ_ASSERT(mNextByte <= plainBufferSize);
uint32_t remaining = plainBufferSize - mNextByte;
// If it is full, then encrypt and flush the data to the base stream.
if (remaining == 0) {
nsresult rv = FlushToBaseStream();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// Now the entire buffer should be available for copying.
MOZ_ASSERT(!mNextByte);
remaining = plainBufferSize;
}
uint32_t numToRead = std::min(remaining, aCount);
uint32_t numRead = 0;
nsresult rv =
aReader(this, aClosure, reinterpret_cast<char*>(&mBuffer[mNextByte]),
*aBytesWrittenOut, numToRead, &numRead);
// As defined in nsIOutputStream.idl, do not pass reader func errors.
if (NS_FAILED(rv)) {
return NS_OK;
}
// End-of-file
if (numRead == 0) {
return NS_OK;
}
mNextByte += numRead;
*aBytesWrittenOut += numRead;
aCount -= numRead;
}
return NS_OK;
}
template <typename CipherStrategy>
bool EncryptingOutputStream<CipherStrategy>::EnsureBuffers() {
// Lazily create the encrypted buffer on our first flush. This
// allows us to report OOM during stream operation. This buffer
// will then get re-used until the stream is closed.
if (!mEncryptedBlock) {
// XXX Do we need to do this fallible (as the comment above suggests)?
mEncryptedBlock.emplace(mBlockSize);
MOZ_ASSERT(mBuffer.IsEmpty());
if (NS_WARN_IF(!mBuffer.SetLength(mEncryptedBlock->MaxPayloadLength(),
fallible))) {
return false;
}
}
return true;
}
template <typename CipherStrategy>
nsresult EncryptingOutputStream<CipherStrategy>::FlushToBaseStream() {
MOZ_ASSERT(mBaseStream);
if (!mNextByte) {
// Nothing to do.
return NS_OK;
}
if (mNextByte < mEncryptedBlock->MaxPayloadLength()) {
if (!mRandomGenerator) {
mRandomGenerator =
do_GetService("@mozilla.org/security/random-generator;1");
if (NS_WARN_IF(!mRandomGenerator)) {
return NS_ERROR_FAILURE;
}
}
const auto payload = mEncryptedBlock->MutablePayload();
const auto unusedPayload = payload.From(mNextByte);
nsresult rv = mRandomGenerator->GenerateRandomBytesInto(
unusedPayload.Elements(), unusedPayload.Length());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
// XXX The compressing stream implementation this was based on wrote a stream
// identifier, containing e.g. the block size. Should we do something like
// that as well? At the moment, we don't need it, but maybe this were
// convenient if we use this for persistent files in the future across version
// updates, which might change such parameters.
const auto iv = mCipherStrategy.MakeBlockPrefix();
static_assert(iv.size() * sizeof(decltype(*iv.begin())) ==
CipherStrategy::BlockPrefixLength);
std::copy(iv.cbegin(), iv.cend(),
mEncryptedBlock->MutableCipherPrefix().begin());
// Encrypt the data to our internal encrypted buffer.
// XXX Do we need to know the actual encrypted size?
nsresult rv = mCipherStrategy.Cipher(
mEncryptedBlock->MutableCipherPrefix(),
mozilla::Span(reinterpret_cast<uint8_t*>(mBuffer.Elements()),
((mNextByte + (CipherStrategy::BasicBlockSize - 1)) /
CipherStrategy::BasicBlockSize) *
CipherStrategy::BasicBlockSize),
mEncryptedBlock->MutablePayload());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
mEncryptedBlock->SetActualPayloadLength(mNextByte);
mNextByte = 0;
// Write the encrypted buffer out to the base stream.
uint32_t numWritten = 0;
const auto& wholeBlock = mEncryptedBlock->WholeBlock();
rv = WriteAll(AsChars(wholeBlock).Elements(), wholeBlock.Length(),
&numWritten);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
MOZ_ASSERT(wholeBlock.Length() == numWritten);
return NS_OK;
}
} // namespace mozilla::dom::quota
#endif
|