| OLD | NEW | 
|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/renderer/webcrypto/webcrypto_util.h" | 5 #include "content/renderer/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 "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 9 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 
| 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 154 Status::Status(const char* error_details_utf8) | 154 Status::Status(const char* error_details_utf8) | 
| 155     : error_details_(error_details_utf8) { | 155     : error_details_(error_details_utf8) { | 
| 156 } | 156 } | 
| 157 | 157 | 
| 158 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { | 158 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { | 
| 159   if (data.empty()) | 159   if (data.empty()) | 
| 160     return NULL; | 160     return NULL; | 
| 161   return &data[0]; | 161   return &data[0]; | 
| 162 } | 162 } | 
| 163 | 163 | 
| 164 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size) { | 164 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned int new_size) { | 
| 165   DCHECK_LE(new_size, buffer->byteLength()); | 165   DCHECK_LE(new_size, buffer->byteLength()); | 
| 166 | 166 | 
| 167   if (new_size == buffer->byteLength()) | 167   if (new_size == buffer->byteLength()) | 
| 168     return; | 168     return; | 
| 169 | 169 | 
| 170   blink::WebArrayBuffer new_buffer = blink::WebArrayBuffer::create(new_size, 1); | 170   blink::WebArrayBuffer new_buffer = blink::WebArrayBuffer::create(new_size, 1); | 
| 171   DCHECK(!new_buffer.isNull()); | 171   DCHECK(!new_buffer.isNull()); | 
| 172   memcpy(new_buffer.data(), buffer->data(), new_size); | 172   memcpy(new_buffer.data(), buffer->data(), new_size); | 
| 173   *buffer = new_buffer; | 173   *buffer = new_buffer; | 
| 174 } | 174 } | 
| 175 | 175 | 
| 176 blink::WebArrayBuffer CreateArrayBuffer(const uint8* data, unsigned data_size) { | 176 blink::WebArrayBuffer CreateArrayBuffer(const uint8* data, | 
|  | 177                                         unsigned int data_size) { | 
| 177   blink::WebArrayBuffer buffer = blink::WebArrayBuffer::create(data_size, 1); | 178   blink::WebArrayBuffer buffer = blink::WebArrayBuffer::create(data_size, 1); | 
| 178   DCHECK(!buffer.isNull()); | 179   DCHECK(!buffer.isNull()); | 
| 179   if (data_size)  // data_size == 0 might mean the data pointer is invalid | 180   if (data_size)  // data_size == 0 might mean the data pointer is invalid | 
| 180     memcpy(buffer.data(), data, data_size); | 181     memcpy(buffer.data(), data, data_size); | 
| 181   return buffer; | 182   return buffer; | 
| 182 } | 183 } | 
| 183 | 184 | 
| 184 // This function decodes unpadded 'base64url' encoded data, as described in | 185 // This function decodes unpadded 'base64url' encoded data, as described in | 
| 185 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. To do this, first | 186 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. To do this, first | 
| 186 // change the incoming data to 'base64' encoding by applying the appropriate | 187 // change the incoming data to 'base64' encoding by applying the appropriate | 
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 233 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( | 234 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( | 
| 234     blink::WebCryptoAlgorithmId hash_id) { | 235     blink::WebCryptoAlgorithmId hash_id) { | 
| 235   DCHECK(IsHashAlgorithm(hash_id)); | 236   DCHECK(IsHashAlgorithm(hash_id)); | 
| 236   return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 237   return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 
| 237       blink::WebCryptoAlgorithmIdHmac, | 238       blink::WebCryptoAlgorithmIdHmac, | 
| 238       new blink::WebCryptoHmacParams(CreateAlgorithm(hash_id))); | 239       new blink::WebCryptoHmacParams(CreateAlgorithm(hash_id))); | 
| 239 } | 240 } | 
| 240 | 241 | 
| 241 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( | 242 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm( | 
| 242     blink::WebCryptoAlgorithmId hash_id, | 243     blink::WebCryptoAlgorithmId hash_id, | 
| 243     unsigned key_length_bytes) { | 244     unsigned int key_length_bytes) { | 
| 244   DCHECK(IsHashAlgorithm(hash_id)); | 245   DCHECK(IsHashAlgorithm(hash_id)); | 
| 245   // key_length_bytes == 0 means unspecified | 246   // key_length_bytes == 0 means unspecified | 
| 246   return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 247   return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 
| 247       blink::WebCryptoAlgorithmIdHmac, | 248       blink::WebCryptoAlgorithmIdHmac, | 
| 248       new blink::WebCryptoHmacKeyParams( | 249       new blink::WebCryptoHmacKeyParams( | 
| 249           CreateAlgorithm(hash_id), (key_length_bytes != 0), key_length_bytes)); | 250           CreateAlgorithm(hash_id), (key_length_bytes != 0), key_length_bytes)); | 
| 250 } | 251 } | 
| 251 | 252 | 
| 252 blink::WebCryptoAlgorithm CreateRsaSsaAlgorithm( | 253 blink::WebCryptoAlgorithm CreateRsaSsaAlgorithm( | 
| 253     blink::WebCryptoAlgorithmId hash_id) { | 254     blink::WebCryptoAlgorithmId hash_id) { | 
| 254   DCHECK(IsHashAlgorithm(hash_id)); | 255   DCHECK(IsHashAlgorithm(hash_id)); | 
| 255   return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 256   return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 
| 256       blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 257       blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | 
| 257       new blink::WebCryptoRsaSsaParams(CreateAlgorithm(hash_id))); | 258       new blink::WebCryptoRsaSsaParams(CreateAlgorithm(hash_id))); | 
| 258 } | 259 } | 
| 259 | 260 | 
| 260 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm( | 261 blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm( | 
| 261     blink::WebCryptoAlgorithmId hash_id) { | 262     blink::WebCryptoAlgorithmId hash_id) { | 
| 262   DCHECK(IsHashAlgorithm(hash_id)); | 263   DCHECK(IsHashAlgorithm(hash_id)); | 
| 263   return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 264   return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 
| 264       blink::WebCryptoAlgorithmIdRsaOaep, | 265       blink::WebCryptoAlgorithmIdRsaOaep, | 
| 265       new blink::WebCryptoRsaOaepParams( | 266       new blink::WebCryptoRsaOaepParams( | 
| 266           CreateAlgorithm(hash_id), false, NULL, 0)); | 267           CreateAlgorithm(hash_id), false, NULL, 0)); | 
| 267 } | 268 } | 
| 268 | 269 | 
| 269 blink::WebCryptoAlgorithm CreateRsaKeyGenAlgorithm( | 270 blink::WebCryptoAlgorithm CreateRsaKeyGenAlgorithm( | 
| 270     blink::WebCryptoAlgorithmId algorithm_id, | 271     blink::WebCryptoAlgorithmId algorithm_id, | 
| 271     unsigned modulus_length, | 272     unsigned int modulus_length, | 
| 272     const std::vector<uint8>& public_exponent) { | 273     const std::vector<uint8>& public_exponent) { | 
| 273   DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || | 274   DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || | 
| 274          algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | 275          algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | 
| 275          algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); | 276          algorithm_id == blink::WebCryptoAlgorithmIdRsaOaep); | 
| 276   return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 277   return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | 
| 277       algorithm_id, | 278       algorithm_id, | 
| 278       new blink::WebCryptoRsaKeyGenParams( | 279       new blink::WebCryptoRsaKeyGenParams( | 
| 279           modulus_length, | 280           modulus_length, | 
| 280           webcrypto::Uint8VectorStart(public_exponent), | 281           webcrypto::Uint8VectorStart(public_exponent), | 
| 281           public_exponent.size())); | 282           public_exponent.size())); | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 313       return 128; | 314       return 128; | 
| 314     default: | 315     default: | 
| 315       NOTREACHED(); | 316       NOTREACHED(); | 
| 316       return 0; | 317       return 0; | 
| 317   } | 318   } | 
| 318 } | 319 } | 
| 319 | 320 | 
| 320 }  // namespace webcrypto | 321 }  // namespace webcrypto | 
| 321 | 322 | 
| 322 }  // namespace content | 323 }  // namespace content | 
| OLD | NEW | 
|---|