Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / dom / crypto / KeyAlgorithmProxy.h
blob1d5f3eac5bfb1b14ec3bc70ca4cf5e6e38c5b104
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
10 #include "pk11pub.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
17 namespace mozilla {
18 namespace dom {
20 // A heap-safe variant of RsaHashedKeyAlgorithm
21 // The only difference is that it uses CryptoBuffer instead of Uint8Array
22 struct RsaHashedKeyAlgorithmStorage {
23 nsString mName;
24 KeyAlgorithm mHash;
25 uint16_t mModulusLength;
26 CryptoBuffer mPublicExponent;
28 void
29 ToKeyAlgorithm(JSContext* aCx, RsaHashedKeyAlgorithm& aRsa) const
31 aRsa.mName = mName;
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 {
44 AES,
45 HMAC,
46 RSA,
49 KeyAlgorithmType mType;
51 // Plain is always populated with the algorithm name
52 // Others are only populated for the corresponding key type
53 nsString mName;
54 AesKeyAlgorithm mAes;
55 HmacKeyAlgorithm mHmac;
56 RsaHashedKeyAlgorithmStorage mRsa;
57 EcKeyAlgorithm mEc;
59 // Structured clone
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
73 void
74 MakeAes(const nsString& aName, uint32_t aLength)
76 mType = AES;
77 mName = aName;
78 mAes.mName = aName;
79 mAes.mLength = aLength;
82 void
83 MakeHmac(uint32_t aLength, const nsString& aHashName)
85 mType = HMAC;
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;
92 void
93 MakeRsa(const nsString& aName, uint32_t aModulusLength,
94 const CryptoBuffer& aPublicExponent, const nsString& aHashName)
96 mType = RSA;
97 mName = aName;
98 mRsa.mName = aName;
99 mRsa.mModulusLength = aModulusLength;
100 mRsa.mHash.mName = aHashName;
101 mRsa.mPublicExponent.Assign(aPublicExponent);
104 void
105 MakeEc(const nsString& aName, const nsString& aNamedCurve)
107 mType = EC;
108 mName = aName;
109 mEc.mName = aName;
110 mEc.mNamedCurve = aNamedCurve;
114 } // namespace dom
115 } // namespace mozilla
117 #endif // mozilla_dom_KeyAlgorithmProxy_h