xref: /aosp_15_r20/external/aws-crt-java/src/native/tls_connection_options.c (revision 3c7ae9de214676c52d19f01067dc1a404272dc11)
1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 
6 #include <jni.h>
7 
8 #include <aws/io/tls_channel_handler.h>
9 
10 #include "crt.h"
11 #include "java_class_ids.h"
12 
13 /* on 32-bit platforms, casting pointers to longs throws a warning we don't need */
14 #if UINTPTR_MAX == 0xffffffff
15 #    if defined(_MSC_VER)
16 #        pragma warning(push)
17 #        pragma warning(disable : 4305) /* 'type cast': truncation from 'jlong' to 'jni_tls_ctx_options *' */
18 #    else
19 #        pragma GCC diagnostic push
20 #        pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
21 #        pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
22 #    endif
23 #endif
24 
25 JNIEXPORT
Java_software_amazon_awssdk_crt_io_TlsConnectionOptions_tlsConnectionOptionsNew(JNIEnv * env,jclass jni_class,jstring jni_alpn,jstring jni_server_name,jint jni_timeout_ms,jlong native_context)26 jlong JNICALL Java_software_amazon_awssdk_crt_io_TlsConnectionOptions_tlsConnectionOptionsNew(
27     JNIEnv *env,
28     jclass jni_class,
29     jstring jni_alpn,
30     jstring jni_server_name,
31     jint jni_timeout_ms,
32     jlong native_context) {
33     (void)jni_class;
34     aws_cache_jni_ids(env);
35 
36     struct aws_tls_ctx *ctx = (struct aws_tls_ctx *)native_context;
37     if (!ctx) {
38         aws_jni_throw_illegal_argument_exception(env, "TlsContext cannot be null for TlsConnectionOptions");
39         return (jlong)0;
40     }
41 
42     struct aws_allocator *allocator = aws_jni_get_allocator();
43     struct aws_tls_connection_options *options =
44         (struct aws_tls_connection_options *)aws_mem_calloc(allocator, 1, sizeof(struct aws_tls_connection_options));
45 
46     aws_tls_connection_options_init_from_ctx(options, ctx);
47     if (jni_alpn) {
48         const char *alpn_chars = (*env)->GetStringUTFChars(env, jni_alpn, NULL);
49         if (!alpn_chars) {
50             aws_jni_throw_runtime_exception(env, "Failed to get alpnList string");
51             goto on_error;
52         }
53         int err = aws_tls_connection_options_set_alpn_list(options, allocator, alpn_chars);
54         (*env)->ReleaseStringUTFChars(env, jni_alpn, alpn_chars);
55         if (err) {
56             goto on_error;
57         }
58     }
59     if (jni_server_name) {
60         struct aws_byte_cursor server_name_cur = aws_jni_byte_cursor_from_jstring_acquire(env, jni_server_name);
61         int err = aws_tls_connection_options_set_server_name(options, allocator, &server_name_cur);
62         aws_jni_byte_cursor_from_jstring_release(env, jni_server_name, server_name_cur);
63         if (err) {
64             goto on_error;
65         }
66     }
67     if (jni_timeout_ms) {
68         options->timeout_ms = jni_timeout_ms;
69     }
70 
71     return (jlong)options;
72 on_error:
73     aws_tls_connection_options_clean_up(options);
74     aws_mem_release(allocator, options);
75     return (jlong)0;
76 }
77 
78 JNIEXPORT
Java_software_amazon_awssdk_crt_io_TlsConnectionOptions_tlsConnectionOptionsDestroy(JNIEnv * env,jclass jni_class,jlong jni_options)79 void JNICALL Java_software_amazon_awssdk_crt_io_TlsConnectionOptions_tlsConnectionOptionsDestroy(
80     JNIEnv *env,
81     jclass jni_class,
82     jlong jni_options) {
83     (void)jni_class;
84     aws_cache_jni_ids(env);
85 
86     struct aws_tls_connection_options *options = (struct aws_tls_connection_options *)jni_options;
87     if (!options) {
88         return;
89     }
90 
91     aws_tls_connection_options_clean_up(options);
92 
93     struct aws_allocator *allocator = aws_jni_get_allocator();
94     aws_mem_release(allocator, options);
95 }
96 
97 #if UINTPTR_MAX == 0xffffffff
98 #    if defined(_MSC_VER)
99 #        pragma warning(pop)
100 #    else
101 #        pragma GCC diagnostic pop
102 #    endif
103 #endif
104