diff options
author | vnugent <public@vaughnnugent.com> | 2023-01-09 15:09:13 -0500 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2023-01-09 15:09:13 -0500 |
commit | a46c3bf452d287b50b2e7dd5a24f5995c9fd26f6 (patch) | |
tree | 3a978b2dd2887b5c0e25f595516594a647d8e880 /VNLib.Plugins.Extensions.Loading/PrivateKey.cs | |
parent | 189c6714057bf45553847eaeb9ce97eb7272eb8c (diff) |
Restructure
Diffstat (limited to 'VNLib.Plugins.Extensions.Loading/PrivateKey.cs')
-rw-r--r-- | VNLib.Plugins.Extensions.Loading/PrivateKey.cs | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/VNLib.Plugins.Extensions.Loading/PrivateKey.cs b/VNLib.Plugins.Extensions.Loading/PrivateKey.cs deleted file mode 100644 index 336f6a4..0000000 --- a/VNLib.Plugins.Extensions.Loading/PrivateKey.cs +++ /dev/null @@ -1,102 +0,0 @@ -/* -* Copyright (c) 2022 Vaughn Nugent -* -* Library: VNLib -* Package: VNLib.Plugins.Extensions.Loading -* File: PrivateKey.cs -* -* PrivateKey.cs is part of VNLib.Plugins.Extensions.Loading which is part of the larger -* VNLib collection of libraries and utilities. -* -* VNLib.Plugins.Extensions.Loading is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published -* by the Free Software Foundation, either version 2 of the License, -* or (at your option) any later version. -* -* VNLib.Plugins.Extensions.Loading is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with VNLib.Plugins.Extensions.Loading. If not, see http://www.gnu.org/licenses/. -*/ - -using System; -using System.Text; -using System.Security.Cryptography; - -using VNLib.Utils; -using VNLib.Utils.Memory; -using VNLib.Utils.Extensions; - -namespace VNLib.Plugins.Extensions.Loading -{ - /// <summary> - /// A container for a PKSC#8 encoed private key - /// </summary> - public sealed class PrivateKey : VnDisposeable - { - private readonly byte[] _utf8RawData; - - /// <summary> - /// Decodes the PKCS#8 encoded private key from a secret, as an EC private key - /// and recovers the ECDsa algorithm from the key - /// </summary> - /// <returns>The <see cref="ECDsa"/> algoritm from the private key</returns> - /// <exception cref="ArgumentNullException"></exception> - /// <exception cref="CryptographicException"></exception> - public ECDsa GetECDsa() - { - //Alloc buffer - using IMemoryHandle<byte> buffer = Memory.SafeAlloc<byte>(_utf8RawData.Length); - //Get base64 bytes from utf8 - ERRNO count = VnEncoding.Base64UrlDecode(_utf8RawData, buffer.Span); - //Parse the private key - ECDsa alg = ECDsa.Create(); - alg.ImportPkcs8PrivateKey(buffer.Span[..(int)count], out _); - //Wipe the buffer - Memory.InitializeBlock(buffer.Span); - return alg; - } - - /// <summary> - /// Decodes the PKCS#8 encoded private key from a secret, as an RSA private key - /// </summary> - /// <returns>The <see cref="RSA"/> algorithm from the private key</returns> - /// <exception cref="ArgumentNullException"></exception> - /// <exception cref="CryptographicException"></exception> - public RSA GetRSA() - { - //Alloc buffer - using IMemoryHandle<byte> buffer = Memory.SafeAlloc<byte>(_utf8RawData.Length); - //Get base64 bytes from utf8 - ERRNO count = VnEncoding.Base64UrlDecode(_utf8RawData, buffer.Span); - //Parse the private key - RSA alg = RSA.Create(); - alg.ImportPkcs8PrivateKey(buffer.Span[..(int)count], out _); - //Wipe the buffer - Memory.InitializeBlock(buffer.Span); - return alg; - } - - internal PrivateKey(SecretResult secret) - { - //Alloc and get utf8 - byte[] buffer = new byte[secret.Result.Length]; - int count = Encoding.UTF8.GetBytes(secret.Result, buffer); - //Verify length - if(count != buffer.Length) - { - throw new FormatException("UTF8 deocde failed"); - } - //Store - _utf8RawData = buffer; - } - - protected override void Free() - { - Memory.InitializeBlock(_utf8RawData.AsSpan()); - } - } -} |