Bug 1850713: remove duplicated setting of early hint preloader id in `ScriptLoader...
[gecko.git] / dom / crypto / KeyAlgorithmProxy.h
blob9936ef8d33b15171e463aafcf5c93fc4f7588f91
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
10 #include <cstdint>
11 #include <utility>
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"
19 #include "pkcs11t.h"
21 class JSObject;
22 struct JSContext;
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 {
33 nsString mName;
34 KeyAlgorithm mHash;
35 uint16_t mModulusLength;
36 CryptoBuffer mPublicExponent;
38 bool ToKeyAlgorithm(JSContext* aCx, RsaHashedKeyAlgorithm& aRsa) const {
39 JS::Rooted<JSObject*> exponent(aCx, mPublicExponent.ToUint8Array(aCx));
40 if (!exponent) {
41 return false;
44 aRsa.mName = mName;
45 aRsa.mModulusLength = mModulusLength;
46 aRsa.mHash.mName = mHash.mName;
47 aRsa.mPublicExponent.Init(exponent);
48 aRsa.mPublicExponent.ComputeState();
50 return true;
54 // This class encapuslates a KeyAlgorithm object, and adds several
55 // methods that make WebCrypto operations simpler.
56 struct KeyAlgorithmProxy {
57 enum KeyAlgorithmType {
58 AES,
59 HMAC,
60 RSA,
61 EC,
63 KeyAlgorithmType mType;
65 // Plain is always populated with the algorithm name
66 // Others are only populated for the corresponding key type
67 nsString mName;
68 AesKeyAlgorithm mAes;
69 HmacKeyAlgorithm mHmac;
70 RsaHashedKeyAlgorithmStorage mRsa;
71 EcKeyAlgorithm mEc;
73 // Structured clone
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) {
88 mType = AES;
89 mName = aName;
90 mAes.mName = aName;
91 mAes.mLength = aLength;
94 void MakeHmac(uint32_t aLength, const nsString& aHashName) {
95 mType = HMAC;
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) {
104 mType = RSA;
105 mName = aName;
106 mRsa.mName = aName;
107 mRsa.mModulusLength = aModulusLength;
108 mRsa.mHash.mName = aHashName;
109 if (!mRsa.mPublicExponent.Assign(aPublicExponent)) {
110 return false;
112 return true;
115 void MakeEc(const nsString& aName, const nsString& aNamedCurve) {
116 mType = EC;
117 mName = aName;
118 mEc.mName = aName;
119 mEc.mNamedCurve = aNamedCurve;
123 } // namespace mozilla::dom
125 #endif // mozilla_dom_KeyAlgorithmProxy_h