signed_web_bundle_id.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/web_package/signed_web_bundles/signed_web_bundle_id.h"
  5. #include "base/containers/span.h"
  6. #include "base/ranges/algorithm.h"
  7. #include "base/strings/string_piece.h"
  8. #include "base/strings/string_util.h"
  9. #include "base/strings/stringprintf.h"
  10. #include "base/types/expected.h"
  11. #include "components/base32/base32.h"
  12. namespace web_package {
  13. namespace {
  14. constexpr uint8_t kTypeSuffixLength = 3;
  15. constexpr uint8_t kTypeDevelopment[] = {0x00, 0x00, 0x02};
  16. constexpr uint8_t kTypeEd25519PublicKey[] = {0x00, 0x01, 0x02};
  17. static_assert(std::size(kTypeDevelopment) == kTypeSuffixLength);
  18. static_assert(std::size(kTypeEd25519PublicKey) == kTypeSuffixLength);
  19. } // namespace
  20. base::expected<SignedWebBundleId, std::string> SignedWebBundleId::Create(
  21. base::StringPiece encoded_id) {
  22. if (encoded_id.size() != kEncodedIdLength) {
  23. return base::unexpected(
  24. base::StringPrintf("The signed web bundle ID must be exactly %zu "
  25. "characters long, but was %zu characters long.",
  26. kEncodedIdLength, encoded_id.size()));
  27. }
  28. for (const char c : encoded_id) {
  29. if (!(base::IsAsciiLower(c) || (c >= '2' && c <= '7'))) {
  30. return base::unexpected(
  31. "The signed web bundle ID must only contain lowercase ASCII "
  32. "characters and digits between 2 and 7 (without any padding).");
  33. }
  34. }
  35. // Base32 decode the ID and convert it into an array.
  36. const std::string decoded_id_string =
  37. base32::Base32Decode(base::ToUpperASCII(encoded_id));
  38. if (decoded_id_string.size() != kDecodedIdLength) {
  39. return base::unexpected(
  40. "The signed web bundle ID could not be decoded from its base32 "
  41. "representation.");
  42. }
  43. std::array<uint8_t, kDecodedIdLength> decoded_id;
  44. base::ranges::copy(decoded_id_string, decoded_id.begin());
  45. auto type_suffix = base::make_span(decoded_id).last<kTypeSuffixLength>();
  46. if (base::ranges::equal(type_suffix, kTypeDevelopment)) {
  47. return SignedWebBundleId(Type::kDevelopment, encoded_id,
  48. std::move(decoded_id));
  49. }
  50. if (base::ranges::equal(type_suffix, kTypeEd25519PublicKey)) {
  51. return SignedWebBundleId(Type::kEd25519PublicKey, encoded_id,
  52. std::move(decoded_id));
  53. }
  54. return base::unexpected("The signed web bundle ID has an unknown type.");
  55. }
  56. SignedWebBundleId::SignedWebBundleId(
  57. Type type,
  58. base::StringPiece encoded_id,
  59. std::array<uint8_t, kDecodedIdLength> decoded_id)
  60. : type_(type),
  61. encoded_id_(encoded_id),
  62. decoded_id_(std::move(decoded_id)) {}
  63. SignedWebBundleId::SignedWebBundleId(const SignedWebBundleId& other) = default;
  64. SignedWebBundleId::~SignedWebBundleId() = default;
  65. Ed25519PublicKey SignedWebBundleId::GetEd25519PublicKey() const {
  66. CHECK_EQ(type(), Type::kEd25519PublicKey);
  67. return Ed25519PublicKey::Create(
  68. base::make_span(decoded_id_)
  69. .first<kDecodedIdLength - kTypeSuffixLength>());
  70. }
  71. } // namespace web_package