KeyAlgorithmProxy.h (3969B)
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/. */ 6 7 #ifndef mozilla_dom_KeyAlgorithmProxy_h 8 #define mozilla_dom_KeyAlgorithmProxy_h 9 10 #include <cstdint> 11 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" 20 21 class JSObject; 22 struct JSContext; 23 struct JSStructuredCloneReader; 24 struct JSStructuredCloneWriter; 25 26 #define KEY_ALGORITHM_SC_VERSION 0x00000001 27 28 namespace mozilla::dom { 29 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; 37 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; 44 } 45 46 aRsa.mName = mName; 47 aRsa.mModulusLength = mModulusLength; 48 aRsa.mHash.mName = mHash.mName; 49 aRsa.mPublicExponent.Init(exponent); 50 51 return true; 52 } 53 }; 54 55 // This class encapuslates a KeyAlgorithm object, and adds several 56 // methods that make WebCrypto operations simpler. 57 struct KeyAlgorithmProxy { 58 enum KeyAlgorithmType { AES, HMAC, RSA, EC, KDF, OKP }; 59 KeyAlgorithmType mType; 60 61 // Plain is always populated with the algorithm name 62 // Others are only populated for the corresponding key type 63 nsString mName; 64 AesKeyAlgorithm mAes; 65 HmacKeyAlgorithm mHmac; 66 RsaHashedKeyAlgorithmStorage mRsa; 67 EcKeyAlgorithm mEc; 68 KeyAlgorithm mKDF; 69 KeyAlgorithm mEd; 70 71 // Structured clone 72 bool WriteStructuredClone(JSStructuredCloneWriter* aWriter) const; 73 bool ReadStructuredClone(JSStructuredCloneReader* aReader); 74 75 // Extract various forms of derived information 76 CK_MECHANISM_TYPE Mechanism() const; 77 nsString JwkAlg() const; 78 79 // And in static form for calling on raw KeyAlgorithm dictionaries 80 static CK_MECHANISM_TYPE GetMechanism(const KeyAlgorithm& aAlgorithm); 81 static CK_MECHANISM_TYPE GetMechanism(const HmacKeyAlgorithm& aAlgorithm); 82 static nsString GetJwkAlg(const KeyAlgorithm& aAlgorithm); 83 84 // Construction of the various algorithm types 85 void MakeAes(const nsString& aName, uint32_t aLength) { 86 mType = AES; 87 mName = aName; 88 mAes.mName = aName; 89 mAes.mLength = aLength; 90 } 91 92 void MakeKDF(const nsString& aName) { 93 mType = KDF; 94 mName = aName; 95 mKDF.mName = aName; 96 } 97 98 void MakeHmac(uint32_t aLength, const nsString& aHashName) { 99 mType = HMAC; 100 mName = NS_LITERAL_STRING_FROM_CSTRING(WEBCRYPTO_ALG_HMAC); 101 mHmac.mName = NS_LITERAL_STRING_FROM_CSTRING(WEBCRYPTO_ALG_HMAC); 102 mHmac.mLength = aLength; 103 mHmac.mHash.mName = aHashName; 104 } 105 106 bool MakeRsa(const nsString& aName, uint32_t aModulusLength, 107 const CryptoBuffer& aPublicExponent, const nsString& aHashName) { 108 mType = RSA; 109 mName = aName; 110 mRsa.mName = aName; 111 mRsa.mModulusLength = aModulusLength; 112 mRsa.mHash.mName = aHashName; 113 if (!mRsa.mPublicExponent.Assign(aPublicExponent)) { 114 return false; 115 } 116 return true; 117 } 118 119 void MakeEc(const nsString& aName, const nsString& aNamedCurve) { 120 mType = EC; 121 mName = aName; 122 mEc.mName = aName; 123 mEc.mNamedCurve = aNamedCurve; 124 } 125 126 void MakeOKP(const nsString& aName) { 127 mType = OKP; 128 mName = aName; 129 mEd.mName = aName; 130 } 131 }; 132 133 } // namespace mozilla::dom 134 135 #endif // mozilla_dom_KeyAlgorithmProxy_h