• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/device_bound_sessions/test_support.h"
6 
7 #include "base/json/json_writer.h"
8 #include "base/strings/string_util.h"
9 #include "net/test/embedded_test_server/http_request.h"
10 #include "net/test/embedded_test_server/http_response.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace net::device_bound_sessions {
14 
15 namespace {
16 
RequestHandler(const GURL & base_url,const net::test_server::HttpRequest & request)17 std::unique_ptr<net::test_server::HttpResponse> RequestHandler(
18     const GURL& base_url,
19     const net::test_server::HttpRequest& request) {
20   auto response = std::make_unique<net::test_server::BasicHttpResponse>();
21   response->set_code(net::HTTP_OK);
22   if (request.relative_url == "/dbsc_required") {
23     response->AddCustomHeader(
24         "Sec-Session-Registration",
25         "(RS256 "
26         "ES256);challenge=\"challenge_value\";path=\"dbsc_register_session\"");
27     return response;
28   } else if (request.relative_url == "/dbsc_register_session") {
29     response->AddCustomHeader("Set-Cookie", "auth_cookie=abcdef0123;");
30 
31     const auto registration_response =
32         base::Value::Dict()
33             .Set("session_identifier", "session_id")
34             .Set("refresh_url",
35                  base_url.Resolve("/dbsc_refresh_session").spec())
36             .Set("credentials",
37                  base::Value::List().Append(base::Value::Dict()
38                                                 .Set("type", "cookie")
39                                                 .Set("name", "auth_cookie")
40                                                 .Set("attributes", "")));
41 
42     std::optional<std::string> json = base::WriteJson(registration_response);
43     EXPECT_TRUE(json.has_value());
44     response->set_content(*json);
45     return response;
46   }
47   return nullptr;
48 }
49 
50 }  // namespace
51 
52 std::pair<base::span<const uint8_t>, std::string>
GetRS256SpkiAndJwkForTesting()53 GetRS256SpkiAndJwkForTesting() {
54   static constexpr uint8_t kSpki[] = {
55       0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
56       0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00,
57       0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xB8, 0x72, 0x09,
58       0xEA, 0xD7, 0x1D, 0x84, 0xD4, 0x9B, 0x22, 0xA1, 0xE8, 0x6A, 0x5F, 0xB1,
59       0x6C, 0x03, 0x8B, 0x45, 0xDA, 0xF7, 0xE5, 0xF9, 0x0E, 0x95, 0xF2, 0x43,
60       0xE6, 0x38, 0x19, 0x2B, 0x23, 0x29, 0x22, 0xA7, 0xE6, 0xF6, 0xEC, 0xB6,
61       0x43, 0x61, 0xFB, 0x5F, 0x4C, 0xEA, 0xB8, 0x77, 0x9E, 0x43, 0x18, 0x76,
62       0x2D, 0x16, 0x84, 0x44, 0xA1, 0x29, 0xA6, 0x93, 0xC3, 0x02, 0x1A, 0x11,
63       0x1F, 0x2A, 0x3D, 0xDC, 0xE9, 0x44, 0xAE, 0x61, 0x9F, 0xC1, 0xDE, 0xDB,
64       0xEA, 0x04, 0x01, 0xE5, 0x2A, 0xAB, 0x55, 0x67, 0xA6, 0x3D, 0xB3, 0x97,
65       0xA7, 0x15, 0x02, 0x7B, 0xCA, 0x4C, 0x44, 0xA1, 0x4D, 0x2B, 0xB9, 0xBE,
66       0xE3, 0x96, 0xC3, 0x17, 0x42, 0x4D, 0xCA, 0x60, 0xA8, 0x30, 0xC5, 0xD0,
67       0xC9, 0x64, 0xD8, 0x39, 0xB0, 0x91, 0xA8, 0x22, 0x94, 0xA0, 0x61, 0x6B,
68       0xE6, 0xF4, 0xD9, 0x64, 0x82, 0x17, 0xB3, 0x27, 0xF6, 0xDA, 0x3D, 0xEF,
69       0xD8, 0x05, 0x87, 0x90, 0x1C, 0xE5, 0xB5, 0xB3, 0xB5, 0x41, 0x0E, 0xFC,
70       0x45, 0xAD, 0x64, 0xCA, 0xB1, 0x39, 0x10, 0x63, 0x32, 0x67, 0x7E, 0x88,
71       0x95, 0x0F, 0xFD, 0x8E, 0xCE, 0x5A, 0xF7, 0x5B, 0x60, 0x85, 0xA3, 0xB0,
72       0x48, 0x26, 0x10, 0x19, 0xDA, 0x0A, 0xC5, 0xD3, 0x78, 0x6E, 0x0B, 0x86,
73       0x78, 0x55, 0xB4, 0xA8, 0xFD, 0x1C, 0x81, 0x8A, 0x33, 0x18, 0x40, 0x1A,
74       0x5F, 0x75, 0x87, 0xD1, 0x05, 0x2B, 0x2B, 0x53, 0x1F, 0xAD, 0x8E, 0x22,
75       0xB3, 0xEE, 0x1C, 0xA1, 0x03, 0x97, 0xF1, 0xE0, 0x88, 0x0F, 0x98, 0xAF,
76       0x05, 0x37, 0xB3, 0xC3, 0x95, 0x1C, 0x34, 0xDE, 0x39, 0xEB, 0x85, 0x12,
77       0xEC, 0x3D, 0x77, 0x27, 0xA7, 0x5C, 0xEA, 0x39, 0x24, 0xD5, 0xE9, 0x49,
78       0xCF, 0x97, 0x88, 0x4A, 0xF4, 0x01, 0x4F, 0xA4, 0x7E, 0x77, 0x57, 0x7F,
79       0x73, 0x02, 0x03, 0x01, 0x00, 0x01};
80 
81   static constexpr char kJwkTemplate[] = R"json({
82       "kty": "RSA",
83       "n": "<n>",
84       "e": "AQAB"})json";
85 
86   static constexpr char kRsaN[] =
87       "uHIJ6tcdhNSbIqHoal-xbAOLRdr35fkOlfJD5jgZKyMpIqfm9uy2Q2H7X0zquHeeQxh2LRaE"
88       "RKEpppPDAhoRHyo93OlErmGfwd7b6gQB5SqrVWemPbOXpxUCe8pMRKFNK7m-45bDF0JNymCo"
89       "MMXQyWTYObCRqCKUoGFr5vTZZIIXsyf22j3v2AWHkBzltbO1QQ78Ra1kyrE5EGMyZ36IlQ_9"
90       "js5a91tghaOwSCYQGdoKxdN4bguGeFW0qP0cgYozGEAaX3WH0QUrK1MfrY4is-4coQOX8eCI"
91       "D5ivBTezw5UcNN4564US7D13J6dc6jkk1elJz5eISvQBT6R-d1d_cw";
92 
93   std::string jwk = kJwkTemplate;
94   base::ReplaceFirstSubstringAfterOffset(&jwk, 0, "<n>", kRsaN);
95 
96   return {kSpki, jwk};
97 }
98 
GetTestRequestHandler(const GURL & base_url)99 EmbeddedTestServer::HandleRequestCallback GetTestRequestHandler(
100     const GURL& base_url) {
101   return base::BindRepeating(&RequestHandler, base_url);
102 }
103 
104 }  // namespace net::device_bound_sessions
105