Skip to content

Commit b918768

Browse files
author
Volodymyr B
committed
log certificate info
1 parent 38dbab7 commit b918768

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

lib/src/sip_ua_helper.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,9 @@ class WebSocketSettings {
805805
/// for self-signed certificate.
806806
bool allowBadCertificate = false;
807807

808+
/// If true, debug the certificate.
809+
bool debugCertificate = false;
810+
808811
/// Custom transport scheme string to use.
809812
/// Otherwise the used protocol will be used (for example WS for ws://
810813
/// or WSS for wss://, based on the given web socket URL).

lib/src/transports/websocket_dart_impl.dart

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ class SIPUAWebSocketImpl {
2525
handleQueue();
2626
logger.i('connect $_url, ${webSocketSettings.extraHeaders}, $protocols');
2727
try {
28-
if (webSocketSettings.allowBadCertificate) {
29-
/// Allow self-signed certificate, for test only.
30-
_socket = await _connectForBadCertificate(_url, webSocketSettings);
31-
} else {
28+
if (webSocketSettings.allowBadCertificate || webSocketSettings.debugCertificate) {
29+
// Depending on the settings, it will allow self-signed certificates or debug them.
30+
_socket = await _connectWithBadCertificateHandling(_url, webSocketSettings);
31+
}
32+
else {
3233
_socket = await WebSocket.connect(_url,
3334
protocols: protocols, headers: webSocketSettings.extraHeaders);
3435
}
@@ -69,8 +70,7 @@ class SIPUAWebSocketImpl {
6970
return _socket != null && _socket!.readyState == WebSocket.connecting;
7071
}
7172

72-
/// For test only.
73-
Future<WebSocket> _connectForBadCertificate(
73+
Future<WebSocket> _connectWithBadCertificateHandling(
7474
String url, WebSocketSettings webSocketSettings) async {
7575
try {
7676
Random r = Random();
@@ -84,8 +84,25 @@ class SIPUAWebSocketImpl {
8484

8585
client.badCertificateCallback =
8686
(X509Certificate cert, String host, int port) {
87-
logger.w('Allow self-signed certificate => $host:$port. ');
88-
return true;
87+
if(webSocketSettings.allowBadCertificate) {
88+
logger.w('Allow self-signed certificate => $host:$port. ');
89+
return true;
90+
}
91+
else if(webSocketSettings.debugCertificate){
92+
logger.w('Server returns a server certificate that cannot be authenticated => $host:$port. ');
93+
String certInfo = '\n';
94+
certInfo+= ' Certificate subject: ${cert.subject}\n';
95+
certInfo+= ' Certificate issuer: ${cert.issuer}\n';
96+
certInfo+= ' Certificate valid from: ${cert.startValidity}\n';
97+
certInfo+= ' Certificate valid to: ${cert.endValidity}\n';
98+
certInfo+= ' Certificate SHA-1 fingerprint: ${cert.sha1}\n';
99+
100+
logger.w('Certificate details: {$certInfo}');
101+
return false;
102+
}
103+
else{
104+
return false; // reject the certificate
105+
}
89106
};
90107

91108
Uri parsed_uri = Uri.parse(url);

0 commit comments

Comments
 (0)