Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / crypto / KeyAlgorithmProxy.h
blobeaaf40892973ea581ba025c1684288cd058a896b
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,
39 ErrorResult& aError) const {
40 JS::Rooted<JSObject*> exponent(aCx,
41 mPublicExponent.ToUint8Array(aCx, aError));
42 if (aError.Failed()) {
43 return false;
46 aRsa.mName = mName;
47 aRsa.mModulusLength = mModulusLength;
48 aRsa.mHash.mName = mHash.mName;
49 aRsa.mPublicExponent.Init(exponent);
51 return true;
55 // This class encapuslates a KeyAlgorithm object, and adds several
56 // methods that make WebCrypto operations simpler.
57 struct KeyAlgorithmProxy {
58 enum KeyAlgorithmType {
59 AES,
60 HMAC,
61 RSA,
62 EC,
64 KeyAlgorithmType mType;
66 // Plain is always populated with the algorithm name
67 // Others are only populated for the corresponding key type
68 nsString mName;
69 AesKeyAlgorithm mAes;
70 HmacKeyAlgorithm mHmac;
71 RsaHashedKeyAlgorithmStorage mRsa;
72 EcKeyAlgorithm mEc;
74 // Structured clone
75 bool WriteStructuredClone(JSStructuredCloneWriter* aWriter) const;
76 bool ReadStructuredClone(JSStructuredCloneReader* aReader);
78 // Extract various forms of derived information
79 CK_MECHANISM_TYPE Mechanism() const;
80 nsString JwkAlg() const;
82 // And in static form for calling on raw KeyAlgorithm dictionaries
83 static CK_MECHANISM_TYPE GetMechanism(const KeyAlgorithm& aAlgorithm);
84 static CK_MECHANISM_TYPE GetMechanism(const HmacKeyAlgorithm& aAlgorithm);
85 static nsString GetJwkAlg(const KeyAlgorithm& aAlgorithm);
87 // Construction of the various algorithm types
88 void MakeAes(const nsString& aName, uint32_t aLength) {
89 mType = AES;
90 mName = aName;
91 mAes.mName = aName;
92 mAes.mLength = aLength;
95 void MakeHmac(uint32_t aLength, const nsString& aHashName) {
96 mType = HMAC;
97 mName = NS_LITERAL_STRING_FROM_CSTRING(WEBCRYPTO_ALG_HMAC);
98 mHmac.mName = NS_LITERAL_STRING_FROM_CSTRING(WEBCRYPTO_ALG_HMAC);
99 mHmac.mLength = aLength;
100 mHmac.mHash.mName = aHashName;
103 bool MakeRsa(const nsString& aName, uint32_t aModulusLength,
104 const CryptoBuffer& aPublicExponent, const nsString& aHashName) {
105 mType = RSA;
106 mName = aName;
107 mRsa.mName = aName;
108 mRsa.mModulusLength = aModulusLength;
109 mRsa.mHash.mName = aHashName;
110 if (!mRsa.mPublicExponent.Assign(aPublicExponent)) {
111 return false;
113 return true;
116 void MakeEc(const nsString& aName, const nsString& aNamedCurve) {
117 mType = EC;
118 mName = aName;
119 mEc.mName = aName;
120 mEc.mNamedCurve = aNamedCurve;
124 } // namespace mozilla::dom
126 #endif // mozilla_dom_KeyAlgorithmProxy_h