diff options
Diffstat (limited to 'src/slskd/Common/Cryptography')
| -rw-r--r-- | src/slskd/Common/Cryptography/PBKDF2.cs | 26 | ||||
| -rw-r--r-- | src/slskd/Common/Cryptography/Pbkdf2.cs | 43 | ||||
| -rw-r--r-- | src/slskd/Common/Cryptography/X509.cs | 40 |
3 files changed, 80 insertions, 29 deletions
diff --git a/src/slskd/Common/Cryptography/PBKDF2.cs b/src/slskd/Common/Cryptography/PBKDF2.cs deleted file mode 100644 index 4b8b83e4..00000000 --- a/src/slskd/Common/Cryptography/PBKDF2.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace slskd.Security -{ - using Microsoft.AspNetCore.Cryptography.KeyDerivation; - using System.Security.Cryptography; - - /// <summary> - /// PBKDF2/RFC 2898 implementation - /// </summary> - public static class PBKDF2 - { - /// <summary> - /// Gets a 256 bit (32 byte) key derived from the specified <paramref name="password"/> using PBKDF2/RFC 2898 - /// </summary> - /// <param name="password"></param> - /// <returns></returns> - public static byte[] GetKey(string password) - { - byte[] salt = new byte[16]; - int iterations = 1000; - - using var rng = RandomNumberGenerator.Create(); - rng.GetBytes(salt); - return KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterations, 32); - } - } -} diff --git a/src/slskd/Common/Cryptography/Pbkdf2.cs b/src/slskd/Common/Cryptography/Pbkdf2.cs new file mode 100644 index 00000000..f6509a73 --- /dev/null +++ b/src/slskd/Common/Cryptography/Pbkdf2.cs @@ -0,0 +1,43 @@ +// <copyright file="Pbkdf2.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd.Security +{ + using System.Security.Cryptography; + using Microsoft.AspNetCore.Cryptography.KeyDerivation; + + /// <summary> + /// PBKDF2/RFC 2898 utility methods. + /// </summary> + public static class Pbkdf2 + { + /// <summary> + /// Gets a 256 bit (32 byte) key derived from the specified <paramref name="password"/> using PBKDF2/RFC 2898. + /// </summary> + /// <param name="password">The password from which to derive the key.</param> + /// <returns>The derived key.</returns> + public static byte[] GetKey(string password) + { + byte[] salt = new byte[16]; + int iterations = 1000; + + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(salt); + return KeyDerivation.Pbkdf2(password, salt, KeyDerivationPrf.HMACSHA256, iterations, 32); + } + } +} diff --git a/src/slskd/Common/Cryptography/X509.cs b/src/slskd/Common/Cryptography/X509.cs index 19b1c4ba..95743cc0 100644 --- a/src/slskd/Common/Cryptography/X509.cs +++ b/src/slskd/Common/Cryptography/X509.cs @@ -1,11 +1,38 @@ -namespace slskd.Common.Cryptography +// <copyright file="X509.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd.Common.Cryptography { using System; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; - public class X509 + /// <summary> + /// X509 utility methods. + /// </summary> + public static class X509 { + /// <summary> + /// Generates an X509 certificate for the specified <paramref name="subject"/>. + /// </summary> + /// <param name="subject">The certificate subject.</param> + /// <param name="password">The optional password for the certificate.</param> + /// <param name="x509KeyStorageFlags">The optional key storage flags for the certificate.</param> + /// <returns>The generated certificate.</returns> public static X509Certificate2 Generate(string subject, string password = null, X509KeyStorageFlags x509KeyStorageFlags = X509KeyStorageFlags.MachineKeySet) { password ??= Guid.NewGuid().ToString(); @@ -25,6 +52,13 @@ return new X509Certificate2(certificate.Export(X509ContentType.Pkcs12, password), password, x509KeyStorageFlags); } + /// <summary> + /// Validates an X509 certificate given a PFX <paramref name="fileName"/> and optional <paramref name="password"/>. + /// </summary> + /// <param name="fileName">The path to the PFX certificate file to validate.</param> + /// <param name="password">The optional password for the certificate.</param> + /// <param name="result">The error message, if the certificate is invalid.</param> + /// <returns>A value indicating whether the certificate is valid.</returns> public static bool TryValidate(string fileName, string password, out string result) { result = null; @@ -41,4 +75,4 @@ } } } -} +}
\ No newline at end of file |
