Skip to content

Commit 8883be5

Browse files
Parse SNI as US_ASCII instead of ASCII
1 parent 4efbd7b commit 8883be5

File tree

7 files changed

+107
-33
lines changed

7 files changed

+107
-33
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#Thu, 02 Oct 2025 00:56:37 -0400
1+
#Sun, 12 Oct 2025 00:00:17 -0400
22
app-version.autoincrement=true
33
config.active=
4-
deployment.counter=49
5-
deployment.number=0.0.48
4+
deployment.counter=70
5+
deployment.number=0.0.69
66
javadoc.preview=true
77
netbeans.user=C\:\\Users\\NealShah\\AppData\\Roaming\\NetBeans\\8.2
88
platform.apis.defaults=J2ME-WS-1.0,MMAPI-1.2,OMDA_AP-1.0,JSR82-1.1,Logging-1.0,JSR211-1.0,SATSA-1.0,JSR234-1.0,AMS-1.0,JSR280-1.0,JSR75-1.0,JSR226-1.0,DEVICE_ACCESS-1.0,JSR256-1.2,JSR184-1.1,JSR239-1.0,WMA-2.0,WMA-1.1,JSR257-1.0,JSR179-1.0

nbproject/private/private.xml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
44
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
55
<group>
6-
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/mobileapplication1/CustomTlsClient.java</file>
7-
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/mobileapplication1/WebSocketConnection.java</file>
8-
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/mobileapplication1/ModernConnector.java</file>
9-
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/mobileapplication1/WebSocketClient.java</file>
10-
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/mobileapplication1/GuiTests.java</file>
6+
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/tech/alicesworld/ModernConnector/Midlet.java</file>
7+
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/tech/alicesworld/ModernConnector/CustomTlsClient.java</file>
8+
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/tech/alicesworld/ModernConnector/WebSocketConnection.java</file>
9+
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/tech/alicesworld/ModernConnector/Utils.java</file>
10+
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/tech/alicesworld/ModernConnector/ModernConnector.java</file>
11+
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/tech/alicesworld/ModernConnector/ModernSecureConnection.java</file>
12+
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/tech/alicesworld/ModernConnector/ModernHTTPSConnection.java</file>
1113
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/nbproject/build-impl.xml</file>
12-
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/mobileapplication1/Midlet.java</file>
13-
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/mobileapplication1/InMemoryPipe.java</file>
14+
<file>file:/C:/Users/NealShah/Documents/NetBeansProjects/j2mewebsocket/src/tech/alicesworld/ModernConnector/WebSocketClient.java</file>
1415
</group>
1516
</open-files>
1617
</project-private>

