1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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
11 #include "js/StructuredClone.h"
12 #include "mozilla/dom/KeyAlgorithmBinding.h"
13 #include "mozilla/dom/WebCryptoCommon.h"
15 #define KEY_ALGORITHM_SC_VERSION 0x00000001
20 // A heap-safe variant of RsaHashedKeyAlgorithm
21 // The only difference is that it uses CryptoBuffer instead of Uint8Array
22 struct RsaHashedKeyAlgorithmStorage
{
25 uint16_t mModulusLength
;
26 CryptoBuffer mPublicExponent
;
29 ToKeyAlgorithm(JSContext
* aCx
, RsaHashedKeyAlgorithm
& aRsa
) const
32 aRsa
.mModulusLength
= mModulusLength
;
33 aRsa
.mHash
.mName
= mHash
.mName
;
34 aRsa
.mPublicExponent
.Init(mPublicExponent
.ToUint8Array(aCx
));
35 aRsa
.mPublicExponent
.ComputeLengthAndData();
39 // This class encapuslates a KeyAlgorithm object, and adds several
40 // methods that make WebCrypto operations simpler.
41 struct KeyAlgorithmProxy
43 enum KeyAlgorithmType
{
49 KeyAlgorithmType mType
;
51 // Plain is always populated with the algorithm name
52 // Others are only populated for the corresponding key type
55 HmacKeyAlgorithm mHmac
;
56 RsaHashedKeyAlgorithmStorage mRsa
;
60 bool WriteStructuredClone(JSStructuredCloneWriter
* aWriter
) const;
61 bool ReadStructuredClone(JSStructuredCloneReader
* aReader
);
63 // Extract various forms of derived information
64 CK_MECHANISM_TYPE
Mechanism() const;
65 nsString
JwkAlg() const;
67 // And in static form for calling on raw KeyAlgorithm dictionaries
68 static CK_MECHANISM_TYPE
GetMechanism(const KeyAlgorithm
& aAlgorithm
);
69 static CK_MECHANISM_TYPE
GetMechanism(const HmacKeyAlgorithm
& aAlgorithm
);
70 static nsString
GetJwkAlg(const KeyAlgorithm
& aAlgorithm
);
72 // Construction of the various algorithm types
74 MakeAes(const nsString
& aName
, uint32_t aLength
)
79 mAes
.mLength
= aLength
;
83 MakeHmac(uint32_t aLength
, const nsString
& aHashName
)
86 mName
= NS_LITERAL_STRING(WEBCRYPTO_ALG_HMAC
);
87 mHmac
.mName
= NS_LITERAL_STRING(WEBCRYPTO_ALG_HMAC
);
88 mHmac
.mLength
= aLength
;
89 mHmac
.mHash
.mName
= aHashName
;
93 MakeRsa(const nsString
& aName
, uint32_t aModulusLength
,
94 const CryptoBuffer
& aPublicExponent
, const nsString
& aHashName
)
99 mRsa
.mModulusLength
= aModulusLength
;
100 mRsa
.mHash
.mName
= aHashName
;
101 mRsa
.mPublicExponent
.Assign(aPublicExponent
);
105 MakeEc(const nsString
& aName
, const nsString
& aNamedCurve
)
110 mEc
.mNamedCurve
= aNamedCurve
;
115 } // namespace mozilla
117 #endif // mozilla_dom_KeyAlgorithmProxy_h