1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_KeyAlgorithmProxy_h
8 #define mozilla_dom_KeyAlgorithmProxy_h
12 #include "js/RootingAPI.h"
13 #include "mozilla/dom/CryptoBuffer.h"
14 #include "mozilla/dom/KeyAlgorithmBinding.h"
15 #include "mozilla/dom/TypedArray.h"
16 #include "mozilla/dom/WebCryptoCommon.h"
17 #include "nsLiteralString.h"
18 #include "nsStringFwd.h"
23 struct JSStructuredCloneReader
;
24 struct JSStructuredCloneWriter
;
26 #define KEY_ALGORITHM_SC_VERSION 0x00000001
28 namespace mozilla::dom
{
30 // A heap-safe variant of RsaHashedKeyAlgorithm
31 // The only difference is that it uses CryptoBuffer instead of Uint8Array
32 struct RsaHashedKeyAlgorithmStorage
{
35 uint16_t mModulusLength
;
36 CryptoBuffer mPublicExponent
;
38 bool ToKeyAlgorithm(JSContext
* aCx
, RsaHashedKeyAlgorithm
& aRsa
) const {
39 JS::Rooted
<JSObject
*> exponent(aCx
, mPublicExponent
.ToUint8Array(aCx
));
45 aRsa
.mModulusLength
= mModulusLength
;
46 aRsa
.mHash
.mName
= mHash
.mName
;
47 aRsa
.mPublicExponent
.Init(exponent
);
48 aRsa
.mPublicExponent
.ComputeState();
54 // This class encapuslates a KeyAlgorithm object, and adds several
55 // methods that make WebCrypto operations simpler.
56 struct KeyAlgorithmProxy
{
57 enum KeyAlgorithmType
{
63 KeyAlgorithmType mType
;
65 // Plain is always populated with the algorithm name
66 // Others are only populated for the corresponding key type
69 HmacKeyAlgorithm mHmac
;
70 RsaHashedKeyAlgorithmStorage mRsa
;
74 bool WriteStructuredClone(JSStructuredCloneWriter
* aWriter
) const;
75 bool ReadStructuredClone(JSStructuredCloneReader
* aReader
);
77 // Extract various forms of derived information
78 CK_MECHANISM_TYPE
Mechanism() const;
79 nsString
JwkAlg() const;
81 // And in static form for calling on raw KeyAlgorithm dictionaries
82 static CK_MECHANISM_TYPE
GetMechanism(const KeyAlgorithm
& aAlgorithm
);
83 static CK_MECHANISM_TYPE
GetMechanism(const HmacKeyAlgorithm
& aAlgorithm
);
84 static nsString
GetJwkAlg(const KeyAlgorithm
& aAlgorithm
);
86 // Construction of the various algorithm types
87 void MakeAes(const nsString
& aName
, uint32_t aLength
) {
91 mAes
.mLength
= aLength
;
94 void MakeHmac(uint32_t aLength
, const nsString
& aHashName
) {
96 mName
= NS_LITERAL_STRING_FROM_CSTRING(WEBCRYPTO_ALG_HMAC
);
97 mHmac
.mName
= NS_LITERAL_STRING_FROM_CSTRING(WEBCRYPTO_ALG_HMAC
);
98 mHmac
.mLength
= aLength
;
99 mHmac
.mHash
.mName
= aHashName
;
102 bool MakeRsa(const nsString
& aName
, uint32_t aModulusLength
,
103 const CryptoBuffer
& aPublicExponent
, const nsString
& aHashName
) {
107 mRsa
.mModulusLength
= aModulusLength
;
108 mRsa
.mHash
.mName
= aHashName
;
109 if (!mRsa
.mPublicExponent
.Assign(aPublicExponent
)) {
115 void MakeEc(const nsString
& aName
, const nsString
& aNamedCurve
) {
119 mEc
.mNamedCurve
= aNamedCurve
;
123 } // namespace mozilla::dom
125 #endif // mozilla_dom_KeyAlgorithmProxy_h