| 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" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 namespace webcrypto { | 14 namespace webcrypto { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 bool IsHashAlgorithm(blink::WebCryptoAlgorithmId alg_id) { | |
| 19 return alg_id == blink::WebCryptoAlgorithmIdSha1 || | |
| 20 alg_id == blink::WebCryptoAlgorithmIdSha224 || | |
| 21 alg_id == blink::WebCryptoAlgorithmIdSha256 || | |
| 22 alg_id == blink::WebCryptoAlgorithmIdSha384 || | |
| 23 alg_id == blink::WebCryptoAlgorithmIdSha512; | |
| 24 } | |
| 25 | |
| 26 } // namespace | 18 } // namespace |
| 27 | 19 |
| 28 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { | 20 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { |
| 29 if (data.empty()) | 21 if (data.empty()) |
| 30 return NULL; | 22 return NULL; |
| 31 return &data[0]; | 23 return &data[0]; |
| 32 } | 24 } |
| 33 | 25 |
| 34 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size) { | 26 void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size) { |
| 35 DCHECK_LE(new_size, buffer->byteLength()); | 27 DCHECK_LE(new_size, buffer->byteLength()); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 57 // transformation including adding padding if required, and then call a base64 | 49 // transformation including adding padding if required, and then call a base64 |
| 58 // decoder. | 50 // decoder. |
| 59 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { | 51 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { |
| 60 std::string base64EncodedText(input); | 52 std::string base64EncodedText(input); |
| 61 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); | 53 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); |
| 62 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); | 54 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); |
| 63 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); | 55 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); |
| 64 return base::Base64Decode(base64EncodedText, output); | 56 return base::Base64Decode(base64EncodedText, output); |
| 65 } | 57 } |
| 66 | 58 |
| 59 bool IsHashAlgorithm(blink::WebCryptoAlgorithmId alg_id) { |
| 60 return alg_id == blink::WebCryptoAlgorithmIdSha1 || |
| 61 alg_id == blink::WebCryptoAlgorithmIdSha224 || |
| 62 alg_id == blink::WebCryptoAlgorithmIdSha256 || |
| 63 alg_id == blink::WebCryptoAlgorithmIdSha384 || |
| 64 alg_id == blink::WebCryptoAlgorithmIdSha512; |
| 65 } |
| 66 |
| 67 blink::WebCryptoAlgorithm GetInnerHashAlgorithm( | 67 blink::WebCryptoAlgorithm GetInnerHashAlgorithm( |
| 68 const blink::WebCryptoAlgorithm& algorithm) { | 68 const blink::WebCryptoAlgorithm& algorithm) { |
| 69 if (algorithm.hmacParams()) | 69 DCHECK(!algorithm.isNull()); |
| 70 return algorithm.hmacParams()->hash(); | 70 switch (algorithm.id()) { |
| 71 if (algorithm.hmacKeyParams()) | 71 case blink::WebCryptoAlgorithmIdHmac: |
| 72 return algorithm.hmacKeyParams()->hash(); | 72 if (algorithm.hmacParams()) |
| 73 if (algorithm.rsaSsaParams()) | 73 return algorithm.hmacParams()->hash(); |
| 74 return algorithm.rsaSsaParams()->hash(); | 74 else if (algorithm.hmacKeyParams()) |
| 75 if (algorithm.rsaOaepParams()) | 75 return algorithm.hmacKeyParams()->hash(); |
| 76 return algorithm.rsaOaepParams()->hash(); | 76 break; |
| 77 case blink::WebCryptoAlgorithmIdRsaOaep: |
| 78 if (algorithm.rsaOaepParams()) |
| 79 return algorithm.rsaOaepParams()->hash(); |
| 80 break; |
| 81 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: |
| 82 if (algorithm.rsaSsaParams()) |
| 83 return algorithm.rsaSsaParams()->hash(); |
| 84 break; |
| 85 default: |
| 86 break; |
| 87 } |
| 77 return blink::WebCryptoAlgorithm::createNull(); | 88 return blink::WebCryptoAlgorithm::createNull(); |
| 78 } | 89 } |
| 79 | 90 |
| 80 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { | 91 blink::WebCryptoAlgorithm CreateAlgorithm(blink::WebCryptoAlgorithmId id) { |
| 81 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); | 92 return blink::WebCryptoAlgorithm::adoptParamsAndCreate(id, NULL); |
| 82 } | 93 } |
| 83 | 94 |
| 84 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( | 95 blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashId( |
| 85 blink::WebCryptoAlgorithmId hash_id) { | 96 blink::WebCryptoAlgorithmId hash_id) { |
| 86 DCHECK(IsHashAlgorithm(hash_id)); | 97 DCHECK(IsHashAlgorithm(hash_id)); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 return 128; | 175 return 128; |
| 165 default: | 176 default: |
| 166 NOTREACHED(); | 177 NOTREACHED(); |
| 167 return 0; | 178 return 0; |
| 168 } | 179 } |
| 169 } | 180 } |
| 170 | 181 |
| 171 } // namespace webcrypto | 182 } // namespace webcrypto |
| 172 | 183 |
| 173 } // namespace content | 184 } // namespace content |
| OLD | NEW |