/// <summary> /// Store content securely using Linux Kernel Key management API. /// see: https://www.kernel.org/doc/html/latest/security/keys/core.html#id2 /// </summary> private void WriteLinuxContent(byte[] content) { if (content != null && content.Length > 0) { string encodedContent = Convert.ToBase64String(content); int key = LibKeyUtils.add_key(LinuxKeyType, $"{KeyIdentifier}:{KeyStorageConfig.ClientId}", encodedContent, encodedContent.Length, (int)KeyStorageConfig.LinuxKeyring); } }
/// <summary> /// Clear content from a secure store. /// </summary> public void ClearContent() { if (CommonUtils.IsLinuxPlatform()) { int key = LibKeyUtils.request_key(LinuxKeyType, $"{KeyIdentifier}:{KeyStorageConfig.ClientId}", (int)KeyStorageConfig.LinuxKeyring); if (key != -1) { LibKeyUtils.keyctl("invalidate", key); } } }
/// <summary> /// Get stored content using Linux Kernel Key management API. /// see: https://www.kernel.org/doc/html/latest/security/keys/core.html#id2 /// </summary> private byte[] ReadLinuxContent() { int key = LibKeyUtils.request_key(LinuxKeyType, $"{KeyIdentifier}:{KeyStorageConfig.ClientId}", (int)KeyStorageConfig.LinuxKeyring); if (key == -1) { return(new byte[0]); } long contentLength = LibKeyUtils.keyctl_read_alloc(key, out IntPtr contentPtr); string content = Marshal.PtrToStringAuto(contentPtr); Marshal.FreeHGlobal(contentPtr); if (String.IsNullOrEmpty(content)) { return(new byte[0]); } return(Convert.FromBase64String(content)); }