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 // A heap-safe variant of DhKeyAlgorithm
40 // The only difference is that it uses CryptoBuffers instead of Uint8Arrays
41 struct DhKeyAlgorithmStorage
{
44 CryptoBuffer mGenerator
;
47 ToKeyAlgorithm(JSContext
* aCx
, DhKeyAlgorithm
& aDh
) const
50 aDh
.mPrime
.Init(mPrime
.ToUint8Array(aCx
));
51 aDh
.mPrime
.ComputeLengthAndData();
52 aDh
.mGenerator
.Init(mGenerator
.ToUint8Array(aCx
));
53 aDh
.mGenerator
.ComputeLengthAndData();
57 // This class encapuslates a KeyAlgorithm object, and adds several
58 // methods that make WebCrypto operations simpler.
59 struct KeyAlgorithmProxy
61 enum KeyAlgorithmType
{
68 KeyAlgorithmType mType
;
70 // Plain is always populated with the algorithm name
71 // Others are only populated for the corresponding key type
74 HmacKeyAlgorithm mHmac
;
75 RsaHashedKeyAlgorithmStorage mRsa
;
77 DhKeyAlgorithmStorage mDh
;
80 bool WriteStructuredClone(JSStructuredCloneWriter
* aWriter
) const;
81 bool ReadStructuredClone(JSStructuredCloneReader
* aReader
);
83 // Extract various forms of derived information
84 CK_MECHANISM_TYPE
Mechanism() const;
85 nsString
JwkAlg() const;
87 // And in static form for calling on raw KeyAlgorithm dictionaries
88 static CK_MECHANISM_TYPE
GetMechanism(const KeyAlgorithm
& aAlgorithm
);
89 static CK_MECHANISM_TYPE
GetMechanism(const HmacKeyAlgorithm
& aAlgorithm
);
90 static nsString
GetJwkAlg(const KeyAlgorithm
& aAlgorithm
);
92 // Construction of the various algorithm types
94 MakeAes(const nsString
& aName
, uint32_t aLength
)
99 mAes
.mLength
= aLength
;
103 MakeHmac(uint32_t aLength
, const nsString
& aHashName
)
106 mName
= NS_LITERAL_STRING(WEBCRYPTO_ALG_HMAC
);
107 mHmac
.mName
= NS_LITERAL_STRING(WEBCRYPTO_ALG_HMAC
);
108 mHmac
.mLength
= aLength
;
109 mHmac
.mHash
.mName
= aHashName
;
113 MakeRsa(const nsString
& aName
, uint32_t aModulusLength
,
114 const CryptoBuffer
& aPublicExponent
, const nsString
& aHashName
)
119 mRsa
.mModulusLength
= aModulusLength
;
120 mRsa
.mHash
.mName
= aHashName
;
121 mRsa
.mPublicExponent
.Assign(aPublicExponent
);
125 MakeEc(const nsString
& aName
, const nsString
& aNamedCurve
)
130 mEc
.mNamedCurve
= aNamedCurve
;
134 MakeDh(const nsString
& aName
, const CryptoBuffer
& aPrime
,
135 const CryptoBuffer
& aGenerator
)
140 mDh
.mPrime
.Assign(aPrime
);
141 mDh
.mGenerator
.Assign(aGenerator
);
146 } // namespace mozilla
148 #endif // mozilla_dom_KeyAlgorithmProxy_h