Skip to content

Commit 86cecc7

Browse files
author
Alex Choulos
committed
Assign credential ids inside newX509/newDelegated so no existing native is renamed
1 parent 5acfd5f commit 86cecc7

3 files changed

Lines changed: 24 additions & 92 deletions

File tree

‎boringssl-static/src/test/java/io/netty/internal/tcnative/SSLCredentialIdTest.java‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import static org.junit.jupiter.api.Assertions.assertEquals;
2424
import static org.junit.jupiter.api.Assertions.assertNotEquals;
25-
import static org.junit.jupiter.api.Assertions.assertThrows;
2625
import static org.junit.jupiter.api.Assertions.assertTrue;
2726

2827
public class SSLCredentialIdTest {
@@ -58,29 +57,6 @@ public void newCredentialsHavePositiveDistinctIds() throws Exception {
5857
}
5958
}
6059

61-
@Test
62-
public void idCannotBeReassigned() throws Exception {
63-
long cred = SSLCredential.newX509();
64-
try {
65-
long id = SSLCredential.getId(cred);
66-
assertThrows(IllegalStateException.class, () -> SSLCredential.setId0(cred, id + 1));
67-
assertEquals(id, SSLCredential.getId(cred));
68-
} finally {
69-
SSLCredential.free(cred);
70-
}
71-
}
72-
73-
@Test
74-
public void nonPositiveIdIsRejected() throws Exception {
75-
long cred = SSLCredential.newX509();
76-
try {
77-
assertThrows(IllegalArgumentException.class, () -> SSLCredential.setId0(cred, 0));
78-
assertThrows(IllegalArgumentException.class, () -> SSLCredential.setId0(cred, -1));
79-
} finally {
80-
SSLCredential.free(cred);
81-
}
82-
}
83-
8460
@Test
8561
public void freshSslHasNoSelectedCredentialId() throws Exception {
8662
long ctx = SSLContext.make(SSL.SSL_PROTOCOL_TLSV1_2, SSL.SSL_MODE_SERVER);

‎openssl-classes/src/main/java/io/netty/internal/tcnative/SSLCredential.java‎

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package io.netty.internal.tcnative;
1717

18-
import java.util.concurrent.atomic.AtomicLong;
19-
2018
/**
2119
* SSL_CREDENTIAL management for BoringSSL.
2220
*
@@ -34,8 +32,6 @@
3432
*/
3533
public final class SSLCredential {
3634

37-
private static final AtomicLong NEXT_ID = new AtomicLong();
38-
3935
private SSLCredential() { }
4036

4137
/**
@@ -48,32 +44,7 @@ private SSLCredential() { }
4844
* @return the SSL_CREDENTIAL instance (SSL_CREDENTIAL *)
4945
* @throws Exception if an error occurred
5046
*/
51-
public static long newX509() throws Exception {
52-
return assignId(newX509Native());
53-
}
54-
55-
private static native long newX509Native() throws Exception;
56-
57-
private static long assignId(long cred) throws Exception {
58-
try {
59-
setId0(cred, NEXT_ID.incrementAndGet());
60-
} catch (Throwable t) {
61-
// Don't leak the credential if tagging fails.
62-
free(cred);
63-
throw t;
64-
}
65-
return cred;
66-
}
67-
68-
/**
69-
* Assign the id of an SSL_CREDENTIAL. A credential can only be assigned an id once.
70-
*
71-
* @param cred the SSL_CREDENTIAL instance (SSL_CREDENTIAL *)
72-
* @param id the id, which must be positive
73-
* @throws IllegalArgumentException if {@code id} is not positive
74-
* @throws IllegalStateException if the credential already has an id
75-
*/
76-
static native void setId0(long cred, long id);
47+
public static native long newX509() throws Exception;
7748

7849
/**
7950
* Get the id that was assigned to an SSL_CREDENTIAL when it was created by {@link #newX509()} or
@@ -224,11 +195,7 @@ private static long assignId(long cred) throws Exception {
224195
* @return the delegated SSL_CREDENTIAL instance (SSL_CREDENTIAL *)
225196
* @throws Exception if an error occurred
226197
*/
227-
public static long newDelegated() throws Exception {
228-
return assignId(newDelegatedNative());
229-
}
230-
231-
private static native long newDelegatedNative() throws Exception;
198+
public static native long newDelegated() throws Exception;
232199

233200
/**
234201
* Set the delegated credential for an SSL_CREDENTIAL.

‎openssl-dynamic/src/main/c/sslcredential.c‎

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
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

4142
static 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.
4646
jlong 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-
112102
TCN_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
390380
static 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

Comments
 (0)