chromium/net/device_bound_sessions/jwk_utils_unittest.cc

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/device_bound_sessions/jwk_utils.h"

#include "base/json/json_reader.h"
#include "net/device_bound_sessions/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net::device_bound_sessions {

namespace {
TEST(JWKUtilsTest, InvalidSpki) {
  base::Value::Dict converted =
      ConvertPkeySpkiToJwk(crypto::SignatureVerifier::ECDSA_SHA256, {});
  EXPECT_TRUE(converted.empty());
}

TEST(JWKUtilsTest, UnsupportedAlgo) {
  static constexpr uint8_t kSpki[] = {
      0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0x9B, 0xED, 0x6F,
      0x30, 0x89, 0xAA, 0x27, 0xD1, 0xEF, 0x09, 0x4B, 0x7A, 0xEF, 0xD0, 0x2C,
      0x8F, 0xDA, 0x13, 0x48, 0x3A, 0x5E, 0x2B, 0xB5, 0xB8, 0x37, 0x06, 0xD4,
      0x0E, 0x2B, 0xAD, 0x89, 0x21, 0xE6, 0x09, 0xCF, 0x09, 0x9E, 0x86, 0x60,
      0x97, 0x59, 0x3E, 0x30, 0xC4, 0x78, 0x57, 0x7B, 0x5D, 0x1C, 0x0F, 0x18,
      0xAF, 0x24, 0x11, 0x4E, 0x30, 0x46, 0x07, 0xF1, 0x4F, 0xFE, 0xE9, 0x0B,
      0xE9, 0x0E, 0xE2, 0xA0, 0xB5, 0xB4, 0xF5, 0x7B, 0x1C, 0xBF, 0xC4, 0x4F,
      0x6D, 0xDC, 0x39, 0x6C, 0x1A, 0xE7, 0x7A, 0xFF, 0xDC, 0x80, 0x4D, 0x49,
      0x8C, 0x98, 0x1D, 0xBB, 0x74, 0x17, 0x17, 0x4C, 0xE9, 0x09, 0x4D, 0xEF,
      0xD8, 0x05, 0x7C, 0x6C, 0x45, 0x73, 0xD0, 0x22, 0xC1, 0xA3, 0x41, 0x70,
      0xFC, 0xC0, 0xB6, 0xC1, 0x81, 0xBA, 0x38, 0x1D, 0x95, 0x3D, 0x0E, 0xAA,
      0x59, 0x8E, 0x93, 0xD2, 0x64, 0x98, 0xB9, 0x0C, 0x6B, 0x50, 0xC7, 0x6D,
      0x42, 0xD5, 0xE1, 0xF3, 0x69, 0xBD, 0x44, 0x78, 0xF7, 0xE4, 0x9F, 0x87,
      0x44, 0x02, 0x28, 0xBB, 0xE0, 0xAA, 0xD0, 0x99, 0x98, 0xBE, 0x5A, 0xD6,
      0xF2, 0x17, 0x17, 0xFB, 0x74, 0xF3, 0xBE, 0xFA, 0xE8, 0x80, 0xA7, 0x33,
      0xFF, 0x0B, 0xDE, 0xB0, 0x8F, 0xE5, 0xD2, 0x62, 0xCB, 0xD0, 0x01, 0xF6,
      0x10, 0xBB, 0xA2, 0x34, 0x91, 0x55, 0xC2, 0x87, 0xA0, 0x6B, 0x25, 0x52,
      0xD8, 0x70, 0x1A, 0x8A, 0x96, 0x63, 0xA8, 0x38, 0x22, 0x99, 0x41, 0xE2,
      0x64, 0xE9, 0xE4, 0x63, 0xA1, 0xD3, 0x13, 0xB0, 0x01, 0xED, 0x9F, 0xA0,
      0x05, 0x03, 0xB2, 0x5A, 0x16, 0x44, 0x1B, 0xC6, 0x6D, 0xF6, 0x79, 0xB4,
      0xA1, 0x63, 0xA8, 0x2D, 0xDB, 0xEE, 0x54, 0xAA, 0x70, 0xEF, 0x2C, 0x45,
      0xC8, 0x7F, 0x42, 0xEB, 0x9F, 0xCA, 0x98, 0xF9, 0xB8, 0x34, 0xDB, 0x83,
      0x11, 0x02, 0x03, 0x01, 0x00, 0x01};

  base::Value::Dict converted =
      ConvertPkeySpkiToJwk(crypto::SignatureVerifier::RSA_PKCS1_SHA1, kSpki);
  EXPECT_TRUE(converted.empty());
}

TEST(JWKUtilsTest, RS256) {
  auto [spki, jwk] = GetRS256SpkiAndJwkForTesting();

  base::Value expected = base::JSONReader::Read(jwk).value();
  base::Value::Dict converted =
      ConvertPkeySpkiToJwk(crypto::SignatureVerifier::RSA_PKCS1_SHA256, spki);
  EXPECT_EQ(converted, expected);
}

TEST(JWKUtilsTest, ES256) {
  static constexpr uint8_t kSpki[] = {
      0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02,
      0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03,
      0x42, 0x00, 0x04, 0xEB, 0xF1, 0x81, 0xDA, 0xFA, 0x10, 0xD2, 0xAA, 0xE8,
      0x32, 0x1E, 0x8E, 0x94, 0x31, 0x42, 0x15, 0x2F, 0xD2, 0x26, 0xB8, 0x90,
      0x8B, 0x53, 0xD3, 0xBE, 0xF0, 0x4E, 0x84, 0x66, 0x77, 0x6C, 0x72, 0x21,
      0xE8, 0x0E, 0x25, 0x42, 0xF2, 0x13, 0xB4, 0xB1, 0x1F, 0xF0, 0x9D, 0xD4,
      0xA0, 0x84, 0x47, 0xB9, 0x56, 0x06, 0xF1, 0x85, 0x1D, 0x0F, 0xA1, 0xD3,
      0x1C, 0xA3, 0xCD, 0x48, 0xC4, 0x21, 0x61};

  static constexpr char kJwk[] = R"json({
      "kty": "EC",
      "crv": "P-256",
      "x": "6_GB2voQ0qroMh6OlDFCFS_SJriQi1PTvvBOhGZ3bHI",
      "y": "IegOJULyE7SxH_Cd1KCER7lWBvGFHQ-h0xyjzUjEIWE"})json";

  base::Value expected = base::JSONReader::Read(kJwk).value();
  base::Value::Dict converted =
      ConvertPkeySpkiToJwk(crypto::SignatureVerifier::ECDSA_SHA256, kSpki);
  EXPECT_EQ(converted, expected);
}
}  // namespace

}  // namespace net::device_bound_sessions