Skip to content

Commit 63db5df

Browse files
committed
fix: Catch Cryptographic Error and throw appropriate error message
Code was throwing an uncaught expection on cert gen when the cert can't be written to disk, which would bubble beyond exception handlers and crash the application. Catch the exception and tell the user to turn off SSL. Also provide forum info on about tab so users can contact devs for support. Fixes #372
1 parent d666682 commit 63db5df

3 files changed

Lines changed: 21 additions & 20 deletions

File tree

Buttplug.Apps.ServerGUI/ServerControl.xaml.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.ObjectModel;
33
using System.Net;
44
using System.Net.NetworkInformation;
@@ -46,8 +46,8 @@ public ServerControl(IButtplugServerFactory bpFactory)
4646
_connUrls = new ConnUrlList();
4747
_port = 12345;
4848

49-
// Usually, if we throw errors then connect, it's not actually an error.
50-
// If we don't connect after a second of throwing an exception, pop the toaster, but not before then.
49+
// Usually, if we throw errors then connect, it's not actually an error. If we don't
50+
// connect after a second of throwing an exception, pop the toaster, but not before then.
5151
_toastTimer = new Timer
5252
{
5353
Interval = 1000,
@@ -206,11 +206,14 @@ public void StartServer()
206206
}
207207
catch (SocketException e)
208208
{
209-
_log.LogException(e, true, e.Message);
209+
_currentExceptionMessage = e.Message;
210+
_log.LogException(e, true, _currentExceptionMessage);
210211
}
211212
catch (CryptographicException e)
212213
{
213-
_log.LogException(e, true, e.Message);
214+
_currentExceptionMessage =
215+
"Cannot start server with SSL. Try turning off SSL. The server can still be used with ScriptPlayer, but not web applications. If you need SSL, contact Buttplug Developers for support (see About Tab).";
216+
_log.LogException(e, true, _currentExceptionMessage);
214217
}
215218
}
216219

@@ -291,8 +294,7 @@ private void ConnItemCopy_Click(object sender, RoutedEventArgs e)
291294
}
292295
catch (Exception ex)
293296
{
294-
// We've seen weird instances of can't open clipboard
295-
// but it's pretty rare. Log it.
297+
// We've seen weird instances of can't open clipboard but it's pretty rare. Log it.
296298
_log.LogException(ex);
297299
}
298300
}

Buttplug.Components.Controls/ButtplugAboutControl.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="Buttplug.Components.Controls.ButtplugAboutControl"
1+
<UserControl x:Class="Buttplug.Components.Controls.ButtplugAboutControl"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -18,11 +18,12 @@
1818
<TextBlock Grid.Row="0" HorizontalAlignment="Left" Margin="150,0,0,0" VerticalAlignment="Stretch" Height="auto" Width="auto">
1919
<Span FontSize="19" FontWeight="Bold">Buttplug <Run Name="AboutVersionNumber">1.0.0.0-branch</Run> (C# Edition)</Span><LineBreak />
2020
<Run Name="AboutGitVersion" Cursor="Hand">0123456789abcdef0123456789abcdef01234567</Run><LineBreak />
21-
By <Hyperlink NavigateUri="https://metafetish.com" RequestNavigate="Hyperlink_RequestNavigate">Metafetish</Hyperlink><LineBreak />
21+
By <Hyperlink NavigateUri="https://metafetish.com" RequestNavigate="Hyperlink_RequestNavigate">Nonpolynomial Labs, LLC</Hyperlink><LineBreak />
2222
<LineBreak/>
2323
Software updates at <Hyperlink NavigateUri="https://github.com/metafetish" RequestNavigate="Hyperlink_RequestNavigate">https://buttplug.io/</Hyperlink><LineBreak/>
2424
Documentation at <Hyperlink NavigateUri="https://buttplug.io/docs" RequestNavigate="Hyperlink_RequestNavigate">https://buttplug.io/docs</Hyperlink><LineBreak/>
2525
Source code at <Hyperlink NavigateUri="https://github.com/metafetish" RequestNavigate="Hyperlink_RequestNavigate">https://github.com/metafetish</Hyperlink><LineBreak/>
26+
Forums and Support at <Hyperlink NavigateUri="https://metafetish.club" RequestNavigate="Hyperlink_RequestNavigate">https://metafetish.club</Hyperlink><LineBreak/>
2627
License information <Hyperlink Click="LicenseHyperlink_Click" >here</Hyperlink><LineBreak/>
2728
</TextBlock>
2829

Buttplug.Components.WebsocketServer/CertUtils.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Security.Cryptography;
@@ -21,13 +21,14 @@ namespace Buttplug.Components.WebsocketServer
2121
{
2222
internal static class CertUtils
2323
{
24+
/// <exception cref="CryptographicException">Sometimes thrown due to issues generating keys.</exception>
2425
public static X509Certificate2 GetCert(string app, string hostname = "localhost")
2526
{
2627
var appPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), app);
2728
var caPfx = Path.Combine(appPath, "ca.pfx");
2829
var certPfx = Path.Combine(appPath, "cert.pfx");
2930

30-
// Patch release rework of cert handling: our websocket server doesn't acept a chain!
31+
// Patch release rework of cert handling: our websocket server doesn't accept a chain!
3132
if (File.Exists(caPfx))
3233
{
3334
File.Delete(caPfx);
@@ -58,7 +59,8 @@ public static X509Certificate2 GetCert(string app, string hostname = "localhost"
5859
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
5960
}
6061

61-
// Note: Much of this code comes from https://stackoverflow.com/a/22247129
62+
/// Note: Much of this code comes from https://stackoverflow.com/a/22247129
63+
/// <exception cref="CryptographicException">Sometimes thrown due to issues generating keys.</exception>
6264
private static X509Certificate2 GenerateSelfSignedCertificate(string subject)
6365
{
6466
const int keyStrength = 2048;
@@ -134,14 +136,10 @@ private static X509Certificate2 GenerateSelfSignedCertificate(string subject)
134136
var rsa = RsaPrivateKeyStructure.GetInstance(seq);
135137
var rsaparams = new RsaPrivateCrtKeyParameters(
136138
rsa.Modulus, rsa.PublicExponent, rsa.PrivateExponent, rsa.Prime1, rsa.Prime2, rsa.Exponent1, rsa.Exponent2, rsa.Coefficient);
137-
try
138-
{
139-
x509.PrivateKey = ToDotNetKey(rsaparams); // x509.PrivateKey = DotNetUtilities.ToRSA(rsaparams);
140-
}
141-
catch (CryptographicException e)
142-
{
143-
throw new Exception($"Exception on cert generation!\nSubject {subject}\nHostname {Environment.MachineName}\nSequenceCount {seq.Count} (should be 9?)", e);
144-
}
139+
140+
// This can throw CryptographicException in some cases. Catch above here and deal with it
141+
// in the application level.
142+
x509.PrivateKey = ToDotNetKey(rsaparams); // x509.PrivateKey = DotNetUtilities.ToRSA(rsaparams);
145143

146144
return x509;
147145
}

0 commit comments

Comments
 (0)