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"
14 KeyAlgorithmProxy::WriteStructuredClone(JSStructuredCloneWriter
* aWriter
) const
16 if (!WriteString(aWriter
, mName
) ||
17 !JS_WriteUint32Pair(aWriter
, mType
, KEY_ALGORITHM_SC_VERSION
)) {
23 return JS_WriteUint32Pair(aWriter
, mAes
.mLength
, 0);
25 return JS_WriteUint32Pair(aWriter
, mHmac
.mLength
, 0) &&
26 WriteString(aWriter
, mHmac
.mHash
.mName
);
28 return JS_WriteUint32Pair(aWriter
, mRsa
.mModulusLength
, 0) &&
29 WriteBuffer(aWriter
, mRsa
.mPublicExponent
) &&
30 WriteString(aWriter
, mRsa
.mHash
.mName
);
33 return WriteString(aWriter
, mEc
.mNamedCurve
);
35 return WriteBuffer(aWriter
, mDh
.mPrime
) &&
36 WriteBuffer(aWriter
, mDh
.mGenerator
);
44 KeyAlgorithmProxy::ReadStructuredClone(JSStructuredCloneReader
* aReader
)
46 uint32_t type
, version
, dummy
;
47 if (!ReadString(aReader
, mName
) ||
48 !JS_ReadUint32Pair(aReader
, &type
, &version
)) {
52 if (version
!= KEY_ALGORITHM_SC_VERSION
) {
56 mType
= (KeyAlgorithmType
) type
;
60 if (!JS_ReadUint32Pair(aReader
, &length
, &dummy
)) {
64 mAes
.mLength
= length
;
69 if (!JS_ReadUint32Pair(aReader
, &mHmac
.mLength
, &dummy
) ||
70 !ReadString(aReader
, mHmac
.mHash
.mName
)) {
78 uint32_t modulusLength
;
80 if (!JS_ReadUint32Pair(aReader
, &modulusLength
, &dummy
) ||
81 !ReadBuffer(aReader
, mRsa
.mPublicExponent
) ||
82 !ReadString(aReader
, mRsa
.mHash
.mName
)) {
86 mRsa
.mModulusLength
= modulusLength
;
92 if (!ReadString(aReader
, mEc
.mNamedCurve
)) {
100 if (!ReadBuffer(aReader
, mDh
.mPrime
) ||
101 !ReadBuffer(aReader
, mDh
.mGenerator
)) {
114 KeyAlgorithmProxy::Mechanism() const
117 return GetMechanism(mHmac
);
119 return MapAlgorithmNameToMechanism(mName
);
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
);
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
);
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
);
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
;
228 } // namespace mozilla