GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Replace c code that translate to wireformat to java (#610) · netty/netty-tcnative@b22b503

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • openssl-dynamic/src/main
    • c
      • ssl_private.h
      • sslcontext.c
    • java/io/netty/internal/tcnative
      • SSLContext.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎openssl-dynamic/src/main/c/ssl_private.h‎

Copy file name to clipboardExpand all lines: openssl-dynamic/src/main/c/ssl_private.h
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@
117117

118118
#define SSL_TO_APR_ERROR(X) (APR_OS_START_USERERR + 1000 + X)
119119

120-
#define MAX_ALPN_NPN_PROTO_SIZE 65535
121-
122120
extern const char* TCN_UNKNOWN_AUTH_METHOD;
123121

124122
/* ECC: make sure we have at least 1.0.0 */
Collapse file

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

Copy file name to clipboardExpand all lines: openssl-dynamic/src/main/c/sslcontext.c
+19-99Lines changed: 19 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -918,105 +918,20 @@ TCN_IMPLEMENT_CALL(jboolean, SSLContext, setCertificateBio)(TCN_STDARGS, jlong c
918918
#endif // OPENSSL_IS_BORINGSSL
919919
}
920920

921-
// Convert protos to wire format
922-
static int initProtocols(JNIEnv *e, unsigned char **proto_data,
923-
unsigned int *proto_len, jobjectArray protos) {
924-
int i;
925-
unsigned char *p_data = NULL;
926-
unsigned char *p_data_tmp = NULL;
927-
928-
// We start with allocate 128 bytes which should be good enough for most use-cases while still be pretty low.
929-
// We will call realloc to increase this if needed.
930-
size_t p_data_size = 128;
931-
size_t p_data_len = 0;
932-
jstring proto_string = NULL;
933-
const char *proto_chars = NULL;
934-
size_t proto_chars_len;
935-
int cnt;
936-
937-
if (protos == NULL) {
938-
// Guard against NULL protos.
939-
return -1;
940-
}
941-
942-
cnt = (*e)->GetArrayLength(e, protos);
943-
944-
if (cnt == 0) {
945-
// if cnt is 0 we not need to continue and can just fail fast.
946-
return -1;
947-
}
948-
949-
p_data = (unsigned char *) OPENSSL_malloc(p_data_size);
950-
if (p_data == NULL) {
951-
// Not enough memory?
952-
return -1;
953-
}
954-
955-
for (i = 0; i < cnt; ++i) {
956-
proto_string = (jstring) (*e)->GetObjectArrayElement(e, protos, i);
957-
proto_chars = (*e)->GetStringUTFChars(e, proto_string, JNI_FALSE);
958-
959-
proto_chars_len = strlen(proto_chars);
960-
if (proto_chars_len > 0 && proto_chars_len <= MAX_ALPN_NPN_PROTO_SIZE) {
961-
// We need to add +1 as each protocol is prefixed by it's length (unsigned char).
962-
// For all except of the last one we already have the extra space as everything is
963-
// delimited by ','.
964-
p_data_len += 1 + proto_chars_len;
965-
if (p_data_len > p_data_size) {
966-
// double size
967-
p_data_size <<= 1;
968-
p_data_tmp = realloc(p_data, p_data_size);
969-
if (p_data_tmp == NULL) {
970-
// Not enough memory?
971-
(*e)->ReleaseStringUTFChars(e, proto_string, proto_chars);
972-
973-
// If realloc failed we need to still free the original pointer to ensure we not leak any memory.
974-
// See https://linux.die.net/man/3/realloc
975-
OPENSSL_free(p_data);
976-
p_data = NULL;
977-
break;
978-
} else {
979-
p_data = p_data_tmp;
980-
p_data_tmp = NULL;
981-
}
982-
}
983-
// Write the length of the protocol and then increment before memcpy the protocol itself.
984-
*p_data = proto_chars_len;
985-
++p_data;
986-
memcpy(p_data, proto_chars, proto_chars_len);
987-
p_data += proto_chars_len;
988-
}
989-
990-
// Release the string to prevent memory leaks
991-
(*e)->ReleaseStringUTFChars(e, proto_string, proto_chars);
992-
}
993-
994-
if (p_data == NULL) {
995-
// Something went wrong so update the proto_len and return -1
996-
*proto_len = 0;
997-
return -1;
998-
} else {
999-
if (*proto_data != NULL) {
1000-
// Free old data
1001-
OPENSSL_free(*proto_data);
1002-
}
1003-
// Decrement pointer again as we incremented it while creating the protocols in wire format.
1004-
p_data -= p_data_len;
1005-
*proto_data = p_data;
1006-
*proto_len = p_data_len;
1007-
return 0;
1008-
}
1009-
}
1010-
1011-
TCN_IMPLEMENT_CALL(void, SSLContext, setNpnProtos)(TCN_STDARGS, jlong ctx, jobjectArray next_protos,
921+
TCN_IMPLEMENT_CALL(void, SSLContext, setNpnProtos0)(TCN_STDARGS, jlong ctx, jbyteArray next_protos,
1012922
jint selectorFailureBehavior)
1013923
{
1014924
tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *);
1015925

1016926
TCN_CHECK_NULL(c, ctx, /* void */);
1017927

1018-
if (initProtocols(e, &c->next_proto_data, &c->next_proto_len, next_protos) == 0) {
1019-
c->next_selector_failure_behavior = selectorFailureBehavior;
928+
if (next_protos != NULL) {
929+
OPENSSL_free(c->next_proto_data);
930+
931+
int next_protos_len = (*e)->GetArrayLength(e, next_protos);
932+
c->next_proto_data = OPENSSL_malloc(next_protos_len);
933+
c->next_proto_len = next_protos_len;
934+
(*e)->GetByteArrayRegion(e, next_protos, 0, next_protos_len, (jbyte*) c->next_proto_data);
1020935

1021936
// depending on if it's client mode or not we need to call different functions.
1022937
if (c->mode == SSL_MODE_CLIENT) {
@@ -1027,7 +942,7 @@ TCN_IMPLEMENT_CALL(void, SSLContext, setNpnProtos)(TCN_STDARGS, jlong ctx, jobje
1027942
}
1028943
}
1029944

1030-
TCN_IMPLEMENT_CALL(void, SSLContext, setAlpnProtos)(TCN_STDARGS, jlong ctx, jobjectArray alpn_protos,
945+
TCN_IMPLEMENT_CALL(void, SSLContext, setAlpnProtos0)(TCN_STDARGS, jlong ctx, jbyteArray alpn_protos,
1031946
jint selectorFailureBehavior)
1032947
{
1033948
// Only supported with GCC
@@ -1043,15 +958,20 @@ TCN_IMPLEMENT_CALL(void, SSLContext, setAlpnProtos)(TCN_STDARGS, jlong ctx, jobj
1043958

1044959
TCN_CHECK_NULL(c, ctx, /* void */);
1045960

1046-
if (initProtocols(e, &c->alpn_proto_data, &c->alpn_proto_len, alpn_protos) == 0) {
1047-
c->alpn_selector_failure_behavior = selectorFailureBehavior;
961+
if (alpn_protos != NULL) {
962+
OPENSSL_free(c->alpn_proto_data);
963+
964+
int alpn_protos_len = (*e)->GetArrayLength(e, alpn_protos);
965+
c->alpn_proto_data = OPENSSL_malloc(alpn_protos_len);
966+
c->alpn_proto_len = alpn_protos_len;
967+
(*e)->GetByteArrayRegion(e, alpn_protos, 0, alpn_protos_len, (jbyte*) c->alpn_proto_data);
968+
1048969

1049970
// depending on if it's client mode or not we need to call different functions.
1050971
if (c->mode == SSL_MODE_CLIENT) {
1051972
SSL_CTX_set_alpn_protos(c->ctx, c->alpn_proto_data, c->alpn_proto_len);
1052973
} else {
1053974
SSL_CTX_set_alpn_select_cb(c->ctx, tcn_SSL_callback_alpn_select_proto, (void *) c);
1054-
1055975
}
1056976
}
1057977
#endif
@@ -2710,8 +2630,8 @@ static const JNINativeMethod fixed_method_table[] = {
27102630
{ TCN_METHOD_TABLE_ENTRY(setVerify, (JII)V, SSLContext) },
27112631
{ TCN_METHOD_TABLE_ENTRY(setCertificate, (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z, SSLContext) },
27122632
{ TCN_METHOD_TABLE_ENTRY(setCertificateBio, (JJJLjava/lang/String;)Z, SSLContext) },
2713-
{ TCN_METHOD_TABLE_ENTRY(setNpnProtos, (J[Ljava/lang/String;I)V, SSLContext) },
2714-
{ TCN_METHOD_TABLE_ENTRY(setAlpnProtos, (J[Ljava/lang/String;I)V, SSLContext) },
2633+
{ TCN_METHOD_TABLE_ENTRY(setNpnProtos0, (J[BI)V, SSLContext) },
2634+
{ TCN_METHOD_TABLE_ENTRY(setAlpnProtos0, (J[BI)V, SSLContext) },
27152635
{ TCN_METHOD_TABLE_ENTRY(setSessionCacheMode, (JJ)J, SSLContext) },
27162636
{ TCN_METHOD_TABLE_ENTRY(getSessionCacheMode, (J)J, SSLContext) },
27172637
{ TCN_METHOD_TABLE_ENTRY(setSessionCacheTimeout, (JJ)J, SSLContext) },
Collapse file

‎openssl-dynamic/src/main/java/io/netty/internal/tcnative/SSLContext.java‎

Copy file name to clipboardExpand all lines: openssl-dynamic/src/main/java/io/netty/internal/tcnative/SSLContext.java
+37-2Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@
3232

3333
package io.netty.internal.tcnative;
3434

35+
import java.io.ByteArrayOutputStream;
36+
import java.io.IOException;
37+
import java.nio.charset.StandardCharsets;
38+
3539
public final class SSLContext {
40+
private static final int MAX_ALPN_NPN_PROTO_SIZE = 65535;
3641

3742
private SSLContext() { }
3843

@@ -529,14 +534,40 @@ public static void setSessionTicketKeys(long ctx, SessionTicketKey[] keys) {
529534
*/
530535
public static native void setSniHostnameMatcher(long ctx, SniHostNameMatcher matcher);
531536

537+
private static byte[] protocolsToWireFormat(String[] protocols) {
538+
ByteArrayOutputStream out = new ByteArrayOutputStream();
539+
try {
540+
for (String p : protocols) {
541+
byte[] bytes = p.getBytes(StandardCharsets.US_ASCII);
542+
if (bytes.length <= MAX_ALPN_NPN_PROTO_SIZE) {
543+
out.write(bytes.length);
544+
out.write(bytes);
545+
}
546+
}
547+
return out.toByteArray();
548+
} catch (IOException e) {
549+
throw new IllegalStateException(e);
550+
} finally {
551+
try {
552+
out.close();
553+
} catch (IOException ignore) {
554+
// ignore
555+
}
556+
}
557+
}
558+
532559
/**
533560
* Set next protocol for next protocol negotiation extension
534561
* @param ctx Server context to use.
535562
* @param nextProtos protocols in priority order
536563
* @param selectorFailureBehavior see {@link SSL#SSL_SELECTOR_FAILURE_NO_ADVERTISE}
537564
* and {@link SSL#SSL_SELECTOR_FAILURE_CHOOSE_MY_LAST_PROTOCOL}
538565
*/
539-
public static native void setNpnProtos(long ctx, String[] nextProtos, int selectorFailureBehavior);
566+
public static void setNpnProtos(long ctx, String[] nextProtos, int selectorFailureBehavior) {
567+
setNpnProtos0(ctx, protocolsToWireFormat(nextProtos), selectorFailureBehavior);
568+
}
569+
570+
private static native void setNpnProtos0(long ctx, byte[] nextProtos, int selectorFailureBehavior);
540571

541572
/**
542573
* Set application layer protocol for application layer protocol negotiation extension
@@ -545,7 +576,11 @@ public static void setSessionTicketKeys(long ctx, SessionTicketKey[] keys) {
545576
* @param selectorFailureBehavior see {@link SSL#SSL_SELECTOR_FAILURE_NO_ADVERTISE}
546577
* and {@link SSL#SSL_SELECTOR_FAILURE_CHOOSE_MY_LAST_PROTOCOL}
547578
*/
548-
public static native void setAlpnProtos(long ctx, String[] alpnProtos, int selectorFailureBehavior);
579+
public static void setAlpnProtos(long ctx, String[] alpnProtos, int selectorFailureBehavior) {
580+
setAlpnProtos0(ctx, protocolsToWireFormat(alpnProtos), selectorFailureBehavior);
581+
}
582+
583+
private static native void setAlpnProtos0(long ctx, byte[] alpnProtos, int selectorFailureBehavior);
549584

550585
/**
551586
* Set length of the DH to use.

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.