/src/mozilla-central/dom/crypto/KeyAlgorithmProxy.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #include "mozilla/dom/KeyAlgorithmProxy.h" |
8 | | #include "mozilla/dom/WebCryptoCommon.h" |
9 | | |
10 | | namespace mozilla { |
11 | | namespace dom { |
12 | | |
13 | | bool |
14 | | KeyAlgorithmProxy::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const |
15 | 0 | { |
16 | 0 | if (!WriteString(aWriter, mName) || |
17 | 0 | !JS_WriteUint32Pair(aWriter, mType, KEY_ALGORITHM_SC_VERSION)) { |
18 | 0 | return false; |
19 | 0 | } |
20 | 0 | |
21 | 0 | switch (mType) { |
22 | 0 | case AES: |
23 | 0 | return JS_WriteUint32Pair(aWriter, mAes.mLength, 0); |
24 | 0 | case HMAC: |
25 | 0 | return JS_WriteUint32Pair(aWriter, mHmac.mLength, 0) && |
26 | 0 | WriteString(aWriter, mHmac.mHash.mName); |
27 | 0 | case RSA: { |
28 | 0 | return JS_WriteUint32Pair(aWriter, mRsa.mModulusLength, 0) && |
29 | 0 | WriteBuffer(aWriter, mRsa.mPublicExponent) && |
30 | 0 | WriteString(aWriter, mRsa.mHash.mName); |
31 | 0 | } |
32 | 0 | case EC: |
33 | 0 | return WriteString(aWriter, mEc.mNamedCurve); |
34 | 0 | case DH: { |
35 | 0 | return WriteBuffer(aWriter, mDh.mPrime) && |
36 | 0 | WriteBuffer(aWriter, mDh.mGenerator); |
37 | 0 | } |
38 | 0 | } |
39 | 0 | |
40 | 0 | return false; |
41 | 0 | } |
42 | | |
43 | | bool |
44 | | KeyAlgorithmProxy::ReadStructuredClone(JSStructuredCloneReader* aReader) |
45 | 0 | { |
46 | 0 | uint32_t type, version, dummy; |
47 | 0 | if (!ReadString(aReader, mName) || |
48 | 0 | !JS_ReadUint32Pair(aReader, &type, &version)) { |
49 | 0 | return false; |
50 | 0 | } |
51 | 0 | |
52 | 0 | if (version != KEY_ALGORITHM_SC_VERSION) { |
53 | 0 | return false; |
54 | 0 | } |
55 | 0 | |
56 | 0 | mType = (KeyAlgorithmType) type; |
57 | 0 | switch (mType) { |
58 | 0 | case AES: { |
59 | 0 | uint32_t length; |
60 | 0 | if (!JS_ReadUint32Pair(aReader, &length, &dummy)) { |
61 | 0 | return false; |
62 | 0 | } |
63 | 0 | |
64 | 0 | mAes.mLength = length; |
65 | 0 | mAes.mName = mName; |
66 | 0 | return true; |
67 | 0 | } |
68 | 0 | case HMAC: { |
69 | 0 | if (!JS_ReadUint32Pair(aReader, &mHmac.mLength, &dummy) || |
70 | 0 | !ReadString(aReader, mHmac.mHash.mName)) { |
71 | 0 | return false; |
72 | 0 | } |
73 | 0 | |
74 | 0 | mHmac.mName = mName; |
75 | 0 | return true; |
76 | 0 | } |
77 | 0 | case RSA: { |
78 | 0 | uint32_t modulusLength; |
79 | 0 | nsString hashName; |
80 | 0 | if (!JS_ReadUint32Pair(aReader, &modulusLength, &dummy) || |
81 | 0 | !ReadBuffer(aReader, mRsa.mPublicExponent) || |
82 | 0 | !ReadString(aReader, mRsa.mHash.mName)) { |
83 | 0 | return false; |
84 | 0 | } |
85 | 0 | |
86 | 0 | mRsa.mModulusLength = modulusLength; |
87 | 0 | mRsa.mName = mName; |
88 | 0 | return true; |
89 | 0 | } |
90 | 0 | case EC: { |
91 | 0 | nsString namedCurve; |
92 | 0 | if (!ReadString(aReader, mEc.mNamedCurve)) { |
93 | 0 | return false; |
94 | 0 | } |
95 | 0 | |
96 | 0 | mEc.mName = mName; |
97 | 0 | return true; |
98 | 0 | } |
99 | 0 | case DH: { |
100 | 0 | if (!ReadBuffer(aReader, mDh.mPrime) || |
101 | 0 | !ReadBuffer(aReader, mDh.mGenerator)) { |
102 | 0 | return false; |
103 | 0 | } |
104 | 0 | |
105 | 0 | mDh.mName = mName; |
106 | 0 | return true; |
107 | 0 | } |
108 | 0 | } |
109 | 0 | |
110 | 0 | return false; |
111 | 0 | } |
112 | | |
113 | | CK_MECHANISM_TYPE |
114 | | KeyAlgorithmProxy::Mechanism() const |
115 | 0 | { |
116 | 0 | if (mType == HMAC) { |
117 | 0 | return GetMechanism(mHmac); |
118 | 0 | } |
119 | 0 | return MapAlgorithmNameToMechanism(mName); |
120 | 0 | } |
121 | | |
122 | | nsString |
123 | | KeyAlgorithmProxy::JwkAlg() const |
124 | 0 | { |
125 | 0 | if (mName.EqualsLiteral(WEBCRYPTO_ALG_AES_CBC)) { |
126 | 0 | switch (mAes.mLength) { |
127 | 0 | case 128: return NS_LITERAL_STRING(JWK_ALG_A128CBC); |
128 | 0 | case 192: return NS_LITERAL_STRING(JWK_ALG_A192CBC); |
129 | 0 | case 256: return NS_LITERAL_STRING(JWK_ALG_A256CBC); |
130 | 0 | } |
131 | 0 | } |
132 | 0 | |
133 | 0 | if (mName.EqualsLiteral(WEBCRYPTO_ALG_AES_CTR)) { |
134 | 0 | switch (mAes.mLength) { |
135 | 0 | case 128: return NS_LITERAL_STRING(JWK_ALG_A128CTR); |
136 | 0 | case 192: return NS_LITERAL_STRING(JWK_ALG_A192CTR); |
137 | 0 | case 256: return NS_LITERAL_STRING(JWK_ALG_A256CTR); |
138 | 0 | } |
139 | 0 | } |
140 | 0 | |
141 | 0 | if (mName.EqualsLiteral(WEBCRYPTO_ALG_AES_GCM)) { |
142 | 0 | switch (mAes.mLength) { |
143 | 0 | case 128: return NS_LITERAL_STRING(JWK_ALG_A128GCM); |
144 | 0 | case 192: return NS_LITERAL_STRING(JWK_ALG_A192GCM); |
145 | 0 | case 256: return NS_LITERAL_STRING(JWK_ALG_A256GCM); |
146 | 0 | } |
147 | 0 | } |
148 | 0 | |
149 | 0 | if (mName.EqualsLiteral(WEBCRYPTO_ALG_AES_KW)) { |
150 | 0 | switch (mAes.mLength) { |
151 | 0 | case 128: return NS_LITERAL_STRING(JWK_ALG_A128KW); |
152 | 0 | case 192: return NS_LITERAL_STRING(JWK_ALG_A192KW); |
153 | 0 | case 256: return NS_LITERAL_STRING(JWK_ALG_A256KW); |
154 | 0 | } |
155 | 0 | } |
156 | 0 | |
157 | 0 | if (mName.EqualsLiteral(WEBCRYPTO_ALG_HMAC)) { |
158 | 0 | nsString hashName = mHmac.mHash.mName; |
159 | 0 | if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA1)) { |
160 | 0 | return NS_LITERAL_STRING(JWK_ALG_HS1); |
161 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA256)) { |
162 | 0 | return NS_LITERAL_STRING(JWK_ALG_HS256); |
163 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA384)) { |
164 | 0 | return NS_LITERAL_STRING(JWK_ALG_HS384); |
165 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA512)) { |
166 | 0 | return NS_LITERAL_STRING(JWK_ALG_HS512); |
167 | 0 | } |
168 | 0 | } |
169 | 0 |
|
170 | 0 | if (mName.EqualsLiteral(WEBCRYPTO_ALG_RSASSA_PKCS1)) { |
171 | 0 | nsString hashName = mRsa.mHash.mName; |
172 | 0 | if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA1)) { |
173 | 0 | return NS_LITERAL_STRING(JWK_ALG_RS1); |
174 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA256)) { |
175 | 0 | return NS_LITERAL_STRING(JWK_ALG_RS256); |
176 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA384)) { |
177 | 0 | return NS_LITERAL_STRING(JWK_ALG_RS384); |
178 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA512)) { |
179 | 0 | return NS_LITERAL_STRING(JWK_ALG_RS512); |
180 | 0 | } |
181 | 0 | } |
182 | 0 |
|
183 | 0 | if (mName.EqualsLiteral(WEBCRYPTO_ALG_RSA_OAEP)) { |
184 | 0 | nsString hashName = mRsa.mHash.mName; |
185 | 0 | if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA1)) { |
186 | 0 | return NS_LITERAL_STRING(JWK_ALG_RSA_OAEP); |
187 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA256)) { |
188 | 0 | return NS_LITERAL_STRING(JWK_ALG_RSA_OAEP_256); |
189 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA384)) { |
190 | 0 | return NS_LITERAL_STRING(JWK_ALG_RSA_OAEP_384); |
191 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA512)) { |
192 | 0 | return NS_LITERAL_STRING(JWK_ALG_RSA_OAEP_512); |
193 | 0 | } |
194 | 0 | } |
195 | 0 |
|
196 | 0 | if (mName.EqualsLiteral(WEBCRYPTO_ALG_RSA_PSS)) { |
197 | 0 | nsString hashName = mRsa.mHash.mName; |
198 | 0 | if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA1)) { |
199 | 0 | return NS_LITERAL_STRING(JWK_ALG_PS1); |
200 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA256)) { |
201 | 0 | return NS_LITERAL_STRING(JWK_ALG_PS256); |
202 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA384)) { |
203 | 0 | return NS_LITERAL_STRING(JWK_ALG_PS384); |
204 | 0 | } else if (hashName.EqualsLiteral(WEBCRYPTO_ALG_SHA512)) { |
205 | 0 | return NS_LITERAL_STRING(JWK_ALG_PS512); |
206 | 0 | } |
207 | 0 | } |
208 | 0 |
|
209 | 0 | return nsString(); |
210 | 0 | } |
211 | | |
212 | | CK_MECHANISM_TYPE |
213 | | KeyAlgorithmProxy::GetMechanism(const KeyAlgorithm& aAlgorithm) |
214 | 0 | { |
215 | 0 | // For everything but HMAC, the name determines the mechanism |
216 | 0 | // HMAC is handled by the specialization below |
217 | 0 | return MapAlgorithmNameToMechanism(aAlgorithm.mName); |
218 | 0 | } |
219 | | |
220 | | CK_MECHANISM_TYPE |
221 | | KeyAlgorithmProxy::GetMechanism(const HmacKeyAlgorithm& aAlgorithm) |
222 | 0 | { |
223 | 0 | // The use of HmacKeyAlgorithm doesn't completely prevent this |
224 | 0 | // method from being called with dictionaries that don't really |
225 | 0 | // represent HMAC key algorithms. |
226 | 0 | MOZ_ASSERT(aAlgorithm.mName.EqualsLiteral(WEBCRYPTO_ALG_HMAC)); |
227 | 0 |
|
228 | 0 | CK_MECHANISM_TYPE hashMech; |
229 | 0 | hashMech = MapAlgorithmNameToMechanism(aAlgorithm.mHash.mName); |
230 | 0 |
|
231 | 0 | switch (hashMech) { |
232 | 0 | case CKM_SHA_1: return CKM_SHA_1_HMAC; |
233 | 0 | case CKM_SHA256: return CKM_SHA256_HMAC; |
234 | 0 | case CKM_SHA384: return CKM_SHA384_HMAC; |
235 | 0 | case CKM_SHA512: return CKM_SHA512_HMAC; |
236 | 0 | } |
237 | 0 | return UNKNOWN_CK_MECHANISM; |
238 | 0 | } |
239 | | |
240 | | } // namespace dom |
241 | | } // namespace mozilla |