Skip to content

Commit 0f352ec

Browse files
committed
Update SSLFDProxy to implement SSLSocketListener
Previously SSLFDProxy.c was accessing the fields in SSLFDProxy class directly using JNI to update handshakeComplete and add SSL alert events into inboundAlerts and outboundAlerts. To make it easier to investigate SSL alert issues, SSLFDProxy has been updated to implement SSLSocketListener then SSLFDProxy.c will call SSLSocketListener methods to perform the above operations.
1 parent 9e39f0c commit 0f352ec

File tree

5 files changed

+125
-59
lines changed

5 files changed

+125
-59
lines changed

base/src/main/java/org/mozilla/jss/nss/SSLFDProxy.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import org.mozilla.jss.crypto.X509Certificate;
66
import org.mozilla.jss.pkcs11.PK11Cert;
77
import org.mozilla.jss.ssl.SSLAlertEvent;
8+
import org.mozilla.jss.ssl.SSLHandshakeCompletedEvent;
9+
import org.mozilla.jss.ssl.SSLSocketListener;
810
import org.mozilla.jss.util.GlobalRefProxy;
911

10-
public class SSLFDProxy extends PRFDProxy {
12+
public class SSLFDProxy extends PRFDProxy implements SSLSocketListener {
1113
public PK11Cert clientCert;
1214
public GlobalRefProxy globalRef;
1315

@@ -60,4 +62,19 @@ public int invokeCertAuthHandler() {
6062
public int invokeBadCertHandler(int error) {
6163
return badCertHandler.check(this, error);
6264
}
65+
66+
@Override
67+
public void handshakeCompleted(SSLHandshakeCompletedEvent event) {
68+
handshakeComplete = true;
69+
}
70+
71+
@Override
72+
public void alertReceived(SSLAlertEvent event) {
73+
inboundAlerts.add(event);
74+
}
75+
76+
@Override
77+
public void alertSent(SSLAlertEvent event) {
78+
outboundAlerts.add(event);
79+
}
6380
}

base/src/main/java/org/mozilla/jss/ssl/SSLHandshakeCompletedEvent.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
package org.mozilla.jss.ssl;
1111

12-
import java.net.*;
13-
import java.util.*;
12+
import java.net.SocketException;
13+
import java.util.EventObject;
1414

15+
import org.mozilla.jss.nss.SSLFDProxy;
1516
import org.mozilla.jss.ssl.javax.JSSEngine;
1617

1718
/*
@@ -30,6 +31,10 @@ public SSLHandshakeCompletedEvent(SSLSocket socket) {
3031
super(socket);
3132
}
3233

34+
public SSLHandshakeCompletedEvent(SSLFDProxy proxy) {
35+
super(proxy);
36+
}
37+
3338
public SSLHandshakeCompletedEvent(JSSEngine engine) {
3439
super(engine);
3540
}

native/src/main/native/org/mozilla/jss/nss/SSLFDProxy.c

Lines changed: 94 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,6 @@ JSS_NSS_getEventArrayList(JNIEnv *env, jobject sslfd_proxy, const char *which, j
7575
return PR_SUCCESS;
7676
}
7777

78-
PRStatus
79-
JSS_NSS_getSSLAlertReceivedList(JNIEnv *env, jobject sslfd_proxy, jobject *list)
80-
{
81-
return JSS_NSS_getEventArrayList(env, sslfd_proxy, "inboundAlerts", list);
82-
}
83-
84-
PRStatus
85-
JSS_NSS_getSSLAlertSentList(JNIEnv *env, jobject sslfd_proxy, jobject *list)
86-
{
87-
return JSS_NSS_getEventArrayList(env, sslfd_proxy, "outboundAlerts", list);
88-
}
89-
9078
PRStatus
9179
JSS_NSS_getGlobalRef(JNIEnv *env, jobject sslfd_proxy, jobject *global_ref)
9280
{
@@ -104,61 +92,41 @@ JSS_NSS_getGlobalRef(JNIEnv *env, jobject sslfd_proxy, jobject *global_ref)
10492
return PR_SUCCESS;
10593
}
10694

107-
PRStatus
108-
JSS_NSS_addSSLAlert(JNIEnv *env, jobject sslfd_proxy, jobject list,
109-
const SSLAlert *alert)
95+
jobject
96+
JSS_NSS_createSSLAlert(JNIEnv *env, jobject sslfd_proxy, const SSLAlert *alert)
11097
{
11198
jclass eventClass;
11299
jmethodID eventConstructor;
113100
jobject event;
114101

115-
jclass eventListClass;
116-
jmethodID arrayListAdd;
117-
118-
PR_ASSERT(env != NULL && sslfd_proxy != NULL && list != NULL && alert != NULL);
102+
PR_ASSERT(env != NULL && sslfd_proxy != NULL && alert != NULL);
119103

120104
/* Build the new alert event object (org.mozilla.jss.ssl.SSLAlertEvent). */
121105
eventClass = (*env)->FindClass(env, SSL_ALERT_EVENT_CLASS);
122106
if (eventClass == NULL) {
123-
return PR_FAILURE;
107+
return NULL;
124108
}
125109

126110
eventConstructor = (*env)->GetMethodID(env, eventClass, "<init>",
127111
"(L" SSLFD_PROXY_CLASS_NAME ";II)V");
128112
if (eventConstructor == NULL) {
129-
return PR_FAILURE;
113+
return NULL;
130114
}
131115

132116
event = (*env)->NewObject(env, eventClass, eventConstructor,
133117
sslfd_proxy, (int)alert->level,
134118
(int)alert->description);
135-
if (event == NULL) {
136-
return PR_FAILURE;
137-
}
138-
139-
/* Add it to the event list. */
140-
eventListClass = (*env)->GetObjectClass(env, list);
141-
if (eventListClass == NULL) {
142-
return PR_FAILURE;
143-
}
144-
145-
arrayListAdd = (*env)->GetMethodID(env, eventListClass, "add",
146-
"(Ljava/lang/Object;)Z");
147-
if (arrayListAdd == NULL) {
148-
return PR_FAILURE;
149-
}
150-
151-
// We ignore the return code: ArrayList.add() always returns true.
152-
(void)(*env)->CallBooleanMethod(env, list, arrayListAdd, event);
153-
return PR_SUCCESS;
119+
return event;
154120
}
155121

156122
void
157123
JSSL_SSLFDAlertReceivedCallback(const PRFileDesc *fd, void *arg, const SSLAlert *alert)
158124
{
159125
JNIEnv *env;
160126
jobject sslfd_proxy = (jobject)arg;
161-
jobject list;
127+
jclass sslfdProxyClass;
128+
jmethodID alertReceivedMethod;
129+
jobject event;
162130

163131
if (fd == NULL || arg == NULL || alert == NULL || JSS_javaVM == NULL) {
164132
return;
@@ -168,21 +136,41 @@ JSSL_SSLFDAlertReceivedCallback(const PRFileDesc *fd, void *arg, const SSLAlert
168136
return;
169137
}
170138

171-
if (JSS_NSS_getSSLAlertReceivedList(env, sslfd_proxy, &list) != PR_SUCCESS) {
139+
sslfdProxyClass = (*env)->GetObjectClass(env, sslfd_proxy);
140+
141+
if (sslfdProxyClass == NULL) {
142+
return;
143+
}
144+
145+
alertReceivedMethod = (*env)->GetMethodID(
146+
env,
147+
sslfdProxyClass,
148+
"alertReceived",
149+
"(L" SSL_ALERT_EVENT_CLASS ";)V");
150+
151+
if (alertReceivedMethod == NULL) {
172152
return;
173153
}
174154

175-
if (JSS_NSS_addSSLAlert(env, sslfd_proxy, list, alert) != PR_SUCCESS) {
155+
// event = new SSLAlertEvent()
156+
event = JSS_NSS_createSSLAlert(env, sslfd_proxy, alert);
157+
158+
if (event == NULL) {
176159
return;
177160
}
161+
162+
// sslfd_proxy.alertReceived(event)
163+
(void)(*env)->CallVoidMethod(env, sslfd_proxy, alertReceivedMethod, event);
178164
}
179165

180166
void
181167
JSSL_SSLFDAlertSentCallback(const PRFileDesc *fd, void *arg, const SSLAlert *alert)
182168
{
183169
JNIEnv *env;
184170
jobject sslfd_proxy = (jobject)arg;
185-
jobject list;
171+
jclass sslfdProxyClass;
172+
jmethodID alertSentMethod;
173+
jobject event;
186174

187175
if (fd == NULL || arg == NULL || alert == NULL || JSS_javaVM == NULL) {
188176
return;
@@ -192,13 +180,31 @@ JSSL_SSLFDAlertSentCallback(const PRFileDesc *fd, void *arg, const SSLAlert *ale
192180
return;
193181
}
194182

195-
if (JSS_NSS_getSSLAlertSentList(env, sslfd_proxy, &list) != PR_SUCCESS) {
183+
sslfdProxyClass = (*env)->GetObjectClass(env, sslfd_proxy);
184+
185+
if (sslfdProxyClass == NULL) {
196186
return;
197187
}
198188

199-
if (JSS_NSS_addSSLAlert(env, sslfd_proxy, list, alert) != PR_SUCCESS) {
189+
alertSentMethod = (*env)->GetMethodID(
190+
env,
191+
sslfdProxyClass,
192+
"alertSent",
193+
"(L" SSL_ALERT_EVENT_CLASS ";)V");
194+
195+
if (alertSentMethod == NULL) {
200196
return;
201197
}
198+
199+
// event = new SSLAlertEvent()
200+
event = JSS_NSS_createSSLAlert(env, sslfd_proxy, alert);
201+
202+
if (event == NULL) {
203+
return;
204+
}
205+
206+
// sslfd_proxy.alertSent(event)
207+
(void)(*env)->CallVoidMethod(env, sslfd_proxy, alertSentMethod, event);
202208
}
203209

204210
SECStatus
@@ -248,7 +254,11 @@ JSSL_SSLFDHandshakeComplete(PRFileDesc *fd, void *client_data)
248254
JNIEnv *env = NULL;
249255
jobject sslfd_proxy = (jobject)client_data;
250256
jclass sslfdProxyClass;
251-
jfieldID handshakeCompleteField;
257+
jmethodID handshakeCompletedMethod;
258+
259+
jclass eventClass;
260+
jmethodID eventConstructor;
261+
jobject event;
252262

253263
if (fd == NULL || client_data == NULL || JSS_javaVM == NULL) {
254264
return;
@@ -259,17 +269,50 @@ JSSL_SSLFDHandshakeComplete(PRFileDesc *fd, void *client_data)
259269
}
260270

261271
sslfdProxyClass = (*env)->GetObjectClass(env, sslfd_proxy);
272+
262273
if (sslfdProxyClass == NULL) {
263274
return;
264275
}
265276

266-
handshakeCompleteField = (*env)->GetFieldID(env, sslfdProxyClass,
267-
"handshakeComplete", "Z");
268-
if (handshakeCompleteField == NULL) {
277+
handshakeCompletedMethod = (*env)->GetMethodID(
278+
env,
279+
sslfdProxyClass,
280+
"handshakeCompleted",
281+
"(L" SSL_HANDSHAKE_COMPLETED_EVENT_CLASS ";)V");
282+
283+
if (handshakeCompletedMethod == NULL) {
284+
return;
285+
}
286+
287+
eventClass = (*env)->FindClass(env, SSL_HANDSHAKE_COMPLETED_EVENT_CLASS);
288+
289+
if (eventClass == NULL) {
290+
return;
291+
}
292+
293+
eventConstructor = (*env)->GetMethodID(
294+
env,
295+
eventClass,
296+
"<init>",
297+
"(L" SSLFD_PROXY_CLASS_NAME ";)V");
298+
299+
if (eventConstructor == NULL) {
300+
return;
301+
}
302+
303+
// event = new SSLHandshakeCompletedEvent()
304+
event = (*env)->NewObject(
305+
env,
306+
eventClass,
307+
eventConstructor,
308+
sslfd_proxy);
309+
310+
if (event == NULL) {
269311
return;
270312
}
271313

272-
(*env)->SetBooleanField(env, sslfd_proxy, handshakeCompleteField, JNI_TRUE);
314+
// sslfd_proxy.handshakeCompleted(event)
315+
(void)(*env)->CallVoidMethod(env, sslfd_proxy, handshakeCompletedMethod, event);
273316
}
274317

275318
SECStatus

native/src/main/native/org/mozilla/jss/nss/SSLFDProxy.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66

77
PRStatus JSS_NSS_getSSLClientCert(JNIEnv *env, jobject sslfd_proxy, CERTCertificate **cert);
88

9-
PRStatus JSS_NSS_getSSLAlertSentList(JNIEnv *env, jobject sslfd_proxy, jobject *list);
10-
11-
PRStatus JSS_NSS_getSSLAlertReceivedList(JNIEnv *env, jobject sslfd_proxy, jobject *list);
12-
13-
PRStatus JSS_NSS_addSSLAlert(JNIEnv *env, jobject sslfd_proxy, jobject list, const SSLAlert *alert);
9+
jobject JSS_NSS_createSSLAlert(JNIEnv *env, jobject sslfd_proxy, const SSLAlert *alert);
1410

1511
PRStatus JSS_NSS_getGlobalRef(JNIEnv *env, jobject sslfd_proxy, jobject *global_ref);
1612

native/src/main/native/org/mozilla/jss/util/java_ids.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ PR_BEGIN_EXTERN_C
285285
*/
286286
#define SSL_ALERT_EVENT_CLASS "org/mozilla/jss/ssl/SSLAlertEvent"
287287

288+
/*
289+
* SSLHandshakeCompletedEvent
290+
*/
291+
#define SSL_HANDSHAKE_COMPLETED_EVENT_CLASS "org/mozilla/jss/ssl/SSLHandshakeCompletedEvent"
292+
288293
/*
289294
* SSLCertificateApprovalCallback
290295
*/

0 commit comments

Comments
 (0)