| | 1 | |
|
| | 2 | | using System.Runtime.InteropServices; |
| | 3 | | using System.Security; |
| | 4 | | using Serilog; |
| | 5 | |
|
| | 6 | | namespace Kestrun.Utilities; |
| | 7 | |
|
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Provides utility methods for working with SecureString and ReadOnlySpan<char>. |
| | 11 | | /// </summary> |
| | 12 | | public static class SecureStringUtils |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Represents a delegate that handles a ReadOnlySpan<char>. |
| | 16 | | /// </summary> |
| | 17 | | public delegate void SpanHandler(ReadOnlySpan<char> span); |
| | 18 | |
|
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Converts a SecureString to a ReadOnlySpan<char> and passes it to the specified handler. |
| | 22 | | /// The unmanaged memory is zeroed and freed after the handler executes. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="secureString">The SecureString to convert.</param> |
| | 25 | | /// <param name="handler">The delegate to handle the ReadOnlySpan<char>.</param> |
| | 26 | | public static unsafe void ToSecureSpan(this SecureString secureString, SpanHandler handler) |
| | 27 | | { |
| 4 | 28 | | Log.Debug("Converting SecureString to ReadOnlySpan<char> for handler {Handler}", handler.Method.Name); |
| | 29 | |
|
| 4 | 30 | | ArgumentNullException.ThrowIfNull(secureString); |
| 4 | 31 | | ArgumentNullException.ThrowIfNull(handler); |
| 4 | 32 | | if (secureString.Length == 0) |
| | 33 | | { |
| 1 | 34 | | throw new ArgumentException("SecureString is empty", nameof(secureString)); |
| | 35 | | } |
| | 36 | | // Convert SecureString to a ReadOnlySpan<char> using a pointer |
| | 37 | | // This is safe because SecureString guarantees that the memory is zeroed after use. |
| 3 | 38 | | var ptr = IntPtr.Zero; |
| | 39 | | try |
| | 40 | | { |
| | 41 | | // Convert SecureString to a pointer |
| | 42 | | // Marshal.SecureStringToCoTaskMemUnicode returns a pointer to the unmanaged memory |
| | 43 | | // that contains the characters of the SecureString. |
| | 44 | | // This memory must be freed after use to avoid memory leaks. |
| 3 | 45 | | Log.Debug("Marshalling SecureString to unmanaged memory"); |
| 3 | 46 | | ptr = Marshal.SecureStringToCoTaskMemUnicode(secureString); |
| 3 | 47 | | var span = new ReadOnlySpan<char>((char*)ptr, secureString.Length); |
| 3 | 48 | | handler(span); |
| 3 | 49 | | Log.Debug("Handler executed successfully with SecureString span"); |
| 3 | 50 | | } |
| 0 | 51 | | catch (Exception ex) |
| | 52 | | { |
| 0 | 53 | | Log.Error(ex, "Error while converting SecureString to ReadOnlySpan<char>"); |
| 0 | 54 | | throw; // rethrow the exception for further handling |
| | 55 | | } |
| | 56 | | finally |
| | 57 | | { |
| | 58 | | // Ensure the unmanaged memory is zeroed and freed |
| 3 | 59 | | Log.Debug("Zeroing and freeing unmanaged memory for SecureString"); |
| 3 | 60 | | if (ptr != IntPtr.Zero) |
| | 61 | | { |
| | 62 | | // zero & free |
| 60 | 63 | | for (var i = 0; i < secureString.Length; i++) |
| | 64 | | { |
| 27 | 65 | | Marshal.WriteInt16(ptr, i * 2, 0); |
| | 66 | | } |
| | 67 | |
|
| 3 | 68 | | Marshal.ZeroFreeCoTaskMemUnicode(ptr); |
| | 69 | | } |
| 3 | 70 | | } |
| 3 | 71 | | } |
| | 72 | | /// <summary> |
| | 73 | | /// Converts a <see cref="ReadOnlySpan{Char}"/> to a <see cref="SecureString"/>. |
| | 74 | | /// </summary> |
| | 75 | | /// <param name="span">The character span to convert.</param> |
| | 76 | | /// <returns>A read-only <see cref="SecureString"/> containing the characters from the span.</returns> |
| | 77 | | /// <exception cref="ArgumentException">Thrown if the span is empty.</exception> |
| | 78 | | public static SecureString ToSecureString(this ReadOnlySpan<char> span) |
| | 79 | | { |
| 5 | 80 | | if (span.Length == 0) |
| | 81 | | { |
| 1 | 82 | | throw new ArgumentException("Span is empty", nameof(span)); |
| | 83 | | } |
| | 84 | |
|
| 4 | 85 | | var secure = new SecureString(); |
| 74 | 86 | | foreach (var c in span) |
| | 87 | | { |
| 33 | 88 | | secure.AppendChar(c); |
| | 89 | | } |
| | 90 | |
|
| 4 | 91 | | secure.MakeReadOnly(); |
| 4 | 92 | | return secure; |
| | 93 | | } |
| | 94 | | } |