2016-03-14 10:56:35 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "ContentVerifier.h"
|
|
|
|
|
|
|
|
#include "mozilla/fallible.h"
|
|
|
|
#include "mozilla/Logging.h"
|
2016-05-19 08:59:48 +00:00
|
|
|
#include "MainThreadUtils.h"
|
2016-03-14 10:56:35 +00:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStringStream.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
static LazyLogModule gContentVerifierPRLog("ContentVerifier");
|
|
|
|
#define CSV_LOG(args) MOZ_LOG(gContentVerifierPRLog, LogLevel::Debug, args)
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
NS_IMPL_ISUPPORTS(ContentVerifier,
|
|
|
|
nsIContentSignatureReceiverCallback,
|
|
|
|
nsIStreamListener);
|
2016-03-14 10:56:35 +00:00
|
|
|
|
|
|
|
nsresult
|
2016-05-19 08:59:48 +00:00
|
|
|
ContentVerifier::Init(const nsACString& aContentSignatureHeader,
|
|
|
|
nsIRequest* aRequest, nsISupports* aContext)
|
2016-03-14 10:56:35 +00:00
|
|
|
{
|
2016-05-19 08:59:48 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (aContentSignatureHeader.IsEmpty()) {
|
|
|
|
CSV_LOG(("Content-Signature header must not be empty!\n"));
|
|
|
|
return NS_ERROR_INVALID_SIGNATURE;
|
|
|
|
}
|
2016-03-14 10:56:35 +00:00
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
// initialise the content signature "service"
|
|
|
|
nsresult rv;
|
|
|
|
mVerifier =
|
|
|
|
do_CreateInstance("@mozilla.org/security/contentsignatureverifier;1", &rv);
|
|
|
|
if (NS_FAILED(rv) || !mVerifier) {
|
2016-03-14 10:56:35 +00:00
|
|
|
return NS_ERROR_INVALID_SIGNATURE;
|
|
|
|
}
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
// Keep references to the request and context. We need them in FinishSignature
|
|
|
|
// and the ContextCreated callback.
|
|
|
|
mContentRequest = aRequest;
|
|
|
|
mContentContext = aContext;
|
|
|
|
|
|
|
|
rv = mVerifier->CreateContextWithoutCertChain(
|
|
|
|
this, aContentSignatureHeader,
|
2016-07-25 07:44:19 +00:00
|
|
|
NS_LITERAL_CSTRING("remotenewtab.content-signature.mozilla.org"));
|
2016-05-19 08:59:48 +00:00
|
|
|
if (NS_FAILED(rv)){
|
|
|
|
mVerifier = nullptr;
|
|
|
|
}
|
|
|
|
return rv;
|
2016-03-14 10:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement nsIStreamListener
|
|
|
|
* We buffer the entire content here and kick off verification
|
|
|
|
*/
|
2016-08-12 07:36:22 +00:00
|
|
|
nsresult
|
2016-03-14 10:56:35 +00:00
|
|
|
AppendNextSegment(nsIInputStream* aInputStream, void* aClosure,
|
|
|
|
const char* aRawSegment, uint32_t aToOffset, uint32_t aCount,
|
|
|
|
uint32_t* outWrittenCount)
|
|
|
|
{
|
|
|
|
FallibleTArray<nsCString>* decodedData =
|
|
|
|
static_cast<FallibleTArray<nsCString>*>(aClosure);
|
2016-05-19 08:59:48 +00:00
|
|
|
nsDependentCSubstring segment(aRawSegment, aCount);
|
2016-03-14 10:56:35 +00:00
|
|
|
if (!decodedData->AppendElement(segment, fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
*outWrittenCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
void
|
|
|
|
ContentVerifier::FinishSignature()
|
2016-03-14 10:56:35 +00:00
|
|
|
{
|
2016-05-19 08:59:48 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsCOMPtr<nsIStreamListener> nextListener;
|
|
|
|
nextListener.swap(mNextListener);
|
2016-03-14 10:56:35 +00:00
|
|
|
|
|
|
|
// Verify the content:
|
|
|
|
// If this fails, we return an invalid signature error to load a fallback page.
|
|
|
|
// If everthing is good, we return a new stream to the next listener and kick
|
2016-05-19 08:59:48 +00:00
|
|
|
// that one off.
|
2016-03-14 10:56:35 +00:00
|
|
|
bool verified = false;
|
2016-05-19 08:59:48 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// If the content signature check fails, stop the load
|
|
|
|
// and return a signature error. NSS resources are freed by the
|
|
|
|
// ContentSignatureVerifier on destruction.
|
|
|
|
if (NS_FAILED(mVerifier->End(&verified)) || !verified) {
|
2016-03-14 10:56:35 +00:00
|
|
|
CSV_LOG(("failed to verify content\n"));
|
2016-05-19 08:59:48 +00:00
|
|
|
(void)nextListener->OnStopRequest(mContentRequest, mContentContext,
|
|
|
|
NS_ERROR_INVALID_SIGNATURE);
|
|
|
|
return;
|
2016-03-14 10:56:35 +00:00
|
|
|
}
|
|
|
|
CSV_LOG(("Successfully verified content signature.\n"));
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
// We emptied the input stream so we have to create a new one from mContent
|
|
|
|
// to hand it to the consuming listener.
|
|
|
|
uint64_t offset = 0;
|
|
|
|
for (uint32_t i = 0; i < mContent.Length(); ++i) {
|
|
|
|
nsCOMPtr<nsIInputStream> oInStr;
|
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(oInStr), mContent[i]);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// let the next listener know that there is data in oInStr
|
|
|
|
rv = nextListener->OnDataAvailable(mContentRequest, mContentContext, oInStr,
|
|
|
|
offset, mContent[i].Length());
|
|
|
|
offset += mContent[i].Length();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
2016-03-14 10:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// propagate OnStopRequest and return
|
2016-05-19 08:59:48 +00:00
|
|
|
nextListener->OnStopRequest(mContentRequest, mContentContext, rv);
|
2016-03-14 10:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-05-19 08:59:48 +00:00
|
|
|
ContentVerifier::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
|
2016-07-20 18:16:37 +00:00
|
|
|
{
|
2016-05-19 08:59:48 +00:00
|
|
|
MOZ_CRASH("This OnStartRequest should've never been called!");
|
2016-03-14 10:56:35 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentVerifier::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext,
|
|
|
|
nsresult aStatus)
|
2016-03-14 10:56:35 +00:00
|
|
|
{
|
2016-05-19 08:59:48 +00:00
|
|
|
// If we don't have a next listener, we handed off this request already.
|
|
|
|
// Return, there's nothing to do here.
|
|
|
|
if (!mNextListener) {
|
|
|
|
return NS_OK;
|
2016-07-20 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
CSV_LOG(("Stream failed\n"));
|
|
|
|
nsCOMPtr<nsIStreamListener> nextListener;
|
|
|
|
nextListener.swap(mNextListener);
|
|
|
|
return nextListener->OnStopRequest(aRequest, aContext, aStatus);
|
2016-07-20 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
mContentRead = true;
|
2016-03-14 10:56:35 +00:00
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
// If the ContentSignatureVerifier is initialised, finish the verification.
|
|
|
|
if (mContextCreated) {
|
|
|
|
FinishSignature();
|
|
|
|
return aStatus;
|
2016-07-20 18:16:37 +00:00
|
|
|
}
|
2016-03-14 10:56:35 +00:00
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
return NS_OK;
|
2016-03-14 10:56:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentVerifier::OnDataAvailable(nsIRequest* aRequest, nsISupports* aContext,
|
|
|
|
nsIInputStream* aInputStream, uint64_t aOffset,
|
|
|
|
uint32_t aCount)
|
2016-03-14 10:56:35 +00:00
|
|
|
{
|
2016-05-19 08:59:48 +00:00
|
|
|
// buffer the entire stream
|
|
|
|
uint32_t read;
|
|
|
|
nsresult rv = aInputStream->ReadSegments(AppendNextSegment, &mContent, aCount,
|
|
|
|
&read);
|
2016-07-20 18:16:37 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-05-19 08:59:48 +00:00
|
|
|
return rv;
|
2016-07-20 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
// Update the signature verifier if the context has been created.
|
|
|
|
if (mContextCreated) {
|
|
|
|
return mVerifier->Update(mContent.LastElement());
|
2016-07-20 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
return NS_OK;
|
2016-07-20 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ContentVerifier::ContextCreated(bool successful)
|
2016-07-20 18:16:37 +00:00
|
|
|
{
|
2016-05-19 08:59:48 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!successful) {
|
|
|
|
// If we don't have a next listener, the request has been handed off already.
|
|
|
|
if (!mNextListener) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// Get local reference to mNextListener and null it to ensure that we don't
|
|
|
|
// call it twice.
|
|
|
|
nsCOMPtr<nsIStreamListener> nextListener;
|
|
|
|
nextListener.swap(mNextListener);
|
|
|
|
|
|
|
|
// Make sure that OnStartRequest was called and we have a request.
|
|
|
|
MOZ_ASSERT(mContentRequest);
|
|
|
|
|
|
|
|
// In this case something went wrong with the cert. Let's stop this load.
|
|
|
|
CSV_LOG(("failed to get a valid cert chain\n"));
|
|
|
|
if (mContentRequest && nextListener) {
|
|
|
|
mContentRequest->Cancel(NS_ERROR_INVALID_SIGNATURE);
|
|
|
|
nsresult rv = nextListener->OnStopRequest(mContentRequest, mContentContext,
|
|
|
|
NS_ERROR_INVALID_SIGNATURE);
|
|
|
|
mContentRequest = nullptr;
|
|
|
|
mContentContext = nullptr;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should never get here!
|
|
|
|
MOZ_ASSERT_UNREACHABLE(
|
|
|
|
"ContentVerifier was used without getting OnStartRequest!");
|
|
|
|
return NS_OK;
|
2016-03-14 10:56:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
// In this case the content verifier is initialised and we have to feed it
|
|
|
|
// the buffered content.
|
|
|
|
mContextCreated = true;
|
|
|
|
for (size_t i = 0; i < mContent.Length(); ++i) {
|
|
|
|
if (NS_FAILED(mVerifier->Update(mContent[i]))) {
|
|
|
|
// Bail out if this fails. We can't return an error here, but if this
|
|
|
|
// failed, NS_ERROR_INVALID_SIGNATURE is returned in FinishSignature.
|
|
|
|
break;
|
2016-07-20 18:16:37 +00:00
|
|
|
}
|
2016-03-14 10:56:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 08:59:48 +00:00
|
|
|
// We read all content, let's verify the signature.
|
|
|
|
if (mContentRead) {
|
|
|
|
FinishSignature();
|
2016-07-20 18:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2016-05-19 08:59:48 +00:00
|
|
|
}
|