OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/webcrypto/webcrypto_util.h" | 5 #include "content/child/webcrypto/webcrypto_util.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "content/child/webcrypto/status.h" | 10 #include "content/child/webcrypto/status.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 base::ListValue* CreateJwkKeyOpsFromWebCryptoUsages( | 107 base::ListValue* CreateJwkKeyOpsFromWebCryptoUsages( |
108 blink::WebCryptoKeyUsageMask usage_mask) { | 108 blink::WebCryptoKeyUsageMask usage_mask) { |
109 base::ListValue* jwk_key_ops = new base::ListValue(); | 109 base::ListValue* jwk_key_ops = new base::ListValue(); |
110 for (size_t i = 0; i < arraysize(kJwkWebCryptoUsageMap); ++i) { | 110 for (size_t i = 0; i < arraysize(kJwkWebCryptoUsageMap); ++i) { |
111 if (usage_mask & kJwkWebCryptoUsageMap[i].webcrypto_usage) | 111 if (usage_mask & kJwkWebCryptoUsageMap[i].webcrypto_usage) |
112 jwk_key_ops->AppendString(kJwkWebCryptoUsageMap[i].jwk_key_op); | 112 jwk_key_ops->AppendString(kJwkWebCryptoUsageMap[i].jwk_key_op); |
113 } | 113 } |
114 return jwk_key_ops; | 114 return jwk_key_ops; |
115 } | 115 } |
116 | 116 |
117 bool IsHashAlgorithm(blink::WebCryptoAlgorithmId alg_id) { | |
118 return alg_id == blink::WebCryptoAlgorithmIdSha1 || | |
119 alg_id == blink::WebCryptoAlgorithmIdSha256 || | |
120 alg_id == blink::WebCryptoAlgorithmIdSha384 || | |
121 alg_id == blink::WebCryptoAlgorithmIdSha512; | |
122 } | |
123 | |
124 blink::WebCryptoAlgorithm GetInnerHashAlgorithm( | 117 blink::WebCryptoAlgorithm GetInnerHashAlgorithm( |
125 const blink::WebCryptoAlgorithm& algorithm) { | 118 const blink::WebCryptoAlgorithm& algorithm) { |
126 DCHECK(!algorithm.isNull()); | 119 DCHECK(!algorithm.isNull()); |
127 switch (algorithm.paramsType()) { | 120 switch (algorithm.paramsType()) { |
128 case blink::WebCryptoAlgorithmParamsTypeHmacImportParams: | 121 case blink::WebCryptoAlgorithmParamsTypeHmacImportParams: |
129 return algorithm.hmacImportParams()->hash(); | 122 return algorithm.hmacImportParams()->hash(); |
130 case blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams: | 123 case blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams: |
131 return algorithm.hmacKeyGenParams()->hash(); | 124 return algorithm.hmacKeyGenParams()->hash(); |
132 case blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams: | 125 case blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams: |
133 return algorithm.rsaHashedImportParams()->hash(); | 126 return algorithm.rsaHashedImportParams()->hash(); |
134 case blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams: | 127 case blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams: |
135 return algorithm.rsaHashedKeyGenParams()->hash(); | 128 return algorithm.rsaHashedKeyGenParams()->hash(); |
136 default: | 129 default: |
137 return blink::WebCryptoAlgorithm::createNull(); | 130 return blink::WebCryptoAlgorithm::createNull(); |
138 } | 131 } |
139 } | 132 } |
140 | 133 |
141 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { | 134 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { |
142 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); | 135 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); |
143 } | 136 } |
144 | 137 |
145 blink::WebCryptoAlgorithm CreateHmacImportAlgorithm( | 138 blink::WebCryptoAlgorithm CreateHmacImportAlgorithm( |
146 blink::WebCryptoAlgorithmId hash_id) { | 139 blink::WebCryptoAlgorithmId hash_id) { |
147 DCHECK(IsHashAlgorithm(hash_id)); | 140 DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id)); |
148 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 141 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
149 blink::WebCryptoAlgorithmIdHmac, | 142 blink::WebCryptoAlgorithmIdHmac, |
150 new blink::WebCryptoHmacImportParams(CreateAlgorithm(hash_id))); | 143 new blink::WebCryptoHmacImportParams(CreateAlgorithm(hash_id))); |
151 } | 144 } |
152 | 145 |
153 blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( | 146 blink::WebCryptoAlgorithm CreateRsaHashedImportAlgorithm( |
154 blink::WebCryptoAlgorithmId id, | 147 blink::WebCryptoAlgorithmId id, |
155 blink::WebCryptoAlgorithmId hash_id) { | 148 blink::WebCryptoAlgorithmId hash_id) { |
156 DCHECK(IsHashAlgorithm(hash_id)); | 149 DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id)); |
157 DCHECK(id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | 150 DCHECK(id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
158 id == blink::WebCryptoAlgorithmIdRsaOaep); | 151 id == blink::WebCryptoAlgorithmIdRsaOaep); |
159 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 152 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( |
160 id, new blink::WebCryptoRsaHashedImportParams(CreateAlgorithm(hash_id))); | 153 id, new blink::WebCryptoRsaHashedImportParams(CreateAlgorithm(hash_id))); |
161 } | 154 } |
162 | 155 |
163 bool CreateSecretKeyAlgorithm(const blink::WebCryptoAlgorithm& algorithm, | 156 bool CreateSecretKeyAlgorithm(const blink::WebCryptoAlgorithm& algorithm, |
164 unsigned int keylen_bytes, | 157 unsigned int keylen_bytes, |
165 blink::WebCryptoKeyAlgorithm* key_algorithm) { | 158 blink::WebCryptoKeyAlgorithm* key_algorithm) { |
166 switch (algorithm.id()) { | 159 switch (algorithm.id()) { |
(...skipping 15 matching lines...) Expand all Loading... |
182 algorithm.id(), keylen_bytes * 8); | 175 algorithm.id(), keylen_bytes * 8); |
183 return true; | 176 return true; |
184 default: | 177 default: |
185 return false; | 178 return false; |
186 } | 179 } |
187 } | 180 } |
188 | 181 |
189 } // namespace webcrypto | 182 } // namespace webcrypto |
190 | 183 |
191 } // namespace content | 184 } // namespace content |
OLD | NEW |