nbproject/project.properties

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
abilities=J2MEXMLRPC=1.0,ColorScreen,MIDP=2.1,ScreenHeight=320,JSR229=1.1.0,JSR226=1.0,SATSAJCRMI=1.0,JSR184=1.1,JSR82=1.1,JSR180=1.0.1,OBEX=1.0,SATSAPKI=1.0,SATSAAPDU=1.0,WMA=2.0,SATSACRYPTO=1.0,ScreenColorDepth=8,JSR238=1.0,JSR239=1.0,JSR234=1.0,JSR179=1.0.1,JSR177=1.0,JSR211=1.0,CLDC=1.1,J2MEWS=1.0,ScreenWidth=240,MMAPI=1.1,JSR172=1.0,JSR75=1.0,
1+
abilities=J2MEXMLRPC=1.0,ColorScreen,MIDP=2.0,ScreenHeight=320,JSR229=1.1.0,JSR226=1.0,SATSAJCRMI=1.0,JSR184=1.1,JSR82=1.1,JSR180=1.0.1,OBEX=1.0,SATSAPKI=1.0,SATSAAPDU=1.0,WMA=2.0,SATSACRYPTO=1.0,ScreenColorDepth=8,JSR238=1.0,JSR239=1.0,JSR234=1.0,JSR179=1.0.1,JSR177=1.0,JSR211=1.0,CLDC=1.1,J2MEWS=1.0,ScreenWidth=240,MMAPI=1.1,JSR172=1.0,JSR75=1.0
22
all.configurations=\
33
application.args=
44
application.description=
@@ -12,6 +12,7 @@ build.root.dir=build
1212
debug.level=debug
1313
debugger.timeout=
1414
deployment.copy.target=deploy
15+
deployment.deviceanywhere.device=-1
1516
deployment.instance=default
1617
deployment.jarurl=${dist.jar}
1718
deployment.method=NONE
@@ -22,7 +23,7 @@ dist.jar=MobileApplication1.jar
2223
dist.javadoc.dir=${dist.dir}/doc
2324
dist.root.dir=dist
2425
extra.classpath=
25-
file.reference.builtin.ks=${netbeans.user}/config/j2me/builtin.ks
26+
file.reference.keystore.ks=../../../keystore.ks
2627
file.reference.magicalbouncycastlethatworks.jar=magicalbouncycastlethatworks.jar
2728
filter.exclude.tests=false
2829
filter.excludes=
@@ -60,18 +61,18 @@ name=MobileApplication1
6061
no.dependencies=false
6162
nokiaS80.application.icon=
6263
obfuscated.classes.dir=${build.dir}/obfuscated
63-
obfuscation.custom=\ -keep public class tech.alicesworld.ModernConnector.** {\n public *;\n }
64+
obfuscation.custom=\ -keep public class tech.alicesworld.ModernConnector.** {\n public *;\n private *;\n }\n\n -keep public class org.bouncycastle.** {\n public *;\n private *;\n }\n
6465
obfuscation.level=9
6566
obfuscator.destjar=${build.dir}/obfuscated.jar
6667
obfuscator.srcjar=${build.dir}/before-obfuscation.jar
6768
platform.active=Oracle_Java_TM__Platform_Micro_Edition_SDK_3_4
6869
platform.active.description=Oracle Java(TM) Platform Micro Edition SDK 3.4
6970
platform.apis=JSR234-1.0,JSR257-1.0,JSR211-1.0,JSR75-1.0,J2ME-WS-1.0,JSR82-1.1,JSR239-1.0,JSR179-1.0,JSR184-1.1,MMAPI-1.2,JSR256-1.2,JSR226-1.0,SATSA-1.0,WMA-2.0,JSR280-1.0
70-
platform.bootclasspath=${platform.home}/lib/jsr205_2.0.jar:${platform.home}/lib/jsr177_1.0.jar:${platform.home}/lib/jsr082_1.1.jar:${platform.home}/lib/jsr234_1.0.jar:${platform.home}/lib/jsr257_1.0.jar:${platform.home}/lib/jsr135_1.2.jar:${platform.home}/lib/jsr226_1.0.jar:${platform.home}/lib/jsr256_1.2.jar:${platform.home}/lib/jsr75_1.0.jar:${platform.home}/lib/jsr211_1.0.jar:${platform.home}/lib/jsr239_1.0.jar:${platform.home}/lib/jsr172_1.0.jar:${platform.home}/lib/jsr280_1.0.jar:${platform.home}/lib/jsr184_1.1.jar:${platform.home}/lib/jsr179_1.0.jar:${platform.home}/lib/cldc_1.1.jar:${platform.home}/lib/midp_2.1.jar
71+
platform.bootclasspath=${platform.home}/lib/jsr205_2.0.jar:${platform.home}/lib/jsr177_1.0.jar:${platform.home}/lib/jsr082_1.1.jar:${platform.home}/lib/jsr234_1.0.jar:${platform.home}/lib/jsr257_1.0.jar:${platform.home}/lib/jsr135_1.2.jar:${platform.home}/lib/jsr226_1.0.jar:${platform.home}/lib/jsr256_1.2.jar:${platform.home}/lib/jsr75_1.0.jar:${platform.home}/lib/jsr211_1.0.jar:${platform.home}/lib/jsr239_1.0.jar:${platform.home}/lib/jsr172_1.0.jar:${platform.home}/lib/jsr280_1.0.jar:${platform.home}/lib/jsr184_1.1.jar:${platform.home}/lib/jsr179_1.0.jar:${platform.home}/lib/cldc_1.1.jar:${platform.home}/lib/midp_2.0.jar
7172
platform.configuration=CLDC-1.1
7273
platform.device=JavaMEPhone2
7374
platform.fat.jar=true
74-
platform.profile=MIDP-2.1
75+
platform.profile=MIDP-2.0
7576
platform.trigger=CLDC
7677
platform.type=UEI-1.0.1
7778
preprocessed.dir=${build.dir}/preprocessed
@@ -99,9 +100,9 @@ semc.application.uid=E4142690
99100
semc.certificate.path=
100101
semc.private.key.password=
101102
semc.private.key.path=
102-
sign.alias=trusted
103-
sign.enabled=true
104-
sign.keystore=${file.reference.builtin.ks}
103+
sign.alias=trustedmore
104+
sign.enabled=false
105+
sign.keystore=${file.reference.keystore.ks}
105106
src.dir=src
106107
use.emptyapis=true
107108
use.preprocessor=true

