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 #include "mozilla/dom/KeyAlgorithmProxy.h"
9 #include "js/StructuredClone.h"
10 #include "mozilla/Assertions.h"
11 #include "mozilla/dom/StructuredCloneHolder.h"
12 #include "mozilla/dom/WebCryptoCommon.h"
14 namespace mozilla::dom
{
16 bool KeyAlgorithmProxy::WriteStructuredClone(
17 JSStructuredCloneWriter
* aWriter
) const {
18 if (!StructuredCloneHolder::WriteString(aWriter
, mName
) ||
19 !JS_WriteUint32Pair(aWriter
, mType
, KEY_ALGORITHM_SC_VERSION
)) {
25 return JS_WriteUint32Pair(aWriter
, mAes
.mLength
, 0);
29 return JS_WriteUint32Pair(aWriter
, mHmac
.mLength
, 0) &&
30 StructuredCloneHolder::WriteString(aWriter
, mHmac
.mHash
.mName
);
32 return JS_WriteUint32Pair(aWriter
, mRsa
.mModulusLength
, 0) &&
33 WriteBuffer(aWriter
, mRsa
.mPublicExponent
) &&
34 StructuredCloneHolder::WriteString(aWriter
, mRsa
.mHash
.mName
);
37 return StructuredCloneHolder::WriteString(aWriter
, mEc
.mNamedCurve
);
45 bool KeyAlgorithmProxy::ReadStructuredClone(JSStructuredCloneReader
* aReader
) {
46 uint32_t type
, version
, dummy
;
47 if (!StructuredCloneHolder::ReadString(aReader
, mName
) ||
48 !JS_ReadUint32Pair(aReader
, &type
, &version
)) {
52 if (version
!= KEY_ALGORITHM_SC_VERSION
) {
61 if (!JS_ReadUint32Pair(aReader
, &length
, &dummy
)) {
65 mAes
.mLength
= length
;
76 if (!JS_ReadUint32Pair(aReader
, &mHmac
.mLength
, &dummy
) ||
77 !StructuredCloneHolder::ReadString(aReader
, mHmac
.mHash
.mName
)) {
87 uint32_t modulusLength
;
89 if (!JS_ReadUint32Pair(aReader
, &modulusLength
, &dummy
) ||
90 !ReadBuffer(aReader
, mRsa
.mPublicExponent
) ||
91 !StructuredCloneHolder::ReadString(aReader
, mRsa
.mHash
.mName
)) {
95 mRsa
.mModulusLength
= modulusLength
;
103 if (!StructuredCloneHolder::ReadString(aReader
, mEc
.mNamedCurve
)) {
122 KeyAlgorithmProxy::Mechanism() const {
124 return GetMechanism(mHmac
);
126 return MapAlgorithmNameToMechanism(mName
);
129 nsString
KeyAlgorithmProxy::JwkAlg() const {
130 if (mName
.EqualsLiteral(WEBCRYPTO_ALG_AES_CBC
)) {
131 switch (mAes
.mLength
) {
133 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A128CBC
);
135 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A192CBC
);
137 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A256CBC
);
141 if (mName
.EqualsLiteral(WEBCRYPTO_ALG_AES_CTR
)) {
142 switch (mAes
.mLength
) {
144 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A128CTR
);
146 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A192CTR
);
148 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A256CTR
);
152 if (mName
.EqualsLiteral(WEBCRYPTO_ALG_AES_GCM
)) {
153 switch (mAes
.mLength
) {
155 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A128GCM
);
157 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A192GCM
);
159 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A256GCM
);
163 if (mName
.EqualsLiteral(WEBCRYPTO_ALG_AES_KW
)) {
164 switch (mAes
.mLength
) {
166 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A128KW
);
168 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A192KW
);
170 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_A256KW
);
174 if (mName
.EqualsLiteral(WEBCRYPTO_ALG_HMAC
)) {
175 nsString hashName
= mHmac
.mHash
.mName
;
176 if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA1
)) {
177 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_HS1
);
178 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA256
)) {
179 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_HS256
);
180 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA384
)) {
181 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_HS384
);
182 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA512
)) {
183 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_HS512
);
187 if (mName
.EqualsLiteral(WEBCRYPTO_ALG_RSASSA_PKCS1
)) {
188 nsString hashName
= mRsa
.mHash
.mName
;
189 if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA1
)) {
190 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_RS1
);
191 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA256
)) {
192 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_RS256
);
193 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA384
)) {
194 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_RS384
);
195 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA512
)) {
196 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_RS512
);
200 if (mName
.EqualsLiteral(WEBCRYPTO_ALG_RSA_OAEP
)) {
201 nsString hashName
= mRsa
.mHash
.mName
;
202 if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA1
)) {
203 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_RSA_OAEP
);
204 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA256
)) {
205 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_RSA_OAEP_256
);
206 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA384
)) {
207 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_RSA_OAEP_384
);
208 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA512
)) {
209 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_RSA_OAEP_512
);
213 if (mName
.EqualsLiteral(WEBCRYPTO_ALG_RSA_PSS
)) {
214 nsString hashName
= mRsa
.mHash
.mName
;
215 if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA1
)) {
216 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_PS1
);
217 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA256
)) {
218 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_PS256
);
219 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA384
)) {
220 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_PS384
);
221 } else if (hashName
.EqualsLiteral(WEBCRYPTO_ALG_SHA512
)) {
222 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_PS512
);
226 if (mName
.EqualsLiteral(WEBCRYPTO_ALG_ECDSA
)) {
227 nsString curveName
= mEc
.mNamedCurve
;
228 if (curveName
.EqualsLiteral(WEBCRYPTO_NAMED_CURVE_P256
)) {
229 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_ECDSA_P_256
);
231 if (curveName
.EqualsLiteral(WEBCRYPTO_NAMED_CURVE_P384
)) {
232 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_ECDSA_P_384
);
234 if (curveName
.EqualsLiteral(WEBCRYPTO_NAMED_CURVE_P521
)) {
235 return NS_LITERAL_STRING_FROM_CSTRING(JWK_ALG_ECDSA_P_521
);
243 KeyAlgorithmProxy::GetMechanism(const KeyAlgorithm
& aAlgorithm
) {
244 // For everything but HMAC, the name determines the mechanism
245 // HMAC is handled by the specialization below
246 return MapAlgorithmNameToMechanism(aAlgorithm
.mName
);
250 KeyAlgorithmProxy::GetMechanism(const HmacKeyAlgorithm
& aAlgorithm
) {
251 // The use of HmacKeyAlgorithm doesn't completely prevent this
252 // method from being called with dictionaries that don't really
253 // represent HMAC key algorithms.
254 MOZ_ASSERT(aAlgorithm
.mName
.EqualsLiteral(WEBCRYPTO_ALG_HMAC
));
256 CK_MECHANISM_TYPE hashMech
;
257 hashMech
= MapAlgorithmNameToMechanism(aAlgorithm
.mHash
.mName
);
261 return CKM_SHA_1_HMAC
;
263 return CKM_SHA256_HMAC
;
265 return CKM_SHA384_HMAC
;
267 return CKM_SHA512_HMAC
;
269 return UNKNOWN_CK_MECHANISM
;
272 } // namespace mozilla::dom