use of org.mozilla.jss.ssl.SSLAlertEvent in project jss by dogtagpki.
the class JSSEngineReferenceImpl method checkSSLAlerts.
private SSLException checkSSLAlerts() {
debug("JSSEngine: Checking inbound and outbound SSL Alerts. Have " + ssl_fd.inboundAlerts.size() + " inbound and " + ssl_fd.outboundAlerts.size() + " outbound alerts.");
// Prefer inbound alerts to outbound alerts.
while (ssl_fd.inboundOffset < ssl_fd.inboundAlerts.size()) {
SSLAlertEvent event = ssl_fd.inboundAlerts.get(ssl_fd.inboundOffset);
ssl_fd.inboundOffset += 1;
if (event.getLevelEnum() == SSLAlertLevel.WARNING && event.getDescriptionEnum() == SSLAlertDescription.CLOSE_NOTIFY) {
debug("Got inbound CLOSE_NOTIFY alert");
closeInbound();
}
debug("JSSEngine: Got inbound alert: " + event);
// Fire inbound alert prior to raising any exception.
fireAlertReceived(event);
// Not every SSL Alert is fatal; toException() only returns a
// SSLException on fatal instances. We shouldn't return NULL
// early without checking all alerts.
SSLException exception = event.toException();
if (exception != null) {
return exception;
}
}
while (ssl_fd.outboundOffset < ssl_fd.outboundAlerts.size()) {
SSLAlertEvent event = ssl_fd.outboundAlerts.get(ssl_fd.outboundOffset);
ssl_fd.outboundOffset += 1;
if (event.getLevelEnum() == SSLAlertLevel.WARNING && event.getDescriptionEnum() == SSLAlertDescription.CLOSE_NOTIFY) {
debug("Sent outbound CLOSE_NOTIFY alert.");
closeOutbound();
}
debug("JSSEngine: Got outbound alert: " + event);
// Fire outbound alert prior to raising any exception. Note that
// this still triggers after this alert is written to the output
// wire buffer.
fireAlertSent(event);
SSLException exception = event.toException();
if (exception != null) {
return exception;
}
}
return null;
}
use of org.mozilla.jss.ssl.SSLAlertEvent in project jss by dogtagpki.
the class TestBufferPRFD method TestSSLHandshake.
public static void TestSSLHandshake(String server_nickname, String client_nickname) throws Exception {
/* Constants */
String host = "localhost";
byte[] peer_info = host.getBytes();
/* Find SSL Server Certificate */
CryptoManager manager = CryptoManager.getInstance();
PK11Cert server_cert = (PK11Cert) manager.findCertByNickname(server_nickname);
PK11PrivKey server_key = (PK11PrivKey) manager.findPrivKeyByCert(server_cert);
assert (server_cert != null);
assert (server_key != null);
/* Find SSL Client Certificate, if nickname given. */
PK11Cert client_cert = null;
if (client_nickname != null) {
client_cert = (PK11Cert) manager.findCertByNickname(client_nickname);
assert (client_cert != null);
}
/* Create Buffers and BufferPRFDs */
BufferProxy read_buf = Buffer.Create(1024);
BufferProxy write_buf = Buffer.Create(1024);
assert (read_buf != null);
assert (write_buf != null);
PRFDProxy c_buffer = PR.NewBufferPRFD(read_buf, write_buf, peer_info);
PRFDProxy s_buffer = PR.NewBufferPRFD(write_buf, read_buf, peer_info);
assert (c_buffer != null);
assert (s_buffer != null);
SSLFDProxy c_nspr = Setup_NSS_Client(c_buffer, host);
SSLFDProxy s_nspr = Setup_NSS_Server(s_buffer, host, server_cert, server_key);
assert (c_nspr != null);
assert (s_nspr != null);
/* Apply Client Certificate, if given. When given, request it as the
* server. */
if (client_cert != null) {
c_nspr.SetClientCert(client_cert);
assert (SSL.AttachClientCertCallback(c_nspr) == SSL.SECSuccess);
assert (SSL.OptionSet(s_nspr, SSL.REQUEST_CERTIFICATE, 1) == SSL.SECSuccess);
}
/* Attach alert logging callback handler. */
assert (SSL.EnableAlertLogging(c_nspr) == SSL.SECSuccess);
assert (SSL.EnableAlertLogging(s_nspr) == SSL.SECSuccess);
assert (!IsHandshakeFinished(c_nspr, s_nspr));
/* Try a handshake */
int count = 0;
while (!IsHandshakeFinished(c_nspr, s_nspr)) {
if (SSL.ForceHandshake(c_nspr) != SSL.SECSuccess) {
int error = PR.GetError();
if (error != PRErrors.WOULD_BLOCK_ERROR) {
System.out.println("Unexpected error: " + new String(PR.ErrorToName(error)) + " (" + error + ")");
System.exit(1);
}
}
if (SSL.ForceHandshake(s_nspr) != SSL.SECSuccess) {
int error = PR.GetError();
if (error != PRErrors.WOULD_BLOCK_ERROR) {
System.out.println("Unexpected error: " + new String(PR.ErrorToName(error)) + " (" + error + ")");
System.exit(1);
}
}
count += 1;
if (count >= 40) {
System.err.println("Error: unable to make progress after " + count + " steps!");
System.exit(1);
}
}
System.out.println("Handshake completed successfully!\n");
assert (IsHandshakeFinished(c_nspr, s_nspr));
/* Test peer data */
assert (SSL.PeerCertificate(c_nspr) != null);
assert (SSL.PeerCertificateChain(c_nspr) != null);
if (client_nickname == null) {
assert (SSL.PeerCertificate(s_nspr) == null);
assert (SSL.PeerCertificateChain(s_nspr) == null);
} else {
assert (SSL.PeerCertificate(s_nspr) != null);
assert (SSL.PeerCertificateChain(s_nspr) != null);
}
/* Send data from client -> server */
byte[] client_message = "Cooking MCs".getBytes();
assert (PR.Write(c_nspr, client_message) == client_message.length);
byte[] server_received = PR.Read(s_nspr, client_message.length);
assert (server_received != null);
if (server_received.length != client_message.length) {
System.out.println("Expected a client message of length " + client_message.length + " but got one of " + server_received.length);
System.exit(1);
}
for (int i = 0; i < client_message.length && i < server_received.length; i++) {
if (client_message[i] != server_received[i]) {
System.out.println("Received byte " + server_received[i] + " on server but expected " + client_message[i]);
System.exit(1);
}
}
/* Send data from server -> client */
byte[] server_message = "like a pound of bacon".getBytes();
assert (PR.Write(s_nspr, server_message) == server_message.length);
byte[] client_received = PR.Read(c_nspr, server_message.length);
assert (client_received != null);
if (client_received.length != server_message.length) {
System.out.println("Expected a server message of length " + server_message.length + " but got one of " + client_received.length);
System.exit(1);
}
for (int i = 0; i < server_message.length && i < client_received.length; i++) {
if (server_message[i] != client_received[i]) {
System.out.println("Received byte " + client_received[i] + " on client but expected " + server_message[i]);
System.exit(1);
}
}
/* Close connections */
assert (PR.Shutdown(c_nspr, PR.SHUTDOWN_BOTH) == PR.SUCCESS);
assert (PR.Shutdown(s_nspr, PR.SHUTDOWN_BOTH) == PR.SUCCESS);
/* Print all alerts. */
for (SSLAlertEvent alert : c_nspr.inboundAlerts) {
System.err.println("client inbound: " + alert);
}
for (SSLAlertEvent alert : c_nspr.outboundAlerts) {
System.err.println("client outbound: " + alert);
}
for (SSLAlertEvent alert : s_nspr.inboundAlerts) {
System.err.println("server inbound: " + alert);
}
for (SSLAlertEvent alert : s_nspr.outboundAlerts) {
System.err.println("server outbound: " + alert);
}
/* Clean up */
assert (PR.Close(c_nspr) == PR.SUCCESS);
assert (PR.Close(s_nspr) == PR.SUCCESS);
Buffer.Free(read_buf);
Buffer.Free(write_buf);
}
Aggregations