src/tech/alicesworld/ModernConnector/CustomTlsClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public CustomTlsClient() {
3636
public Hashtable getClientExtensions() throws IOException {
3737
Hashtable ext = TlsExtensionsUtils.ensureExtensionsInitialised(super.getClientExtensions());
3838
Vector serverNames = new Vector();
39-
serverNames.addElement(new ServerName(NameType.host_name, sniHost));
39+
serverNames.addElement(new LegacyServerName(NameType.host_name, sniHost));
4040
TlsExtensionsUtils.addServerNameExtension(ext, new ServerNameList(serverNames));
4141
return ext;
4242
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
package tech.alicesworld.ModernConnector;
8+
9+
import java.io.IOException;
10+
import java.io.OutputStream;
11+
import org.bouncycastle.crypto.tls.AlertDescription;
12+
import org.bouncycastle.crypto.tls.NameType;
13+
import org.bouncycastle.crypto.tls.TlsFatalAlert;
14+
import org.bouncycastle.crypto.tls.TlsUtils;
15+
16+
/**
17+
*
18+
* @author NealShah
19+
*/
20+
public class LegacyServerName extends org.bouncycastle.crypto.tls.ServerName {
21+
22+
public LegacyServerName(short s, Object o) {
23+
super(s, o);
24+
}
25+
26+
public void encode(OutputStream output) throws IOException
27+
{
28+
TlsUtils.writeUint8(nameType, output);
29+
30+
switch (nameType)
31+
{
32+
case NameType.host_name:
33+
byte[] asciiEncoding = ((String)name).getBytes("US_ASCII"); // Real bouncy castle uses ASCII here which breaks some devices
34+
if (asciiEncoding.length < 1)
35+
{
36+
throw new TlsFatalAlert(AlertDescription.internal_error);
37+
}
38+
TlsUtils.writeOpaque16(asciiEncoding, output);
39+
break;
40+
default:
41+
throw new TlsFatalAlert(AlertDescription.internal_error);
42+
}
43+
}
44+
45+
}

src/tech/alicesworld/ModernConnector/Midlet.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class Midlet extends MIDlet
4343

4444
SocketConnection socket;
4545
// form
46-
Form form = new Form("WebSocket Client");
46+
Form form = new Form("HTTPS Client");
4747

4848
// gauge
4949
Gauge gauge = new Gauge("Progress Bar", false, 20, 9);
@@ -62,7 +62,7 @@ public class Midlet extends MIDlet
6262
static final Command exitCommand
6363
= new Command("Exit", Command.STOP, 2);
6464
String currentMenu;
65-
TextField wsurl = new TextField("Websocket URL (host:port)", "wss://gateway.discord.gg/", 50, 0);
65+
TextField wsurl = new TextField("HTTPS URL (host:port)", "https://cloudflare.com:8443/cdn-cgi/trace", 50, 0);
6666

6767
// constructor.
6868
public Midlet() {
@@ -131,25 +131,33 @@ private static byte[] readAll(InputStream in, int max) throws IOException {
131131
* Test the Form component.
132132
*/
133133
public void testForm() {
134+
form.addCommand(sendCommand);
135+
form.addCommand(backCommand);
136+
form.setCommandListener(this);
137+
display.setCurrent(form);
138+
currentMenu = "form";
134139

135140
String reply = "";
136141
try {
137142
// ws = (WebSocketClient) ModernConnector.open("wss://gateway.discord.gg/");
138143
//
139144
// reply = ws.receiveMessageString();
140145
textfield.setLabel("hey");
141-
HttpConnection hc = (HttpConnection) ModernConnector.open("https://cloudflare.com/cdn-cgi/trace");
146+
HttpConnection hc = (HttpConnection) ModernConnector.open(wsurl.getString());
142147
hc.setRequestMethod(HttpConnection.GET);
143148
hc.setRequestProperty("Accept", "*/*");
144-
149+
textfield.setLabel("Did the accept thing");
145150
// 4) Get code (forces the send), then read body
146151
int code = hc.getResponseCode();
152+
textfield.setLabel("Sending accept");
147153
if (code < 200 || code >= 300) {
148154
// throw new IOException("HTTP " + code + " " + hc.getResponseMessage());
149155
}
150156

151157
InputStream in = hc.openInputStream();
158+
textfield.setLabel("Got input stream");
152159
String output = new String(readAll(in, Integer.MAX_VALUE));
160+
textfield.setLabel("Done");
153161
System.out.println(output);
154162
textfield.setLabel(output);
155163
} catch (IOException e) {
@@ -177,11 +185,7 @@ public void testForm() {
177185

178186
// Thread thread = new Thread(myRunnable);
179187
// thread.start();
180-
form.addCommand(sendCommand);
181-
form.addCommand(backCommand);
182-
form.setCommandListener(this);
183-
display.setCurrent(form);
184-
currentMenu = "form";
188+
185189
}
186190

187191
/**

src/tech/alicesworld/ModernConnector/ModernConnector.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* @author NealShah
1616
*/
1717
public class ModernConnector {
18-
18+
public static Object socketImplementation;
19+
1920
public static Object open(String connectionURL) throws IOException {
2021
String protocol = Utils.split(connectionURL, ':')[0];
2122
if (protocol.equals("ssl") || protocol.equals("tls")) {
@@ -24,7 +25,7 @@ public static Object open(String connectionURL) throws IOException {
2425
String host = endpointData[0];
2526
int port = 443;
2627
try {
27-
Integer.parseInt(endpointData[1]);
28+
port = Integer.parseInt(endpointData[1]);
2829
} catch (Exception e) {
2930
// Nothing
3031
}
@@ -37,7 +38,7 @@ public static Object open(String connectionURL) throws IOException {
3738
String host = endpointData[0];
3839
int port = 80;
3940
try {
40-
Integer.parseInt(endpointData[1]);
41+
port = Integer.parseInt(endpointData[1]);
4142
} catch (Exception e) {
4243
// Nothing
4344
}
@@ -53,7 +54,7 @@ public static Object open(String connectionURL) throws IOException {
5354
String host = endpointData[0];
5455
int port = 443;
5556
try {
56-
Integer.parseInt(endpointData[1]);
57+
port = Integer.parseInt(endpointData[1]);
5758
} catch (Exception e) {
5859
// Nothing
5960
}
@@ -69,15 +70,37 @@ public static Object open(String connectionURL) throws IOException {
6970
String host = endpointData[0];
7071
int port = 443;
7172
try {
72-
Integer.parseInt(endpointData[1]);
73+
port = Integer.parseInt(endpointData[1]);
7374
} catch (Exception e) {
7475
// Nothing
7576
}
77+
System.out.println("Using port " + port + " There was no " + endpointData[1]);
7678
SecureConnection socket = (SecureConnection) ModernConnector.open("tls://" + host + ":" + port);
7779
return new ModernHTTPSConnection(socket, connectionURL);
7880
}
81+
82+
if (protocol.equals("socket") && ModernConnector.socketImplementation != null) {
83+
String endpoint = Utils.split(connectionURL.substring(8), '/')[0];
84+
85+
String[] endpointData = Utils.split(endpoint, ':');
86+
String host = endpointData[0];
87+
int port = 443;
88+
try {
89+
port = Integer.parseInt(endpointData[1]);
90+
} catch (Exception e) {
91+
// Nothing
92+
}
93+
94+
return ((ModernConnector) ModernConnector.socketImplementation).openSocket(host, port);
95+
}
7996

8097
return Connector.open(connectionURL);
8198
}
8299

100+
public Object openSocket(String host, int port) {
101+
throw new UnsupportedOperationException("Only supported when new upstream is set");
102+
}
103+
104+
105+
83106
}

0 commit comments

Comments
 (0)