xref: /aosp_15_r20/external/aws-crt-java/src/native/tls_key_operation.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 'aws_pkcs11_lib *' */
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 
Java_software_amazon_awssdk_crt_io_TlsKeyOperation_tlsKeyOperationComplete(JNIEnv * env,jclass jni_class,jlong jni_operation,jbyteArray jni_output_data)25 JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_io_TlsKeyOperation_tlsKeyOperationComplete(
26     JNIEnv *env,
27     jclass jni_class,
28     jlong jni_operation,
29     jbyteArray jni_output_data) {
30 
31     (void)jni_class;
32     aws_cache_jni_ids(env);
33 
34     struct aws_tls_key_operation *operation = (struct aws_tls_key_operation *)jni_operation;
35 
36     struct aws_byte_cursor output_data = aws_jni_byte_cursor_from_jbyteArray_acquire(env, jni_output_data);
37     if (output_data.ptr == NULL) {
38         AWS_LOGF_ERROR(
39             AWS_LS_COMMON_IO,
40             "aws_tls_key_operation tlsKeyOperationComplete: Could not allocate byte cursor from Java byte array");
41         aws_tls_key_operation_complete_with_error(operation, AWS_ERROR_INVALID_ARGUMENT);
42         return;
43     }
44 
45     aws_tls_key_operation_complete(operation, output_data);
46 
47     aws_jni_byte_cursor_from_jbyteArray_release(env, jni_output_data, output_data);
48 }
49 
Java_software_amazon_awssdk_crt_io_TlsKeyOperation_tlsKeyOperationCompleteExceptionally(JNIEnv * env,jclass jni_class,jlong jni_operation,jthrowable jni_throwable)50 JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_io_TlsKeyOperation_tlsKeyOperationCompleteExceptionally(
51     JNIEnv *env,
52     jclass jni_class,
53     jlong jni_operation,
54     jthrowable jni_throwable) {
55 
56     (void)jni_class;
57     aws_cache_jni_ids(env);
58 
59     struct aws_tls_key_operation *operation = (struct aws_tls_key_operation *)jni_operation;
60 
61     int error_code = 0;
62     if ((*env)->IsInstanceOf(env, jni_throwable, crt_runtime_exception_properties.crt_runtime_exception_class)) {
63         error_code = (*env)->GetIntField(env, jni_throwable, crt_runtime_exception_properties.error_code_field_id);
64     }
65 
66     if (error_code == 0) {
67         AWS_LOGF_ERROR(
68             AWS_LS_COMMON_IO,
69             "aws_tls_key_operation tlsKeyOperationCompleteExceptionally: "
70             "Completed with exception but with an error code of zero");
71         error_code = AWS_ERROR_UNKNOWN;
72     }
73 
74     aws_tls_key_operation_complete_with_error(operation, error_code);
75 }
76