public byte[] DecryptMessage(int messageLength, byte[] encryptedBytes) { using var inputBuffer = new GssInputBuffer(encryptedBytes); using var outputBuffer = new GssOutputBuffer(); var majorStatus = NativeMethods.gss_unwrap(out uint minorStatus, handle, inputBuffer, outputBuffer, out int _, out int _); Gss.ThrowIfError(majorStatus, minorStatus); return(outputBuffer.ToByteArray()); }
public byte[] EncryptMessage(byte[] plainTextBytes) { using var inputBuffer = new GssInputBuffer(plainTextBytes); using var outputBuffer = new GssOutputBuffer(); var majorStatus = NativeMethods.gss_wrap(out uint minorStatus, handle, 0, 0, inputBuffer, out int _, outputBuffer); Gss.ThrowIfError(majorStatus, minorStatus); return(outputBuffer.ToByteArray()); }
public byte[] Next(byte[] challenge) { using var inputToken = new GssInputBuffer(challenge); using var outputToken = new GssOutputBuffer(); const GssFlags authenticationFlags = GssFlags.GSS_C_MUTUAL_FLAG | GssFlags.GSS_C_SEQUENCE_FLAG; var majorStatus = NativeMethods.gss_init_sec_context(out var minorStatus, _credential, in handle, _servicePrincipalName, IntPtr.Zero, authenticationFlags, 0, IntPtr.Zero, inputToken, out var _, outputToken, out var _, out var _); Gss.ThrowIfError(majorStatus, minorStatus); _isInitialized = true; return(outputToken.ToByteArray()); }
public static void ThrowIfError(uint majorStatus, uint minorStatus) { var majorMessages = new List <string>(); var minorMessages = new List <string>(); if (majorStatus != (uint)GssStatus.GSS_S_COMPLETE && majorStatus != (uint)GssStatus.GSS_S_CONTINUE_NEEDED) { uint messageContext; do { using var outputBuffer = new GssOutputBuffer(); var localMajorStatus = NativeMethods.gss_display_status(out _, majorStatus, GssCode.GSS_C_GSS_CODE, in Oid.GSS_C_NO_OID, out messageContext, outputBuffer); if (localMajorStatus != 0) { throw new LibgssapiException($"Error encountered while attempting to convert majorStatus to textual description. majorStatus: {majorStatus} minorStatus: {minorStatus}."); } majorMessages.Add(Marshal.PtrToStringAnsi(outputBuffer.Value)); } while (messageContext != 0); } if (minorStatus != 0) { uint messageContext; do { using var outputBuffer = new GssOutputBuffer(); var localMajorStatus = NativeMethods.gss_display_status(out _, minorStatus, GssCode.GSS_C_MECH_CODE, in Oid.GSS_C_NO_OID, out messageContext, outputBuffer); if (localMajorStatus != 0) { throw new LibgssapiException($"Error encountered while attempting to convert minorStatus to textual description. majorStatus: {majorStatus} minorStatus: {minorStatus}."); } minorMessages.Add(Marshal.PtrToStringAnsi(outputBuffer.Value)); } while (messageContext != 0); } if (majorMessages.Count > 0 || minorMessages.Count > 0) { var message = $"Libgssapi failure - majorStatus: {string.Join("; ", majorMessages)}; minorStatus: {string.Join("; ", minorMessages)}"; throw new LibgssapiException(message); } }