blob: 9fd44fc7d08d79961a4b67d3d53045e3ff647849 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2021 The Chromium Authors
Liquan (Max) Gu1c74b662021-07-23 13:48:232// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Andrew Paseltinerc3a359132024-08-28 16:00:465#include "content/browser/webauth/client_data_json.h"
6
Liquan (Max) Gu1c74b662021-07-23 13:48:237#include <jni.h>
Andrew Paseltinerc3a359132024-08-28 16:00:468
Liquan (Max) Gu1c74b662021-07-23 13:48:239#include <cstddef>
10#include <cstring>
11#include <utility>
12#include <vector>
13
14#include "base/android/jni_android.h"
15#include "base/android/jni_array.h"
Andrew Paseltinerc3a359132024-08-28 16:00:4616#include "base/android/jni_bytebuffer.h"
Liquan (Max) Gu1c74b662021-07-23 13:48:2317#include "base/android/jni_string.h"
18#include "base/android/scoped_java_ref.h"
19#include "base/containers/span.h"
Francois Pierre Doray00b861a2024-05-29 16:08:2420#include "third_party/blink/public/mojom/webauthn/authenticator.mojom.h"
Andrew Grieve6a66fe92024-05-29 15:36:0221
Andrew Grieveecb885bb2024-05-29 18:14:1922// Must come after all headers that specialize FromJniType() / ToJniType().
23#include "content/public/android/content_jni_headers/ClientDataJsonImpl_jni.h"
24
Liquan (Max) Gu1c74b662021-07-23 13:48:2325namespace content {
26namespace {
27
28void DeserializePaymentOptionsFromJavaByteBuffer(
29 JNIEnv* env,
30 const base::android::JavaRef<jobject>& jbuffer,
31 mojo::StructPtr<blink::mojom::PaymentOptions>* out) {
32 DCHECK(out);
33 if (jbuffer.is_null()) {
34 *out = nullptr;
35 return;
36 }
Andrew Paseltinerc3a359132024-08-28 16:00:4637 base::span<const uint8_t> span =
Andrew Grieve88b4c5fb2025-07-17 17:06:4738 base::android::JavaByteBufferToSpan(env, jbuffer);
Andrew Paseltinerc3a359132024-08-28 16:00:4639 if (span.empty()) {
Liquan (Max) Gu1c74b662021-07-23 13:48:2340 *out = nullptr;
41 return;
42 }
Andrew Paseltinerc3a359132024-08-28 16:00:4643 bool success = blink::mojom::PaymentOptions::Deserialize(span, out);
Liquan (Max) Gu1c74b662021-07-23 13:48:2344 DCHECK(success);
45}
46
47} // namespace
48
49static base::android::ScopedJavaLocalRef<jstring>
50JNI_ClientDataJsonImpl_BuildClientDataJson(
51 JNIEnv* env,
52 jint jclient_data_request_type,
53 const base::android::JavaParamRef<jstring>& jcaller_origin,
54 const base::android::JavaParamRef<jbyteArray>& jchallenge,
55 jboolean jis_cross_origin,
56 const base::android::JavaParamRef<jobject>& joptions_byte_buffer,
57 const base::android::JavaParamRef<jstring>& jrelying_party_id,
Nick Burris2c9cfd12023-02-07 20:23:4058 const base::android::JavaParamRef<jobject>& jtop_origin) {
Liquan (Max) Gu1c74b662021-07-23 13:48:2359 ClientDataRequestType type =
60 static_cast<ClientDataRequestType>(jclient_data_request_type);
61 std::string caller_origin =
62 base::android::ConvertJavaStringToUTF8(env, jcaller_origin);
63 std::vector<uint8_t> challenge;
64 base::android::JavaByteArrayToByteVector(env, jchallenge, &challenge);
65 bool is_cross_origin = static_cast<bool>(jis_cross_origin);
66
67 blink::mojom::PaymentOptionsPtr options;
68 DeserializePaymentOptionsFromJavaByteBuffer(env, joptions_byte_buffer,
69 &options);
70
71 std::string relying_party_id =
72 jrelying_party_id
73 ? base::android::ConvertJavaStringToUTF8(env, jrelying_party_id)
74 : "";
Liquan (Max) Gu1c74b662021-07-23 13:48:2375
Martin Kreichgauere255af062022-04-18 19:40:5676 ClientDataJsonParams client_data_json_params(
77 /*type=*/type, /*origin=*/url::Origin::Create(GURL(caller_origin)),
Andrew Grieve6a37bcd2024-09-26 21:47:4478 /*top_origin=*/url::Origin::FromJavaObject(env, jtop_origin),
Martin Kreichgauere255af062022-04-18 19:40:5679 /*challenge=*/challenge, /*is_cross_origin_iframe=*/is_cross_origin);
80 client_data_json_params.payment_options = std::move(options);
81 client_data_json_params.payment_rp = relying_party_id;
Liquan (Max) Gu1c74b662021-07-23 13:48:2382 std::string client_data_json =
Martin Kreichgauere255af062022-04-18 19:40:5683 BuildClientDataJson(std::move(client_data_json_params));
Liquan (Max) Gu1c74b662021-07-23 13:48:2384 return base::android::ConvertUTF8ToJavaString(env, client_data_json);
85}
86
87} // namespace content