2424
2525
2626#include "tcn.h"
27+ #include "apr_atomic.h"
2728#include "ssl_private.h"
2829#include "sslcredential.h"
2930
@@ -39,26 +40,38 @@ static void throw_openssl_error(JNIEnv* env, const char* msg) {
3940}
4041
4142static int tcn_SSL_CREDENTIAL_id_idx = -1 ;
42-
43- typedef char tcn_SSL_CREDENTIAL_id_requires_64bit_pointers [sizeof (void * ) >= sizeof (jlong ) ? 1 : -1 ];
43+ static volatile apr_uint32_t tcn_SSL_CREDENTIAL_next_id = 0 ;
4444
4545// The id is a scalar packed into the ex_data slot, so no free callback is needed.
4646jlong tcn_SSL_CREDENTIAL_get_id (const SSL_CREDENTIAL * cred ) {
4747 if (cred == NULL || tcn_SSL_CREDENTIAL_id_idx < 0 ) {
4848 return 0 ;
4949 }
50- return (jlong )(intptr_t ) SSL_CREDENTIAL_get_ex_data (cred , tcn_SSL_CREDENTIAL_id_idx );
50+ return (jlong )(uintptr_t ) SSL_CREDENTIAL_get_ex_data (cred , tcn_SSL_CREDENTIAL_id_idx );
51+ }
52+
53+ static SSL_CREDENTIAL * assign_id (JNIEnv * e , SSL_CREDENTIAL * cred ) {
54+ apr_uint32_t id ;
55+ do {
56+ id = apr_atomic_inc32 (& tcn_SSL_CREDENTIAL_next_id ) + 1 ;
57+ } while (id == 0 );
58+ if (!SSL_CREDENTIAL_set_ex_data (cred , tcn_SSL_CREDENTIAL_id_idx , (void * )(uintptr_t ) id )) {
59+ SSL_CREDENTIAL_free (cred );
60+ throw_openssl_error (e , "Failed to set SSL_CREDENTIAL id" );
61+ return NULL ;
62+ }
63+ return cred ;
5164}
5265#endif
5366
5467
5568
5669// Core SSL_CREDENTIAL functions
57- TCN_IMPLEMENT_CALL (jlong , SSLCredential , newX509Native )(TCN_STDARGS ) {
70+ TCN_IMPLEMENT_CALL (jlong , SSLCredential , newX509 )(TCN_STDARGS ) {
5871#ifdef OPENSSL_IS_BORINGSSL
5972 SSL_CREDENTIAL * cred = SSL_CREDENTIAL_new_x509 ();
6073 TCN_CHECK_NULL (cred , credential , 0 );
61- return (jlong )(intptr_t )cred ;
74+ return (jlong )(intptr_t )assign_id ( e , cred ) ;
6275#else
6376 tcn_ThrowUnsupportedOperationException (e , "SSL_CREDENTIAL API not available." );
6477 return 0 ;
@@ -86,29 +99,6 @@ TCN_IMPLEMENT_CALL(void, SSLCredential, free)(TCN_STDARGS, jlong cred) {
8699#endif
87100}
88101
89- TCN_IMPLEMENT_CALL (void , SSLCredential , setId0 )(TCN_STDARGS , jlong cred , jlong id ) {
90- #ifdef OPENSSL_IS_BORINGSSL
91- SSL_CREDENTIAL * c = (SSL_CREDENTIAL * )(intptr_t )cred ;
92- TCN_CHECK_NULL (c , credential , /* void */ );
93- if (id <= 0 ) {
94- tcn_ThrowIllegalArgumentException (e , "credential id must be positive" );
95- return ;
96- }
97- if (tcn_SSL_CREDENTIAL_get_id (c ) != 0 ) {
98- jclass ise = (* e )-> FindClass (e , "java/lang/IllegalStateException" );
99- if (ise != NULL ) {
100- (* e )-> ThrowNew (e , ise , "credential already has an id" );
101- }
102- return ;
103- }
104- if (!SSL_CREDENTIAL_set_ex_data (c , tcn_SSL_CREDENTIAL_id_idx , (void * )(intptr_t ) id )) {
105- throw_openssl_error (e , "Failed to set SSL_CREDENTIAL id" );
106- }
107- #else
108- tcn_ThrowUnsupportedOperationException (e , "SSL_CREDENTIAL API not available." );
109- #endif
110- }
111-
112102TCN_IMPLEMENT_CALL (jlong , SSLCredential , getId )(TCN_STDARGS , jlong cred ) {
113103#ifdef OPENSSL_IS_BORINGSSL
114104 SSL_CREDENTIAL * c = (SSL_CREDENTIAL * )(intptr_t )cred ;
@@ -340,14 +330,14 @@ TCN_IMPLEMENT_CALL(void, SSLCredential, setTrustAnchorId)(TCN_STDARGS, jlong cre
340330}
341331
342332// Delegated credentials
343- TCN_IMPLEMENT_CALL (jlong , SSLCredential , newDelegatedNative )(TCN_STDARGS ) {
333+ TCN_IMPLEMENT_CALL (jlong , SSLCredential , newDelegated )(TCN_STDARGS ) {
344334#ifdef OPENSSL_IS_BORINGSSL
345335 SSL_CREDENTIAL * credential = SSL_CREDENTIAL_new_delegated ();
346336 if (credential == NULL ) {
347337 throw_openssl_error (e , "Failed to create delegated SSL_CREDENTIAL" );
348338 return 0 ;
349339 }
350- return (jlong )(intptr_t )credential ;
340+ return (jlong )(intptr_t )assign_id ( e , credential ) ;
351341#else
352342 tcn_ThrowUnsupportedOperationException (e , "SSL_CREDENTIAL API not available." );
353343 return 0 ;
@@ -389,10 +379,9 @@ TCN_IMPLEMENT_CALL(void, SSLCredential, setDelegatedCredential)(TCN_STDARGS, jlo
389379// JNI Method Registration Table Begin
390380static const JNINativeMethod method_table [] = {
391381 // Core functions
392- { TCN_METHOD_TABLE_ENTRY (newX509Native , ( )J , SSLCredential ) },
382+ { TCN_METHOD_TABLE_ENTRY (newX509 , ( )J , SSLCredential ) },
393383 { TCN_METHOD_TABLE_ENTRY (upRef , (J )V , SSLCredential ) },
394384 { TCN_METHOD_TABLE_ENTRY (free , (J )V , SSLCredential ) },
395- { TCN_METHOD_TABLE_ENTRY (setId0 , (JJ )V , SSLCredential ) },
396385 { TCN_METHOD_TABLE_ENTRY (getId , (J )J , SSLCredential ) },
397386
398387 // Configuration
@@ -408,7 +397,7 @@ static const JNINativeMethod method_table[] = {
408397 { TCN_METHOD_TABLE_ENTRY (setTrustAnchorId , (J [B )V , SSLCredential ) },
409398
410399 // Delegated credentials
411- { TCN_METHOD_TABLE_ENTRY (newDelegatedNative , ( )J , SSLCredential ) },
400+ { TCN_METHOD_TABLE_ENTRY (newDelegated , ( )J , SSLCredential ) },
412401 { TCN_METHOD_TABLE_ENTRY (setDelegatedCredential , (J [B )V , SSLCredential ) }
413402};
414403
0 commit comments