Bumping gaia.json for 4 gaia revision(s) a=gaia-bump
[gecko.git] / dom / crypto / KeyAlgorithmProxy.h
blobc6358311e74e0eb162535f24492f6b057fa8d6d8
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 // A heap-safe variant of DhKeyAlgorithm
40 // The only difference is that it uses CryptoBuffers instead of Uint8Arrays
41 struct DhKeyAlgorithmStorage {
42 nsString mName;
43 CryptoBuffer mPrime;
44 CryptoBuffer mGenerator;
46 void
47 ToKeyAlgorithm(JSContext* aCx, DhKeyAlgorithm& aDh) const
49 aDh.mName = mName;
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 {
62 AES,
63 HMAC,
64 RSA,
65 EC,
66 DH,
68 KeyAlgorithmType mType;
70 // Plain is always populated with the algorithm name
71 // Others are only populated for the corresponding key type
72 nsString mName;
73 AesKeyAlgorithm mAes;
74 HmacKeyAlgorithm mHmac;
75 RsaHashedKeyAlgorithmStorage mRsa;
76 EcKeyAlgorithm mEc;
77 DhKeyAlgorithmStorage mDh;
79 // Structured clone
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
93 void
94 MakeAes(const nsString& aName, uint32_t aLength)
96 mType = AES;
97 mName = aName;
98 mAes.mName = aName;
99 mAes.mLength = aLength;
102 void
103 MakeHmac(uint32_t aLength, const nsString& aHashName)
105 mType = HMAC;
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;
112 void
113 MakeRsa(const nsString& aName, uint32_t aModulusLength,
114 const CryptoBuffer& aPublicExponent, const nsString& aHashName)
116 mType = RSA;
117 mName = aName;
118 mRsa.mName = aName;
119 mRsa.mModulusLength = aModulusLength;
120 mRsa.mHash.mName = aHashName;
121 mRsa.mPublicExponent.Assign(aPublicExponent);
124 void
125 MakeEc(const nsString& aName, const nsString& aNamedCurve)
127 mType = EC;
128 mName = aName;
129 mEc.mName = aName;
130 mEc.mNamedCurve = aNamedCurve;
133 void
134 MakeDh(const nsString& aName, const CryptoBuffer& aPrime,
135 const CryptoBuffer& aGenerator)
137 mType = DH;
138 mName = aName;
139 mDh.mName = aName;
140 mDh.mPrime.Assign(aPrime);
141 mDh.mGenerator.Assign(aGenerator);
145 } // namespace dom
146 } // namespace mozilla
148 #endif // mozilla_dom_KeyAlgorithmProxy_h