Bumping manifests a=b2g-bump
[gecko.git] / dom / crypto / KeyAlgorithmProxy.cpp
blobd843cbe6a6968181e6de50de47f496c72e5d19fc
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 #include "mozilla/dom/KeyAlgorithmProxy.h"
8 #include "mozilla/dom/WebCryptoCommon.h"
10 namespace mozilla {
11 namespace dom {
13 bool
14 KeyAlgorithmProxy::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const
16 if (!WriteString(aWriter, mName) ||
17 !JS_WriteUint32Pair(aWriter, mType, KEY_ALGORITHM_SC_VERSION)) {
18 return false;
21 switch (mType) {
22 case AES:
23 return JS_WriteUint32Pair(aWriter, mAes.mLength, 0);
24 case HMAC:
25 return JS_WriteUint32Pair(aWriter, mHmac.mLength, 0) &&
26 WriteString(aWriter, mHmac.mHash.mName);
27 case RSA: {
28 return JS_WriteUint32Pair(aWriter, mRsa.mModulusLength, 0) &&
29 WriteBuffer(aWriter, mRsa.mPublicExponent) &&
30 WriteString(aWriter, mRsa.mHash.mName);
32 case EC:
33 return WriteString(aWriter, mEc.mNamedCurve);
34 case DH: {
35 return WriteBuffer(aWriter, mDh.mPrime) &&
36 WriteBuffer(aWriter, mDh.mGenerator);
40 return false;
43 bool
44 KeyAlgorithmProxy::ReadStructuredClone(JSStructuredCloneReader* aReader)
46 uint32_t type, version, dummy;
47 if (!ReadString(aReader, mName) ||
48 !JS_ReadUint32Pair(aReader, &type, &version)) {
49 return false;
52 if (version != KEY_ALGORITHM_SC_VERSION) {
53 return false;
56 mType = (KeyAlgorithmType) type;
57 switch (mType) {
58 case AES: {
59 uint32_t length;
60 if (!JS_ReadUint32Pair(aReader, &length, &dummy)) {
61 return false;
64 mAes.mLength = length;
65 mAes.mName = mName;
66 return true;
68 case HMAC: {
69 if (!JS_ReadUint32Pair(aReader, &mHmac.mLength, &dummy) ||
70 !ReadString(aReader, mHmac.mHash.mName)) {
71 return false;
74 mHmac.mName = mName;
75 return true;
77 case RSA: {
78 uint32_t modulusLength;
79 nsString hashName;
80 if (!JS_ReadUint32Pair(aReader, &modulusLength, &dummy) ||
81 !ReadBuffer(aReader, mRsa.mPublicExponent) ||
82 !ReadString(aReader, mRsa.mHash.mName)) {
83 return false;
86 mRsa.mModulusLength = modulusLength;
87 mRsa.mName = mName;
88 return true;
90 case EC: {
91 nsString namedCurve;
92 if (!ReadString(aReader, mEc.mNamedCurve)) {
93 return false;
96 mEc.mName = mName;
97 return true;
99 case DH: {
100 if (!ReadBuffer(aReader, mDh.mPrime) ||
101 !ReadBuffer(aReader, mDh.mGenerator)) {
102 return false;
105 mDh.mName = mName;
106 return true;
110 return false;
113 CK_MECHANISM_TYPE
114 KeyAlgorithmProxy::Mechanism() const
116 if (mType == HMAC) {
117 return GetMechanism(mHmac);
119 return MapAlgorithmNameToMechanism(mName);
122 nsString
123 KeyAlgorithmProxy::JwkAlg() const
125 if (mName.EqualsLiteral(WEBCRYPTO_ALG_AES_CBC)) {
126 switch (mAes.mLength) {
127 case 128: return NS_LITERAL_STRING(JWK_ALG_A128CBC);
128 case 192: return NS_LITERAL_STRING(JWK_ALG_A192CBC);
129 case 256: return NS_LITERAL_STRING(JWK_ALG_A256CBC);
133 if (mName.EqualsLiteral(WEBCRYPTO_ALG_AES_CTR)) {
134 switch (mAes.mLength) {
135 case 128: return NS_LITERAL_STRING(JWK_ALG_A128CTR);
136 case 192: return NS_LITERAL_STRING(JWK_ALG_A192CTR);
137 case 256: return NS_LITERAL_STRING(JWK_ALG_A256CTR);
141 if (mName.EqualsLiteral(WEBCRYPTO_ALG_AES_GCM)) {
142 switch (mAes.mLength) {
143 case 128: return NS_LITERAL_STRING(JWK_ALG_A128GCM);
144 case 192: return NS_LITERAL_STRING(JWK_ALG_A192GCM);
145 case 256: return NS_LITERAL_STRING(JWK_ALG_A256GCM);
149 if (mName.EqualsLiteral(WEBCRYPTO_ALG_AES_KW)) {
150 switch (mAes.mLength) {
151 case 128: return NS_LITERAL_STRING(JWK_ALG_A128KW);
152 case 192: return NS_LITERAL_STRING(JWK_ALG_A192KW);
153 case 256: return NS_LITERAL_STRING(JWK_ALG_A256KW);
157 if (mName.EqualsLiteral(WEBCRYPTO_ALG_HMAC)) {
158 nsString hashName = mHmac.mHash.mName;
159 if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA1)) {
160 return NS_LITERAL_STRING(JWK_ALG_HS1);
161 } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA256)) {
162 return NS_LITERAL_STRING(JWK_ALG_HS256);
163 } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA384)) {
164 return NS_LITERAL_STRING(JWK_ALG_HS384);
165 } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA512)) {
166 return NS_LITERAL_STRING(JWK_ALG_HS512);
170 if (mName.EqualsLiteral(WEBCRYPTO_ALG_RSASSA_PKCS1)) {
171 nsString hashName = mRsa.mHash.mName;
172 if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA1)) {
173 return NS_LITERAL_STRING(JWK_ALG_RS1);
174 } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA256)) {
175 return NS_LITERAL_STRING(JWK_ALG_RS256);
176 } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA384)) {
177 return NS_LITERAL_STRING(JWK_ALG_RS384);
178 } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA512)) {
179 return NS_LITERAL_STRING(JWK_ALG_RS512);
183 if (mName.EqualsLiteral(WEBCRYPTO_ALG_RSA_OAEP)) {
184 nsString hashName = mRsa.mHash.mName;
185 if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA1)) {
186 return NS_LITERAL_STRING(JWK_ALG_RSA_OAEP);
187 } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA256)) {
188 return NS_LITERAL_STRING(JWK_ALG_RSA_OAEP_256);
189 } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA384)) {
190 return NS_LITERAL_STRING(JWK_ALG_RSA_OAEP_384);
191 } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA512)) {
192 return NS_LITERAL_STRING(JWK_ALG_RSA_OAEP_512);
196 return nsString();
199 CK_MECHANISM_TYPE
200 KeyAlgorithmProxy::GetMechanism(const KeyAlgorithm& aAlgorithm)
202 // For everything but HMAC, the name determines the mechanism
203 // HMAC is handled by the specialization below
204 return MapAlgorithmNameToMechanism(aAlgorithm.mName);
207 CK_MECHANISM_TYPE
208 KeyAlgorithmProxy::GetMechanism(const HmacKeyAlgorithm& aAlgorithm)
210 // The use of HmacKeyAlgorithm doesn't completely prevent this
211 // method from being called with dictionaries that don't really
212 // represent HMAC key algorithms.
213 MOZ_ASSERT(aAlgorithm.mName.EqualsLiteral(WEBCRYPTO_ALG_HMAC));
215 CK_MECHANISM_TYPE hashMech;
216 hashMech = MapAlgorithmNameToMechanism(aAlgorithm.mHash.mName);
218 switch (hashMech) {
219 case CKM_SHA_1: return CKM_SHA_1_HMAC;
220 case CKM_SHA256: return CKM_SHA256_HMAC;
221 case CKM_SHA384: return CKM_SHA384_HMAC;
222 case CKM_SHA512: return CKM_SHA512_HMAC;
224 return UNKNOWN_CK_MECHANISM;
227 } // namespace dom
228 } // namespace mozilla