xref: /aosp_15_r20/external/aws-crt-java/src/native/tls_ctx.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/common/string.h>
9 #include <aws/io/tls_channel_handler.h>
10 
11 #include "crt.h"
12 #include "java_class_ids.h"
13 
14 /* on 32-bit platforms, casting pointers to longs throws a warning we don't need */
15 #if UINTPTR_MAX == 0xffffffff
16 #    if defined(_MSC_VER)
17 #        pragma warning(push)
18 #        pragma warning(disable : 4305) /* 'type cast': truncation from 'jlong' to 'jni_tls_ctx_options *' */
19 #    else
20 #        pragma GCC diagnostic push
21 #        pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
22 #        pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
23 #    endif
24 #endif
25 
26 JNIEXPORT
27 jlong JNICALL
Java_software_amazon_awssdk_crt_io_TlsContext_tlsContextNew(JNIEnv * env,jclass jni_class,jlong jni_options)28     Java_software_amazon_awssdk_crt_io_TlsContext_tlsContextNew(JNIEnv *env, jclass jni_class, jlong jni_options) {
29     (void)jni_class;
30     aws_cache_jni_ids(env);
31 
32     struct aws_tls_ctx_options *options = (struct aws_tls_ctx_options *)jni_options;
33     if (!options) {
34         aws_jni_throw_runtime_exception(env, "TlsContext.tls_ctx_new: Invalid TlsOptions");
35         return (jlong)NULL;
36     }
37 
38     struct aws_allocator *allocator = aws_jni_get_allocator();
39     struct aws_tls_ctx *tls_ctx = aws_tls_client_ctx_new(allocator, options);
40     if (!tls_ctx) {
41         aws_jni_throw_runtime_exception(env, "TlsContext.tls_ctx_new: Failed to create new aws_tls_ctx");
42         return (jlong)NULL;
43     }
44     return (jlong)tls_ctx;
45 }
46 
47 JNIEXPORT
48 void JNICALL
Java_software_amazon_awssdk_crt_io_TlsContext_tlsContextDestroy(JNIEnv * env,jclass jni_class,jlong jni_ctx)49     Java_software_amazon_awssdk_crt_io_TlsContext_tlsContextDestroy(JNIEnv *env, jclass jni_class, jlong jni_ctx) {
50     (void)jni_class;
51     aws_cache_jni_ids(env);
52 
53     struct aws_tls_ctx *tls_ctx = (struct aws_tls_ctx *)jni_ctx;
54     if (!tls_ctx) {
55         return;
56     }
57 
58     aws_tls_ctx_release(tls_ctx);
59 }
60 
61 #if UINTPTR_MAX == 0xffffffff
62 #    if defined(_MSC_VER)
63 #        pragma warning(pop)
64 #    else
65 #        pragma GCC diagnostic pop
66 #    endif
67 #endif
68