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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_WebCryptoTask_h
|
|
|
|
#define mozilla_dom_WebCryptoTask_h
|
|
|
|
|
2016-11-17 19:49:25 +03:00
|
|
|
#include "ScopedNSSTypes.h"
|
|
|
|
#include "mozilla/dom/CryptoKey.h"
|
2014-05-15 14:20:00 +04:00
|
|
|
#include "mozilla/dom/DOMException.h"
|
2016-11-17 19:49:25 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-05-15 14:20:00 +04:00
|
|
|
#include "mozilla/dom/SubtleCryptoBinding.h"
|
2016-11-17 19:49:25 +03:00
|
|
|
#include "nsIGlobalObject.h"
|
2014-05-15 14:20:00 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-06-18 23:37:21 +03:00
|
|
|
class ThreadSafeWorkerRef;
|
|
|
|
|
2014-05-15 14:20:00 +04:00
|
|
|
typedef ArrayBufferViewOrArrayBuffer CryptoOperationData;
|
|
|
|
typedef ArrayBufferViewOrArrayBuffer KeyData;
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
The execution of a WebCryptoTask happens in several phases
|
|
|
|
|
|
|
|
1. Constructor
|
|
|
|
2. BeforeCrypto
|
|
|
|
3. CalculateResult -> DoCrypto
|
|
|
|
4. AfterCrypto
|
|
|
|
5. Resolve or FailWithError
|
|
|
|
6. Cleanup
|
|
|
|
|
|
|
|
If any of these steps produces an error (setting mEarlyRv), then
|
|
|
|
subsequent steps will not proceed. If the constructor or BeforeCrypto
|
|
|
|
sets mEarlyComplete to true, then we will skip step 3, saving the
|
|
|
|
thread overhead.
|
|
|
|
|
|
|
|
In general, the constructor should handle any parsing steps that
|
|
|
|
require JS context, and otherwise just cache information for later
|
|
|
|
steps to use.
|
|
|
|
|
|
|
|
All steps besides step 3 occur on the main thread, so they should
|
|
|
|
avoid blocking operations.
|
|
|
|
|
|
|
|
Only step 3 is guarded to ensure that NSS has not been shutdown,
|
|
|
|
so all NSS interactions should occur in DoCrypto
|
|
|
|
|
|
|
|
Cleanup should execute regardless of what else happens.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAYBE_EARLY_FAIL(rv) \
|
|
|
|
if (NS_FAILED(rv)) { \
|
|
|
|
FailWithError(rv); \
|
|
|
|
return; \
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:44:01 +03:00
|
|
|
class WebCryptoTask : public CancelableRunnable
|
2014-05-15 14:20:00 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-09-12 00:41:58 +03:00
|
|
|
virtual void DispatchWithPromise(Promise* aResultPromise);
|
2014-05-15 14:20:00 +04:00
|
|
|
|
|
|
|
protected:
|
2014-07-19 17:24:00 +04:00
|
|
|
static WebCryptoTask* CreateEncryptDecryptTask(JSContext* aCx,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aAlgorithm,
|
2014-06-24 01:25:13 +04:00
|
|
|
CryptoKey& aKey,
|
2014-05-15 14:20:00 +04:00
|
|
|
const CryptoOperationData& aData,
|
|
|
|
bool aEncrypt);
|
|
|
|
|
2014-07-19 17:24:00 +04:00
|
|
|
static WebCryptoTask* CreateSignVerifyTask(JSContext* aCx,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aAlgorithm,
|
2014-06-24 01:25:13 +04:00
|
|
|
CryptoKey& aKey,
|
2014-05-15 14:20:00 +04:00
|
|
|
const CryptoOperationData& aSignature,
|
|
|
|
const CryptoOperationData& aData,
|
|
|
|
bool aSign);
|
|
|
|
|
|
|
|
public:
|
2014-07-19 17:24:00 +04:00
|
|
|
static WebCryptoTask* CreateEncryptTask(JSContext* aCx,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aAlgorithm,
|
2014-06-24 01:25:13 +04:00
|
|
|
CryptoKey& aKey,
|
2014-05-15 14:20:00 +04:00
|
|
|
const CryptoOperationData& aData)
|
|
|
|
{
|
2014-07-19 17:24:00 +04:00
|
|
|
return CreateEncryptDecryptTask(aCx, aAlgorithm, aKey, aData, true);
|
2014-05-15 14:20:00 +04:00
|
|
|
}
|
|
|
|
|
2014-07-19 17:24:00 +04:00
|
|
|
static WebCryptoTask* CreateDecryptTask(JSContext* aCx,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aAlgorithm,
|
2014-06-24 01:25:13 +04:00
|
|
|
CryptoKey& aKey,
|
2014-05-15 14:20:00 +04:00
|
|
|
const CryptoOperationData& aData)
|
|
|
|
{
|
2014-07-19 17:24:00 +04:00
|
|
|
return CreateEncryptDecryptTask(aCx, aAlgorithm, aKey, aData, false);
|
2014-05-15 14:20:00 +04:00
|
|
|
}
|
|
|
|
|
2014-07-19 17:24:00 +04:00
|
|
|
static WebCryptoTask* CreateSignTask(JSContext* aCx,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aAlgorithm,
|
2014-06-24 01:25:13 +04:00
|
|
|
CryptoKey& aKey,
|
2014-05-15 14:20:00 +04:00
|
|
|
const CryptoOperationData& aData)
|
|
|
|
{
|
|
|
|
CryptoOperationData dummy;
|
2014-05-23 17:29:00 +04:00
|
|
|
dummy.SetAsArrayBuffer(aCx);
|
2014-07-19 17:24:00 +04:00
|
|
|
return CreateSignVerifyTask(aCx, aAlgorithm, aKey, dummy, aData, true);
|
2014-05-15 14:20:00 +04:00
|
|
|
}
|
|
|
|
|
2014-07-19 17:24:00 +04:00
|
|
|
static WebCryptoTask* CreateVerifyTask(JSContext* aCx,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aAlgorithm,
|
2014-06-24 01:25:13 +04:00
|
|
|
CryptoKey& aKey,
|
2014-05-15 14:20:00 +04:00
|
|
|
const CryptoOperationData& aSignature,
|
|
|
|
const CryptoOperationData& aData)
|
|
|
|
{
|
2014-07-19 17:24:00 +04:00
|
|
|
return CreateSignVerifyTask(aCx, aAlgorithm, aKey, aSignature, aData, false);
|
2014-05-15 14:20:00 +04:00
|
|
|
}
|
|
|
|
|
2014-07-19 17:24:00 +04:00
|
|
|
static WebCryptoTask* CreateDigestTask(JSContext* aCx,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aAlgorithm,
|
|
|
|
const CryptoOperationData& aData);
|
|
|
|
|
2016-03-08 13:14:37 +03:00
|
|
|
static WebCryptoTask* CreateImportKeyTask(nsIGlobalObject* aGlobal,
|
|
|
|
JSContext* aCx,
|
2014-05-15 14:20:00 +04:00
|
|
|
const nsAString& aFormat,
|
2014-07-19 17:25:00 +04:00
|
|
|
JS::Handle<JSObject*> aKeyData,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aAlgorithm,
|
|
|
|
bool aExtractable,
|
|
|
|
const Sequence<nsString>& aKeyUsages);
|
2014-07-19 17:24:00 +04:00
|
|
|
static WebCryptoTask* CreateExportKeyTask(const nsAString& aFormat,
|
2014-06-24 01:25:13 +04:00
|
|
|
CryptoKey& aKey);
|
2016-03-08 13:15:29 +03:00
|
|
|
static WebCryptoTask* CreateGenerateKeyTask(nsIGlobalObject* aGlobal,
|
|
|
|
JSContext* aCx,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aAlgorithm,
|
|
|
|
bool aExtractable,
|
|
|
|
const Sequence<nsString>& aKeyUsages);
|
|
|
|
|
2016-03-08 13:14:37 +03:00
|
|
|
static WebCryptoTask* CreateDeriveKeyTask(nsIGlobalObject* aGlobal,
|
|
|
|
JSContext* aCx,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aAlgorithm,
|
2014-06-24 01:25:13 +04:00
|
|
|
CryptoKey& aBaseKey,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aDerivedKeyType,
|
|
|
|
bool extractable,
|
|
|
|
const Sequence<nsString>& aKeyUsages);
|
2014-07-19 17:24:00 +04:00
|
|
|
static WebCryptoTask* CreateDeriveBitsTask(JSContext* aCx,
|
2014-05-15 14:20:00 +04:00
|
|
|
const ObjectOrString& aAlgorithm,
|
2014-06-24 01:25:13 +04:00
|
|
|
CryptoKey& aKey,
|
2014-05-15 14:20:00 +04:00
|
|
|
uint32_t aLength);
|
|
|
|
|
2014-07-19 17:24:00 +04:00
|
|
|
static WebCryptoTask* CreateWrapKeyTask(JSContext* aCx,
|
|
|
|
const nsAString& aFormat,
|
|
|
|
CryptoKey& aKey,
|
|
|
|
CryptoKey& aWrappingKey,
|
|
|
|
const ObjectOrString& aWrapAlgorithm);
|
2016-03-08 13:14:37 +03:00
|
|
|
static WebCryptoTask* CreateUnwrapKeyTask(nsIGlobalObject* aGlobal,
|
|
|
|
JSContext* aCx,
|
2014-07-19 17:24:00 +04:00
|
|
|
const nsAString& aFormat,
|
|
|
|
const ArrayBufferViewOrArrayBuffer& aWrappedKey,
|
|
|
|
CryptoKey& aUnwrappingKey,
|
|
|
|
const ObjectOrString& aUnwrapAlgorithm,
|
|
|
|
const ObjectOrString& aUnwrappedKeyAlgorithm,
|
|
|
|
bool aExtractable,
|
|
|
|
const Sequence<nsString>& aKeyUsages);
|
|
|
|
|
2014-05-15 14:20:00 +04:00
|
|
|
protected:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> mResultPromise;
|
2014-05-15 14:20:00 +04:00
|
|
|
nsresult mEarlyRv;
|
|
|
|
bool mEarlyComplete;
|
|
|
|
|
2016-07-01 16:49:45 +03:00
|
|
|
WebCryptoTask();
|
|
|
|
virtual ~WebCryptoTask();
|
2015-09-12 00:41:58 +03:00
|
|
|
|
|
|
|
bool IsOnOriginalThread() {
|
2017-06-01 23:42:05 +03:00
|
|
|
return !mOriginalEventTarget || mOriginalEventTarget->IsOnCurrentThread();
|
2015-09-12 00:41:58 +03:00
|
|
|
}
|
|
|
|
|
2014-05-15 14:20:00 +04:00
|
|
|
// For things that need to happen on the main thread
|
|
|
|
// either before or after CalculateResult
|
|
|
|
virtual nsresult BeforeCrypto() { return NS_OK; }
|
|
|
|
virtual nsresult DoCrypto() { return NS_OK; }
|
|
|
|
virtual nsresult AfterCrypto() { return NS_OK; }
|
|
|
|
virtual void Resolve() {}
|
|
|
|
virtual void Cleanup() {}
|
|
|
|
|
2014-06-10 06:01:00 +04:00
|
|
|
void FailWithError(nsresult aRv);
|
2014-05-15 14:20:00 +04:00
|
|
|
|
2018-02-09 12:31:38 +03:00
|
|
|
nsresult CalculateResult();
|
2015-09-12 00:41:58 +03:00
|
|
|
|
2018-02-09 12:31:38 +03:00
|
|
|
void CallCallback(nsresult rv);
|
2015-09-12 00:41:58 +03:00
|
|
|
|
|
|
|
private:
|
2018-02-06 09:50:00 +03:00
|
|
|
NS_IMETHOD Run() final;
|
|
|
|
nsresult Cancel() final;
|
2015-09-12 00:41:58 +03:00
|
|
|
|
2017-06-01 23:42:05 +03:00
|
|
|
nsCOMPtr<nsISerialEventTarget> mOriginalEventTarget;
|
2018-06-18 23:37:21 +03:00
|
|
|
RefPtr<ThreadSafeWorkerRef> mWorkerRef;
|
2015-09-12 00:41:58 +03:00
|
|
|
nsresult mRv;
|
2014-05-15 14:20:00 +04:00
|
|
|
};
|
|
|
|
|
2015-07-06 20:40:04 +03:00
|
|
|
// XXX This class is declared here (unlike others) to enable reuse by WebRTC.
|
|
|
|
class GenerateAsymmetricKeyTask : public WebCryptoTask
|
|
|
|
{
|
|
|
|
public:
|
2016-03-08 13:15:58 +03:00
|
|
|
GenerateAsymmetricKeyTask(nsIGlobalObject* aGlobal, JSContext* aCx,
|
2015-07-06 20:40:04 +03:00
|
|
|
const ObjectOrString& aAlgorithm, bool aExtractable,
|
|
|
|
const Sequence<nsString>& aKeyUsages);
|
|
|
|
protected:
|
2016-11-17 19:49:25 +03:00
|
|
|
UniquePLArenaPool mArena;
|
2015-09-21 15:52:40 +03:00
|
|
|
UniquePtr<CryptoKeyPair> mKeyPair;
|
2015-07-06 20:40:04 +03:00
|
|
|
nsString mAlgName;
|
|
|
|
CK_MECHANISM_TYPE mMechanism;
|
|
|
|
PK11RSAGenParams mRsaParams;
|
|
|
|
SECKEYDHParams mDhParams;
|
|
|
|
nsString mNamedCurve;
|
|
|
|
|
|
|
|
virtual nsresult DoCrypto() override;
|
|
|
|
virtual void Resolve() override;
|
2015-09-21 15:52:40 +03:00
|
|
|
virtual void Cleanup() override;
|
2015-07-06 20:40:04 +03:00
|
|
|
|
|
|
|
private:
|
2016-11-17 19:49:25 +03:00
|
|
|
UniqueSECKEYPublicKey mPublicKey;
|
|
|
|
UniqueSECKEYPrivateKey mPrivateKey;
|
2015-07-06 20:40:04 +03:00
|
|
|
};
|
|
|
|
|
2014-05-15 14:20:00 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_WebCryptoTask_h
|