| // Copyright 2021 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "content/browser/webauth/client_data_json.h" | |
| #include <jni.h> | |
| #include <cstddef> | |
| #include <cstring> | |
| #include <utility> | |
| #include <vector> | |
| #include "base/android/jni_android.h" | |
| #include "base/android/jni_array.h" | |
| #include "base/android/jni_bytebuffer.h" | |
| #include "base/android/jni_string.h" | |
| #include "base/android/scoped_java_ref.h" | |
| #include "base/containers/span.h" | |
| #include "third_party/blink/public/mojom/webauthn/authenticator.mojom.h" | |
| // Must come after all headers that specialize FromJniType() / ToJniType(). | |
| #include "content/public/android/content_jni_headers/ClientDataJsonImpl_jni.h" | |
| namespace content { | |
| namespace { | |
| void DeserializePaymentOptionsFromJavaByteBuffer( | |
| JNIEnv* env, | |
| const base::android::JavaRef<jobject>& jbuffer, | |
| mojo::StructPtr<blink::mojom::PaymentOptions>* out) { | |
| CHECK(out, base::NotFatalUntil::M159); | |
| if (jbuffer.is_null()) { | |
| *out = nullptr; | |
| return; | |
| } | |
| base::span<const uint8_t> span = | |
| base::android::JavaByteBufferToSpan(env, jbuffer); | |
| if (span.empty()) { | |
| *out = nullptr; | |
| return; | |
| } | |
| bool success = blink::mojom::PaymentOptions::Deserialize(span, out); | |
| CHECK(success, base::NotFatalUntil::M159); | |
| } | |
| } // namespace | |
| static base::android::ScopedJavaLocalRef<jstring> | |
| JNI_ClientDataJsonImpl_BuildClientDataJson( | |
| JNIEnv* env, | |
| int32_t jclient_data_request_type, | |
| const base::android::JavaRef<jstring>& jcaller_origin, | |
| const base::android::JavaRef<jbyteArray>& jchallenge, | |
| bool jis_cross_origin, | |
| const base::android::JavaRef<jobject>& joptions_byte_buffer, | |
| const base::android::JavaRef<jstring>& jrelying_party_id, | |
| const base::android::JavaRef<jobject>& jtop_origin) { | |
| webauthn::ClientDataRequestType type = | |
| static_cast<webauthn::ClientDataRequestType>(jclient_data_request_type); | |
| std::string caller_origin = | |
| base::android::ConvertJavaStringToUTF8(env, jcaller_origin); | |
| std::vector<uint8_t> challenge; | |
| base::android::JavaByteArrayToByteVector(env, jchallenge, &challenge); | |
| bool is_cross_origin = jis_cross_origin; | |
| blink::mojom::PaymentOptionsPtr options; | |
| DeserializePaymentOptionsFromJavaByteBuffer(env, joptions_byte_buffer, | |
| &options); | |
| std::string relying_party_id = | |
| jrelying_party_id | |
| ? base::android::ConvertJavaStringToUTF8(env, jrelying_party_id) | |
| : ""; | |
| webauthn::ClientDataJsonParams client_data_json_params( | |
| /*type=*/type, /*origin=*/url::Origin::Create(GURL(caller_origin)), | |
| /*top_origin=*/url::Origin::FromJavaObject(env, jtop_origin), | |
| /*challenge=*/challenge, /*is_cross_origin_iframe=*/is_cross_origin); | |
| std::string client_data_json = BuildClientDataJsonWithPayment( | |
| std::move(client_data_json_params), std::move(options), relying_party_id); | |
| return base::android::ConvertUTF8ToJavaString(env, client_data_json); | |
| } | |
| } // namespace content | |
| DEFINE_JNI(ClientDataJsonImpl) |