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 "components/webcrypto/jwk.h" | 5 #include "components/webcrypto/jwk.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 reinterpret_cast<const char*>(value.bytes()), | 361 reinterpret_cast<const char*>(value.bytes()), |
362 value.byte_length()))); | 362 value.byte_length()))); |
363 } | 363 } |
364 | 364 |
365 void JwkWriter::ToJson(std::vector<uint8_t>* utf8_bytes) const { | 365 void JwkWriter::ToJson(std::vector<uint8_t>* utf8_bytes) const { |
366 std::string json; | 366 std::string json; |
367 base::JSONWriter::Write(dict_, &json); | 367 base::JSONWriter::Write(dict_, &json); |
368 utf8_bytes->assign(json.begin(), json.end()); | 368 utf8_bytes->assign(json.begin(), json.end()); |
369 } | 369 } |
370 | 370 |
371 Status ReadSecretKeyNoExpectedAlg(const CryptoData& key_data, | |
372 bool expected_extractable, | |
373 blink::WebCryptoKeyUsageMask expected_usages, | |
374 std::vector<uint8_t>* raw_key_data, | |
375 JwkReader* jwk) { | |
376 Status status = jwk->Init(key_data, expected_extractable, expected_usages, | |
377 "oct", std::string()); | |
378 if (status.IsError()) | |
379 return status; | |
380 | |
381 std::string jwk_k_value; | |
382 status = jwk->GetBytes("k", &jwk_k_value); | |
383 if (status.IsError()) | |
384 return status; | |
385 raw_key_data->assign(jwk_k_value.begin(), jwk_k_value.end()); | |
386 | |
387 return Status::Success(); | |
388 } | |
389 | |
390 void WriteSecretKeyJwk(const CryptoData& raw_key_data, | |
391 const std::string& algorithm, | |
392 bool extractable, | |
393 blink::WebCryptoKeyUsageMask usages, | |
394 std::vector<uint8_t>* jwk_key_data) { | |
395 JwkWriter writer(algorithm, extractable, usages, "oct"); | |
396 writer.SetBytes("k", raw_key_data); | |
397 writer.ToJson(jwk_key_data); | |
398 } | |
399 | |
400 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { | 371 bool Base64DecodeUrlSafe(const std::string& input, std::string* output) { |
401 // The JSON web signature spec specifically says that padding is omitted. | 372 // The JSON web signature spec specifically says that padding is omitted. |
402 if (input.find_first_of("+/=") != std::string::npos) | 373 if (input.find_first_of("+/=") != std::string::npos) |
403 return false; | 374 return false; |
404 | 375 |
405 std::string base64_encoded_text(input); | 376 std::string base64_encoded_text(input); |
406 std::replace(base64_encoded_text.begin(), base64_encoded_text.end(), '-', | 377 std::replace(base64_encoded_text.begin(), base64_encoded_text.end(), '-', |
407 '+'); | 378 '+'); |
408 std::replace(base64_encoded_text.begin(), base64_encoded_text.end(), '_', | 379 std::replace(base64_encoded_text.begin(), base64_encoded_text.end(), '_', |
409 '/'); | 380 '/'); |
(...skipping 16 matching lines...) Expand all Loading... |
426 return Base64EncodeUrlSafe(string_piece); | 397 return Base64EncodeUrlSafe(string_piece); |
427 } | 398 } |
428 | 399 |
429 Status GetWebCryptoUsagesFromJwkKeyOpsForTest( | 400 Status GetWebCryptoUsagesFromJwkKeyOpsForTest( |
430 const base::ListValue* key_ops, | 401 const base::ListValue* key_ops, |
431 blink::WebCryptoKeyUsageMask* usages) { | 402 blink::WebCryptoKeyUsageMask* usages) { |
432 return GetWebCryptoUsagesFromJwkKeyOps(key_ops, usages); | 403 return GetWebCryptoUsagesFromJwkKeyOps(key_ops, usages); |
433 } | 404 } |
434 | 405 |
435 } // namespace webcrypto | 406 } // namespace webcrypto |
OLD | NEW |