| 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" |
| 11 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 11 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 13 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 13 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 | 16 |
| 17 namespace webcrypto { | 17 namespace webcrypto { |
| 18 | 18 |
| 19 const uint8* Uint8VectorStart(const std::vector<uint8>& data) { | 19 const uint8_t* Uint8VectorStart(const std::vector<uint8_t>& data) { |
| 20 if (data.empty()) | 20 if (data.empty()) |
| 21 return NULL; | 21 return NULL; |
| 22 return &data[0]; | 22 return &data[0]; |
| 23 } | 23 } |
| 24 | 24 |
| 25 uint8* Uint8VectorStart(std::vector<uint8>* data) { | 25 uint8_t* Uint8VectorStart(std::vector<uint8_t>* data) { |
| 26 if (data->empty()) | 26 if (data->empty()) |
| 27 return NULL; | 27 return NULL; |
| 28 return &(*data)[0]; | 28 return &(*data)[0]; |
| 29 } | 29 } |
| 30 | 30 |
| 31 // This function decodes unpadded 'base64url' encoded data, as described in | 31 // This function decodes unpadded 'base64url' encoded data, as described in |
| 32 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. To do this, first | 32 // RFC4648 (http://www.ietf.org/rfc/rfc4648.txt) Section 5. To do this, first |
| 33 // change the incoming data to 'base64' encoding by applying the appropriate | 33 // change the incoming data to 'base64' encoding by applying the appropriate |
| 34 // transformation including adding padding if required, and then call a base64 | 34 // transformation including adding padding if required, and then call a base64 |
| 35 // decoder. | 35 // decoder. |
| 36 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { | 36 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { |
| 37 std::string base64EncodedText(input); | 37 std::string base64EncodedText(input); |
| 38 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); | 38 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '-', '+'); |
| 39 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); | 39 std::replace(base64EncodedText.begin(), base64EncodedText.end(), '_', '/'); |
| 40 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); | 40 base64EncodedText.append((4 - base64EncodedText.size() % 4) % 4, '='); |
| 41 return base::Base64Decode(base64EncodedText, output); | 41 return base::Base64Decode(base64EncodedText, output); |
| 42 } | 42 } |
| 43 | 43 |
| 44 // Returns an unpadded 'base64url' encoding of the input data, using the | 44 // Returns an unpadded 'base64url' encoding of the input data, using the |
| 45 // inverse of the process above. | 45 // inverse of the process above. |
| 46 std::string Base64EncodeUrlSafe(const base::StringPiece& input) { | 46 std::string Base64EncodeUrlSafe(const base::StringPiece& input) { |
| 47 std::string output; | 47 std::string output; |
| 48 base::Base64Encode(input, &output); | 48 base::Base64Encode(input, &output); |
| 49 std::replace(output.begin(), output.end(), '+', '-'); | 49 std::replace(output.begin(), output.end(), '+', '-'); |
| 50 std::replace(output.begin(), output.end(), '/', '_'); | 50 std::replace(output.begin(), output.end(), '/', '_'); |
| 51 output.erase(std::remove(output.begin(), output.end(), '='), output.end()); | 51 output.erase(std::remove(output.begin(), output.end(), '='), output.end()); |
| 52 return output; | 52 return output; |
| 53 } | 53 } |
| 54 | 54 |
| 55 std::string Base64EncodeUrlSafe(const std::vector<uint8>& input) { | 55 std::string Base64EncodeUrlSafe(const std::vector<uint8_t>& input) { |
| 56 const base::StringPiece string_piece( | 56 const base::StringPiece string_piece( |
| 57 reinterpret_cast<const char*>(Uint8VectorStart(input)), input.size()); | 57 reinterpret_cast<const char*>(Uint8VectorStart(input)), input.size()); |
| 58 return Base64EncodeUrlSafe(string_piece); | 58 return Base64EncodeUrlSafe(string_piece); |
| 59 } | 59 } |
| 60 | 60 |
| 61 struct JwkToWebCryptoUsage { | 61 struct JwkToWebCryptoUsage { |
| 62 const char* const jwk_key_op; | 62 const char* const jwk_key_op; |
| 63 const blink::WebCryptoKeyUsage webcrypto_usage; | 63 const blink::WebCryptoKeyUsage webcrypto_usage; |
| 64 }; | 64 }; |
| 65 | 65 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 Status CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages, | 256 Status CheckKeyCreationUsages(blink::WebCryptoKeyUsageMask all_possible_usages, |
| 257 blink::WebCryptoKeyUsageMask actual_usages) { | 257 blink::WebCryptoKeyUsageMask actual_usages) { |
| 258 if (!ContainsKeyUsages(all_possible_usages, actual_usages)) | 258 if (!ContainsKeyUsages(all_possible_usages, actual_usages)) |
| 259 return Status::ErrorCreateKeyBadUsages(); | 259 return Status::ErrorCreateKeyBadUsages(); |
| 260 return Status::Success(); | 260 return Status::Success(); |
| 261 } | 261 } |
| 262 | 262 |
| 263 } // namespace webcrypto | 263 } // namespace webcrypto |
| 264 | 264 |
| 265 } // namespace content | 265 } // namespace content |
| OLD | NEW |