@@ -918,105 +918,20 @@ TCN_IMPLEMENT_CALL(jboolean, SSLContext, setCertificateBio)(TCN_STDARGS, jlong c |
918 | 918 | #endif // OPENSSL_IS_BORINGSSL |
919 | 919 | } |
920 | 920 |
|
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, |
1012 | 922 | jint selectorFailureBehavior) |
1013 | 923 | { |
1014 | 924 | tcn_ssl_ctxt_t *c = J2P(ctx, tcn_ssl_ctxt_t *); |
1015 | 925 |
|
1016 | 926 | TCN_CHECK_NULL(c, ctx, /* void */); |
1017 | 927 |
|
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); |
1020 | 935 |
|
1021 | 936 | // depending on if it's client mode or not we need to call different functions. |
1022 | 937 | if (c->mode == SSL_MODE_CLIENT) { |
@@ -1027,7 +942,7 @@ TCN_IMPLEMENT_CALL(void, SSLContext, setNpnProtos)(TCN_STDARGS, jlong ctx, jobje |
1027 | 942 | } |
1028 | 943 | } |
1029 | 944 |
|
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, |
1031 | 946 | jint selectorFailureBehavior) |
1032 | 947 | { |
1033 | 948 | // Only supported with GCC |
@@ -1043,15 +958,20 @@ TCN_IMPLEMENT_CALL(void, SSLContext, setAlpnProtos)(TCN_STDARGS, jlong ctx, jobj |
1043 | 958 |
|
1044 | 959 | TCN_CHECK_NULL(c, ctx, /* void */); |
1045 | 960 |
|
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 | + |
1048 | 969 |
|
1049 | 970 | // depending on if it's client mode or not we need to call different functions. |
1050 | 971 | if (c->mode == SSL_MODE_CLIENT) { |
1051 | 972 | SSL_CTX_set_alpn_protos(c->ctx, c->alpn_proto_data, c->alpn_proto_len); |
1052 | 973 | } else { |
1053 | 974 | SSL_CTX_set_alpn_select_cb(c->ctx, tcn_SSL_callback_alpn_select_proto, (void *) c); |
1054 | | - |
1055 | 975 | } |
1056 | 976 | } |
1057 | 977 | #endif |
@@ -2710,8 +2630,8 @@ static const JNINativeMethod fixed_method_table[] = { |
2710 | 2630 | { TCN_METHOD_TABLE_ENTRY(setVerify, (JII)V, SSLContext) }, |
2711 | 2631 | { TCN_METHOD_TABLE_ENTRY(setCertificate, (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z, SSLContext) }, |
2712 | 2632 | { 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) }, |
2715 | 2635 | { TCN_METHOD_TABLE_ENTRY(setSessionCacheMode, (JJ)J, SSLContext) }, |
2716 | 2636 | { TCN_METHOD_TABLE_ENTRY(getSessionCacheMode, (J)J, SSLContext) }, |
2717 | 2637 | { TCN_METHOD_TABLE_ENTRY(setSessionCacheTimeout, (JJ)J, SSLContext) }, |
|